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
/dev/root 7.2G 4.4G 2.5G 64% /
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.