FunKey-OS/FunKey/board/funkey/rootfs-overlay/usr/sbin/resize_rootfs

81 lines
1.9 KiB
Bash
Executable File

#!/bin/sh
#set -x
THIS=$(basename $0)
echo "$THIS: resizing rootfs" | tee /dev/kmsg
if [ $(id -u) -ne 0 ]; then
echo "$THIS: this script must be run as root. Try 'sudo resize_rootfs'\n" | tee /dev/kmsg
exit 1
fi
ROOT_PART=$(cat /proc/cmdline | sed -n 's|^.*root=/dev/\([^ ]*\).*|\1|p')
PART_NUM=${ROOT_PART#mmcblk0p}
if [ "$PART_NUM" = "$ROOT_PART" ]; then
echo "$THIS: $ROOT_PART is not an SD card. Don't know how to expand" | tee /dev/kmsg
exit 2
fi
if [ "$PART_NUM" -ne 2 ]; then
echo "$THIS: your partition layout is not currently supported by this tool" | tee /dev/kmsg
exit 3
fi
LAST_PART_LINE=$(fdisk /dev/mmcblk0 -l | grep '^/' | tail -n 1)
set $LAST_PART_LINE
LAST_PART=${1#/dev/}
PART_START=${2}
if [ "$LAST_PART" != "$ROOT_PART" ]; then
echo "$THIS: $ROOT_PART is not the last partition. Don't know how to expand" | tee /dev/kmsg
exit 4
fi
# Return value will likely be error for fdisk as it fails to reload the
# partition table because the root fs is mounted
# NOTE: This script only works the genuine fdisk, NOT with the busybox one!!!
fdisk /dev/mmcblk0 <<EOF
p
d
$PART_NUM
n
p
$PART_NUM
$PART_START
n
p
w
EOF
sync
# Now set up an init.d script
cat <<EOF > /etc/init.d/S02resize2fs &&
#!/bin/sh
THIS=\$(basename \$0)
case "\$1" in
start)
sleep 1
echo "\$THIS: starting resizing the root filesystem" | tee /dev/kmsg &&
resize2fs /dev/$ROOT_PART &&
rm /etc/init.d/S02resize2fs
if [ \$? -eq 0 ]; then
echo "\$THIS: finished resizing the root filesystem" | tee /dev/kmsg
else
echo "\$THIS: cannot resize the root filesystem, aborting" | tee /dev/kmsg
fi
;;
*)
echo "Usage: \$0 start" >&2
exit 3
;;
esac
EOF
chmod +x /etc/init.d/S02resize2fs
echo "$THIS: the root partition has been resized" | tee /dev/kmsg
echo "$THIS: the system must reboot to enlarge the root filesystem" | tee /dev/kmsg