libvirt
Most management tasks, such as starting or stopping a VM Guest, can
either be done using the graphical application Virtual Machine Manager or on the command
line using virsh. Connecting to the graphical console
via VNC is only possible from a graphical user interface.
If started on a VM Host Server the libvirt tools Virtual Machine Manager
virsh and virt-viewercan be used to
manage VM Guests on the host. However, it is also possible to manage
VM Guests on a remote VM Host Server. This requires to configure remote access
for libvirt on the host. See Chapter 11, Connecting and Authorizing for
instructions.
In order to connect to such a remote host with Virtual Machine Manager, you need to set up
a connection as explained in
Section 11.2.2, “Managing Connections with Virtual Machine Manager”. If connecting to
a remote host using virsh or
virt-viewer, you need to specify a connection URI with
the parameter -c (for example virsh -c
qemu+tls://saturn.example.com/system or virsh -c
xen+ssh://). The form of connection URI depends on the
connection type and the hypervisor—see
Section 11.2, “Connecting to a VM Host Server” for details.
Examples in this chapter are all listed without a connection URI.
The VM Guest listing shows all VM Guests managed by libvirt on a
VM Host Server.
The main window of the Virtual Machine Manager shows a list of all VM Guests for each VM Host Server it is connected to. Each VM Guest entry contains the machine's name, its status (, , or ) displayed as icon and literally, and a CPU usage bar.
virsh #
Use the command virsh to get a
list of VM Guests:
list
virsh list
virsh --all
For more information and further options, see virsh help
list or man 1 virsh.
Opening a Graphical Console to a VM Guest lets you interact with the machine like a physical host via a VNC connection. If accessing the VNC server requires authentication, you are prompted to enter a user name (if applicable) and a password.
Once you click into the VNC console, the cursor is “grabbed” and cannot be used outside the console anymore. To release it, press Alt–Ctrl.
In order to prevent the console from grabbing the cursor and to enable seamless cursor movement, add a tablet input device to the VM Guest. See Section 13.1, “Enabling Seamless and Synchronized Cursor Movement” for more information.
Certain key combinations such as Ctrl–Alt–Del are
interpreted by the host system and are not passed to the VM Guest. To
pass such key combinations to a VM Guest, open the menu from the VNC window and choose the desired key
combination entry. The menu is only available
when using Virtual Machine Manager and virt-viewer. With Virtual Machine Manager you can
alternatively use the “sticky key” feature as explained in
Tip: Passing Key Combinations to Virtual Machines.
Principally all VNC viewers are able to connect to the console of a
VM Guest. However, if you are using SASL authentication and/or TLS/SSL
connection to access the guest, the options become limited. Common VNC
viewers such as tigervnc support neither SASL
authentication nor TSL/SSL. The only supported alternative to Virtual Machine Manager and
virt-viewer is vinagre.
In the Virtual Machine Manager, right-click a VM Guest entry.
Choose from the pop-up menu.
virt-viewer #
virt-viewer is a simple VNC viewer with added
functionality for displaying VM Guest consoles. It can, for example, be
started in “wait” mode, where it waits for a VM Guest to
start before it connects. It also supports automatically reconnecting to
a VM Guest that is rebooted.
virt-viewer addresses VM Guests by name, by ID or by
UUID. Use virsh to get
this data.
list --all
To connect to a guest that is running or paused, use either the ID, UUID, or name. VM Guests that are shut off do not have an ID—you can only connect by UUID or name.
8
virt-viewer 8
sles12; the
connection window will open once the guest startsvirt-viewer --wait sles12
With the --wait option, the connection will be
upheld even if the VM Guest is not running at the moment. Once the
guest starts, the viewer will be launched.
For more information, see virt-viewer
or --helpman 1
virt-viewer.
When using virt-viewer to open a connection to a
remote host via SSH, the SSH password needs to be entered twice. The
first time for authenticating with libvirt, the second time for
authenticating with the VNC server. The second password needs to be
provided on the command line where virt-viewer was started.
Starting, stopping or pausing a VM Guest can be done with either Virtual Machine Manager
or virsh. You can also configure a VM Guest to be
automatically started when booting the VM Host Server.
When shutting down a VM Guest, you may either shut it down gracefully, or force the shutdown. The latter is equivalent to pulling the power plug on a physical host and is only recommended if there are no alternatives. Forcing a shutdown may cause file system corruption and loss of data on the VM Guest.
In order to be able to perform a graceful shutdown, the VM Guest must
be configured to support ACPI. If you have
created the guest with vm-install or with Virtual Machine Manager,
ACPI should be available. Use the following
procedure in Virtual Machine Manager to check:
Double-click the VM Guest entry in Virtual Machine Manager. Choose › and then › . should be checked.
Depending on the guest operating system, enabling ACPI may not be sufficient. It is strongly recommended to test shutting down and rebooting a guest before releasing it to production. openSUSE or SUSE Linux Enterprise Desktop, for example, may require PolKit authorization for shutdown and reboot. Make sure this policy is turned off on all VM Guests.
If ACPI was enabled during a Windows XP/Server 2003 guest installation, turning it on in the VM Guest configuration alone is not sufficient. See the following articles for more information:
| http://support.microsoft.com/kb/314088/EN-US/ |
| http://support.microsoft.com/?kbid=309283 |
A graceful shutdown is of course always possible from within the guest operating system, regardless of the VM Guest's configuration.
Changing a VM Guest's state can be done either from Virtual Machine Manager's main window, or from a VNC window.
Right-click a VM Guest entry.
Choose , , or one of the from the pop-up menu.
Open a VNC Window as described in Section 10.2.1, “Opening a Graphical Console with Virtual Machine Manager”.
Choose , , or one of the options either from the toolbar or from the menu.
Automatically starting a guest when the VM Host Server boots is not enabled by default. This feature needs to be turned on for each VM Guest individually. There is no way to activate it globally.
Double-click the VM Guest entry in Virtual Machine Manager to open its console.
Choose › to open the VM Guest configuration window.
Choose and check .
Save the new configuration with .
virsh #In the following examples the state of a VM Guest named “sles12” is changed.
virsh start sles12
virsh suspend sles12
virsh reboot sles12
virsh shutdown sles12
virsh destroy sles12
virsh autostart sles12
virsh autostart --disable sles12
Saving a VM Guest preserves the exact state of the guest’s memory. The operation is slightly similar to hibernating a computer. A saved VM Guest can be quickly restored to its previously saved running condition.
When saved, the VM Guest is paused, its current memory state is saved to disk, and then the guest is stopped. The operation does not make a copy of any portion of the VM Guest’s virtual disk. The amount of time taken to save the virtual machine depends on the amount of memory allocated. When saved, a VM Guest’s memory is returned to the pool of memory available on the VM Host Server.
The restore operation loads a VM Guest’s previously saved memory state file and starts it. The guest is not booted but rather resumes at the point where it was previously saved. The operation is slightly similar to coming out of hibernation.
The VM Guest is saved to a state file. Make sure there is enough space on the partition you are going to save to. Issue the following command on the guest to get a rough estimation of the file size in megabytes to be expected:
free -m | awk '/^Mem:/ {print $3}'After using the save operation, do not boot or start the saved VM Guest. Doing so would cause the machine's virtual disk and the saved memory state to get out of sync and can result in critical errors when restoring the guest. The only "proper" way to use a saved VM Guest is using the restore operation.
Open a VNC connection window to a VM Guest. Make sure the guest is running.
Choose ›
Choose a location and a file name.
Click . Saving the guest's state may take some time. After the operation has finished, the VM Guest will automatically shut down.
Start the Virtual Machine Manager.
Type Alt–R or choose › .
Choose the file you want to restore and proceed with . Once the file has been successfully loaded, the VM Guest is up and running.
virsh #
Save a running VM Guest with the command virsh
and specify the file to where it is
saved.
save
opensuse13
virsh save opensuse13 /virtual/saves/opensuse13.vmsav
37
virsh save 37 /virtual/saves/opensuse13.vmsave
To restore it, use virsh :
restore
virsh restore /virtual/saves/opensuse13.vmsave
VM Guest snapshots are snapshots of the complete virtual machine including the state of CPU, RAM, and the content of all writable disks. To use virtual machine snapshots, you must have at least one non-removable and writable block device using the qcow2 disk image format.
Snapshots are supported on KVM VM Host Servers only.
Snapshots let you restore the state of the machine at a particular point in time. This is for example useful to undo a faulty configuration or the installation of a lot of packages. It's also helpful for testing purposes, since it allows you to go back to a defined state at any time.
Snapshots can be taken either from running guests or from a guest currently not running. Taking a screenshot from a guest that is shut down ensures data integrity. In case you want to create a snapshot from a running system, be aware of the fact that the snapshot only captures the state of the disk(s), not the state of the memory. Therefore you need to ensure that:
all running programs have written their data to the disk. If unsure, terminate the application and/or stop the respective service.
buffers have been written to disc. This can be achieved by running the
command sync on the VM Guest.
Starting a snapshot reverts the machine back to the state it was in when
the snapshot was taken. Any changes written to the disk after that point
in time will get lost when starting the snapshot. Also note that starting
a snapshot will restore the state (shut off or running) the machine was
in when having taken the snapshot. If starting a snapshot of the state
Shutoff, you will have to start the VM Guest
afterwards.
To open the snapshot management view in Virtual Machine Manager, open the VNC window as described in Section 10.2.1, “Opening a Graphical Console with Virtual Machine Manager”. Now either choose › or click the snapshot icon in the toolbar.
The list of existing snapshots for the chosen VM Guest is displayed in the left-hand part of the window. The snapshot that was last started is marked with a green tick. The right-hand part of the window shows details of the snapshot currently marked in the list. These details include the snapshot's title and time stamp, the state of the VM Guest at the time the snapshot was taken and a description. Snapshots from running guests also include a screenshot. The can be changed or added directly in this view; the other snapshot data cannot be changed.
To take a new snapshot of a VM Guest, proceed as follows:
Shut down the VM Guest in case you want to create a snapshot from a guest that is not running.
Click the plus symbol in the bottom left corner of the VNC window to open the window.
Provide a and, optionally, a description. The name cannot be changed once the snapshot has been taken. It is recommended to use a “speaking name”, since it makes it easier to identify the snapshot.
Once all data is entered, choose .
To delete a snapshot of a VM Guest proceed as follows:
Click the symbol with the red circle in the bottom left corner of the VNC window.
Confirm the deletion with .
To start a snapshot, proceed as follows:
Click the “play” symbol in the bottom left corner of the VNC window.
Confirm the start with .
virsh #
To list all existing snapshots for a domain
(admin_server in the following), run the
snapshot-list command:
tux > virsh snapshot-list
Name Creation Time State
------------------------------------------------------------
Basic installation incl. SMT finished 2013-09-18 09:45:29 +0200 shutoff
Basic installation incl. SMT for CLOUD3 2013-12-11 15:11:05 +0100 shutoff
Basic installation incl. SMT for CLOUD3-HA 2014-03-24 13:44:03 +0100 shutoff
Basic installation incl. SMT for CLOUD4 2014-07-07 11:27:47 +0200 shutoff
Beta1 Running 2013-07-12 12:27:28 +0200 shutoff
Beta2 prepared 2013-07-12 17:00:44 +0200 shutoff
Beta2 running 2013-07-29 12:14:11 +0200 shutoff
Beta3 admin node deployed 2013-07-30 16:50:40 +0200 shutoff
Beta3 prepared 2013-07-30 17:07:35 +0200 shutoff
Beta3 running 2013-09-02 16:13:25 +0200 shutoff
Cloud2 GM running 2013-12-10 15:44:58 +0100 shutoff
CLOUD3 RC prepared 2013-12-20 15:30:19 +0100 shutoff
CLOUD3-HA Build 680 prepared 2014-03-24 14:20:37 +0100 shutoff
CLOUD3-HA Build 796 installed (zypper up) 2014-04-14 16:45:18 +0200 shutoff
GMC2 post Cloud install 2013-09-18 10:53:03 +0200 shutoff
GMC2 pre Cloud install 2013-09-18 10:31:17 +0200 shutoff
GMC2 prepared (incl. Add-On Installation) 2013-09-17 16:22:37 +0200 shutoff
GMC_pre prepared 2013-09-03 13:30:38 +0200 shutoff
OS + SMT + eth[01] 2013-06-14 16:17:24 +0200 shutoff
OS + SMT + Mirror + eth[01] 2013-07-30 15:50:16 +0200 shutoff
The snapshot that was last started is shown with the
snapshot-current command:
tux > virsh snapshot-current --name admin_server
Basic installation incl. SMT for CLOUD4
Details about a particular snapshot can be obtained by running the
snapshot-info command:
tux > virsh snapshot-info sles "Basic installation incl. SMT for CLOUD4"
Name: Basic installation incl. SMT for CLOUD4
Domain: admin_server
Current: yes
State: shutoff
Location: internal
Parent: Basic installation incl. SMT for CLOUD3-HA
Children: 0
Descendants: 0
Metadata: yes
To take a new snapshot of a VM Guest currently not running, use the
snapshot-create-as command as follows:
virsh snapshot-screate-as --domain admin_server1 --name "Snapshot 1"2 \ --description "First snapshot"3
Domain name. Mandatory. | |
Name of the snapshot. It is recommended to use a “speaking name”, since it makes it easier to identify the snapshot. Mandatory. | |
Description for the snapshot. Optional. |
To take a snapshot of a running VM Guest, you need to specify the
--live parameter:
virsh snapshot-screate-as --domain admin_server --name "Snapshot 2" \ --description "First live snapshot" --live
Refer to the SNAPSHOT COMMANDS section in
man 1 virsh for more details.
To delete a snapshot of a VM Guest, use the
snapshot-delete command:
virsh snapshot-delete --domain admin_server --snapshotname "Snapshot 2"
To start a snapshot, use the snapshot-revert
command:
virsh snapshot-revert --domain admin_server --snapshotname "Snapshot 1"
To start the current snapshot (the one the VM Guest was started off),
it is sufficient to use --current rather than
specifying the snapshot name:
virsh snapshot-revert --domain admin_server --current
Deleting a VM Guest removes its XML configuration by default. Since the attached storage is not deleted by default, you will be able to use it with another VM Guest. With Virtual Machine Manager you may also delete a guest's storage files as well—this will completely erase the guest.
In order to delete a VM Guest, it has to be shut down first (refer to Section 10.3, “Changing a VM Guest's State: Start, Stop, Pause” for instructions). It is not possible to delete a running guest.
In the Virtual Machine Manager, right-click a VM Guest entry.
Choose from the pop-up menu.
A confirmation window opens. Clicking will permanently erase the VM Guest. The deletion is not recoverable.
You may also choose to permanently delete the guest's virtual disk by ticking . The deletion is not recoverable either.
virsh #
To delete a VM Guest with virsh, run virsh
.
undefine VM_NAME
virsh undefine sles12
There is no option to automatically delete the attached storage files. If they are managed by libvirt, delete them as described in Section 12.2.4, “Deleting Volumes from a Storage Pool”.