blob: 22016f5df477e63d5d3f7edbba5ff5e0f811770e (
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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
#!/bin/sh
: ${ROOT_MKFS_CMD:=mkfs.ext4 -q}
: ${ROOT_FS_TYPE:=ext4}
losetup() { /sbin/losetup "$@"; }
lvm()
{
# get rid of warnings from lvm because we are holding open these fds
command lvm "$@" 3>&- 4>&-
}
luks_secret()
{
local parms=$-; # this junk keeps set -x from being too annoying
set +x
[ -n "$luks_secret" ] || luks_secret="$(head -c256 /dev/urandom)"
printf %s "$luks_secret"
case $parms in *x*) set -x; set -x ;; esac
}
mount_squashfs_images()
{
modprobe squashfs
find_squashfs_root | while read dirname basename; do
[ -n "$dirname" -a -n "$basename" ] || continue
local f="$dirname/$basename"
[ -f "$f" ] || return
local name=${basename%.squashfs}
mkdir -p /squashes/$name
mount -r -o loop "$f" /squashes/$name
done
bootdone squashfs-root
}
lv_exists()
{
[ -n "$1" ] && lvm lvs "$1" >/dev/null 2>&1
}
floor4()
{
# Negatives round up, but aren't used.
echo $(($1 / 4 * 4))
}
ceil4()
{
local x="$1"
[ $((x % 4)) -eq 0 ] || x=$((x + 4 - x % 4))
printf '%d\n' "$x"
}
vgfree_megs()
{
local vg="$1" out
out=$(lvm vgs -o pv_free --noheadings --nosuffix --units m "$vg") || return
echo ${out%.*}
}
init_samizdat_lvs()
{
local megs_free cdrom_dev cdrom_sectors cdrom_megs root_megs
megs_free=$(vgfree_megs samizdat) || return
cdrom_dev="$(mountpoint_to_dev /cdrom)" || return
cdrom_sectors=$(blockdev --getsz "$cdrom_dev") || return
cdrom_megs=$(ceil4 $(( cdrom_sectors / 2048 )))
root_megs=$(floor4 $(( megs_free - cdrom_megs )))
if [ $root_megs -le 0 ]; then # No room for cdrom mirror. Oh well, charge forward.
root_megs=$megs_free
fi
lvm lvcreate -Z n -L ${root_megs}m -n root samizdat || return
$ROOT_MKFS_CMD /dev/mapper/samizdat-root || return
}
mount_aufs_branches()
{
local new="$1"
mkdir /overlay
mount -t${ROOT_FS_TYPE} /dev/mapper/samizdat-root /overlay || return
if [ "$new" ]; then
cp -a /gpg /overlay || return
touch /overlay/samizdat-filesystem-is-new
fi
bootdone rw-overlay
mirror_cdrom || return
mount_squashfs_images || return
}
init_samizdat()
{
local imgfile="$1" megs="$2" keyfile="$3" dev
init_samizdat_vg "$imgfile" "$megs" "$keyfile" || return
init_samizdat_lvs || return
mount_aufs_branches new
}
open_samizdat()
{
open_samizdat_vg "$@" || return
lvs=$(lvm lvs --separator / samizdat -o vg_name,lv_name --noheadings) || return
lvm lvchange -ay $lvs || return
mount_aufs_branches
}
init_samizdat_lodev()
{
local imgfile="$1" megs=$(ceil4 "$2")
truncate -s ${megs}M "$imgfile" || return
dev=$(losetup -f) && losetup "$dev" "$imgfile" || return
echo "$dev"
}
open_samizdat_vg()
{
local imgfile="$1" keyfile="$2" dev
local cryptname=samizdatcrypt
dev=$(losetup -f) && losetup "$dev" "$imgfile" || return
gpg2 --verify "$keyfile" || return
# The first --decrypt merely strips the signature. The option is
# poorly named for that case.
gpg2 --decrypt "$keyfile" | gpg2 --decrypt | cryptsetup --key-file - luksOpen "$dev" "$cryptname" || return
[ -b /dev/mapper/"$cryptname" ] || return
}
init_samizdat_vg()
{
local imgfile="$1" megs="$2" keyfile="$3" dev
local cryptname=samizdatcrypt
dev=$(init_samizdat_lodev "$imgfile" "$megs") || return
[ ! -b /dev/mapper/"$cryptname" ] || return
luks_secret >/dev/null
luks_secret | gpg2 --default-recipient-self --encrypt --armor | gpg2 --clearsign --output "$keyfile" || return
luks_secret | cryptsetup luksFormat "$dev" - || return
cryptsetup luksDump "$dev" >&2
luks_secret | cryptsetup --key-file - luksOpen "$dev" "$cryptname" || return
[ -b /dev/mapper/"$cryptname" ] || return
lvm pvcreate /dev/mapper/"$cryptname" || return
lvm vgcreate samizdat /dev/mapper/"$cryptname"
}
grow_samizdat_vg_free()
{
# Grow the samizdat VG sufficiently to ensure it has at least $want_free_megs free.
local want_free_megs=$(ceil4 "$1") free_megs
free_megs=$(vgfree_megs samizdat) || return
if [ "$free_megs" -lt "$want_free_megs" ]; then
grow_samizdat_vg $((want_free_megs - free_megs)) || return
fi
}
majmin()
{
local dev="$1" major minor
eval $(stat -c 'major=%t minor=%T' "$dev") || return
[ "$major" -a "$minor" ] || return
printf '%d:%d\n' 0x$major 0x$minor
}
vg_to_pv()
{
lvm vgs "$1" -o devices --noheadings |
(
found= multidev=
while read dev; do
dev=${dev%(*}
if [ "$found" -a "$found" != "$dev" ]; then
exit 1
fi
found=$dev
done
readlink -f "$found"
)
}
cryptdev_to_dev()
{
local dev="$1" majmin
majmin=$(majmin "$dev") || return
set -- /sys/dev/block/$majmin/slaves/*
[ $# = 1 ] || return
cryptsetup status "$dev" |while read k v; do if [ "$k" = device: ]; then echo $v; break; fi; done
}
cryptdev_to_backing_file()
{
local dev="$1" majmin result
majmin="$(majmin "$dev")" || return
set -- /sys/dev/block/$majmin/slaves/*
[ $# = 1 ] || return
read result < "$1"/loop/backing_file || return
printf '%s\n' "$result"
}
samizdat_backing_file()
{
local pv
pv=$(vg_to_pv samizdat) && [ "$pv" ] || return
cryptdev_to_backing_file "$pv"
}
grow_samizdat_lv()
{
# Increase the size of the specified LV by $megs MB, creating the LV and resizing the VG as necessary.
local lv_name="$1" megs="$2" stat imgfile freemegs
imgfile=$(samizdat_backing_file) || return
if lv_exists samizdat/"$lv_name"; then
grow_samizdat_vg_free "$megs" || return
lvm lvresize -r -L +${megs}m samizdat/"$lv_name" || return
else
grow_samizdat_vg_free "$megs" || return
lvm lvcreate -Z n -L ${megs}m -n "$lv_name" samizdat || return
fi
}
lodev_to_file()
{
local result majmin dev="$1"
majmin="$(majmin "$dev")" || return
read result < /sys/dev/block/$majmin/loop/backing_file || return
printf '%s' "$result"
}
grow_samizdat_vg()
{
# Increase the size of the samizdat VG by $megs MB, resizing the backing file as necessary.
local megs="$1" cryptdev dev imgfile stat
cryptdev=$(vg_to_pv samizdat)
dev=$(cryptdev_to_dev "$cryptdev") || return
[ -b "$dev" ] || return
if [ "$(stat -c '%t' "$dev")" = 7 ]; then # this is a loop device
imgfile=$(lodev_to_file "$dev") || return
stat="$(stat -c 'local du=$((%B*%b)) sz=%s' "$imgfile")" || return
eval "$stat"
stat=$(stat -f -c 'local df=$((%f*%S))' "$imgfile") || return
eval "$stat"
local min_free_space=30
if [ $(( df - megs*1024*1024 - sz + du )) -le $((min_free_space * 1024 * 1024)) ]; then
echo 'grow_samizdat_vg: Not enough disk space!' >&2
return -1
fi
truncate -cs +${megs}M "$imgfile" || return
losetup -c "$dev" || return
cryptsetup resize "$cryptdev" || return
lvm pvresize "$cryptdev" || return
else
echo 'grow_samizdat_vg: Unimplemented!' >&2
return 1
fi
}
mountpoint_to_dev()
{
local wantmp="$1" dev mp rest
mountpoint -q "$wantmp" || return
while read dev mp rest; do if [ "$mp" = "$wantmp" ]; then echo "$dev"; return; fi; done < /proc/mounts
return 1
}
get_cdrom_sizelimit()
{
# returns bytes
local dev="$1" sectors
sectors=$(blockdev --getsz "$dev") || return
if dd count=2 if="$dev" bs=2048 skip=$((sectors/4 - 2)) of=/dev/null 2>/dev/null; then
return
else
echo $(((sectors-8)*512))
fi
}
mirror_cdrom()
{
local md_num=55 dev mp rest cdrom_dev sectors
cdrom_dev="$(mountpoint_to_dev /cdrom)" || return
local lv_name=samizdat/cdrom
local lv_dev=/dev/mapper/samizdat-cdrom
local md_name=/dev/md$md_num
if [ -b $md_name ]; then
echo "RAID device already exists: '$md_name'; try removing (mdadm -S $md_name) and retry" >&2
return 1
fi
if lv_exists $lv_name.tmp; then
lvm lvchange --available n $lv_name.tmp && lvm lvremove $lv_name.tmp || return
fi
if lv_exists $lv_name; then
umount /cdrom || return
mount -r "$lv_dev" /cdrom
return
fi
umount /cdrom || return
sectors=$(blockdev --getsz "$cdrom_dev") || return
grow_samizdat_lv ${lv_name#samizdat/}.tmp $((sectors / 2 / 1024 + 1)) || return
# In order to trick mdadm into accepting a read-only device, we need
# to create a (read-write) loopback device.
# Furthermore, in order to deal with block device errors caused by
# TAO "run-out blocks" we may need to discard the last two 2048-byte
# sectors.
local cdrom_loopdev sizelimit
cdrom_loopdev=$(losetup -f) || return
sizelimit=$(get_cdrom_sizelimit "$cdrom_dev")
# Apparently loopdev sizelimit is not respected by linux md. Nor
# blockdev --getsz. Does it even work? Anyway, although this is used
# here redundantly, 'mdadm --size' is used as well.
/sbin/losetup ${sizelimit:+--sizelimit=$sizelimit} "$cdrom_loopdev" "$cdrom_dev" || return
mdadm --build $md_name ${sizelimit:+--size=$((sizelimit / 1024))} \
--level=1 --raid-devices=1 --force --write-mostly "$cdrom_loopdev" || return
mdadm -D $md_name >&2
mdadm --add $md_name $lv_dev.tmp
mdadm -D $md_name >&2
mdadm --grow $md_name -n 2
mdadm -D $md_name >&2
mount -r $md_name /cdrom || { mount -r "$cdrom_dev" /cdrom; return 1; }
chpst -P samizdat-cdrom-copy "$md_name" "$lv_name" "$lv_dev" "$cdrom_loopdev" "$cdrom_dev" &
echo "[$$] Launched RAID monitor with pid $!." >&2
}
init_gpg()
{
bootwait samizdat-cdrom
export GNUPGHOME=/gpg/gnupghome
(umask 077; rsync --exclude '/luks-key*' --ignore-existing -rpP /cdrom/samizdat/gpg/ /gpg/)
if samizdat-password-agent >/var/log/samizdat-password-agent.log 2>&1; then
clear
true
else
false
fi
}
start_meter()
{
local startmsg="$*"
(exec >&4
clear
echo -n $startmsg
set +x
while sleep 2; do
echo -n .
done) &
meterpid=$!
}
stop_meter()
{
local endmsg="$*"
kill $meterpid
echo " $endmsg" >&4
}
|