aboutsummaryrefslogtreecommitdiffstats
path: root/tdfvm-install
blob: ed4c8e8e372ce7435a1087b00387c98e6df456ce (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
#!/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
}


LIBVIRT_URI=qemu:///system
ARCH=$(dpkg-architecture -qDEB_TARGET_ARCH)
unset ISO
USER_NAME=root
PROMPT_PASSWORD=n
unset AUTHORIZED_KEYS
FORCE=n
unset OUTPUT

CPU="host"
unset VCPUS
GRAPHICS="none"
NETWORK="none"
unset MEMORY
unset DISK
TRANSIENT=

HELP_MESSAGE="$(cat <<-EOF
	Install a new VM in an unattended fashion
	Usage $0 [OPTIONS] NAME
	    --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 pass 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=}";;

        --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='--transient';;

        --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?"

[ $(id -u) -eq 0 ] || error "This script needs to run as root"

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
#

VMTMPDIR="$(mktemp --tmpdir=/var/lib/libvirt/images --directory "$VM_NAME.XXXXXX" )"
trap 'rm -rf "$VMTMPDIR"' EXIT TERM INT
chmod a+x "$VMTMPDIR"
install -o libvirt-qemu -m 0400 /dev/null   "$VMTMPDIR/install.iso"
install -o libvirt-qemu -m 0700 --directory "$VMTMPDIR/virtfs"

(
    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 \
        -o "$VMTMPDIR/install.iso" ./
)


#######################################################################
#
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

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=$VMTMPDIR/install.iso,device=cdrom,bus=sata,readonly=on" \
    --disk       "$DISK,bus=virtio" \
    --channel    "unix,target_type=virtio,name=org.qemu.guest_agent.0" \
    --filesystem "source=$VMTMPDIR/virtfs,target=virtfs" \
    --network    "$NETWORK" \
    --graphics   "$GRAPHICS" \
    --noautoconsole $TRANSIENT

(
    vmdef="$(mktemp --tmpdir)"
    trap 'rm -f "$vmdef"' EXIT TERM INT
    virsh -c "$LIBVIRT_URI" dumpxml "$VM_NAME" >"$vmdef"

    for xpath in \
        "/domain/devices/filesystem[source/@dir=\"$VMTMPDIR/virtfs\"][target/@dir='virtfs']" \
        "/domain/devices/disk[@type='file'][@device='cdrom']"; do
        if [ -z "$TRANSIENT" ]; then
            virsh -c "$LIBVIRT_URI" --quiet detach-device --config "$VM_NAME" \
                <(xmlstarlet select --template --copy-of "$xpath" <"$vmdef")
        elif [ ${OUTPUT+x} ]; then
            xmlstarlet edit --inplace --delete "$xpath" "$vmdef"
        fi
    done
    [ ! ${OUTPUT+x} ] || cp --no-preserve=mode "$vmdef" "$OUTPUT/$VM_NAME.xml"
)
# wait until the VM terminates (there is actually a race condition here,
# but the XML massaging above should be faster than any install)
virsh -c "$LIBVIRT_URI" console "$VM_NAME" --safe >/dev/null

(
    if [ -f "$VMTMPDIR/virtfs/minion.pub" ]; then
        echo "Salt minion MD5 fingerprint:"
        # salt uses a bizarre scheme...
        grep -v '^-----.*-----$' "$VMTMPDIR/virtfs/minion.pub" \
            | openssl dgst -md5 -c | sed '/.*=\s*/ {s//\t/;q}'
    fi
    echo

    echo "SSH hostkey fingerprints:"
    find "$VMTMPDIR/virtfs" -maxdepth 1 -type f -name 'ssh_host_*_key.pub' \
        -execdir ssh-keygen -lf {} \; | sed 's/^/\t/'
) >&2

if [ ${OUTPUT+x} ]; then
    find "$VMTMPDIR/virtfs" -name '*.pub' -print0 \
        | xargs -r0 cp --no-preserve=mode -t "$OUTPUT"
fi