1

Edit: This question was originally about debugging /usr/bin/raspi-config --expand-rootfs. What I was really trying to do was expand the Raspbian file system the first time a newly flashed card was booted. I'm still not sure why what I was doing didn't work, but the accepted answer describes a standard way of expanding the file system on first boot. I've re-titled the question to make it easier for others to find the accepted answer. Original question below, but there's no need to slog through it. Just go straight to the answer.


I am copying, with dcfldd, an 8 GB Raspbian 9 (Stretch) image onto 16 GB micro SD cards because the 8 GB image goes twice as fast and I don't have a supply of 8 GB cards. I want to automagically resize the file system the first time the 16 GB card is booted. I've read this question and the answers, but there appears to be no automatic resizing with the software I'm using.

Using information from Andrew Oakley I created a sentinel file on the 8 GB image with sudo touch /boot/1stboot.txt and added the following to root's crontab @reboot if [ -e /boot/1stboot.txt ]; then rm /boot/1stboot.txt ; /usr/bin/raspi-config --expand-rootfs ; /sbin/shutdown -r now ; fi using sudo crontab -e.

When I boot the newly-flashed card 16 GB, the reboot happens, i.e. I see two rainbow screens, and the sentinel file is gone. However, the file system is still 8 GB as shown by df -h. If I manually run sudo raspi-config --expand-rootfs and reboot, the file system is correctly resized.

There appears to be nothing logged to indicate why the file system isn't resized on that first boot. I've used grep to look for raspi-config throughout the /var/log directory, and I've verified that raspi-config really is in /usr/bin. How can I debug this? (Or, if what I'm doing wrong is glaringly obvious, please tell me!)

Edit: As suggested in the comments by Jaromanda X, I added init=/usr/lib/raspi-config/init_resize.sh to the end of /boot/cmdline.txt on the card that's the origin of the image, made an image file, and flashed the image onto a 16 GB SD card. On first boot of the 16 GB card, I got a gray box with the message, "Resized root file system. Rebooting in 5 seconds." The system rebooted, but the root file system was still 7.2 GB as reported by df -h.

/dev/root 7.2G 4.4G 2.5G 64% /

Running sudo /usr/bin/raspi-config --expand-rootfs and rebooting now reports a 15 GB root file system.

/dev/root 15G 4.4G 9.5G 32% /

For whatever reason, attempting to run raspi-config --expand-rootfs fails silently at boot time, but works on a running system.

Bob Brown
  • 1,091
  • 8
  • 14

1 Answers1

6

First edit /boot/cmdline.txt and append init=/usr/lib/raspi-config/init_resize.sh on to the first (and only) line

e.g. it may end up looking like

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=a8fe70f4-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait init=/usr/lib/raspi-config/init_resize.sh

next, you'll need to create the "/etc/init.d/resize2fs_once" file as follows

sudo wget -O /etc/init.d/resize2fs_once https://raw.githubusercontent.com/RPi-Distro/pi-gen/master/stage2/01-sys-tweaks/files/resize2fs_once
sudo chmod +x /etc/init.d/resize2fs_once
sudo systemctl enable resize2fs_once

Note: the current content of that file is

#!/bin/sh
### BEGIN INIT INFO
# Provides:          resize2fs_once
# Required-Start:
# Required-Stop:
# Default-Start: 3
# 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"
    ROOT_DEV=$(findmnt / -o source -n) &&
    resize2fs $ROOT_DEV &&
    update-rc.d resize2fs_once remove &&
    rm /etc/init.d/resize2fs_once &&
    log_end_msg $?
    ;;
  *)
    echo "Usage: $0 start" >&2
    exit 3
    ;;
esac

This has not changed since 8 March 2017

Jaromanda X
  • 2,481
  • 1
  • 15
  • 15