0

I'm mounting a bunch of encrypted disks like so

cryptsetup luksOpen some_source some_mapping &&
mount /dev/mapper/some_mapping /some_mountpoint

How can I abstract away the middle mapping?

I suppose I could do something like:

uuid=$(sudo some_device |head -n1)
some_mapping=$uuid
 cryptsetup luksOpen some_source $some_mapping &&
    mount /dev/mapper/$some_mapping /some_mountpoint

and put this in a script.

Is there a better way? Can I also make sure that umounting automatically removes the middle mapping?

1

Yah I use scripts for that because I also grow my encripted volumes with LVM sometimes. I use a script like this;

#!/bin/bash

set -e
trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR

M="$1"
C=$(mount | grep -c "$M" || true)
if [ "$C" -gt 0 ] ; then
    echo "/media/$M already mounted" >&2
    exit 1
fi
vgimport "vg_$M"
vgchange -ay "vg_$M"
cryptsetup luksOpen "/dev/vg_$M/lv_$M" "crypt_$M"
if [ ! -d "/media/$M" ] ; then
    mkdir "/media/$M"
fi
mount "/dev/mapper/crypt_$M" "/media/$M"
  • I never seem to need the vgimport/vchange commands. My vg mappings seem to be always even in initramfs. IDK why. Anyway, thanks for the inspiration. – PSkocik Aug 9 '18 at 19:12
  • @PSkocik I use VG import/export because these particular drives are detached from the system and one would not want to detach them without first making absolutely sure they are not in use. – user1133275 Aug 9 '18 at 19:45
0

I tried

   mount the_encrypted_lv the_mount_point

and it asked for a password and correctly decrypted and mounted the volume.

mount then showed the mountpoint and an autogenerated intemmediary mapping. umount then removed both.

Sometimes the naive thing works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.