Dan
Dan

Reputation: 7737

shut down ("destroy") libvirt VM when it reboots

For a while, I was using virt-install to install an OS on libvirt VMs. I learned that the OS has an autoinstaller feature that requires the use of a second CD-ROM (to feed information about the desired configuration to the installer), but I found that virt-install unfortunately ignores all but one --cdrom argument. The alternative that I came up with is to output the VM configuration virt-install would use with just one CD-ROM to a file using the --print-xml argument, edit that file to add the second CD-ROM, and then use virsh create <xml config file>.

When I was using virt-install before, the VM rebooted itself at the end of installation and virt-install would notice and shut down ("destroy") the VM instead of allowing it to reboot, leaving me with a nice clean installed disk image. However, now when the VM reboots after completing installation, it actually boots up again instead of shutting down cleanly, so I can't programmatically tell when the installation has completed. After the reboot it looks like the same qemu-system-x86_64 process is being used, so I also can't use it to tell when the installation has completed.

How can I force libvirt to shut down ("destroy") the VM instead of rebooting the way virt-install did? Alternatively, is there some other indicator I can use to tell that a VM reboot has occurred?

Upvotes: 1

Views: 2282

Answers (1)

Dan
Dan

Reputation: 7737

Although there doesn't seem to be a way to automatically destroy a libvirt VM on reboot through a special incantation of virsh create or by changing options in the domain XML file, I stumbled across the very useful virsh event command:

$ virsh help event
  NAME
    event - (null)

  SYNOPSIS
    event [<domain>] [<event>] [--all] [--loop] [--timeout <number>] [--list]

  DESCRIPTION
    List event types, or wait for domain events to occur

  OPTIONS
    [--domain] <string>  filter by domain name, id, or uuid
    [--event] <string>   which event type to wait for
    --all                wait for all events instead of just one type
    --loop               loop until timeout or interrupt, rather than one-shot
    --timeout <number>   timeout seconds
    --list               list valid event types

The command blocks until an event of the specified type occurs for the specified domain. This allowed me to achieve my goal of emulating the behavior in virt-install by doing:

$ virsh event domain1 --event restart
event 'reboot' for domain -
events received: 1
$ virsh destroy domain1

And it even gives me a built-in timeout mechanism!

Upvotes: 1

Related Questions