0

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.

  • 1
    have you tried simply adding init=/usr/lib/raspi-config/init_resize.sh in /boot/cmdline.txt ? – Jaromanda X Jul 29 '18 at 23:30
  • @JaromandaX No, I haven't, but I will, and I'll report back. (Probably not until tomorrow, though.) – Bob Brown Jul 29 '18 at 23:33
  • to be clear, /boot/cmdline.txt must contain only a single line, so that init=... needs to be added to the end of the existing line with a space before init – Jaromanda X Jul 29 '18 at 23:39
  • @JaromandaX Thanks. I'd have messed that up. Testing now... – Bob Brown Jul 29 '18 at 23:45
  • 1
    Found it - raspberrypi.org/forums/viewtopic.php?t=174434 - there's link to a github to copy the current resize2fs_once script but the link appears broken - use raw.githubusercontent.com/RPi-Distro/pi-gen/master/stage2/… – Jaromanda X Jul 30 '18 at 0:53
2

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

  • damn yes, wrong file :p (wow, I'm having senior moments today :p ) – Jaromanda X Jul 30 '18 at 1:57
  • I think the error is in the Raspberry Pi forum post. It looks like quite a lot of that first post is subtly wrong. Might be there's supposed to be a quiet before init=..., too. – Bob Brown Jul 30 '18 at 2:02
  • 1
    quiet is irrelevant. it can be there or not, doesn't change anything regarding this question - that's why I said it may end up looking like - that cmdline is basically what I have in my Pi's – Jaromanda X Jul 30 '18 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.