Function: Mount Data

This is a function to mount one of the external encrypted USB drives that I use. It makes a lot of assumptions about names so it's pretty much an alias or shortcut, but sometimes things change so I added a little bit of flexibility to it and will try to document it enough that I can figure out what it's doing if I need to change (or fix) it.

The Function

This is the function declaration. There's going to be three optional arguments that it might take.

Argument Default Description
device sdb1 Name of the device as shown by fdisk -l without the directory (/dev/)
name monkeymount Name to use when opening the device
folder irvin Place to mount the contents of the disk (without /media/ at the beginning).

So if I plug in the USB drive and it shows up at /dev/sda1 and I decide that the mapped name will be "umma" and the folder will be "gumma". Then I

function mount-data --argument-names device name folder --description "Mount an encrypted drive."
    set ERROR 1

The ERROR is a number to return in the event we bail on mounting the drive for some reason.

The Parts

The Device

This is the name of the device in /dev that represents the drive to mount.

This command is useful sometimes. More so after it's been mounted.

lsblk -e7

This one is generally useful if you know the model of the drive.

sudo fdisk -l | grep sd -A 1

Note to future self: Consider making this an environment variable so you don't have to edit it if you switch USB ports.

if test -z $device
    set device "sdb1"
end

We'll add the /dev to it (without checking that it's already there for now) and then make sure it's a valid block-device.

set DEVICE_PATH "/dev/$device"

if not test -b $DEVICE_PATH
    echo "'$DEVICE_PATH' is not a valid block-device"
    mount-data-help
    return $ERROR
end

To be nice we'll let the user know what we're using.

echo "Using Device: $DEVICE_PATH"

The Mapping Name

This is the name that will be put into /dev/mapper/ which mount will use to mount the drive.

if test -z $name
    set name "monkeymount"
end
echo "Device Mapping: $name"

Mount Path

Now we'll handle the folder where the drive will be mounted. If the user doesn't give one the default is "irvin".

if test -z $folder
    set folder "irvin"
end

We'll assume that the folder is going inside the /media/ folder and that only the final folder (or path below /media/) is being passed in.

set MOUNT_PATH "/media/$folder"

If there's already something in the folder then we won't try and mount it.

if set COUNT (count (ls $MOUNT_PATH/))
    echo "$MOUNT_PATH not empty, not mounting"
    return $ERROR
end

If the folder doesn't exist we'll create it.

if not test -d $MOUNT_PATH
    echo "Creating '$MOUNT_PATH'"
    sudo mkdir --parents $MOUNT_PATH
end
echo "Mounting at: $MOUNT_PATH"

Mounting the Drive

First we'll use cryptsetup to decrypt the drive and put the mapping into /dev/mapper/.

echo
echo "Mounting External Data Drive"

sudo cryptsetup open $DEVICE_PATH $name

Now we'll mount the device to a folder.

sudo mount /dev/mapper/$name $MOUNT_PATH

A Help Printer

This is just a function to print a help string.

function mount-data-help
    echo "Mount an attached encrypted drive."
    echo
    echo "Usage: mount-data [device [name [folder]]]"
    echo "    device: /dev/<device block file>"
    echo "    name: /dev/wrapper/<name> to use"
    echo "    folder: /media/<folder> to mount into"
end

Links