Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save valyakuttan/4e3bc0437170e72378bd2caec3ac517b to your computer and use it in GitHub Desktop.
Save valyakuttan/4e3bc0437170e72378bd2caec3ac517b to your computer and use it in GitHub Desktop.
Mounting an encrypted USB drive (Luks) with udisksctl

See what's the block device

[yabbes@x220 ~]$ lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 298,1G  0 disk
├─sda1           8:1    0   500M  0 part  /boot
└─sda2           8:2    0 297,6G  0 part
  └─lvm        254:0    0 297,6G  0 crypt
    ├─vg0-swap 254:1    0     4G  0 lvm   [SWAP]
    ├─vg0-root 254:2    0    46G  0 lvm   /
    └─vg0-home 254:3    0 247,6G  0 lvm   /home
sdb              8:16   1   3,8G  0 disk
└─sdb1           8:17   1   3,8G  0 part

We can try to mount it but it's not mountable as it is an encrypted container that we first need to unlock so let's unlock and mount

[yabbes@x220 ~]$ udisksctl unlock -b /dev/sdb1
Passphrase:
Unlocked /dev/sdb1 as /dev/dm-4.

[yabbes@x220 ~]$ udisksctl mount -b /dev/dm-4
Mounted /dev/dm-4 at /run/media/yabbes/usbstick.

See that it is really mounted and do some 'work'

[yabbes@x220 ~]$ lsblk
NAME           MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda              8:0    0 298,1G  0 disk
├─sda1           8:1    0   500M  0 part  /boot
└─sda2           8:2    0 297,6G  0 part
  └─lvm        254:0    0 297,6G  0 crypt
    ├─vg0-swap 254:1    0     4G  0 lvm   [SWAP]
    ├─vg0-root 254:2    0    46G  0 lvm   /
    └─vg0-home 254:3    0 247,6G  0 lvm   /home
sdb              8:16   1   3,8G  0 disk
└─sdb1           8:17   1   3,8G  0 part
  └─luks-fb31ca8e-808f-4405-a034-351ef13696ee
               254:4    0   3,8G  0 crypt /run/media/yabbes/usbstick
[yabbes@x220 ~]$ cd /run/media/yabbes/usbstick/
[yabbes@x220 usbstick]$ echo "do some work"
do some work
[yabbes@x220 usbstick]$ touch newfile
[yabbes@x220 usbstick]$ cd # leave the device

safely unmount and lock the device

# first unmount
[yabbes@x220 ~]$ udisksctl unmount -b /dev/dm-4
Unmounted /dev/dm-4.

# then lock
[yabbes@x220 ~]$ udisksctl lock -b /dev/sdb1
Locked /dev/sdb1.

# check that it's unmounted and locked 
[yabbes@x220 ~]$ lsblk /dev/sdb
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  3,8G  0 disk
└─sdb1   8:17   1  3,8G  0 part
[yabbes@x220 ~]$

# power-off the device
[yabbes@x220 ~]$ udisksctl power-off -b /dev/sdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment