This commit is contained in:
yablacky 2023-02-27 12:02:14 +01:00 committed by GitHub
commit 032e07846b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 82 additions and 10 deletions

View File

@ -84,15 +84,10 @@ 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
[ -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')
@ -152,12 +147,89 @@ 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
# 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=\(none\|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=\(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"
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