Persistent Filesystem Mounts

This content is licensed under The MIT License. See here for more details.

While mounting external storage in the form of a USB thumb drive may be an everyday task on a Juniper switch/router; be it for upgrades or filesystem recovery; there may be certain circumstances where it is desirable to have a mount persist across reboots.

If you find this necessary on production gear, please reconsider whether you’d be better served by an actual fileserver. As stated in The MIT License, I take no responsibility for this somehow microwaving your cat.

Frustratingly, the root partition (and thus /etc/fstab) of Junos are write-protected, which prevents the typical *nix approach to such a problem.

So what to do? Brute force it via cron script of course!

/root/mountusb.sh
#!/bin/sh
logger "Starting USB mount"
# Recreate the PATH that you typically get from a login shell.
#   You probably don't need all of it, but I couldn't be bothered to check which binary was where.
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/opt/sbin:/opt/bin:/usr/local/bin
export PATH

# Wait for the thumb drive to show up.
#   It may be wise to add a timeout here, such as an incrementing counter with a maximum number of attempts.
# Make sure the /dev path reflects how the specific chassis names USB devices!
#   EX4300's for example, use da0, and your USB device may be formatted differently.
devicepath="/dev/da1s1"
until `test -r $devicepath` && `test -r /var/images`
do
        logger "Waiting for USB device..."
        sleep 60
done
logger "Found USB device, mounting..."

# Assuming, of course, a FAT-formatted drive
mount -w -t msdosfs $devicepath /var/images

# Check if the mount was successful
mountreturn=${?}
if [ "$mountreturn" != 0 ];
then
        logger "Failed to mount USB device; error $mountreturn"
else
        logger "Mounted USB device"
fi
Make sure you mark the script executable with chmod o+x!

And then to get it to run at boot, add the following entry to root’s crontab (crontab -e):

@reboot /bin/sh /root/mountusb.sh