Raspberry Pi: Auto-Expand File System Shell Script

By phunter | Posted June 3, 2016 | Updated October 15, 2017

So with work, we have developed a process to develop and distribute software for Raspberry Pi’s for many purposes ranging from Menu Boards, to Kiosks all under Raspbian.

One of the critical failures of this process, is the learning curve of configuration once they are being deployed. Before, the Support Team had to have an understanding of Linux File System, how to SSH, and even if they knew all of this, it still took way to long to image an SD card, and configure it before sending it to a location. So today I have decided to update one of our oldest software packages, our Menu Board.

The first part was configuring the URL and Monitor ID for use at the locations. The URL is opened in Chromium after LXDE is started on boot (Boot to desktop option with raspi-config). Before you would have to login in and modify a shell script with the hard-coded URL. Instead, we have moved the files referencing the MonitorID and the sites subdomain to the /boot folder, which shows up under Windows when plugged in via USB, and can be edited using Notepad. The script then cat’s the file on LXDE Startup, and dumps it to a variable which is referenced in the URL. Easy.

The next part is the distribution process. When we make software, we make a master copy by doing all the code, scripts, and make it work. Once we are done, we shrink the image by sucking almost all of the excess unused space out by using an Ultimate Boot CD and create a .img file. Then we zip it, and upload it to a host somewhere offsite. Our support team can then grab the latest image and write it to an SD Card. Once this is done, they would have to normally SSH in, and expand and configure the Pi, as stated above.

Instead of that, I made a handy script that uses the same code from the Raspberry Pi’s raspi-config that will do it automatically.  It checks the physical disk size, as well as the partition size, and if the partition uses less than 90% of the disk (I’ve found that expanding usually only goes to about 96%), and if so it does the expansion and reboots. On reboot it checks to see if the temporary file /etc/init.d/resize2fs_once exists. If it does, it exits. This is because the expansion process is 2 stages. The first stage is preparing the partition, and the rest is on boot, before the filesystem is actually mounted (I think). Once that second stage is completed, it removes the resize2fs_once script and it is finished. So if it exists when LXDE starts (which it shouldn’t), this check script will not execute. Nice safeguard if you ask me.

Feel free to modify it and test it, and if there are any issues, feel free to drop me a line and ill patch it when possible.

Happy coding.


EDIT: I forgot about the serious differences between Jessie and Wheezy, and their impact on this kind of operation. As a result, I have now made one for Wheezy and one for Jessie.

EDIT 2: I made Stretch as well!

Wheezy Auto-Expand Script

Jessie Auto-Expand Script

Stretch Auto-Expand Script

Just to be clear, this will check partition size, versus actual disk size, and if the ratio is less than 90%, it will auto expand and reboot. In the event a reboot loop occurs, place a blank file named ‘NOCHECKUPDATE’ in /boot (or the drive you see when you put the SD card into a Windows computer) and it will stop the script from being executed 1 time.