Startseite
Astronomie
Gipfelbuch
Photos
Whisky
Whiskyrechner
Passwortgenerator
Simpsons
Code
xkcd

Kleingedrucktes
Kontakt
#!/bin/bash

# store the information about the mounted luks devices
# make sure that this file is in a ramdisk or in a directory which gets cleaned upon system start

logfile=/tmp/lukslog


function exit_print
{
    echo $1
    exit $2
}

# make sure that the script ends with the first command that does not return zero
set -e

if [ -z "$1" ];then
    exit_print "Argument necessary" 1
fi

# decide whether we have a file or a block device
if [ -b $1 ];then
    echo "$1 is block device..."
    crypted_device=$1    
elif [ -f $1 ];then
    echo "$1 is regular file..."
    # bind the file to a loop device
    crypted_device=$(udisksctl loop-setup -f $1 | awk '{print $NF}' | tr -d .)
else
    exit_print "Invalid argument" 1
fi

# unlock the device
device=$(udisksctl unlock -b $crypted_device | awk '/^Unlocked/{print $NF}')

device=$(echo $device | tr -d .)

# check for possible errors
if [ ! -b "$device" ];then
    exit_print "\"$device\" is no block device. Wrong password?" 1
fi

# mount the mapper device and store the name
target=$(udisksctl mount -b $device | awk '{gsub(/\.$/,"",$NF); print $NF}')

# look for the next unused number
if [ -f $logfile ];then
    num=$(tail -n1 $logfile 2>/dev/null | grep -o '^[^)]*' | awk '{print $1+1}')
else
    num=1
fi

# write the line in the logfile
echo "${num}) $device $target" >> $logfile