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,037
  • 8
  • 12
  • 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
  • @JaromandaX I really wish that had worked because it would have been the perfect solution. I've edited the question to report the results of trying. – Bob Brown Jul 30 '18 at 00:20
  • did the partition size change at all? the first reboot simply expands the partition, but does not expand the filesystem to fill the whole partition - I know there is a second phase to extending the partition, but not sure what triggers it - perhaps it's the /etc/init.d/resizefs_once - I'm not really sure to be honest – Jaromanda X Jul 30 '18 at 00:36
  • @JaromandaX I didn't check the partition size, only the filesystem. I'll try checking the partition tomorrow. Have to re-flash the card first. – Bob Brown Jul 30 '18 at 00:45
  • 1
    Found it - https://www.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 https://raw.githubusercontent.com/RPi-Distro/pi-gen/master/stage2/01-sys-tweaks/files/resize2fs_once – Jaromanda X Jul 30 '18 at 00:53

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,415
  • 1
  • 13
  • 14
  • damn yes, wrong file :p (wow, I'm having senior moments today :p ) – Jaromanda X Jul 30 '18 at 01: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 02: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 02:07
  • The file still hasn't changed - June 2020. – Janos Jun 20 '20 at 15:15