Added -e option to set the rootfs new size after first boot.

This commit is contained in:
Phuong Vu 2022-01-19 01:58:35 +07:00
parent 9137ba5e5f
commit 4162f2136c
1 changed files with 92 additions and 82 deletions

View File

@ -56,102 +56,108 @@ function checkFilesystem() {
e2fsck -y "$loopback" e2fsck -y "$loopback"
(( $? < 4 )) && return (( $? < 4 )) && return
if [[ $repair == true ]]; then if [[ $repair == true ]]; then
info "Trying to recover corrupted filesystem - Phase 2" info "Trying to recover corrupted filesystem - Phase 2"
e2fsck -fy -b 32768 "$loopback" e2fsck -fy -b 32768 "$loopback"
(( $? < 4 )) && return (( $? < 4 )) && return
fi fi
error $LINENO "Filesystem recoveries failed. Giving up..." error $LINENO "Filesystem recoveries failed. Giving up..."
exit 9 exit 9
} }
function set_autoexpand() { function set_autoexpand() {
#Make pi expand rootfs on next boot # Make pi expand root fs on next boot.
mountdir=$(mktemp -d) local mountdir=$(mktemp -d)
partprobe "$loopback" partprobe "$loopback"
mount "$loopback" "$mountdir" mount "$loopback" "$mountdir"
if [ ! -d "$mountdir/etc" ]; then if [ ! -d "$mountdir/etc" ]; then
info "/etc not found, autoexpand will not be enabled" info "/etc not found, autoexpand will not be enabled"
umount "$mountdir" umount "$mountdir"
return return
fi fi
if [[ -f "$mountdir/etc/rc.local" ]] && [[ "$(md5sum "$mountdir/etc/rc.local" | cut -d ' ' -f 1)" != "1c579c7d5b4292fd948399b6ece39009" ]]; then if [[ -f "$mountdir/etc/rc.local" ]] && [[ "$(md5sum "$mountdir/etc/rc.local" | cut -d ' ' -f 1)" != "1c579c7d5b4292fd948399b6ece39009" ]]; then
echo "Creating new /etc/rc.local" echo "Creating new /etc/rc.local"
if [ -f "$mountdir/etc/rc.local" ]; then if [ -f "$mountdir/etc/rc.local" ]; then
mv "$mountdir/etc/rc.local" "$mountdir/etc/rc.local.bak" if [ "$(awk 'FNR==2{print $1}' $mountdir/etc/rc.local)" != '#DONOTBACKUP' ]; then
fi # Creata a backup of rc.local if it's not marked.
mv "$mountdir/etc/rc.local" "$mountdir/etc/rc.local.bak"
fi
fi
#####Do not touch the following lines##### # Generate image's rc.local file to expand rootfs on first boot.
cat <<\EOF1 > "$mountdir/etc/rc.local" cat <<-RCLOCAL1 > "$mountdir/etc/rc.local"
#!/bin/bash #!/bin/bash
do_expand_rootfs() { #DONOTBACKUP Prohibit pishrink.sh from creating backup to avoid boot looping.
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p') SIZE=$newsize
if ! [[ "$ROOT_PART" =~ mmcblk.+ ]]; then RCLOCAL1
echo "$ROOT_PART is not an SD card. Don't know how to expand"
return 0
fi
PART_NUM=${ROOT_PART: -1} cat <<-'RCLOCAL2' >> "$mountdir/etc/rc.local"
DEV=$(ls /dev/mmcblk[0-9]) do_expand_rootfs() {
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
# Get the starting offset of the root partition if ! [[ "$ROOT_PART" =~ mmcblk.+ ]]; then
PART_START=$(parted $DEV -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g') echo "$ROOT_PART is not an SD card. Don't know how to expand"
[ "$PART_START" ] || return 1 return 0
# Return value will likely be error for fdisk as it fails to reload the fi
# partition table because the root fs is mounted
fdisk $DEV <<EOF
p
d
$PART_NUM
n
p
$PART_NUM
$PART_START
p PART_NUM=${ROOT_PART: -1}
w DEV=$(ls /dev/mmcblk[0-9])
EOF
cat <<EOF > /etc/rc.local && # Get the starting offset of the root partition
#!/bin/sh PART_START=$(parted $DEV -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g')
echo "Expanding /dev/$ROOT_PART" [ "$PART_START" ] || return 1
resize2fs /dev/$ROOT_PART echo Root part $ROOT_PART, no. $PART_NUM, on $DEV, start $PART_START.
rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; /etc/rc.local if [ -n "$SIZE" ]; then
echo Will resize root partition to $SIZE
SIZE=+$SIZE
else
echo Will resize root partition to maximum available.
fi
# Return value will likely be error for fdisk as it fails to reload the
# partition table because the root fs is mounted
fdisk $DEV <<-EOFDISK
p
d
$PART_NUM
n
p
$PART_NUM
$PART_START
$SIZE
p
w
EOFDISK
EOF cat <<-EOF > /etc/rc.local &&
reboot #!/bin/sh
exit echo Expanding /dev/$ROOT_PART to ${SIZE:-maximum}
} resize2fs /dev/$ROOT_PART $SIZE
raspi_config_expand() { rm -f /etc/rc.local
/usr/bin/env raspi-config --expand-rootfs cp -f /etc/rc.local.bak /etc/rc.local
if [[ $? != 0 ]]; then . /etc/rc.local
return -1
else EOF
rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; /etc/rc.local reboot
reboot exit
exit }
fi
} echo Expanding root...
raspi_config_expand do_expand_rootfs
echo "WARNING: Using backup expand..." echo ERROR: Expanding failed! Revert to original rc.local...
sleep 5 if [ -f /etc/rc.local.bak ]; then
do_expand_rootfs cp -f /etc/rc.local.bak /etc/rc.local
echo "ERROR: Expanding failed..." . /etc/rc.local
sleep 5 fi
if [[ -f /etc/rc.local.bak ]]; then exit 0
cp -f /etc/rc.local.bak /etc/rc.local RCLOCAL2
/etc/rc.local
fi chmod +x "$mountdir/etc/rc.local"
exit 0 fi
EOF1 umount "$mountdir"
#####End no touch zone#####
chmod +x "$mountdir/etc/rc.local"
fi
umount "$mountdir"
} }
help() { help() {
@ -159,6 +165,8 @@ help() {
read -r -d '' help << EOM read -r -d '' help << EOM
Usage: $0 [-adhrspvzZ] imagefile.img [newimagefile.img] Usage: $0 [-adhrspvzZ] imagefile.img [newimagefile.img]
-e n Expand to size "n" during first boot. See the "size" argument of
the resize2fs command. Example: "-e 5G".
-s Don't expand filesystem when image is booted the first time -s Don't expand filesystem when image is booted the first time
-v Be verbose -v Be verbose
-r Use advanced filesystem repair option if the normal one fails -r Use advanced filesystem repair option if the normal one fails
@ -172,6 +180,7 @@ EOM
exit 1 exit 1
} }
newsize=
should_skip_autoexpand=false should_skip_autoexpand=false
debug=false debug=false
repair=false repair=false
@ -180,8 +189,9 @@ verbose=false
prep=false prep=false
ziptool="" ziptool=""
while getopts ":adhprsvzZ" opt; do while getopts ":ade:hprsvzZ" opt; do
case "${opt}" in case "${opt}" in
e) newsize=$OPTARG;;
a) parallel=true;; a) parallel=true;;
d) debug=true;; d) debug=true;;
h) help;; h) help;;