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
|
#!/bin/bash
set -ue
error() {
echo "Error:" "$@" >&2
exit 1
}
usage() {
[ ${1+x} ] && echo "Unknown option '$1'" >&2
echo "Usage: $0 [OPTIONS] NAME" >&2
echo " $0 --help" >&2
exit 1
}
# Run a command locally or remotely
unset SSH_ARGS
run() {
if [ ${SSH_ARGS+x} ]; then ssh $SSH_ARGS -- "$@"; else "$@"; fi
}
unset CDROM
unset SSH_COMMAND
unset OUTPUT
unset VMDIR
SEED=$(head -c24 /dev/urandom | base64 -w0 | tr '+/' '-_')
SSH_CONTROLPATH="${XDG_RUNTIME_DIR:-$HOME/.ssh}/S.$SEED" # unique, unpredicable socket path
cleanup() {
set +e
[ ! ${CDROM+x} ] || run rm -f "$CDROM"
[ ! ${SSH_COMMAND+x} ] || rm -f "$SSH_COMMAND"
[ "${OUTPUT+x}" -o ! "${VMDIR+x}" ] || rm -rf "$VMDIR"
[ ! -S "$SSH_CONTROLPATH" ] || ssh -Oexit -S"$SSH_CONTROLPATH" x 2>/dev/null
}
trap cleanup EXIT TERM INT
LIBVIRT_URI=qemu:///system
VIRT_USER="$USER"
prepare_remote_uri() {
local dest="$1" user port
[ ! ${SSH_COMMAND+x} ] || return # already defined
if [ "${dest%:*}" != "$dest" ]; then
port="${dest##*:}"
dest="${dest%:*}"
fi
if [ "${dest%@*}" != "$dest" ]; then
VIRT_USER="${dest%%@*}"
dest="${dest#*@}"
fi
SSH_COMMAND="$(mktemp --tmpdir)"
SSH_ARGS="-oControlMaster=auto -oControlPath=$SSH_CONTROLPATH ${VIRT_USER+-l $VIRT_USER} ${port+-p $port} $dest"
cat >"$SSH_COMMAND" <<-EOF
#!/bin/sh
export SSH_AUTH_SOCK="${SSH_AUTH_SOCK-}"
exec ssh -oControlPersist=yes $SSH_ARGS -- nc -q0 -U /run/libvirt/libvirt-sock
EOF
chmod u+x "$SSH_COMMAND"
LIBVIRT_URI="qemu+ext:///system?command=${SSH_COMMAND//\//%2F}"
}
ARCH=$(dpkg-architecture -qDEB_TARGET_ARCH)
unset ISO
USER_NAME=root
PROMPT_PASSWORD=n
unset AUTHORIZED_KEYS
FORCE=n
CPU="host"
unset VCPUS
GRAPHICS="none"
NETWORK="none"
unset MEMORY
unset DISK
TRANSIENT=n
HELP_MESSAGE="$(cat <<-EOF
Install a new VM in an unattended fashion
Usage $0 [OPTIONS] NAME
--ssh=[USERNAME@]HOSTNAME[:PORT] connect over SSH to a remote libvirtd
--arch=ARCH target architecture (default: "$ARCH")
--iso=FILENAME path to the installation ISO image to preseed (required)
-u,--username=USERNAME user account to create (default: "$USER_NAME")
-p,--password prompt for USERNAME's password (password login are disabled by default)
--authorized-keys=FILENAME path to USERNAME's authorized_keys(5) file
-f,--force imediately the domain NAME if it exists, and remove existing configuration
-o,--output=DIRNAME directory where to export the XML definition and key material from the guest
virt-install(1) options
--cpu=MODEL,... CPU model and CPU features exposed to the guest (default: "$CPU")
--vcpus=STRING number of virtual cpus to configure for the guest
--graphics=STRING graphical display configuration (default: "$GRAPHICS")
--network=STRING network configuration (default: "$NETWORK")
--memory=STRING memory to allocate for the guest, in MiB (required)
--disk=STRING media to use as storage for the guest (required)
--transient create a transient libvirt VM
EOF
)"
while [ $# -gt 0 ]; do
case "$1" in
--arch) ARCH="$2"; shift;;
--arch=*) ARCH="${1#--arch=}";;
--iso) ISO="$2"; shift;;
--iso=*) ISO="${1#--iso=}";;
-u|--username) USER_NAME="$2"; shift;;
-u*) USER_NAME="${1#-u}";;
--username=*) USER_NAME="${1#--username=}";;
-p|--password) PROMPT_PASSWORD=y;;
--authorized-keys) AUTHORIZED_KEYS="$2"; shift;;
--authorized-keys=*) AUTHORIZED_KEYS="${1#--authorized-keys=}";;
-f|--force) FORCE=y;;
-o|--output) OUTPUT="$2"; shift;;
-o*) OUTPUT="${1#-o}";;
--output=*) OUTPUT="${1#--output=}";;
--ssh=*) prepare_remote_uri "${1#--ssh=}";;
--vcpus) VCPUS="$2"; shift;;
--vcpus=*) VCPUS="${1#--vcpus=}";;
--graphics) GRAPHICS="$2"; shift;;
--graphics=*) GRAPHICS="${1#--graphics=}";;
--network) NETWORK="$2"; shift;;
--network=*) NETWORK="${1#--network=}";;
--memory) MEMORY="$2"; shift;;
--memory=*) MEMORY="${1#--memory=}";;
--disk) DISK="$2"; shift;;
--disk=*) DISK="${1#--disk=}";;
--transient) TRANSIENT=y;;
--help|-\?) printf '%s\n' "$HELP_MESSAGE"; exit;;
-*) usage "$1";;
*) break;;
esac
shift
done
[ $# -eq 1 ] || usage
VM_NAME="$1"
for x in ISO MEMORY DISK; do
if ! eval [ "\${$x+x}" ]; then
echo "Error missing non-optional argument --$(echo "$x" | tr 'A-Z' 'a-z')" >&2
exit 1
fi
done
for prog in fuseiso fusermount rsync md5sum xorriso find xargs \
dpkg-architecture dpkg-buildpackage dpkg-scanpackages \
virsh virt-install xmlstarlet ssh-keygen openssl; do
which "$prog" >/dev/null || error "Missing $prog"
done
ISOHDPFX=/usr/lib/ISOLINUX/isohdpfx.bin
[ -f "$ISOHDPFX" ] || error "Missing $ISOHDPFX. Is the 'isolinux' package installed?"
if [ "$PROMPT_PASSWORD" = n ]; then
PASSWORD_CRYPTED='*'
else
PASSWORD_CRYPTED="$(
[ "$USER_NAME" = root ] && prompt="Enter root password" || prompt="Enter password for $USER_NAME"
read -rs -p "$prompt (leave blank to auto-generate): " pw
if [ "$pw" ]; then
printf '\n' >/dev/tty
read -rs -p "Re-enter password to confirm: " pw2
printf '\n' >/dev/tty
if [ "$pw" != "$pw2" ]; then
echo "Password do not match, aborting" >&2
exit 1
fi
else
pw="$(pwgen -syn 32 1 | sed 's/\s$//')"
printf '%s\n' "$pw" >/dev/tty
fi
printf '%s' "$pw" | mkpasswd --stdin --method=SHA-512
)"
fi
#######################################################################
# Presseed the ISO image
#
CDROM="$(run mktemp --tmpdir "$VM_NAME-XXXXXX.iso")"
(
mountdir="$(mktemp --tmpdir --directory)"
fuseiso "$ISO" "$mountdir"
isoeditdir="$(mktemp --tmpdir --directory)"
trap 'rm -rf "$isoeditdir"' EXIT TERM INT
rsync -aH --exclude=TRANS.TBL --chmod=u+w "$mountdir/" "$isoeditdir/"
fusermount -u "$mountdir"
rmdir "$mountdir"
. ./preseed-cfg >"$isoeditdir/preseed.cfg"
[ ! "${AUTHORIZED_KEYS+x}" ] || cat "$AUTHORIZED_KEYS" >"$isoeditdir/authorized_keys"
(
builddir="$(mktemp --tmpdir --directory)"
trap 'rm -rf "$builddir"' EXIT TERM INT
rsync -aH "./tdf-postinst-udeb" "$builddir"
find "$builddir" -mindepth 2 -maxdepth 2 -name debian -type d \
-execdir dpkg-buildpackage -us -uc -b -a "$ARCH" \;
mkdir "$isoeditdir/pool-extra"
find "$builddir" -maxdepth 1 -type f -name '*.udeb' -print0 | \
xargs -r0 cp -vlt "$isoeditdir/pool-extra"
cd "$isoeditdir"
find ./dists -type f \
| grep -P "^\./dists/[^\/]+/main/debian-installer/binary-\Q$ARCH\E/Packages(\.gz)?$" \
| while read packages; do
[ "${packages%.gz}" = "$packages" ] || gunzip -f "$packages"
dpkg-scanpackages -tudeb -a"$ARCH" ./pool-extra >>"${packages%.gz}"
[ "${packages%.gz}" = "$packages" ] || gzip -f "${packages%.gz}"
done
find ./pool-extra -maxdepth 1 -type f -name '*.udeb' -print0 | \
xargs -r0 md5sum >>./md5sum.txt
)
cd "$isoeditdir"
md5sums=$(mktemp --tmpdir="$isoeditdir" md5sum.txt-XXXXXX)
while read sum file; do
if [ "${file%/main/debian-installer/binary-$ARCH/Packages}" != "$file" ] ||
[ "${file%/main/debian-installer/binary-$ARCH/Packages.gz}" != "$file" ]; then
md5sum "$file"
else
echo "$sum $file"
fi
done <./md5sum.txt >"$md5sums"
mv -f "$md5sums" ./md5sum.txt
md5sum ./preseed.cfg >>./md5sum.txt
kernel="$(sed -rn '/^\s+kernel\s+/ {s///p; q}' ./isolinux/txt.cfg)"
initrd="$(sed -rn '/^\s+append\s(.*\s)?initrd=(\S+)(\s.*)?$/ {s//\2/p;q}' ./isolinux/txt.cfg)"
cat >./isolinux/isolinux.cfg <<-EOF
default install
label install
kernel $kernel
append initrd=$initrd preseed/file=/cdrom/preseed.cfg auto=true --- fb=false
EOF
xorriso -as mkisofs -r \
-checksum_algorithm_iso all \
-isohybrid-mbr "$ISOHDPFX" \
-b isolinux/isolinux.bin -c isolinux/boot.cat \
-partition_offset 16 \
-no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot \
--efi-boot boot/grub/efi.img -append_partition 2 0x01 ./boot/grub/efi.img \
./ \
| if [ ${SSH_ARGS+x} ]; then run "cat >$CDROM"; else cat >"$CDROM"; fi
run chmod g+r "$CDROM"
)
#######################################################################
#
grep -q '^kvm\s' /proc/modules || echo 'WARN: KVM not available!' >&2
[ "$NETWORK" = none ] || NETWORK="$NETWORK,model=virtio"
[ ! ${OUTPUT+x} ] || mkdir -p "$OUTPUT"
if [ "$FORCE" = y ]; then
virsh -c "$LIBVIRT_URI" destroy "$VM_NAME" >/dev/null 2>&1 || true
virsh -c "$LIBVIRT_URI" undefine "$VM_NAME" >/dev/null 2>&1 || true
fi
VMDIR="${OUTPUT-$(mktemp --tmpdir --directory)}"
virt-install -q --connect "$LIBVIRT_URI" \
--name "$VM_NAME" \
--os-variant "debianwheezy" \
--arch "$(dpkg-architecture -A"$ARCH" -qDEB_TARGET_GNU_CPU)" \
--virt-type "kvm" \
--cpu "$CPU" ${VCPUS+--vcpus "$VCPUS"} \
--memory "$MEMORY" --memballoon "virtio" \
--disk "path=$CDROM,device=cdrom,bus=sata,readonly=on,seclabel.model=dac,seclabel.label=$VIRT_USER:libvirt-qemu" \
--disk "$DISK,bus=virtio" \
--channel "unix,target_type=virtio,name=org.qemu.guest_agent.0" \
--rng "random,device=/dev/random,model=virtio,rate_bytes=512" \
--security "type=static,model=dac,label=libvirt-qemu:libvirt-qemu,relabel=yes" \
--network "$NETWORK" \
--graphics "$GRAPHICS" \
--controller "virtio-serial" \
--noautoconsole \
--print-xml 1 >"$VMDIR/$VM_NAME.xml"
virsh -c "$LIBVIRT_URI" create "$VMDIR/$VM_NAME.xml"
# never boot again on CDROM, detach unnecessary devices and remove
# unnecessary controllers
xmlstarlet edit --inplace \
--delete "/domain/os/boot[@dev='cdrom']" \
--delete "/domain/devices/disk[@type='file'][@device='cdrom']" \
--delete "/domain/devices/sound" \
--delete "/domain/devices/controller[@type='usb']" \
--delete "/domain/devices/input[@type='tablet'][@bus='usb']" \
--delete "/domain/devices/redirdev[@type='spicevmc'][@bus='usb']" \
"$VMDIR/$VM_NAME.xml"
[ "$TRANSIENT" = y ] || virsh -c "$LIBVIRT_URI" define "$VMDIR/$VM_NAME.xml"
# wait until the guest starts its QEMU Agent at the end of the installation
while :; do
./virsh-ga -c "$LIBVIRT_URI" "$VM_NAME" ping && break || rv=$?
[ $rv -eq 128 ] || exit $rv
sleep 1
done
# then copy the public key material
for path in /etc/ssh/ssh_host_rsa_key.pub \
/etc/ssh/ssh_host_ed25519_key.pub \
/etc/salt/pki/minion/minion.pub; do
./virsh-ga -c "$LIBVIRT_URI" "$VM_NAME" cat "$path" >"$VMDIR/${path##*/}"
done
./virsh-ga -c "$LIBVIRT_URI" "$VM_NAME" touch /tmp/tdf-install-continue
virsh -c "$LIBVIRT_URI" console "$VM_NAME" --safe >/dev/null # wait until shutdown
(
echo "Salt minion MD5 fingerprint:"
# salt uses a bizarre scheme...
grep -v '^-----.*-----$' "$VMDIR/minion.pub" \
| openssl dgst -md5 -c | sed '/.*=\s*/ {s//\t/;q}'
echo
echo "SSH hostkey fingerprints:"
find "$VMDIR" -maxdepth 1 -type f -name 'ssh_host_*_key.pub' \
-execdir ssh-keygen -lf {} \; | sed 's/^/\t/'
) >&2
if [ ${OUTPUT+x} ]; then
printf "\nExported files:\n" >&2
find "$OUTPUT" -type f -printf '\t%p\n' >&2
fi
|