blob: 28dc235c8687e836d9e44eca00fdfbb0363ffc79 (
plain) (
blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
#!/bin/sh
set -e
prompt_password() {
printf "Enter password: "
trap 'stty echo' INT
stty -echo
read -r PASSWORD
stty echo
printf "\n"
PASSWORD="$(echo "$PASSWORD" | shasum | awk '{print $1}')"
}
usage() {
echo "Usage: vault <new|open|close|resize> <vault>"
exit 1
}
random=$(tr -dc _A-Z-a-z-0-9 </dev/urandom | head -c10)
if [ "$1" = "new" ]; then
if [ -z "$2" ]; then
usage
fi
if [ -f "$2" ]; then
echo "File already exists"
exit 1
fi
touch "$2"
if [ ! -f ./"$(basename "$2")" ]; then
echo "You need to be in the same directory as the vault file"
exit 1
fi
vault="$2"
ident="$vault$random"
prompt_password
dd if=/dev/zero of="$vault" bs=1M count=32 >/dev/null 2>&1
echo "$PASSWORD" | cryptsetup -q -d - luksFormat "$vault"
echo "$PASSWORD" | sudo cryptsetup -q -d - luksOpen "$vault" "$ident"
sudo mkfs.ext4 -Fq /dev/mapper/"$ident"
sudo cryptsetup -q luksClose "$ident"
elif [ "$1" = "open" ]; then
if [ -z "$2" ]; then
usage
fi
base="$(basename "$2")"
if [ ! -f ./"$base" ]; then
echo "You need to be in the same directory as the vault file"
exit 1
fi
if [ -d ./"$base" ]; then
echo "There already exists a directory $base"
exit 1
fi
vault="$2"
ident="$vault$random"
newname=".$vault-$ident"
prompt_password
mv "$vault" "$newname"
mkdir "$vault"
echo "$PASSWORD" | sudo cryptsetup -q -d - luksOpen "$newname" "$ident"
sudo mount /dev/mapper/"$ident" "$vault"
sudo chown "$USER" "$vault"
elif [ "$1" = "close" ]; then
if [ -z "$2" ]; then
usage
fi
base="$(basename "$2")"
if [ ! -d ./"$base/" ]; then
echo "You need to be in the same directory as the opened vault"
exit 1
fi
opened="$base"
candidates="$(find . -type f -name ".$opened-*" 2>/dev/null | cat)"
if [ "$(echo "$candidates" | wc -l)" -gt 1 ]; then
echo "Multiple vaults with the same name are opened"
exit 1
fi
vault="$candidates"
ident="$(echo "$vault" | cut -d- -f2)"
sudo umount "$opened"
sudo cryptsetup -q luksClose "$ident"
rm -rf "$opened"
mv "$vault" "$opened"
elif [ "$1" = "resize" ]; then
if [ -z "$2" ]; then
usage
fi
if [ ! -f ./"$(basename "$2")" ]; then
echo "You need to be in the same directory as the vault file"
exit 1
fi
vault="$2"
ident="$vault$random"
# echo "Backing up $vault to .$ident.bak"
cp "$vault" ".$ident.bak"
current="$(ls -lh "$vault" | awk '{print $5}')"
echo "Current: $current"
printf "Expand by: "
read -r increase
# TODO: remove requirement for qemu-img
# I tried using dd/truncate/etc but it didn't work..
qemu-img resize -q -f raw "$vault" +"$increase"
prompt_password
echo "$PASSWORD" | sudo cryptsetup -q -d - luksOpen "$vault" "$ident"
echo "$PASSWORD" | sudo cryptsetup -q -d - resize /dev/mapper/"$ident"
sudo e2fsck -f /dev/mapper/"$ident" >/dev/null 2>&1
sudo resize2fs /dev/mapper/"$ident" >/dev/null 2>&1
sudo cryptsetup -q luksClose "$ident"
rm ".$ident.bak"
else
usage
fi
|