truecrypt_scripts/truecrypt_init.sh
author ft
Tue, 09 Sep 2014 09:19:17 +0200
changeset 25 25581dcb9e62
parent 17 980ca72ff1f1
child 27 a8c8d86b8501
permissions -rwxr-xr-x
changed information notifications (no popup anymore)
ft@1
     1
#!/bin/sh
ft@1
     2
ft@1
     3
BASEDIR="$(dirname $0)"
ft@1
     4
DEVICE="$1"
ft@1
     5
MOUNTPOINT="$2"
ft@1
     6
PASSWORD="$3"
ft@1
     7
KEYFILE="$4"
ft@1
     8
ft@16
     9
ft@16
    10
getRemoteIp ()
ft@16
    11
{
ft@16
    12
	ip_address=$(ifconfig eth0 | grep "inet " | awk '{ print $2 }' | cut -d ":" -f 2)
ft@16
    13
	ip_netmask=$(ifconfig eth0 | grep "inet " | awk '{ print $4 }' | cut -d ":" -f 2)
ft@16
    14
	remote_ip=$(ipcalc $ip_address/$ip_netmask | grep HostMin | awk '{ print $2}')
ft@16
    15
ft@16
    16
	echo $remote_ip
ft@16
    17
}
ft@16
    18
ft@16
    19
sendInfoNotification ()
ft@16
    20
{
ft@16
    21
	MESSAGE="$1"
ft@25
    22
	wget -q -T 3 -t 1 -O /dev/null "http://$(getRemoteIp):8090/message?msgtype=information&text=$MESSAGE"
ft@16
    23
}
ft@16
    24
ft@16
    25
sendErrorNotification ()
ft@16
    26
{
ft@16
    27
	MESSAGE="$1"
ft@17
    28
	wget -q -T 3 -t 1 -O /dev/null "http://$(getRemoteIp):8090/notification?msgtype=critical&text=$MESSAGE"
ft@16
    29
}
ft@16
    30
ft@16
    31
ft@1
    32
if [ -r "$BASEDIR/truecrypt_config.cfg" ]
ft@1
    33
then
ft@1
    34
	. "$BASEDIR/truecrypt_config.cfg"
ft@1
    35
else
ft@1
    36
	echo "truecrypt_config.cfg not found" >&2
ft@1
    37
	exit 1
ft@1
    38
fi
ft@1
    39
ft@8
    40
# make sure to have "/dev/sdb" (not "/dev/sdb1")
ft@15
    41
#DEVICE="${DEVICE:0:8}" the bash way does not work in dash -.-
ft@13
    42
DEVICE="$(echo "$DEVICE" | awk '{print substr($1,0,9)}')"
ft@8
    43
ft@16
    44
sendInfoNotification "Encrypt device"
ft@7
    45
if [ -z "$KEYFILE" ]
ft@7
    46
then
ft@7
    47
	message="$($tc_cmd -c --non-interactive --quick --filesystem=none --encryption=AES --hash=RIPEMD-160 -p "$PASSWORD" "$DEVICE")"
ft@7
    48
	result="$?"
ft@7
    49
else
ft@7
    50
	message="$($tc_cmd -c --non-interactive --quick --filesystem=none --encryption=AES --hash=RIPEMD-160 -p "$PASSWORD" -k "$KEYFILE" "$DEVICE")"
ft@7
    51
	result="$?"
ft@7
    52
fi
ft@16
    53
ft@7
    54
	
ft@7
    55
if [ "$result" != "0" ]
ft@7
    56
then
ft@16
    57
	sendErrorNotification "Encryption failed"
ft@7
    58
	exit 1
ft@7
    59
fi
ft@1
    60
ft@16
    61
sendInfoNotification "Device encrypted"
ft@16
    62
ft@1
    63
ft@1
    64
ft@1
    65
if [ -z "$KEYFILE" ]
ft@1
    66
then
ft@7
    67
	message="$message\n$($tc_cmd --non-interactive --filesystem=none -p "$PASSWORD" "$DEVICE")"
ft@7
    68
	result="$?"
ft@1
    69
else
ft@7
    70
	message="$message\n$($tc_cmd --non-interactive --filesystem=none -p "$PASSWORD" -k "$KEYFILE" "$DEVICE")"
ft@7
    71
	result="$?"
ft@1
    72
fi
ft@1
    73
	
ft@7
    74
if [ "$result" != "0" ]
ft@7
    75
then
ft@7
    76
	exit 1
ft@7
    77
fi
ft@7
    78
ft@7
    79
ft@7
    80
ft@16
    81
sendInfoNotification "Create NTFS filesystem on encrypted device"
ft@7
    82
ft@7
    83
TC_DEVICE=$(truecrypt -l | awk '{print $3}')
ft@7
    84
ft@7
    85
message="$message\n$(mkfs.ntfs --quick "$TC_DEVICE")"
ft@1
    86
result="$?"
ft@1
    87
ft@1
    88
if [ "$result" != "0" ]
ft@1
    89
then
ft@16
    90
	sendErrorNotification "Filesystem creation failed"
ft@1
    91
	exit 1
ft@1
    92
fi
ft@1
    93
ft@16
    94
sendInfoNotification "Filesystem successfully created"
ft@16
    95
ft@7
    96
mount "$TC_DEVICE" "$MOUNTPOINT" 
ft@7
    97
ft@16
    98
sendInfoNotification "Stick is now initialized"
ft@16
    99
ft@1
   100
echo "$message"
ft@1
   101
exit 0