Add support for expanding a bootable USB

I changed $ROOT_PART to get its value from findmnt, which will give us /dev/sdaX in the event we are booting off of a USB. This will still work for when booting of a microSD card in the microSD card slot, as findmnt will give us /dev/mmcblk0pX.

Since I can't predict how many partitions someone might have before their root partition, I used lsblk to find the name of the root device (/dev/sda2 -> /dev/sda and /dev/mmcblk0p2 -> /dev/mmcblk0)

I've tested this out booting off USB, and off of a microSD card in the microSD card slot, and it's worked every time so far.

I did remove the  portion checking if PART_NUM == ROOT_PART, but since we can expand a bootable USB, I don't see why it's necessary. 

I believe this should fix #63
This commit is contained in:
Chris Weir 2019-01-29 15:50:56 -08:00 committed by GitHub
parent 76d2254d90
commit a91980d7ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 13 deletions

View File

@ -82,26 +82,21 @@ if [ "$should_skip_autoexpand" = false ]; then
cat <<\EOF1 > "$mountdir/etc/rc.local" cat <<\EOF1 > "$mountdir/etc/rc.local"
#!/bin/bash #!/bin/bash
do_expand_rootfs() { do_expand_rootfs() {
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p') ROOT_PART=$(findmnt / -o source -n)
ROOT_DEV=/dev/$(lsblk -no pkname $ROOT_PART)
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
# Get the starting offset of the root partition # 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=$(parted $ROOT_PART -ms unit s p | grep "^${ROOT_PART}" | cut -f 2 -d: | sed 's/[^0-9]//g')
[ "$PART_START" ] || return 1 [ "$PART_START" ] || return 1
# 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/mmcblk0 <<EOF fdisk $ROOT_DEV <<EOF
p p
d d
$PART_NUM $ROOT_PART
n n
p p
$PART_NUM $ROOT_PART
$PART_START $PART_START
p p
@ -110,8 +105,8 @@ EOF
cat <<EOF > /etc/rc.local && cat <<EOF > /etc/rc.local &&
#!/bin/sh #!/bin/sh
echo "Expanding /dev/$ROOT_PART" echo "Expanding $ROOT_PART"
resize2fs /dev/$ROOT_PART resize2fs $ROOT_PART
rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; /etc/rc.local rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; /etc/rc.local
EOF EOF