2

I currently have the following script. I run this manually when I need to mount the disk described. I don't need/want this disk auto-mounted on boot:

disk1="/dev/disk/by-uuid/84ea2a10-abcd-abcd-abcd-06deabcdabcd"
sudo cryptsetup luksOpen "${disk1}" disk1-crypt
sudo mount /dev/mapper/disk1-crypt /mnt/disk1

Is it possible to set up Linux such that it knows to automatically do the luksOpen command when I try to mount this disk? If possible, also luksClose when I unmount it.

1 Answer 1

1

Not an elegant solution, but it may serve your use case (and it's definitely how I would solve it myself).

You can create a bash script that overrides the mount command as an alias, checking the arguments and doing what you need when that exact argument is passed, falling back to the actual mount command when anything else is passed.

Have a look at this script:

#!/bin/bash

mount_disk1 () {
    disk1="/dev/disk/by-uuid/84ea2a10-abcd-abcd-abcd-06deabcdabcd"
    sudo cryptsetup luksOpen "${disk1}" disk1-crypt
    sudo mount /dev/mapper/disk1-crypt /mnt/disk1
}

[[ "$@" == "disk1" ]] && mount_disk1 \
|| mount "$@"

Here you have a function that does what you want. If the only thing you pass into it is disk1 it will execute that function, else it'll execute the original mount passing all arguments.

You can put this script in a folder and set an alias in your ~/.bashrc or ~/.zshrc using

alias mount='<path to script>'

Then if you want to use the original mount, you can run \mount in the terminal.

Hope it helps (again)!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .