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. 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.
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!
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..."

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