blob: 730dc26bc4cc9031abc0a30bd4c7375938c53f58 (
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
|
#!/bin/bash
die()
{
printf 'Error: %s\n' "$*" >&2
exit 1
}
KiB()
{
printf %s $(("$1" * 1024))
}
MiB()
{
KiB $(KiB "$1")
}
get_rootfs_size()
{
[ "$ROOTFS" ] || return
if [ -b "$ROOTFS" ]
then
rootfs_size=$(blockdev --getsize64 "$ROOTFS")
else
rootfs_size=$(stat -c%s "$ROOTFS")
fi
}
initialize_target()
{
if [ -b "$target" ]
then return
fi
rm -f "$target"
if [ "$ROOTFS" ]
then
get_rootfs_size
fallocate -l $(($(KiB 17) + $(MiB $end_keys) + rootfs_size)) "$target"
else
fallocate -l $(($(KiB 17) + $(MiB $end_keys))) "$target"
fi
}
# TODO: Use GUID type tags for each Samizdat partition type.
partition_target()
{
parted "$target" -sm -a optimal \
unit MiB \
mklabel gpt \
mkpart samizdat-grub 1 8 \
set 1 bios_grub on \
mkpart samizdat-keys btrfs ${start_keys} ${end_keys}
if [ -b "$target" ]
then
partx -u "$target"
fi
if [ "$ROOTFS" ]
then
parted "$target" -sm -a optimal \
mkpart samizdat-rootfs btrfs ${end_keys}MiB 100%
fi
}
make_target_bootable()
{
mkfs.btrfs -f --mixed "$part"2
mkdir -p "$mnt"
mount "$part"2 "$mnt"
mkdir -p "$mnt"/boot/grub
cp -aL "$GRUB_CONFIG" "$mnt"/boot/grub
eatmydata -- grub-install -v --boot-directory "$mnt"/boot "$dev"
sync
}
acquire_target_block_devices()
{
if [ -b "$target" ]
then
dev=$target
part=$target
mnt=/mnt/${target#/dev/}
else
trap release_target_block_devices EXIT
losetup -f "$target"
dev=$(losetup -j "$target" -O NAME --noheadings)
kpartx -vasas "$dev"
part=/dev/mapper/${dev##*/}p
mnt=$target.mnt
fi
}
release_target_block_devices()
{
(
set +e
[ -z "$mnt" ] || ! mountpoint "$mnt" || umount "$mnt"
case "$dev" in
/dev/loop*)
kpartx -d "$dev"
losetup -d "$dev"
;;
esac
true
)
unset mnt dev
}
add_keys()
{
rsync -a --info=STATS "$GPG_INPUT_DIR"/ "$mnt"/gnupghome/
}
add_initrd()
{
initrd_suffix=.samizdat
mkdir -p "$mnt"/linux
rsync -aL --info=STATS "${1}vmlinuz${2}" "$mnt"/linux/vmlinuz
rsync -aL --info=STATS "${1}initrd.img${2}${initrd_suffix}" "$mnt"/linux/initrd.img
}
add_grub_cfg()
{
mkdir -p "$mnt"/boot/grub
cp -aL "$GRUB_CONFIG" "$mnt"/boot/grub
}
individualize()
{
mkdir -p "$mnt"
mountpoint "$mnt" || mount "$part"2 "$mnt"
add_keys
add_initrd "$samizdat_linux_dir"/ "${version_suffix}"
add_grub_cfg
}
globalize()
{
initialize_target
partition_target
acquire_target_block_devices
make_target_bootable
}
sanity_checks()
{
[[ $UID = 0 ]] || die "You are not root."
for d in "$samizdat_linux_dir" "$GPG_INPUT_DIR"
do
[ -d "$d" ] || die "Not a directory: $d"
done
}
find_mac()
{
start_mac=$1
for mac in $(ip link show | grep link/ether | (read _ mac _; echo $mac | tr : -)); do
if [ "${mac%??}" = "${start_mac%??}" ]; then
prefix=${mac%??}
suffix=$(printf %x $(( 0x${mac##*-} + 1 )))
MAC=${prefix}${suffix}
return
fi
done
MAC=$start_mac
}
boot_vm()
{
boot_drive=$1
installer_target=samizdat.disk.img
if [ ! -e "$installer_target" ]
then
fallocate -l 10G "$installer_target"
fi
find_mac 52-54-00-12-34-56
qemu-system-x86_64 \
-enable-kvm \
-smp 2 \
-m 512 \
-k en-us \
-net nic,model=virtio,macaddr=$MAC \
-vga qxl \
-drive file="$boot_drive",format=raw \
-drive file="$installer_target",format=raw
}
. samizdat-paths.sh || die 'samizdat-paths.sh not found'
samizdat_linux_dir=/boot
: ${GPG_INPUT_DIR:=/cdrom/gnupghome}
: ${GRUB_CONFIG:=conf/grub.cfg}
start_keys=64 # megs
end_keys=256 # megs
if [ "$KERNEL_VERSION" ]
then
version_suffix=-$KERNEL_VERSION
fi
sanity_checks
set -e
if [ "$1" ]
then
[ -b "$1" ] || die "Not a block device: $1"
target=$1
globalize
individualize
release_target_block_devices
else
template=boot-disk.template.img
if [ ! -e "$template" ]
then
target="$template".tmp globalize
release_target_block_devices
mv -T "$template".tmp "$template"
fi
target=boot-disk.img.tmp
cp -T --reflink=always "$template" "$target"
acquire_target_block_devices
individualize
release_target_block_devices
final=${target%.tmp}
mv -T "$target" "$final"
target=$final
fi
boot_vm "$target"
|