From 2722423acbf10126b06ff3d13430e2f05991513a Mon Sep 17 00:00:00 2001 From: Silvan Calarco Date: Fri, 13 Oct 2017 13:17:42 +0200 Subject: [PATCH] rootfsinstall.sh: kill processes run from chroot before unmounting; -g option to grow filesystem and update PARTUUID (for Raspbian) --- src/rootfsinstall.sh | 47 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/rootfsinstall.sh b/src/rootfsinstall.sh index 5328c2c..0058e05 100755 --- a/src/rootfsinstall.sh +++ b/src/rootfsinstall.sh @@ -25,6 +25,7 @@ function usage() { echo " Use @ROOT@ as reference to rootfs path and @BOOT@ as reference to bootfs path" echo "-c command : command to run through chroot in rootfs (may be specified multiple times)" echo "-fs fstype : filesystem type for system partition (default: ext4)" + echo "-g : grow system partition to fit storage size (to be used with -I)" echo "-h hostname : set given hostname" echo "-k kernel : extract and use specified kernel file from ROOTFS/boot/ for boot" echo "-k2 kernel : extract and use specified second kernel file from ROOTFS/boot/ for boot" @@ -100,6 +101,7 @@ while [ "$1" ]; do USEKERNEL2=$2 shift ;; + -g) GROWIMAGE=1 ;; -h) [ "$2" ] || { echo "Error: -h option requires a hostname as argument; aborting." exit 1 @@ -302,11 +304,21 @@ if [ ! "$IMAGEMODE" ]; then else # Image mode - gunzip -c ${ROOTFSARCHIVE} | dd of=${DRIVE} bs=32M - # Grow system partition to 100% - parted ${DRIVE} resizepart 2 100% - e2fsck -f ${PARTITION2} - resize2fs ${PARTITION2} + if [ "${ROOTFSARCHIVE: -4}" == ".zip" ]; then + 7z e -so ${ROOTFSARCHIVE} | dd of=${DRIVE} bs=4M conv=fsync status=progress + elif [ "${ROOTFSARCHIVE: -3}" == ".gz" ]; then + gunzip -c ${ROOTFSARCHIVE} | dd of=${DRIVE} bs=4M conv=fsync status=progress + else + dd if=${ROOTFSARCHIVE} of=${DRIVE} bs=4M conv=fsync status=progress + fi + if [ "${GROWIMAGE}" ]; then + # Grow system partition to 100% + SAVE_BOOT_PARTUUID=`blkid -s PARTUUID -o value ${PARTITION1}` + SAVE_ROOT_PARTUUID=`blkid -s PARTUUID -o value ${PARTITION2}` + parted ${DRIVE} resizepart 2 100% + e2fsck -f ${PARTITION2} + resize2fs ${PARTITION2} + fi fi [ "$BOOTFSTYPE" ] && BOOTMP=`mktemp -d -t rootfsinstall.XXXXXXXX` @@ -605,6 +617,31 @@ for c in `seq 1 ${#CHROOTCOMMANDS[*]}`; do } done +if [ "${IMAGEMODE}" -a "${SAVE_ROOT_PARTUUID}" -a "${GROWIMAGE}" ]; then + # Update PARTUUID after growing root filesystem (Raspbian) + NEW_BOOT_PARTUUID=`blkid -s PARTUUID -o value ${PARTITION1}` + NEW_ROOT_PARTUUID=`blkid -s PARTUUID -o value ${PARTITION2}` + sed -i "s|PARTUUID=${SAVE_ROOT_PARTUUID}|PARTUUID=${NEW_ROOT_PARTUUID}|" ${ROOTMP}/etc/fstab + sed -i "s|PARTUUID=${SAVE_BOOT_PARTUUID}|PARTUUID=${NEW_BOOT_PARTUUID}|" ${ROOTMP}/etc/fstab + if [ "$TARGETDEVICE" = "rpi" ]; then + sed -i "s|PARTUUID=${SAVE_ROOT_PARTUUID}|PARTUUID=${NEW_ROOT_PARTUUID}|" ${BOOTMP}/cmdline.txt + fi +fi + +# Kill any daemon process run in chroot +FOUND=0 +for ROOT in /proc/*/root; do + LINK=$(readlink $ROOT) + if [ "x$LINK" != "x" ]; then + if [ "x${LINK:0:${#ROOTMP}}" = "x$ROOTMP" ]; then + # this process is in the chroot... + PID=$(basename $(dirname "$ROOT")) + kill -9 "$PID" + FOUND=1 + fi + fi +done + sync echo "Done."