From a03e217233d19052cc24f51e8ed4efcd78c6aafd 2016-03-06 09:43:34 From: Jan Wagner Date: 2016-03-06 09:43:34 Subject: [PATCH] Merge branch 'stylesuxx-rootfs-size-calculation-improvement' --- diff --git a/rpi2-gen-image.sh b/rpi2-gen-image.sh index 6759f76..c974731 100755 --- a/rpi2-gen-image.sh +++ b/rpi2-gen-image.sh @@ -864,35 +864,42 @@ rm -f $R/var/lib/urandom/random-seed rm -f $R/etc/machine-id rm -fr $R/etc/apt/apt.conf.d/10proxy -# Calculate size of the chroot directory -CHROOT_SIZE=$(expr `du -s $R | awk '{ print $1 }'` / 1024) +# Calculate size of the chroot directory in KB +CHROOT_SIZE=$(expr `du -s $R | awk '{ print $1 }'`) -# Calculate required image size -IMAGE_SIZE=`expr $(expr ${CHROOT_SIZE} / 1024 + 1) \* 1024` +# Calculate the amount of needed 512 Byte sectors +TABLE_SECTORS=$(expr 1 \* 1024 \* 1024 \/ 512) +BOOT_SECTORS=$(expr 64 \* 1024 \* 1024 \/ 512) +ROOT_OFFSET=$(expr ${TABLE_SECTORS} + ${BOOT_SECTORS}) -# Calculate number of sectors for the partition -IMAGE_SECTORS=`expr $(expr ${IMAGE_SIZE} \* 1048576) / 512 - 133120` +# The root partition is EXT4 +# This means more space than the actual used space of the chroot is used. +# As overhead for journaling and reserved blocks 20% are added. +ROOT_SECTORS=$(expr $(expr ${CHROOT_SIZE} + ${CHROOT_SIZE} \/ 100 \* 20) \* 1024 \/ 512) + +# Calculate required image size in 512 Byte sectors +IMAGE_SECTORS=$(expr ${TABLE_SECTORS} + ${BOOT_SECTORS} + ${ROOT_SECTORS}) # Prepare date string for image file name DATE="$(date +%Y-%m-%d)" # Prepare image file -dd if=/dev/zero of="$BASEDIR/${DATE}-debian-${RELEASE}.img" bs=1M count=1 -dd if=/dev/zero of="$BASEDIR/${DATE}-debian-${RELEASE}.img" bs=1M count=0 seek=${IMAGE_SIZE} +dd if=/dev/zero of="$BASEDIR/${DATE}-debian-${RELEASE}.img" bs=512 count=${TABLE_SECTORS} +dd if=/dev/zero of="$BASEDIR/${DATE}-debian-${RELEASE}.img" bs=512 count=0 seek=${IMAGE_SECTORS} # Write partition table -sfdisk -q -L -f "$BASEDIR/${DATE}-debian-${RELEASE}.img" <