The Raspbian Approach is as follows, I would duplicate this approach. raspi-config
provides a resize script that uses parted
(a command line utility) to resize the filesystem on boot. This script is located in /usr/lib/raspi-config/init_resize.sh
- Create image with minimal fs size
- Specify in
/boot/cmdline.txt
the option init=/usr/lib/raspi-config/init_resize.sh
- On first boot this script will execute and delete the line from
/boot/cmdline.txt
Before: dwc_otg.lpm_enable=0 init=/usr/lib/raspi-config/init_resize.sh console=serial0,115200 console=tty1 root=PARTUUID=23668fa2-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
After: dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=23668fa2-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
You can choose to use the default raspi-config
provided resize script by including the package in your base image, or, you may choose to create your own script to package with your image, using the same approach to do whatever other first-boot configuration you wish.
The resizing brains of the script are reproduced below
if [ "$NOOBS" = "1" ]; then
if ! parted -m "$ROOT_DEV" u s resizepart "$EXT_PART_NUM" yes "$TARGET_END"; then
FAIL_REASON="Extended partition resize failed"
return 1
fi
fi
if ! parted -m "$ROOT_DEV" u s resizepart "$ROOT_PART_NUM" "$TARGET_END"; then
FAIL_REASON="Root partition resize failed"
return 1
fi
Snippet to Resize Root Partition : parted -m "$ROOT_DEV" u s resizepart "$ROOT_PART_NUM" "$TARGET_END"
Rewriting the /boot/cmdline.txt
is done with this line
sed -i 's| init=/usr/lib/raspi-config/init_resize.sh||' /boot/cmdline.txt
/usr/lib/raspi-config/init_resize.sh
. I would simply copy their approach. The magic strep is thatinit_resize
is actually specified asinit
for the first boot in/boot/cmdline.txt
, and then deletes itself from that file and reverts to default (systemd
orinit
depending on version)