diff options
-rwxr-xr-x | tdfvm-install | 47 |
1 files changed, 22 insertions, 25 deletions
diff --git a/tdfvm-install b/tdfvm-install index 54a0e63..0497e98 100755 --- a/tdfvm-install +++ b/tdfvm-install @@ -29,7 +29,7 @@ GRAPHICS="none" NETWORK="none" unset MEMORY unset DISK -TRANSIENT= +TRANSIENT=n HELP_MESSAGE="$(cat <<-EOF Install a new VM in an unattended fashion @@ -80,7 +80,7 @@ while [ $# -gt 0 ]; do --memory=*) MEMORY="${1#--memory=}";; --disk) DISK="$2"; shift;; --disk=*) DISK="${1#--disk=}";; - --transient) TRANSIENT='--transient';; + --transient) TRANSIENT=y;; --help|-\?) printf '%s\n' "$HELP_MESSAGE"; exit;; -*) usage "$1";; @@ -240,29 +240,25 @@ virt-install -q --connect "$LIBVIRT_URI" \ --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 - - # remove libvirt-generated UNIX socket - xpath="/domain/devices/graphics/listen[@type='socket'][starts-with(@socket,'/var/lib/libvirt/qemu/domain-')]/@socket" - xmlstarlet edit --inplace --delete "$xpath" "$vmdef" - [ ! ${OUTPUT+x} ] || cp --no-preserve=mode "$vmdef" "$OUTPUT/$VM_NAME.xml" -) + --controller "virtio-serial" \ + --noautoconsole \ + --print-xml 1 >"$VMTMPDIR/domain.xml" + +virsh -c "$LIBVIRT_URI" create "$VMTMPDIR/domain.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/filesystem[source/@dir='$VMTMPDIR/virtfs'][target/@dir='virtfs']" \ + --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']" \ + "$VMTMPDIR/domain.xml" + +[ "$TRANSIENT" = y ] || virsh -c "$LIBVIRT_URI" define "$VMTMPDIR/domain.xml" # wait until the VM terminates (there is actually a race condition here, # but the XML massaging above should be faster than any install) @@ -287,6 +283,7 @@ virsh -c "$LIBVIRT_URI" console "$VM_NAME" --safe >/dev/null ) >&2 if [ ${OUTPUT+x} ]; then + cp --no-preserve=mode "$VMTMPDIR/domain.xml" "$OUTPUT/$VM_NAME.xml" find "$VMTMPDIR/virtfs" -name '*.pub' -print0 \ | xargs -r0 cp --no-preserve=mode -t "$OUTPUT" printf "\nExported files:\n" >&2 |