From 87ef6d70e3a49ad6a83bb183c60fe07ba24c6741 Mon Sep 17 00:00:00 2001 From: Lutz Schwarz Date: Thu, 3 Sep 2020 15:05:57 +0200 Subject: [PATCH 1/3] Detect existing auto expander without using md5. Make installation of auto expander more stable. --- pishrink.sh | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index dbf6b09..fadd90d 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -78,14 +78,8 @@ function set_autoexpand() { return fi - if [[ -f "$mountdir/etc/rc.local" ]] && [[ "$(md5sum "$mountdir/etc/rc.local" | cut -d ' ' -f 1)" != "1c579c7d5b4292fd948399b6ece39009" ]]; then - echo "Creating new /etc/rc.local" - if [ -f "$mountdir/etc/rc.local" ]; then - mv "$mountdir/etc/rc.local" "$mountdir/etc/rc.local.bak" - fi - - #####Do not touch the following lines##### -cat <<\EOF1 > "$mountdir/etc/rc.local" + local new_rc_local=$( +cat <<\EOF1 #!/bin/bash do_expand_rootfs() { ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p') @@ -146,8 +140,40 @@ if [[ -f /etc/rc.local.bak ]]; then fi exit 0 EOF1 - #####End no touch zone##### - chmod +x "$mountdir/etc/rc.local" +) + if [[ ! -f "$mountdir/etc/rc.local" ]] || [[ "$(cat "$mountdir/etc/rc.local")" != "$new_rc_local" ]]; then + if [ -f "$mountdir/etc/rc.local" ]; then + if fgrep -q expand "$mountdir/etc/rc.local" && + fgrep -q /etc/rc.local "$mountdir/etc/rc.local" && + fgrep -q /etc/rc.local.bak "$mountdir/etc/rc.local" + then + info "Image already has autoexpander of different $MYNAME version: replacing autoexpander" + # In this case we replace /etc/rc.local but leave an existing rc.local.bak unchanged. + else + [ -e "$mountdir/etc/rc.local.bak" ] && mv -Tf "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local.bak.bak" && + info "Found unexpected /etc/rc.local.bak - renamed to /etc/rc.local.bak.bak" + # Save original /etc/rc.local to be executed after expand. + if ! mv -Tf "$mountdir/etc/rc.local" "$mountdir/etc/rc.local.bak"; then + info "WARNING: autoexpand could not be installed." + umount "$mountdir" + return + fi + fi + else + # No original /etc/rc.local exists; ensure we leave with a rc.local.bak that does nothing. + [ -e "$mountdir/etc/rc.local.bak" ] && mv -Tf "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local.bak.bak" && + info "Found unexpected /etc/rc.local.bak - renamed to /etc/rc.local.bak.bak" + fi + + info "Creating new /etc/rc.local" + echo "$new_rc_local" > "$mountdir/etc/rc.local" + chmod +x "$mountdir/etc/rc.local" + + # If no /etc/rc.local.bak exists, create one that does nothing. + if [ ! -e "$mountdir/etc/rc.local.bak" ]; then + cp /dev/null "$mountdir/etc/rc.local.bak" + chmod +x "$mountdir/etc/rc.local.bak" + fi fi umount "$mountdir" } From 9db4e9b68bca4327730a123f2b90960ef5d88173 Mon Sep 17 00:00:00 2001 From: Lutz Schwarz Date: Thu, 3 Sep 2020 17:02:34 +0200 Subject: [PATCH 2/3] Ensure a console exists when booting for autoexpand to prevent start failure --- pishrink.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/pishrink.sh b/pishrink.sh index fadd90d..3c70801 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -81,6 +81,7 @@ function set_autoexpand() { local new_rc_local=$( cat <<\EOF1 #!/bin/bash +[ -f /boot/cmdline.txt.pishrink-bak ] && mv -fT /boot/cmdline.txt.pishrink-bak /boot/cmdline.txt do_expand_rootfs() { ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p') @@ -175,9 +176,54 @@ EOF1 chmod +x "$mountdir/etc/rc.local.bak" fi fi + + # If rc.local is started by systemd it may fail to start if no console exists. + # Ensure a console is there until autoexpand was done: + boot_enable_console "$mountdir" + umount "$mountdir" } +function boot_enable_console () { + + local fsroot="$1" + local bootstart="$(echo "$parted_output" | grep '^1:' | cut -d ':' -f 2 | tr -d 'B')" + local boot bootloop bootroot + local SEDCMDS='s/console=null/console=tty0/' + + if [ -z "$fsroot" -o "x$bootstart" != "x$partstart" ] + then + bootloop="$(losetup -f --show -o "$bootstart" "$img")" + bootroot="$(mktemp -d)" + partprobe "$bootloop" + mount "$bootloop" "$bootroot" + boot="$bootroot" + else + boot="$fsroot/boot" + fi + + logVariables $LINENO fsroot bootstart bootloop bootroot boot + + if grep -q console=null "$boot/cmdline.txt" ; then + info "/boot/cmdline.txt changed to temporarily enable console for autoexpand" + cp -p "$boot/cmdline.txt" "$boot/cmdline.txt.pishrink-bak" + sed -i "$boot/cmdline.txt" -e "$SEDCMDS" + else + # ok, already enabled. If a backup exists, it was done by us; check if the backup is stil valid. + if [ -f "$boot/cmdline.txt.pishrink-bak" ]; then + if [ "$(sed "$boot/cmdline.txt.pishrink-bak" -e "$SEDCMDS")" = "$(cat "$boot/cmdline.txt")" ]; then + info "/boot/cmdline.txt already changed to temporarily enable console for autoexpand" + else + # console was enabled by design - not by us. Remove backup to prevent installing it. + rm -f "$boot/cmdline.txt.pishrink-bak" + fi + fi + fi + + [ -n "$bootroot" ] && umount "$bootroot" + [ -n "$bootloop" ] && losetup -d "$bootloop" +} + help() { local help read -r -d '' help << EOM From 53ec477188c664b5ea4117953245b1e0eca7fa22 Mon Sep 17 00:00:00 2001 From: Lutz Schwarz Date: Thu, 3 Sep 2020 18:07:17 +0200 Subject: [PATCH 3/3] Fix detection of disabled console --- pishrink.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 3c70801..fbe88c7 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -189,7 +189,7 @@ function boot_enable_console () { local fsroot="$1" local bootstart="$(echo "$parted_output" | grep '^1:' | cut -d ':' -f 2 | tr -d 'B')" local boot bootloop bootroot - local SEDCMDS='s/console=null/console=tty0/' + local SEDCMDS='s/console=\(none\|null\)/console=tty0/' if [ -z "$fsroot" -o "x$bootstart" != "x$partstart" ] then @@ -204,7 +204,7 @@ function boot_enable_console () { logVariables $LINENO fsroot bootstart bootloop bootroot boot - if grep -q console=null "$boot/cmdline.txt" ; then + if grep -q 'console=\(none\|null\)' "$boot/cmdline.txt" ; then info "/boot/cmdline.txt changed to temporarily enable console for autoexpand" cp -p "$boot/cmdline.txt" "$boot/cmdline.txt.pishrink-bak" sed -i "$boot/cmdline.txt" -e "$SEDCMDS"