diff -Nru initramfs-tools-devices-0.3/debian/changelog initramfs-tools-devices-0.4/debian/changelog --- initramfs-tools-devices-0.3/debian/changelog 2018-12-05 14:47:23.000000000 +0100 +++ initramfs-tools-devices-0.4/debian/changelog 2019-03-11 12:30:29.000000000 +0100 @@ -1,3 +1,10 @@ +initramfs-tools-devices (0.4) disco; urgency=medium + + * Change resize script so it expands the filesystem also when + the partition is bigger than it already. + + -- Alfonso Sanchez-Beato (email Canonical) Mon, 11 Mar 2019 12:30:29 +0100 + initramfs-tools-devices (0.3) disco; urgency=medium * Add some missing binaries to the initramfs diff -Nru initramfs-tools-devices-0.3/hooks/resize initramfs-tools-devices-0.4/hooks/resize --- initramfs-tools-devices-0.3/hooks/resize 2018-12-05 14:36:56.000000000 +0100 +++ initramfs-tools-devices-0.4/hooks/resize 2019-03-11 12:28:59.000000000 +0100 @@ -23,6 +23,7 @@ copy_exec /sbin/findfs /sbin copy_exec /sbin/parted /sbin copy_exec /sbin/sgdisk /sbin +copy_exec /sbin/dumpe2fs /sbin copy_exec /sbin/resize2fs /sbin copy_exec /sbin/blockdev /sbin copy_exec /usr/bin/realpath diff -Nru initramfs-tools-devices-0.3/scripts/local-premount/resize initramfs-tools-devices-0.4/scripts/local-premount/resize --- initramfs-tools-devices-0.3/scripts/local-premount/resize 2018-08-23 10:22:35.000000000 +0200 +++ initramfs-tools-devices-0.4/scripts/local-premount/resize 2019-03-11 12:30:02.000000000 +0100 @@ -21,6 +21,7 @@ wait-for-root "LABEL=writable" "${ROOTDELAY:-180}" >/dev/null || true +# shellcheck disable=SC2013 for opt in $(cat /proc/cmdline); do case $opt in debug) @@ -31,12 +32,13 @@ writable_part="$(findfs LABEL=writable)" -syspath="$(dirname $(realpath /sys/class/block/$(basename $writable_part)))" -device="$(realpath /dev/block/$(cat $syspath/dev))" -partition=$(cat $syspath/$(basename $writable_part)/partition) - -device_size="$(($(cat $syspath/size)/2))" -sum_size="$(($(grep $(basename $device)[a-z0-9] /proc/partitions|\ +syspath="$(dirname "$(realpath /sys/class/block/"$(basename "$writable_part")")")" +device="$(realpath /dev/block/"$(cat "$syspath"/dev)")" +partition=$(cat "$syspath"/"$(basename "$writable_part")"/partition) + +# We use 1024 bytes blocks for the operations +device_size="$(($(cat "$syspath"/size)/2))" +sum_size="$(($(grep "$(basename "$device")[a-z0-9]" /proc/partitions|\ tr -s ' '|cut -d' ' -f4|tr '\n' '+'|sed 's/+$//')))" get_end() @@ -44,11 +46,11 @@ NUM=$1 lastpart="$(grep -cE ^'[0-9]{1,}': $TMPFILE)" if [ "$lastpart" = "$NUM" ]; then - endsize="$(parted -ms $DEV print| grep ^/ | cut -d: -f2)" + endsize="$(parted -ms "$DEV" print| grep ^/ | cut -d: -f2)" else # we are not at the end ! get the start of the next partition # (minus 1 byte) instead of using the absolute end of the disk - endsize=$(($(parted -ms $DEV unit B print|grep ^$(($num+1)):|\ + endsize=$(($(parted -ms "$DEV" unit B print|grep ^$((NUM+1)):|\ cut -d: -f2|sed 's/B$//')-1)) fi echo "$endsize" @@ -58,66 +60,89 @@ { DISK=$1 - PARTNUM="$(sgdisk -p $DISK|grep writable|sed -r 's/^([^ ]*[ ]*){1}([^ ]*).*/\2/')" - GUID="$(sgdisk -i $PARTNUM $DISK|grep unique| sed 's/^.*: //g')" - FIRST="$(sgdisk -i $PARTNUM $DISK|grep ^First| sed 's/^.*: //g;s/ .*$//')" - - echo "fixing backup GPT" >>$LOGFILE 2>&1 - sgdisk --move-second-header $DISK >>$LOGFILE 2>&1 - - echo "Deleting partition $PARTNUM" >>$LOGFILE 2>&1 - sgdisk -d $PARTNUM $DISK >>$LOGFILE 2>&1 - - echo "Creating partition $PARTNUM at $FIRST" >>$LOGFILE 2>&1 - sgdisk -n $PARTNUM:$FIRST:0 $DISK >>$LOGFILE 2>&1 - - echo "Setting GUID of partition $PARTNUM to $GUID" >>$LOGFILE 2>&1 - sgdisk -u $PARTNUM:$GUID $DISK >>$LOGFILE 2>&1 - - echo "Setting name of $PARTNUM to writable" >>$LOGFILE 2>&1 - sgdisk -c $PARTNUM:writable $DISK >>$LOGFILE 2>&1 - - sgdisk -p $DISK >/run/initramfs/new-gpt-table.txt 2>/dev/null + PARTNUM="$(sgdisk -p "$DISK"|grep writable|sed -r 's/^([^ ]*[ ]*){1}([^ ]*).*/\2/')" + GUID="$(sgdisk -i "$PARTNUM" "$DISK"|grep unique| sed 's/^.*: //g')" + FIRST="$(sgdisk -i "$PARTNUM" "$DISK"|grep ^First| sed 's/^.*: //g;s/ .*$//')" + + { + echo "fixing backup GPT" + sgdisk --move-second-header "$DISK" + + echo "Deleting partition $PARTNUM" + sgdisk -d "$PARTNUM" "$DISK" + + echo "Creating partition $PARTNUM at $FIRST" + sgdisk -n "$PARTNUM":"$FIRST":0 "$DISK" + + echo "Setting GUID of partition $PARTNUM to $GUID" + sgdisk -u "$PARTNUM":"$GUID" "$DISK" + + echo "Setting name of $PARTNUM to writable" + sgdisk -c "$PARTNUM":writable "$DISK" + + sgdisk -p "$DISK" >/run/initramfs/new-gpt-table.txt 2>/dev/null + } >>$LOGFILE 2>&1 } do_mbr() { DEV=$1 PART=$2 - endsize=$(get_end $PART) - parted -s $DEV resizepart $PART $endsize + endsize=$(get_end "$PART") + parted -s "$DEV" resizepart "$PART" "$endsize" } +resize_filesystem() +{ + # make sure the partitions are ready for further use + udevadm settle + # check the filesystem before attempting re-size + e2fsck -fy "$writable_part" + # resize the filesystem to full size of the partition + resize2fs "$resizeopts" "$writable_part" +} -free_space=$(($device_size-$sum_size)) -min_free_space=$(($device_size/10)) +free_space=$((device_size-sum_size)) +min_free_space=$((device_size/10)) +filesystem_size=$(dumpe2fs -h "$writable_part" | + awk -F: '/Block count/{count=$2} /Block size/{size=$2} END{print count*size/1024}') +partition_size=$(grep "$(basename "$writable_part")" < /proc/partitions | + tr -s ' ' | cut -d' ' -f4) +free_partition_space=$((partition_size - filesystem_size)) +min_free_partition_size=$((partition_size/10)) + +# this should never be empty, else argument checking of resize2fs +# will recognize it as empty string (due to the quoting of the arg below) and fail +resizeopts="-p" if [ "$min_free_space" -lt "$free_space" ]; then - echo "initrd: found more than 10% free space on disk, resizing ${writable_part}" >/dev/kmsg || true + echo "initrd: found more than 10% free space on disk, resizing ${writable_part}"\ + >/dev/kmsg || true echo "initrd: partition to full disk size, see ${LOGFILE} for details" >/dev/kmsg || true # back up the original partition table for later use or debugging - parted -ms $device unit B print >$TMPFILE 2>/dev/null + parted -ms "$device" unit B print >$TMPFILE 2>/dev/null # grow our selected partition to max space available - table="$(parted -ms $device print| grep ^/| cut -d: -f6)" + table="$(parted -ms "$device" print| grep ^/| cut -d: -f6)" case $table in gpt) # do_gpt needs the device name - do_gpt $device >>$LOGFILE 2>&1 + do_gpt "$device" >>$LOGFILE 2>&1 ;; mbr|msdos) # do_mbr needs the device node and partition number - do_mbr $device $partition >>$LOGFILE 2>&1 - resizeopts="-f" + do_mbr "$device" "$partition" >>$LOGFILE 2>&1 + resizeopts="-fp" ;; *) echo "unknown partition table type, not resizing" >>$LOGFILE exit 0 ;; esac - # make sure the partitions are ready for further use - udevadm settle >>$LOGFILE 2>&1 - # check the filesystem before attempting re-size - e2fsck -fy $writable_part >>$LOGFILE 2>&1 - # resize the filesystem to full size of the partition - resize2fs $resizeopts $writable_part >>$LOGFILE 2>&1 + resize_filesystem >>$LOGFILE 2>&1 + +elif [ "$min_free_partition_size" -lt "$free_partition_space" ]; then + + echo "initrd: found more than 10% free space on ${writable_part}, resizing filesystem"\ + >/dev/kmsg || true + resize_filesystem >>$LOGFILE 2>&1 fi