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,19 +56,19 @@ 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"
@ -81,13 +81,22 @@ function set_autoexpand() {
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
if [ "$(awk 'FNR==2{print $1}' $mountdir/etc/rc.local)" != '#DONOTBACKUP' ]; then
# Creata a backup of rc.local if it's not marked.
mv "$mountdir/etc/rc.local" "$mountdir/etc/rc.local.bak" mv "$mountdir/etc/rc.local" "$mountdir/etc/rc.local.bak"
fi 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.
SIZE=$newsize
RCLOCAL1
cat <<-'RCLOCAL2' >> "$mountdir/etc/rc.local"
do_expand_rootfs() {
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p') ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
if ! [[ "$ROOT_PART" =~ mmcblk.+ ]]; then if ! [[ "$ROOT_PART" =~ mmcblk.+ ]]; then
@ -101,54 +110,51 @@ do_expand_rootfs() {
# Get the starting offset of the root partition # Get the starting offset of the root partition
PART_START=$(parted $DEV -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g') PART_START=$(parted $DEV -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g')
[ "$PART_START" ] || return 1 [ "$PART_START" ] || return 1
echo Root part $ROOT_PART, no. $PART_NUM, on $DEV, start $PART_START.
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 # Return value will likely be error for fdisk as it fails to reload the
# partition table because the root fs is mounted # partition table because the root fs is mounted
fdisk $DEV <<EOF fdisk $DEV <<-EOFDISK
p p
d d
$PART_NUM $PART_NUM
n n
p p
$PART_NUM $PART_NUM
$PART_START $PART_START
$SIZE
p
w
EOFDISK
p cat <<-EOF > /etc/rc.local &&
w #!/bin/sh
EOF echo Expanding /dev/$ROOT_PART to ${SIZE:-maximum}
resize2fs /dev/$ROOT_PART $SIZE
rm -f /etc/rc.local
cp -f /etc/rc.local.bak /etc/rc.local
. /etc/rc.local
cat <<EOF > /etc/rc.local && EOF
#!/bin/sh
echo "Expanding /dev/$ROOT_PART"
resize2fs /dev/$ROOT_PART
rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; /etc/rc.local
EOF
reboot
exit
}
raspi_config_expand() {
/usr/bin/env raspi-config --expand-rootfs
if [[ $? != 0 ]]; then
return -1
else
rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; /etc/rc.local
reboot reboot
exit exit
fi }
}
raspi_config_expand echo Expanding root...
echo "WARNING: Using backup expand..." do_expand_rootfs
sleep 5 echo ERROR: Expanding failed! Revert to original rc.local...
do_expand_rootfs if [ -f /etc/rc.local.bak ]; then
echo "ERROR: Expanding failed..."
sleep 5
if [[ -f /etc/rc.local.bak ]]; then
cp -f /etc/rc.local.bak /etc/rc.local cp -f /etc/rc.local.bak /etc/rc.local
/etc/rc.local . /etc/rc.local
fi fi
exit 0 exit 0
EOF1 RCLOCAL2
#####End no touch zone#####
chmod +x "$mountdir/etc/rc.local" chmod +x "$mountdir/etc/rc.local"
fi fi
umount "$mountdir" umount "$mountdir"
@ -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;;