[% topdir = "../.." -%] [% PROCESS globals -%] [% WRAPPER page title = "Starting and stopping VMs from the command line" h1 = "Starting and stopping VMs from the command line" section = "learning" %]

Lifecycle management (which is a fancy way of saying starting and stopping virtual machines) can be done either from virt-manager or directly from the command line or shell scripts using virsh.

These commands generally require that you are root. On Fedora and Debian, do:

su

and type the root password.

On Ubuntu you can prefix each command with sudo, or get a root shell first by doing:

sudo bash

List out the virtual machines that you have available. You can then substitute the name of one of your virtual machines for generic Guest below.

[% WRAPPER h2 h2="Starting a virtual machine with virsh" anchor="start" %]

To start a virtual machine running, do:

virsh start Guest

where Guest is the name of the virtual machine. Check that it is running by using virsh list or connecting to the console with:

virt-viewer Guest
[% END %] [% WRAPPER h2 h2="Stopping the virtual machine with virsh" anchor="stop" %]

Stopping the virtual machine is a little more complex and requires some understanding of how real PC hardware works.

Pull the plug

On a real PC you can just pull out the power cord (and battery if it's a laptop). This abruptly shuts the machine down, but it's not usually a good thing to do since the operating system gets no time to gracefully shut down applications and synchronize the disk.

You can do this from virsh too using the virsh destroy command, but be careful as the guest is switched off with no warning and no chance to sync the disks:

virsh destroy Guest

ACPI

The more common option is to reboot or shutdown the guest:

virsh reboot Guest
virsh shutdown Guest

You cannot reboot or shut down a machine (real or virtual) without cooperation from the operating system. On a real PC the power switch just sends a signal to the operating system, and the operating system has to be listening out for the signal and has to react by doing the right thing. This signal mechanism is known as ACPI. The same mechanism is used to wire the virtual power switch to the virtual machine.

Using the virsh reboot or virsh shutdown commands is like pressing the virtual power button, and it may open a dialog inside the VM like this one (taken from an Ubuntu guest):

The precise action taken depends on:

If a guest won't shut down or reboot, it is usually caused by a problem with ACPI and/or guest configuration. Eventually you may need to virsh destroy guests that don't respond to ordinary events.

[% END %] [% END -%]