aboutsummaryrefslogtreecommitdiff
path: root/cmount.sh
blob: a6580c50446c02b6eff73623ad02f3b321d78338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/sh

if [ -z "$1" ] ; then
    echo 'Need a name to mount (if you know what i mean 💋)'
    exit 1
fi

name="${1}"


device=$(sudo cat /etc/crypttab | grep -v "^#" | grep '.' | tr -s ' ' | grep "^${name} " | cut -d' ' -f 2)
rootkey=$(sudo cat /etc/crypttab | grep -v "^#" | grep '.' | tr -s ' ' | grep "^${name} " | cut -d' ' -f 3)

res=0
if [ -n "${device}" ] ; then
    res=$(echo -e "${device}" | wc -l)
fi

if [ "${res}" -gt 1 ] ; then
    echo 'Too many crypted part with that name, sry baby'
    exit 2
elif [ "${res}" -eq 0 ] ; then
    echo "Didn't found that babe"
    exit 3
elif [ "${res}" -eq 1 ] ; then
#    echo '🍌 🍩'

    if [ -n "${rootkey}" -a "${rootkey}" != 'none' ] ; then
        sudo cryptsetup luksOpen --key-file "${rootkey}" "${device}" "${name}"
        res=${?}
        if [ "${res}" -ne 0 ] ; then
            echo 'Error with the key-file' "${rootkey}"
            echo 'Trying without'
            res=0
            rootkey=""
        fi
    fi

    if [ -z "${rootkey}" -o "${rootkey}" = 'none' ] ; then
        sudo cryptsetup luksOpen "${device}" "${name}" 
        res=${?}
    fi

    if [ "${res}" -eq 0 ] ; then
        if sudo mount "/dev/mapper/${name}" ; then
            echo 'Finish'
            exit
        else
            echo 'Error while mounting'
            echo 'undecrypting'
            if sudo cryptsetup luksClose "${device}" "${name}" ; then
                echo 'undecrypted'
                exit 4
            else
                echo 'Shit, i fail'
                exit 5
            fi
        fi
    else
        echo 'Error while decrypting'
        exit 6
    fi
else
    echo "I don't know why, but i can't mount him"
    exit 7
fi