Raspberry Pi: Auto-Mount Veracrypt Volumes at Startup

I use a Raspberry Pi with four drives housed in a Yottamaster USB enclosure. These are encrypted using VeraCrypt which can be a pain if/when the Pi loses power and restarts since the volumes become unmounted and inaccessible. The solution is to auto-mount the drives when the Pi starts up via the crontab.

Please note: This method requires the drive passwords to be stored in a text (shell script) file. If you need stronger security, this method is not for you.

Part 1: find the UUID of the drives:

ls /dev/disk/by-id

In my case, this results in:

mmc-SC64G_0x5ac51d9c
mmc-SC64G_0x5ac51d9c-part1
mmc-SC64G_0x5ac51d9c-part2
usb-JMicron_Generic_DISK00_0123456789ABCDEF-0:0
usb-JMicron_Generic_DISK00_0123456789ABCDEF-0:0-part1
usb-JMicron_Generic_DISK00_0123456789ABCDEF-0:0-part2
usb-JMicron_Generic_DISK01_0123456789ABCDEF-0:1
usb-JMicron_Generic_DISK01_0123456789ABCDEF-0:1-part1
usb-JMicron_Generic_DISK01_0123456789ABCDEF-0:1-part2
usb-JMicron_Generic_DISK02_0123456789ABCDEF-0:2
usb-JMicron_Generic_DISK02_0123456789ABCDEF-0:2-part1
usb-JMicron_Generic_DISK02_0123456789ABCDEF-0:2-part2
usb-JMicron_Generic_DISK03_0123456789ABCDEF-0:3
usb-JMicron_Generic_DISK03_0123456789ABCDEF-0:3-part1
usb-JMicron_Generic_DISK03_0123456789ABCDEF-0:3-part2

From experience, trial and error, I know the lines I want are those ending in “-part2”.

Part 2: Create a shell script:

I now create a shell script (mount.sh) on the Desktop with the following. This script will run at startup, sleep 20 seconds to let the Pi finish all its startup stuff, and then mount the drives:

nano /home/root/Desktop/mount.sh

…and add the following. Don’t forget to replace the usb-JMicron_Generic…part2 with your drive ids from Step 1. Notice also that there is a space between part2 and /media/veracrypt1.

#!/bin/bash
sleep 20
veracrypt -t --non-interactive -p 'diskPasswordHere' /dev/disk/by-id/usb-JMicron_Generic_DISK00_0123456789ABCDEF-0:0-part2 /media/veracrypt1
sleep 5
veracrypt -t --non-interactive -p 'diskPasswordHere' /dev/disk/by-id/usb-JMicron_Generic_DISK01_0123456789ABCDEF-0:1-part2 /media/veracrypt2
sleep 5
veracrypt -t --non-interactive -p 'diskPasswordHere' /dev/disk/by-id/usb-JMicron_Generic_DISK02_0123456789ABCDEF-0:2-part2 /media/veracrypt3
sleep 5
veracrypt -t --non-interactive -p 'diskPasswordHere' /dev/disk/by-id/usb-JMicron_Generic_DISK03_0123456789ABCDEF-0:3-part2 /media/veracrypt4

Don’t forget to make the script executable:

sudo chmod +x /home/root/Desktop/mount.sh

Part 3: Add it to the crontab

Those drives won’t do much good if they’re not mounted. To resolve this, edit your crontab to get the job done on startup.

crontab -e

Add the following to the end of the file:

@reboot /home/root/Desktop/./mount.sh &

Now, when your Raspberry Pi reboots, it should mount your Veracrypt volumes automatically.