2010-05-31

How to write a script to reliably mount Truecrypt volumes on an external drive in Ubuntu

I have had an annoying problem for years.  I have two external hard drives hooked up to my Ubuntu server that are encrypted with Truecrypt.  In order to mount these Truecrypt volumes on the correct mountpoints after a reboot I had to figure out which external drive was mounted as /dev/sdb and which as /dev/sdc since Ubuntu doesn't necessarily mount the same external drive in the same place every time the system is restarted.  For a long time I deduced which drive was mounted as /dev/sdb and which as /dev/sdc by running sudo fdisk -l which shows the sizes and mount points off all attached drives.  However, I recently found directions written by Ubuntu Forums user B-Con on how to make a script to reliably mount each Truecrypt volume in the right place regardless of which /dev/sd* it gets:

http://ubuntuforums.org/showthread.php?t=468664

Basically, the script uses ls -l /dev/disk/by-id to get a listing of all of the attached drives that includes each drives unique id code and it's mount point, then it uses grep and cut to extract the mount point and put it in a variable, which can then be used with the Truecrypt mount command. In the example below you will have to change the part in quotes to be the unique id code of your drive.

#! /bin/bash

my_var=`ls -l /dev/disk/by-id | grep "scsi-1ATA_Maxtor_7H500F0_H81E4GAH-part1" | cut -d / -f 3`
vol_path="/dev/${my_var}"
# Use vol_path as the path for your drive, now.

3 comments:

李嘉娟 said...

IS VERY GOOD..............................

Danial Namousi said...

That's great! Thanks.

I'll be grep:ing for disk serial number only (usually last part of the identifier string) and use 'head -1' to limit multiple matches.

ls -l /dev/disk/by-id/ | grep $disk_serial | cut -d / -f 3 | head -1

Danial Namousi said...

That's great! Thanks.

I'll be grep:ing for disk serial number only and use 'head -1' to limit matches.

ls -l /dev/disk/by-id/ | grep $serial | cut -d / -f 3 | head -1