Created
December 16, 2020 14:19
-
-
Save oldsj/74bfcb867e3f9b098b4b77ca2263fc47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### BEGIN INIT INFO | |
# Provides: expand_rootfs | |
# Required-Start: | |
# Required-Stop: | |
# Default-Start: 3 | |
# Default-Stop: | |
# Short-Description: Expand the root filesystem to fill partition | |
# Description: | |
### END INIT INFO | |
do_expand_rootfs() { | |
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p') | |
PART_NUM=${ROOT_PART#mmcblk0p} | |
if [ "$PART_NUM" = "$ROOT_PART" ]; then | |
echo "$ROOT_PART is not an SD card. Don't know how to expand" | |
return 0 | |
fi | |
# NOTE: the NOOBS partition layout confuses parted. For now, let's only | |
# agree to work with a sufficiently simple partition layout | |
if [ "$PART_NUM" -ne 2 ]; then | |
log_daemon_msg "Your partition layout is not currently supported by this tool. You are probably using NOOBS, in which case your root filesystem is already expanded anyway." | |
return 0 | |
fi | |
LAST_PART_NUM=$(parted /dev/mmcblk0 -ms unit s p | tail -n 1 | cut -f 1 -d:) | |
if [ $LAST_PART_NUM -ne $PART_NUM ]; then | |
log_daemon_msg "$ROOT_PART is not the last partition. Don't know how to expand" | |
return 0 | |
fi | |
# Get the starting offset of the root partition | |
PART_START=$(parted /dev/mmcblk0 -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g') | |
[ "$PART_START" ] || return 1 | |
# Return value will likely be error for fdisk as it fails to reload the | |
# partition table because the root fs is mounted | |
fdisk /dev/mmcblk0 <<EOF | |
p | |
d | |
$PART_NUM | |
n | |
p | |
$PART_NUM | |
$PART_START | |
p | |
w | |
EOF | |
# now set up an init.d script | |
cat <<EOF > /etc/init.d/resize2fs_once && | |
#!/bin/sh | |
### BEGIN INIT INFO | |
# Provides: resize2fs_once | |
# Required-Start: | |
# Required-Stop: | |
# Default-Start: 2 3 4 5 S | |
# Default-Stop: | |
# Short-Description: Resize the root filesystem to fill partition | |
# Description: | |
### END INIT INFO | |
. /lib/lsb/init-functions | |
case "\$1" in | |
start) | |
log_daemon_msg "Starting resize2fs_once" && | |
resize2fs /dev/$ROOT_PART && | |
update-rc.d resize2fs_once remove && | |
rm /etc/init.d/resize2fs_once && | |
log_end_msg \$? | |
;; | |
*) | |
echo "Usage: \$0 start" >&2 | |
exit 3 | |
;; | |
esac | |
EOF | |
chmod +x /etc/init.d/resize2fs_once && | |
systemctl enable resize2fs_once | |
rm -rf /etc/init.d/expand_rootfs | |
} | |
. /lib/lsb/init-functions | |
case "$1" in | |
start) | |
log_daemon_msg "Starting expand rootfs" && | |
do_expand_rootfs && | |
/sbin/reboot | |
;; | |
*) | |
echo "Usage: $0 start" >&2 | |
exit 3 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment