22.9.1. Shut down a guest virtual machine




Shut down a guest virtual machine using the virsh shutdown command:
# virsh shutdown {domain-id, domain-name or domain-uuid} [--mode method]You can control the behavior of the rebooting guest virtual machine by modifying the on_shutdownparameter in the guest virtual machine's configuration file.

22.9.2. Shutting down Red Hat Enterprise Linux 6 guests on a Red Hat Enterprise Linux 7 host




Installing Red Hat Enterprise Linux 6 guest virtual machines with the Minimal installation option does not install the acpid package. Red Hat Enterprise Linux 7 no longer requires this package, as it has been taken over by systemd. However, Red Hat Enterprise Linux 6 guest virtual machines running on a Red Hat Enterprise Linux 7 host still require it.
Without the acpid package, the Red Hat Enterprise Linux 6 guest virtual machine does not shut down when the virsh shutdown command is executed. The virsh shutdown command is designed to gracefully shut down guest virtual machines.
Using virsh shutdown is easier and safer for system administration. Without graceful shut down with the virsh shutdown command a system administrator must log into a guest virtual machine manually or send the
Ctrl
-
Alt
-
Del
key combination to each guest virtual machine.
NOTE

Other virtualized operating systems may be affected by this issue. The virsh shutdowncommand requires that the guest virtual machine operating system is configured to handle ACPI shut down requests. Many operating systems require additional configuration on the guest virtual machine operating system to accept ACPI shut down requests.


Procedure 22.4. Workaround for Red Hat Enterprise Linux 6 guests

  • Install the acpid package
    The acpid service listen and processes ACPI requests.
    Log into the guest virtual machine and install the acpid package on the guest virtual machine:
    # yum install acpid
  • Enable the acpid service
    Set the acpid service to start during the guest virtual machine boot sequence and start the service:
    # systemctl enable acpid# service acpid start
  • Prepare guest domain xml
    Edit the domain XML file to include the following element. Replace the virtio serial port with org.qemu.guest_agent.0 and use your guest's name instead of $guestname

    Figure 22.2. Guest XML replacement
  • Install the QEMU guest agent
    Install the QEMU guest agent (QEMU-GA) and start the service as directed in Chapter 11,Enhancing virtualization with the QEMU guest agent and SPICE agent.
  • Shutdown the guest

    • Run the following commands
      # virsh list --all - this command lists all of the known domains Id Name State---------------------------------- rhel7 running
    • Shut down the guest virtual machine
      # virsh shutdown rhel7Domain rhel7 is being shutdown
    • Wait a few seconds for the guest virtual machine to shut down.
      # virsh list --all Id Name State---------------------------------- . rhel7 shut off
    • Start the domain named rhel7, with the XML file you edited.
      # virsh start rhel7
    • Shut down the acpi in the rhel7 guest virtual machine.
      # virsh shutdown --mode acpi rhel7
    • List all the domains again, rhel6 should still be on the list, and it should indicate it is shut off.
      # virsh list --all Id Name State---------------------------------- rhel7 shut off
    • Start the domain named rhel7, with the XML file you edited.
      # virsh start rhel7
    • Shut down the rhel7 guest virtual machine guest agent.
      # virsh shutdown --mode agent rhel7
    • List the domains. rhel7 should still be on the list, and it should indicate it is shut off
      # virsh list --all Id Name State---------------------------------- rhel7 shut off



The guest virtual machine will shut down using the virsh shutdown command for the consecutive shutdowns, without using the workaround described above.
In addition to the method described above, a guest can be automatically shutdown, by stopping thelibvirt-guest service. Refer to Section 22.9.3, “Manipulating the libvirt-guests configuration settings” for more information on this method.

22.9.3. Manipulating the libvirt-guests configuration settings




