From 3687eb638dfcdc641b77e46f44814638d5b9bf81 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sat, 17 Sep 2016 15:16:06 -0700 Subject: initial commit --- mkrootimg | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 mkrootimg (limited to 'mkrootimg') diff --git a/mkrootimg b/mkrootimg new file mode 100755 index 0000000..01d509b --- /dev/null +++ b/mkrootimg @@ -0,0 +1,85 @@ +#!/bin/bash +set -e + +print_usage() { + cat 1>&2 <&2 + exit 1 + esac + shift +done + +# Process last argument, the root file system location +if [ -z "$1" ] || [ -z "$2" ] || [ "$1" = -h ] || [ "$1" = --help ]; then + print_usage + exit 1 +fi +if [ "$EUID" -ne 0 ]; then + echo "This must be run as root." 1>&2 + exit 1 +fi + +# Directory to package +rootfs="$1" + +# Binary image file +image="$2" + +# Number of 512-byte sectors reserved at the beginning of the image +# (usually 1M for e.g. a bootloader) +table_sectors=$(expr 1 \* 1024 \* 1024 \/ 512) + +# Calculate size of the rootfs directory in KB +rootfs_size=$(expr `du -s "${rootfs}" | awk '{ print $1 }'`) + +# The root partition is EXT4 +# This means more space than the actual used space of the rootfs is used. +# As overhead for journaling and reserved blocks 25% are added. +rootfs_sectors=$(expr $(expr ${rootfs_size} + ${rootfs_size} \/ 100 \* 25) \* 1024 \/ 512) + +# Calculate required image size in 512 Byte sectors +image_sectors=$(expr ${table_sectors} + ${rootfs_sectors}) + +# Initialize image file +dd if=/dev/zero of="$image" bs=512 count=${table_sectors} +dd if=/dev/zero of="$image" bs=512 count=0 seek=${image_sectors} + +# Write partition table +sfdisk "$image" < /dev/null + trap - 0 1 2 3 6 +} +trap cleanup 0 1 2 3 6 + +# Mount the temporary loop devices +mount_dir=$(mktemp -d) +mount "$image_loop" "$mount_dir" + +# Copy all files from the chroot to the loop device mount point directory +rsync -a "$rootfs/" "$mount_dir" +umount "$mount_dir" +cleanup -- cgit v1.2.3