You are not logged in.

#1 2016-04-25 00:11:22

zunder
Member
Registered: 2016-04-22
Posts: 10

[SOLVED] cryptsetup - map and mount

I am trying to bind an encrypted drive via cryptsetup and an i3 shortcut:

bindsym $mod+t exec urxvt -e sudo cryptsetup --type tcrypt open /dev/sdc2 tc
bindsym $mod+z exec sudo mount -o uid=1000 /dev/mapper/tc ~/tc

This works, however I would prefer a one-liner solution. Using truecrypt directly works:

bindsym $mod+t exec truecrypt /dev/sdc2 ~/tc

This maps and mounts the volume. Is this also possible with cryptsetup? The first line ($mod+t) requires entering of password. After hitting enter, the urxvt terminal closes. That is the moment the command $mod+z should kick in (or a second after). A bash command like this would do as well.

Last edited by zunder (2016-04-26 16:16:59)

Offline

#2 2016-04-25 00:44:11

frostschutz
Member
Registered: 2013-11-15
Posts: 1,418

Re: [SOLVED] cryptsetup - map and mount

You could always write a shell script wrapper and then execute your own script with your binding. Cryptsetup by itself does not mount.

Or you find another command that already wraps these for you. udisksctl mount perhaps? But the mount would then most likely be located somewhere in /media/

Or you run it as `bash -c 'cmd a && cmd b'`

Last edited by frostschutz (2016-04-25 00:45:15)

Offline

#3 2016-04-25 21:44:54

zunder
Member
Registered: 2016-04-22
Posts: 10

Re: [SOLVED] cryptsetup - map and mount

A wrapper like that would require a sleep time in between map and mount commands, am I correct? Just wondering if there is a nicer solution for that. Maybe a loop checking for existence of /dev/mapper/tcdrive? I am not that good in writing scripts.. could somebody give me a hint to achieve that? I am looking for something like this:

exec urxvt -e sudo cryptsetup --type tcrypt open /dev/sdc2 tc
loop until /dev/mapper/tc exists
exec sudo mount -o uid=1000 /dev/mapper/tc ~/tc

Offline

#4 2016-04-25 21:51:12

frostschutz
Member
Registered: 2013-11-15
Posts: 1,418

Re: [SOLVED] cryptsetup - map and mount

It shouldn't require sleep... the device should exist as soon as cryptsetup exits.

Based on your example, maybe this would work, I have no way to try it myself:

bindsym $mod+t exec urxvt -e sudo bash -c cryptsetup --type tcrypt open /dev/sdc2 tc && mount -o uid=1000 /dev/mapper/tc ~/tc

or

bindsym $mod+t exec urxvt -e bash -c sudo cryptsetup --type tcrypt open /dev/sdc2 tc && sudo mount -o uid=1000 /dev/mapper/tc ~/tc

Maybe && must be quoted in '&&', depending whether bindsym already executes in a shell context or not.

Last edited by frostschutz (2016-04-25 21:53:30)

Offline

#5 2016-04-25 22:33:25

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: [SOLVED] cryptsetup - map and mount

zunder wrote:

A wrapper like that would require a sleep time in between map and mount commands, am I correct? Just wondering if there is a nicer solution for that. Maybe a loop checking for existence of /dev/mapper/tcdrive? I am not that good in writing scripts.. could somebody give me a hint to achieve that? I am looking for something like this:

exec urxvt -e sudo cryptsetup --type tcrypt open /dev/sdc2 tc
loop until /dev/mapper/tc exists
exec sudo mount -o uid=1000 /dev/mapper/tc ~/tc
#!/usr/bin/bash
# set -e ensures that the script exits immediately with an error if any of the following commands exit with an error.
set -e

urxvt -e sudo cryptsetup --type tcrypt open /dev/sdc2 tc

# Make sure it exists. This is probably unnecessary if the previous command exited without error.
max_loops=50
i=0
while [[ ! -e /dev/mapper/tc ]]
do
  sleep 0.1
  i=$((i+1))
  [[ $i -gt $max_loops ]] && exit 1
done

sudo mount -o uid=1000 /dev/mapper/tc ~/tc

My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#6 2016-04-26 16:16:46

zunder
Member
Registered: 2016-04-22
Posts: 10

Re: [SOLVED] cryptsetup - map and mount

frostschutz wrote:

It shouldn't require sleep... the device should exist as soon as cryptsetup exits.

bindsym $mod+t exec urxvt -e bash -c sudo cryptsetup --type tcrypt open /dev/sdc2 tc && sudo mount -o uid=1000 /dev/mapper/tc ~/tc

You are right, that did work out perfectly. Thanks.

And thank you Xyne for posting the script. I might need something like this at some point, so it is good to know an example here.

Offline

Board footer

Powered by FluxBB