The libvirt-guests service has parameter settings that can be configured to assure that the guest is shutdown properly. It is a package that is a part of the libvirt installation and is installed by default. This service automatically saves guests to the disk when the host shuts down, and restores them to their pre-shutdown state when the host reboots. By default, this setting is set to suspend the guest. If you want the guest to be shutoff, you will need to change one of the parameters of the libvirt-guestsconfiguration file.
Procedure 22.5. Changing the libvirt-guests service parameters to allow for the graceful shutdown of guests
The procedure described here allows for the graceful shutdown of guest virtual machines when the host physical machine is stuck, powered off, or needs to be restarted.

  • Open the configuration file
    The configuration file is located in /etc/sysconfig/libvirt-guests. Edit the file, remove the comment mark (#) and change the ON_SHUTDOWN=suspend to ON_SHUTDOWN=shutdown. Remember to save the change.
    $ vi /etc/sysconfig/libvirt-guests# URIs to check for running guests# example: URIS='default xen:/// vbox+tcp://host/system lxc:///'#URIS=default# action taken on host boot# - start all guests which were running on shutdown are started on boot# regardless on their autostart settings # - ignore libvirt-guests init script won't start any guest on boot, however, # guests marked as autostart will still be automatically started by # libvirtd #ON_BOOT=start # Number of seconds to wait between each guest start. Set to 0 to allow # parallel startup.#START_DELAY=0# action taken on host shutdown# - suspend all running guests are suspended using virsh managedsave# - shutdown all running guests are asked to shutdown. Please be careful with# this settings since there is no way to distinguish between a# guest which is stuck or ignores shutdown requests and a guest# which just needs a long time to shutdown. When setting# ON_SHUTDOWN=shutdown, you must also set SHUTDOWN_TIMEOUT to a# value suitable for your guests.ON_SHUTDOWN=shutd own# If set to non-zero, shutdown will suspend guests concurrently. Number of# guests on shutdown at any time will not exceed number set in this variable.#PARALLEL_SHUTD OWN=0# Number of seconds we're willing to wait for a guest to shut down. If parallel# shutdown is enabled, this timeout applies as a timeout for shutting down all# guests on a single URI defined in the variable URIS. If this is 0, then there# is no time out (use with caution, as guests might not respond to a shutdown# request). The default value is 300 seconds (5 minutes).#SHUTDOWN_TIMEO UT=300# If non-zero, try to bypass the file system cache when saving and# restoring guests, even though this may give slower operation for# some file systems.#BYPASS_CACHE=0
    URIS - checks the specified connections for a running guest. The Default setting functions in the same manner as virsh does when no explicit URI is set In addition, one can explicitly set the URI from /etc/libvirt/libvirt.conf. It should be noted that when using the libvirtconfiguration file default setting, no probing will be used.

    ON_BOOT - specifies the action to be done to / on the guests when the host boots. The startoption starts all guests that were running prior to shutdown regardless on their autostart settings. The ignore option will not start the formally running guest on boot, however, any guest marked as autostart will still be automatically started by libvirtd.

    The START_DELAY - sets a delay interval in between starting up the guests. This time period is set in seconds. Use the 0 time setting to make sure there is no delay and that all guests are started simultaneously.

    ON_SHUTDOWN - specifies the action taken when a host shuts down. Options that can be set include: suspend which suspends all running guests using virsh managedsave and shutdownwhich shuts down all running guests. It is best to be careful with using the shutdown option as there is no way to distinguish between a guest which is stuck or ignores shutdown requests and a guest that just needs a longer time to shutdown. When setting the ON_SHUTDOWN=shutdown, you must also set SHUTDOWN_TIMEOUT to a value suitable for the guests.

    PARALLEL_SHUTDOWN Dictates that the number of guests on shutdown at any time will not exceed number set in this variable and the guests will be suspended concurrently. If set to 0, then guests are not shutdown concurrently.

    Number of seconds to wait for a guest to shut down. If SHUTDOWN_TIMEOUT is enabled, this timeout applies as a timeout for shutting down all guests on a single URI defined in the variable URIS. If SHUTDOWN_TIMEOUT is set to 0, then there is no time out (use with caution, as guests might not respond to a shutdown request). The default value is 300 seconds (5 minutes).

    BYPASS_CACHE can have 2 values, 0 to disable and 1 to enable. If enabled it will by-pass the file system cache when guests are restored. Note that setting this may effect performance and may cause slower operation for some file systems.

  • Start libvirt-guests service
    If you have not started the service, start the libvirt-guests service. Do not restart the service as this will cause all running domains to shutdown.



22.9.4. Rebooting a guest virtual machine




Reboot a guest virtual machine using virsh reboot command. Remember that this action will return once it has executed the reboot, but there may be a time lapse from that point until the domain actually reboots.
#virsh reboot {domain-id, domain-name or domain-uuid} [--mode method]You can control the behavior of the rebooting guest virtual machine by modifying the on_rebootelement in the guest virtual machine's configuration file.
By default, the hypervisor will try to pick a suitable shutdown method. To specify an alternative method, the --mode argument can specify a comma separated list which includes initctl, acpi, agent, signal. The order in which drivers will try each mode is undefined, and not related to the order specified in virsh. For strict control over ordering, use a single mode at a time and repeat the command.

22.9.5. Forcing a guest virtual machine to stop




Force a guest virtual machine to stop with the virsh destroy command:
# virsh destroy {domain-id, domain-name or domain-uuid} [--graceful]This command does an immediate ungraceful shutdown and stops the specified guest virtual machine. Using virsh destroy can corrupt guest virtual machine file systems. Use the destroyoption only when the guest virtual machine is unresponsive. If you want to initiate a graceful shutdown, use the virsh destroy --graceful command.

22.9.6. Resetting a virtual machine




virsh reset domain resets the domain immediately without any guest shutdown. A reset emulates the power reset button on a machine, where all guest hardware sees the RST line and re-initializes the internal state. Note that without any guest virtual machine OS shutdown, there are risks for data loss.