forked from boyska/freepto-usb-utils
-
Notifications
You must be signed in to change notification settings - Fork 3
/
check-persistence
executable file
·245 lines (227 loc) · 4.81 KB
/
check-persistence
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env bash
RET_PERSISTENCE_FOUND=0
RET_SYNTAX_ERROR=1
RET_NOPERSISTENCE=10
VERBOSE=0
usage() {
# the -n and the trailing backslash are to avoid useless newlines
echo -en "\
Usage: $0 [options] COMMAND
Options can be:
\t-v\tverbose (without this, there is barely any output)
COMMAND can be one of:
\tis-mounted\tcheck if persistence is mounted
\tget-root-device\treturns the device from where freepto is running
\thas-avail-persistence\tcheck if there is a persistence partition
\tget-device-size\treturns the root device size
\tget-live-size\treturns the live partition size
\tget-persistence-size\treturns the persistence partition size
Exit codes are:
0\ton success
10\tfor 'not found'
1\tfor generic errors
2\tfor syntax errors
"
}
verbose() {
if [ "$VERBOSE" -eq 1 ]; then
echo $* >&2
fi
}
is_mounted() {
mapper=$(awk '$2 ~ /^\/lib\/live\/mount\/persistence/ { print $1 }' \
/proc/mounts | head -n1)
if [ -z "$mapper" ]; then
verbose "No persistence mounted"
return ${RET_NOPERSISTENCE}
fi
#device=$(dmsetup TODO)
#mountpoint=$(awk '{ print $NF }' <<<$dfrow)
echo "$mapper"
return ${RET_PERSISTENCE_FOUND}
}
get_root_partition() {
partition=$(awk '$2 ~ /^\/lib\/live\/mount\/medium$/ { print $1 }' \
/proc/mounts | head -n1)
if [ -z "$partition" ]; then
verbose "No root partition found; are you inside freepto?"
return ${RET_NOPERSISTENCE}
fi
if [ ! -b "$partition" ]; then
verbose "We found $partition, which doesn't look like a block" \
"device"
return 1
fi
echo $partition
return ${RET_PERSISTENCE_FOUND}
}
get_root_device() {
partition=$(get_root_partition)
ret=$?
if [[ $ret -ne ${RET_PERSISTENCE_FOUND} ]]; then
return $ret
fi
dev=${partition:0:$(( ${#partition} - 1 ))}
if [[ "$dev" = "$partition" ]]; then
verbose "Some error occurred going from partition to device"
return 1
elif [[ ! -b "$dev" ]]; then
verbose "Device '$dev' is not a proper block file"
return 1
fi
echo $dev
return ${RET_PERSISTENCE_FOUND}
}
# check if there is a partition that seems to be a persistence
# if process is privileged enough, deeper checks will be done
# if found, output the partition device (ie: /dev/sdb2)
has_avail_persistence() {
dev=$(get_root_device)
ret=$?
if [[ $ret -ne 0 ]]; then
return $ret
fi
persistence="${dev}2"
if [[ ! -b "$persistence" ]]; then
verbose "There is no persistence partition"
return ${RET_NOPERSISTENCE}
fi
if which cryptsetup &> /dev/null; then
cryptsetup=cryptsetup
elif [[ -x /sbin/cryptsetup ]]; then
cryptsetup=/sbin/cryptsetup
else
echo "Cryptsetup not found!" >&2
exit 1
fi
if [[ ! -r "$persistence" ]]; then
echo "Not able to check encryption; maybe you should be root?" >&2
echo $persistence
return ${RET_PERSISTENCE_FOUND}
fi
if (( $(udisks --show-info "$persistence" | fgrep 'type:' | grep -c 'crypto_LUKS') == 0 )); then
verbose "$persistence is not an encrypted partition"
return ${RET_NOPERSISTENCE}
fi
echo $persistence
return ${RET_PERSISTENCE_FOUND}
}
get_device_size() {
dev=$(get_root_device)
ret=$?
if [[ $ret -ne 0 ]]; then
return $ret
fi
dev_name=$(basename $dev)
size=$(lsblk $dev -o KNAME,SIZE|awk "{if(\$1==\"${dev_name}\"){print \$2;}}")
ret=$?
if [[ $ret -ne 0 ]]; then
return $ret
fi
echo $size
return ${RET_PERSISTENCE_FOUND}
}
get_live_size() {
dev=$(get_root_device)
ret=$?
if [[ $ret -ne 0 ]]; then
return $ret
fi
dev_name=$(basename $dev)
size=$(lsblk $dev -o KNAME,SIZE|awk "{if(\$1==\"${dev_name}1\"){print \$2;}}")
ret=$?
if [[ $ret -ne 0 ]]; then
return $ret
fi
echo $size
return ${RET_PERSISTENCE_FOUND}
}
get_persistence_size() {
avail_persistence=$(has_avail_persistence)
ret=$?
if [[ $ret -ne 0 ]]; then
return $ret
fi
dev=$(get_root_device)
ret=$?
if [[ $ret -ne 0 ]]; then
return $ret
fi
dev_name=$(basename $dev)
size=$(lsblk $dev -o KNAME,SIZE|awk "{if(\$1==\"${dev_name}2\"){print \$2;}}")
ret=$?
if [[ $ret -ne 0 ]]; then
return $ret
fi
echo $size
return ${RET_PERSISTENCE_FOUND}
}
while getopts v opt; do
case $opt in
v)
VERBOSE=1
;;
\?)
exit ${RET_SYNTAX_ERROR}
;;
esac
done
shift $((OPTIND - 1))
cmd=$1
shift
case "$cmd" in
is-mounted)
if [ $# != 0 ]; then
usage
exit 1
fi
is_mounted
exit $?
;;
get-root-device)
if [ $# != 0 ]; then
usage
exit 1
fi
get_root_device
exit $?
;;
has-avail-persistence)
if [ $# != 0 ]; then
usage
exit 1
fi
has_avail_persistence
exit $?
;;
get-device-size)
if [ $# != 0 ]; then
usage
exit 1
fi
get_device_size
exit $?
;;
get-live-size)
if [ $# != 0 ]; then
usage
exit 1
fi
get_live_size
exit $?
;;
get-persistence-size)
if [ $# != 0 ]; then
usage
exit 1
fi
get_persistence_size
exit $?
;;
*)
echo "Command '$cmd' not found" >&2
usage
exit 2
;;
esac
# vim: set noet: