Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autoresize if no console #159

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 82 additions & 10 deletions pishrink.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,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')

Expand Down Expand Up @@ -146,12 +141,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
Expand Down