Red Hat Enterprise Linux-6-Virtualization Administration Guide-En-US
Red Hat Enterprise Linux-6-Virtualization Administration Guide-En-US
Jiri Herrmann
Red Hat Customer Content Services
[email protected]
Yehuda Zimmerman
Red Hat Customer Content Services
[email protected]
Laura Novich
Red Hat Customer Content Services
Scott Radvan
Red Hat Customer Content Services
Dayle Parker
Red Hat Customer Content Services
Legal Notice
Copyright © 2017 Red Hat, Inc.
This document is licensed by Red Hat under the Creative Commons Attribution-ShareAlike 3.0
Unported License. If you distribute this document, or a modified version of it, you must provide
attribution to Red Hat, Inc. and provide a link to the original. If the document is modified, all Red Hat
trademarks must be removed.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert,
Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, JBoss, OpenShift, Fedora, the Infinity
logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other
countries.
Linux ® is the registered trademark of Linus Torvalds in the United States and other countries.
XFS ® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States
and/or other countries.
MySQL ® is a registered trademark of MySQL AB in the United States, the European Union and
other countries.
Node.js ® is an official trademark of Joyent. Red Hat Software Collections is not formally related to
or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack ® Word Mark and OpenStack logo are either registered trademarks/service marks
or trademarks/service marks of the OpenStack Foundation, in the United States and other countries
and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or
sponsored by the OpenStack Foundation, or the OpenStack community.
Abstract
The Virtualization Administration Guide covers administration of host physical machines,
networking, storage, device and guest virtual machine management, and troubleshooting. Note:
This document is under development, is subject to substantial change, and is provided only as a
preview. The included information and instructions should not be considered complete, and should
be used with caution. To expand your expertise, you might also be interested in the Red Hat
Virtualization (RH318) training course.
Table of Contents
Table of Contents
. . . . . . . . . .1.. .SERVER
CHAPTER . . . . . . . .BEST
. . . . .PRACTICES
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
...........
.CHAPTER
. . . . . . . . .2.. .SVIRT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
...........
2.1. SECURITY AND VIRTUALIZATION 15
2.2. SVIRT LABELING 16
.CHAPTER
. . . . . . . . .3.. .CLONING
. . . . . . . . .VIRTUAL
. . . . . . . .MACHINES
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
...........
3.1. PREPARING VIRTUAL MACHINES FOR CLONING 17
3.2. CLONING A VIRTUAL MACHINE 20
3.2.1. Cloning Guests with virt-clone 20
3.2.2. Cloning Guests with virt-manager 21
.CHAPTER
. . . . . . . . .4.. .KVM
. . . . LIVE
. . . . .MIGRATION
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
...........
4.1. LIVE MIGRATION REQUIREMENTS 24
4.2. LIVE MIGRATION AND RED HAT ENTERPRISE LINUX VERSION COMPATIBILITY 26
4.3. SHARED STORAGE EXAMPLE: NFS FOR A SIMPLE MIGRATION 27
4.4. LIVE KVM MIGRATION WITH VIRSH 28
4.4.1. Additional Tips for Migration with virsh 30
4.4.2. Additional Options for the virsh migrate Command 32
4.5. MIGRATING WITH VIRT-MANAGER 32
. . . . . . . . . .5.. .REMOTE
CHAPTER . . . . . . . .MANAGEMENT
. . . . . . . . . . . . . .OF
. . .GUESTS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
...........
5.1. REMOTE MANAGEMENT WITH SSH 39
5.2. REMOTE MANAGEMENT OVER TLS AND SSL 41
5.3. TRANSPORT MODES 44
.CHAPTER
. . . . . . . . .6.. .OVERCOMMITTING
. . . . . . . . . . . . . . . . .WITH
. . . . . KVM
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
...........
6.1. OVERCOMMITTING MEMORY 48
6.2. OVERCOMMITTING VIRTUALIZED CPUS 48
. . . . . . . . . .7.. .KSM
CHAPTER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
...........
.CHAPTER
. . . . . . . . .8.. .ADVANCED
. . . . . . . . . . GUEST
. . . . . . . VIRTUAL
. . . . . . . . .MACHINE
. . . . . . . . ADMINISTRATION
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
...........
8.1. CONTROL GROUPS (CGROUPS) 54
8.2. HUGE PAGE SUPPORT 54
8.3. RUNNING RED HAT ENTERPRISE LINUX AS A GUEST VIRTUAL MACHINE ON A HYPER-V
HYPERVISOR 55
8.4. GUEST VIRTUAL MACHINE MEMORY ALLOCATION 55
8.5. AUTOMATICALLY STARTING GUEST VIRTUAL MACHINES 56
8.6. DISABLE SMART DISK MONITORING FOR GUEST VIRTUAL MACHINES 57
8.7. CONFIGURING A VNC SERVER 57
8.8. GENERATING A NEW UNIQUE MAC ADDRESS 57
8.8.1. Another Method to Generate a New MAC for Your Guest Virtual Machine 58
8.9. IMPROVING GUEST VIRTUAL MACHINE RESPONSE TIME 58
8.10. VIRTUAL MACHINE TIMER MANAGEMENT WITH LIBVIRT 59
8.10.1. timer Child Element for clock 60
8.10.2. track 61
8.10.3. tickpolicy 61
8.10.4. frequency, mode, and present 61
8.10.5. Examples Using Clock Synchronization 62
8.11. USING PMU TO MONITOR GUEST VIRTUAL MACHINE PERFORMANCE 63
8.12. GUEST VIRTUAL MACHINE POWER MANAGEMENT 63
. . . . . . . . . .9.. .GUEST
CHAPTER . . . . . . VIRTUAL
. . . . . . . . .MACHINE
. . . . . . . . .DEVICE
. . . . . . .CONFIGURATION
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
...........
1
Virtualization Administration Guide
. . . . . . . . . .10.
CHAPTER . . .QEMU-IMG
. . . . . . . . . .AND
. . . . QEMU
. . . . . .GUEST
. . . . . . .AGENT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93
...........
10.1. USING QEMU-IMG 93
10.2. QEMU GUEST AGENT 98
10.2.1. Install and Enable the Guest Agent 98
10.2.2. Setting up Communication between Guest Agent and Host 98
10.2.3. Using the QEMU Guest Agent 99
10.2.4. Using the QEMU Guest Agent with libvirt 99
10.2.5. Creating a Guest Virtual Machine Disk Backup 100
10.3. RUNNING THE QEMU GUEST AGENT ON A WINDOWS GUEST 101
10.3.1. Using libvirt Commands with the QEMU Guest Agent on Windows Guests 104
10.4. SETTING A LIMIT ON DEVICE REDIRECTION 104
10.5. DYNAMICALLY CHANGING A HOST PHYSICAL MACHINE OR A NETWORK BRIDGE THAT IS
ATTACHED TO A VIRTUAL NIC 105
. . . . . . . . . .11.
CHAPTER . . .STORAGE
. . . . . . . . . CONCEPTS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107
............
11.1. STORAGE POOLS 107
11.2. VOLUMES 108
. . . . . . . . . .12.
CHAPTER . . .STORAGE
. . . . . . . . . POOLS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110
............
12.1. DISK-BASED STORAGE POOLS 110
12.1.1. Creating a Disk-based Storage Pool Using virsh 111
12.1.2. Deleting a Storage Pool Using virsh 113
12.2. PARTITION-BASED STORAGE POOLS 114
12.2.1. Creating a Partition-based Storage Pool Using virt-manager 114
12.2.2. Deleting a Storage Pool Using virt-manager 117
12.2.3. Creating a Partition-based Storage Pool Using virsh 118
12.2.4. Deleting a Storage Pool Using virsh 120
12.3. DIRECTORY-BASED STORAGE POOLS 121
12.3.1. Creating a Directory-based Storage Pool with virt-manager 121
12.3.2. Deleting a Storage Pool Using virt-manager 124
12.3.3. Creating a Directory-based Storage Pool with virsh 125
12.3.4. Deleting a Storage Pool Using virsh 127
12.4. LVM-BASED STORAGE POOLS 127
12.4.1. Creating an LVM-based Storage Pool with virt-manager 128
12.4.2. Deleting a Storage Pool Using virt-manager 133
12.4.3. Creating an LVM-based Storage Pool with virsh 134
12.4.4. Deleting a Storage Pool Using virsh 136
2
Table of Contents
. . . . . . . . . .13.
CHAPTER . . . VOLUMES
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155
............
13.1. CREATING VOLUMES 155
13.2. CLONING VOLUMES 155
13.3. ADDING STORAGE DEVICES TO GUESTS 156
13.3.1. Adding File-based Storage to a Guest 156
13.3.2. Adding Hard Drives and Other Block Devices to a Guest 159
13.4. DELETING AND REMOVING VOLUMES 160
. . . . . . . . . .14.
CHAPTER . . .MANAGING
. . . . . . . . . . GUEST
. . . . . . .VIRTUAL
. . . . . . . . MACHINES
. . . . . . . . . . WITH
. . . . . .VIRSH
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161
............
14.1. GENERIC COMMANDS 161
14.1.1. help 161
14.1.2. quit and exit 162
14.1.3. version 162
14.1.4. Argument Display 162
14.1.5. connect 162
14.1.6. Displaying Basic Information 163
14.1.7. Injecting NMI 163
14.2. ATTACHING AND UPDATING A DEVICE WITH VIRSH 163
14.3. ATTACHING INTERFACE DEVICES 164
14.4. CHANGING THE MEDIA OF A CDROM 165
14.5. DOMAIN COMMANDS 165
14.5.1. Configuring a Domain to be Started Automatically at Boot 165
14.5.2. Connecting the Serial Console for the Guest Virtual Machine 166
14.5.3. Defining a Domain with an XML File 166
14.5.4. Editing and Displaying a Description and Title of a Domain 166
14.5.5. Displaying Device Block Statistics 166
14.5.6. Retrieving Network Statistics 167
14.5.7. Modifying the Link State of a Domain's Virtual Interface 167
14.5.8. Listing the Link State of a Domain's Virtual Interface 167
14.5.9. Setting Network Interface Bandwidth Parameters 167
14.5.10. Retrieving Memory Statistics for a Running Domain 168
14.5.11. Displaying Errors on Block Devices 168
14.5.12. Displaying the Block Device Size 168
14.5.13. Displaying the Block Devices Associated with a Domain 168
14.5.14. Displaying Virtual Interfaces Associated with a Domain 169
14.5.15. Using blockcommit to Shorten a Backing Chain 169
14.5.16. Using blockpull to Shorten a Backing Chain 170
3
Virtualization Administration Guide
4
Table of Contents
5
Virtualization Administration Guide
. . . . . . . . . .15.
CHAPTER . . .MANAGING
. . . . . . . . . . GUESTS
. . . . . . . . WITH
. . . . . THE
. . . . VIRTUAL
. . . . . . . . .MACHINE
. . . . . . . . .MANAGER
. . . . . . . . . .(VIRT-MANAGER)
. . . . . . . . . . . . . . . . . . . . . . .223
............
15.1. STARTING VIRT-MANAGER 223
15.2. THE VIRTUAL MACHINE MANAGER MAIN WINDOW 224
15.3. THE VIRTUAL HARDWARE DETAILS WINDOW 224
15.3.1. Attaching USB Devices to a Guest Virtual Machine 226
15.4. VIRTUAL MACHINE GRAPHICAL CONSOLE 228
15.5. ADDING A REMOTE CONNECTION 230
15.6. DISPLAYING GUEST DETAILS 231
15.7. PERFORMANCE MONITORING 238
15.8. DISPLAYING CPU USAGE FOR GUESTS 239
15.9. DISPLAYING CPU USAGE FOR HOSTS 240
15.10. DISPLAYING DISK I/O 241
15.11. DISPLAYING NETWORK I/O 243
.CHAPTER
. . . . . . . . .16.
. . .GUEST
. . . . . . VIRTUAL
. . . . . . . . .MACHINE
. . . . . . . . .DISK
. . . . .ACCESS
. . . . . . . .WITH
. . . . .OFFLINE
. . . . . . . .TOOLS
. . . . . . . . . . . . . . . . . . . . . . . . . . . .247
............
16.1. INTRODUCTION 247
16.2. TERMINOLOGY 248
16.3. INSTALLATION 249
16.4. THE GUESTFISH SHELL 249
16.4.1. Viewing File Systems with guestfish 250
16.4.1.1. Manual Listing and Viewing 250
16.4.1.2. Using guestfish inspection 251
16.4.1.3. Accessing a guest virtual machine by name 252
16.4.2. Modifying Files with guestfish 252
16.4.3. Other Actions with guestfish 253
6
Table of Contents
.CHAPTER
. . . . . . . . .17.
. . .GRAPHICAL
. . . . . . . . . . .USER
. . . . . INTERFACE
. . . . . . . . . . . TOOLS
. . . . . . .FOR
. . . . GUEST
. . . . . . . VIRTUAL
. . . . . . . . MACHINE
. . . . . . . . . MANAGEMENT
. . . . . . . . . . . . . . . .271
............
17.1. VIRT-VIEWER 271
Syntax 271
Connecting to a guest virtual machine 271
Interface 272
Setting hotkeys 272
Kiosk mode 273
17.2. REMOTE-VIEWER 273
Syntax 273
Connecting to a guest virtual machine 273
Interface 274
.CHAPTER
. . . . . . . . .18.
. . .VIRTUAL
. . . . . . . . NETWORKING
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275
............
18.1. VIRTUAL NETWORK SWITCHES 275
18.2. BRIDGED MODE 275
18.3. NETWORK ADDRESS TRANSLATION MODE 276
18.3.1. DNS and DHCP 277
18.4. ROUTED MODE 278
18.5. ISOLATED MODE 278
18.6. THE DEFAULT CONFIGURATION 279
18.7. EXAMPLES OF COMMON SCENARIOS 280
18.7.1. Bridged Mode 280
18.7.2. Routed Mode 280
18.7.3. NAT Mode 281
18.7.4. Isolated Mode 281
18.8. MANAGING A VIRTUAL NETWORK 282
18.9. CREATING A VIRTUAL NETWORK 283
7
Virtualization Administration Guide
. . . . . . . . . .19.
CHAPTER . . .QEMU-KVM
. . . . . . . . . . COMMANDS,
. . . . . . . . . . . . FLAGS,
. . . . . . . AND
. . . . .ARGUMENTS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329
............
19.1. INTRODUCTION 329
Whitelist Format 329
19.2. BASIC OPTIONS 329
Emulated Machine 329
Processor Type 329
Processor Topology 330
NUMA System 330
Memory Size 330
Keyboard Layout 330
Guest Name 330
Guest UUID 330
8
Table of Contents
. . . . . . . . . .20.
CHAPTER . . .MANIPULATING
. . . . . . . . . . . . . . THE
. . . . DOMAIN
. . . . . . . .XML
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .345
............
20.1. GENERAL INFORMATION AND METADATA 345
20.2. OPERATING SYSTEM BOOTING 346
20.2.1. BIOS Boot loader 346
20.2.2. Host Physical Machine Boot Loader 348
20.2.3. Direct kernel boot 348
20.3. SMBIOS SYSTEM INFORMATION 349
20.4. CPU ALLOCATION 350
20.5. CPU TUNING 351
20.6. MEMORY BACKING 353
9
Virtualization Administration Guide
10
Table of Contents
. . . . . . . . . .21.
CHAPTER . . .TROUBLESHOOTING
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .420
............
21.1. DEBUGGING AND TROUBLESHOOTING TOOLS 420
21.2. PREPARING FOR DISASTER RECOVERY 421
21.3. CREATING VIRSH DUMP FILES 423
21.4. KVM_STAT 423
21.5. GUEST VIRTUAL MACHINE FAILS TO SHUTDOWN 426
21.6. TROUBLESHOOTING WITH SERIAL CONSOLES 427
21.7. VIRTUALIZATION LOG FILES 428
21.8. LOOP DEVICE ERRORS 428
21.9. LIVE MIGRATION ERRORS 428
21.10. ENABLING INTEL VT-X AND AMD-V VIRTUALIZATION HARDWARE EXTENSIONS IN BIOS 428
21.11. KVM NETWORKING PERFORMANCE 429
21.12. WORKAROUND FOR CREATING EXTERNAL SNAPSHOTS WITH LIBVIRT 431
21.13. MISSING CHARACTERS ON GUEST CONSOLE WITH JAPANESE KEYBOARD 431
21.14. VERIFYING VIRTUALIZATION EXTENSIONS 432
. . . . . . . . . . A.
APPENDIX . . .THE
. . . .VIRTUAL
. . . . . . . .HOST
. . . . . METRICS
. . . . . . . . . DAEMON
. . . . . . . . .(VHOSTMD)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .434
............
.APPENDIX
. . . . . . . . . B.
. . .ADDITIONAL
. . . . . . . . . . . RESOURCES
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .435
............
B.1. ONLINE RESOURCES 435
B.2. INSTALLED DOCUMENTATION 435
. . . . . . . . . . C.
APPENDIX . . .REVISION
. . . . . . . . .HISTORY
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .436
............
11
Virtualization Administration Guide
12
CHAPTER 1. SERVER BEST PRACTICES
Run SELinux in enforcing mode. Set SELinux to run in enforcing mode with the setenforce
command.
# setenforce 1
Remove or disable any unnecessary services such as AutoFS, NFS, FTP, HTTP, NIS,
telnetd, sendmail and so on.
Only add the minimum number of user accounts needed for platform management on the server
and remove unnecessary user accounts.
Avoid running any unessential applications on your host. Running applications on the host may
impact virtual machine performance and can affect server stability. Any application which may
crash the server will also cause all virtual machines on the server to go down.
Use a central location for virtual machine installations and images. Virtual machine images
should be stored under /var/lib/libvirt/images/. If you are using a different directory for
your virtual machine images make sure you add the directory to your SELinux policy and relabel
it before starting the installation. Use of shareable, network storage in a central location is highly
recommended.
13
Virtualization Administration Guide
CHAPTER 2. SVIRT
sVirt is a technology included in Red Hat Enterprise Linux 6 that integrates SELinux and virtualization.
sVirt applies Mandatory Access Control (MAC) to improve security when using guest virtual machines.
This integrated technology improves security and hardens the system against bugs in the hypervisor. It
is particularly helpful in preventing attacks on the host physical machine or on another guest virtual
machine.
This chapter describes how sVirt integrates with virtualization technologies in Red Hat Enterprise Linux
6.
Non-virtualized Environments
In a non-virtualized environment, host physical machines are separated from each other physically and
each host physical machine has a self-contained environment, consisting of services such as a web
server, or a DNS server. These services communicate directly to their own user space, host physical
machine's kernel and physical hardware, offering their services directly to the network. The following
image represents a non-virtualized environment:
User Space - memory area where all user mode applications and some drivers execute.
Web App (web application server) - delivers web content that can be accessed through the a browser.
Host Kernel - is strictly reserved for running the host physical machine's privileged kernel, kernel
extensions, and most device drivers.
DNS Server - stores DNS records allowing users to access web pages using logical names instead of IP
addresses.
Virtualized Environments
In a virtualized environment, several virtual operating systems can run on a single kernel residing on a
host physical machine. The following image represents a virtualized environment:
14
CHAPTER 2. SVIRT
sVirt is an effort to isolate guest virtual machines and limit their ability to launch further attacks if
exploited. This is demonstrated in the following image, where an attack cannot break out of the guest
virtual machine and invade other guest virtual machines:
SELinux introduces a pluggable security framework for virtualized instances in its implementation of
Mandatory Access Control (MAC). The sVirt framework allows guest virtual machines and their resources
to be uniquely labeled. Once labeled, rules can be applied which can reject access between different
guest virtual machines.
15
Virtualization Administration Guide
As shown in the following output, when using sVirt, each virtualized guest virtual machine process is
labeled and runs with a dynamically generated level. Each process is isolated from other VMs with
different levels:
The actual disk images are automatically labeled to match the processes, as shown in the following
output:
# ls -lZ /var/lib/libvirt/images/*
system_u:object_r:svirt_image_t:s0:c87,c520 image1
The following table outlines the different context labels that can be assigned when using sVirt:
It is also possible to perform static labeling when using sVirt. Static labels allow the administrator to
select a specific label, including the MCS/MLS field, for a guest virtual machine. Administrators who run
statically-labeled virtualized guest virtual machines are responsible for setting the correct label on the
image files. The guest virtual machine will always be started with that label, and the sVirt system will
never modify the label of a statically-labeled virtual machine's content. This allows the sVirt component to
run in an MLS environment. You can also run multiple guest virtual machines with different sensitivity
levels on a system, depending on your requirements.
16
CHAPTER 3. CLONING VIRTUAL MACHINES
Clones are instances of a single virtual machine. Clones can be used to set up a network of
identical virtual machines, and they can also be distributed to other destinations.
Templates are instances of a virtual machine that are designed to be used as a source for
cloning. You can create multiple clones from a template and make minor modifications to each
clone. This is useful in seeing the effects of these changes on the system.
Both clones and templates are virtual machine instances. The difference between them is in how they
are used.
For the created clone to work properly, information and configurations unique to the virtual machine that
is being cloned usually has to be removed before cloning. The information that needs to be removed
differs, based on how the clones will be used.
The information and configurations to be removed may be on any of the following levels:
Platform level information and configurations include anything assigned to the virtual machine by
the virtualization solution. Examples include the number of Network Interface Cards (NICs) and
their MAC addresses.
Guest operating system level information and configurations include anything configured within
the virtual machine. Examples include SSH keys.
NOTE
This chapter does not include information about removing the application level,
because the information and approach is specific to each application.
As a result, some of the information and configurations must be removed from within the virtual machine,
while other information and configurations must be removed from the virtual machine using the
virtualization environment (for example, Virtual Machine Manager or VMware).
a. Build the virtual machine that is to be used for the clone or template.
17
Virtualization Administration Guide
# rm -f /etc/udev/rules.d/70-persistent-net.rules
NOTE
If udev rules are not removed, the name of the first NIC may be eth1 instead
of eth0.
b. Remove unique network details from ifcfg scripts by making the following edits to
/etc/sysconfig/network-scripts/ifcfg-eth[x]:
NOTE
If the HWADDR does not match the new guest's MAC address, the ifcfg
will be ignored. Therefore, it is important to remove the HWADDR from
the file.
DEVICE=eth[x]
BOOTPROTO=none
ONBOOT=yes
#NETWORK=10.0.1.0 <- REMOVE
#NETMASK=255.255.255.0 <- REMOVE
#IPADDR=10.0.1.20 <- REMOVE
#HWADDR=xx:xx:xx:xx:xx <- REMOVE
#USERCTL=no <- REMOVE
# Remove any other *unique* or non-desired settings, such as
UUID.
ii. Ensure that a DHCP configuration remains that does not include a HWADDR or any
unique information.
DEVICE=eth[x]
BOOTPROTO=dhcp
ONBOOT=yes
DEVICE=eth[x]
ONBOOT=yes
c. If the following files exist, ensure that they contain the same content:
/etc/sysconfig/networking/devices/ifcfg-eth[x]
/etc/sysconfig/networking/profiles/default/ifcfg-eth[x]
18
CHAPTER 3. CLONING VIRTUAL MACHINES
NOTE
For Red Hat Network (RHN) registered guest virtual machines, run the following
command:
# rm /etc/sysconfig/rhn/systemid
For Red Hat Subscription Manager (RHSM) registered guest virtual machines:
If the original virtual machine will not be used, run the following commands:
If the original virtual machine will be used, run only the following command:
# subscription-manager clean
NOTE
a. Remove any sshd public/private key pairs using the following command:
# rm -rf /etc/ssh/ssh_host_*
NOTE
Removing ssh keys prevents problems with ssh clients not trusting these
hosts.
b. Remove any other application-specific identifiers or configurations that may cause conflicts if
running on multiple machines.
5. Configure the virtual machine to run configuration wizards on the next boot
a. Configure the virtual machine to run the relevant configuration wizards the next time it is
booted by doing one of the following:
For Red Hat Enterprise Linux 6 and below, create an empty file on the root file system
called .unconfigured using the following command:
19
Virtualization Administration Guide
# touch /.unconfigured
For Red Hat Enterprise Linux 7, enable the first boot and initial-setup wizards by running
the following commands:
NOTE
The wizards that run on the next boot depend on the configurations that have
been removed from the virtual machine. In addition, on the first boot of the
clone, it is recommended that you change the host name.
Note that you need root privileges for virt-clone to complete successfully.
The virt-clone command provides a number of options that can be passed on the command line.
These include general options, storage configuration options, networking configuration options, and
miscellaneous options. Only the --original is required. To see a complete list of options, enter the
following command:
# virt-clone --help
The virt-clone man page also documents each command option, important variables, and examples.
The following example shows how to clone a guest virtual machine called "demo" on the default
connection, automatically generating a new name and disk clone path.
The following example shows how to clone a QEMU guest virtual machine called "demo" with multiple
disks.
20
CHAPTER 3. CLONING VIRTUAL MACHINES
1. Open virt-manager
Start virt-manager. Launch the Virtual Machine Manager application from the Applications
menu and System Tools submenu. Alternatively, run the virt-manager command as root.
Select the guest virtual machine you want to clone from the list of guest virtual machines in
Virtual Machine Manager.
Right-click the guest virtual machine you want to clone and select Clone. The Clone Virtual
Machine window opens.
21
Virtualization Administration Guide
To change the name of the clone, enter a new name for the clone.
Click OK.
22
CHAPTER 3. CLONING VIRTUAL MACHINES
For each disk in the cloned guest virtual machine, select one of the following options:
Clone this disk - The disk will be cloned for the cloned guest virtual machine
Share disk with guest virtual machine name - The disk will be shared by
the guest virtual machine that will be cloned and its clone
Details - Opens the Change storage path window, which enables selecting a new
path for the disk
23
Virtualization Administration Guide
Migration describes the process of moving a guest virtual machine from one host physical machine to
another. This is possible because guest virtual machines are running in a virtualized environment instead
of directly on the hardware. Migration is useful for:
Load balancing - guest virtual machines can be moved to host physical machines with lower
usage when their host physical machine becomes overloaded, or another host physical machine
is under-utilized.
Hardware independence - when we need to upgrade, add, or remove hardware devices on the
host physical machine, we can safely relocate guest virtual machines to other host physical
machines. This means that guest virtual machines do not experience any downtime for hardware
improvements.
Energy saving - guest virtual machines can be redistributed to other host physical machines and
can thus be powered off to save energy and cut costs in low usage periods.
Geographic migration - guest virtual machines can be moved to another location for lower
latency or in serious circumstances.
Migration works by sending the state of the guest virtual machine's memory and any virtualized devices
to a destination host physical machine. It is recommended to use shared, networked storage to store the
guest virtual machine's images to be migrated. It is also recommended to use libvirt-managed storage
pools for shared storage when migrating virtual machines.
In a live migration, the guest virtual machine continues to run on the source host physical machine while
its memory pages are transferred, in order, to the destination host physical machine. During migration,
KVM monitors the source for any changes in pages it has already transferred, and begins to transfer
these changes when all of the initial pages have been transferred. KVM also estimates transfer speed
during migration, so when the remaining amount of data to transfer will take a certain configurable period
of time (10 milliseconds by default), KVM suspends the original guest virtual machine, transfers the
remaining data, and resumes the same guest virtual machine on the destination host physical machine.
A migration that is not performed live, suspends the guest virtual machine, then moves an image of the
guest virtual machine's memory to the destination host physical machine. The guest virtual machine is
then resumed on the destination host physical machine and the memory the guest virtual machine used
on the source host physical machine is freed. The time it takes to complete such a migration depends on
network bandwidth and latency. If the network is experiencing heavy use or low bandwidth, the migration
will take much longer.
If the original guest virtual machine modifies pages faster than KVM can transfer them to the destination
host physical machine, offline migration must be used, as live migration would never complete.
Migration requirements
A guest virtual machine installed on shared storage using one of the following protocols:
24
CHAPTER 4. KVM LIVE MIGRATION
iSCSI
FCoE
NFS
GFS2
SCSI RDMA protocols (SCSI RCP): the block export protocol used in Infiniband and 10GbE
iWARP adapters
The migration platforms and versions should be checked against table Table 4.1, “Live Migration
Compatibility”. It should also be noted that Red Hat Enterprise Linux 6 supports live migration of
guest virtual machines using raw and qcow2 images on shared storage.
Both systems must have the appropriate TCP/IP ports open. In cases where a firewall is used,
refer to the Red Hat Enterprise Linux Virtualization Security Guide which can be found at
https://access.redhat.com/site/documentation/ for detailed port information.
A separate system exporting the shared storage medium. Storage should not reside on either of
the two host physical machines being used for migration.
Shared storage must mount at the same location on source and destination systems. The
mounted directory names must be identical. Although it is possible to keep the images using
different paths, it is not recommended. Note that, if you are intending to use virt-manager to
perform the migration, the path names must be identical. If however you intend to use virsh to
perform the migration, different network configurations and mount directories can be used with
the help of --xml option or pre-hooks when doing migrations. Even without shared storage,
migration can still succeed with the option --copy-storage-all (deprecated). For more
information on prehooks, refer to libvirt.org, and for more information on the XML option, refer
to Chapter 20, Manipulating the Domain XML.
In Red Hat Enterprise Linux 5 and 6, the default cache mode of KVM guest virtual machines is
set to none, which prevents inconsistent disk states. Setting the cache option to none (using
virsh attach-disk cache none, for example), causes all of the guest virtual machine's
files to be opened using the O_DIRECT flag (when calling the open syscall), thus bypassing the
host physical machine's cache, and only providing caching on the guest virtual machine. Setting
the cache mode to none prevents any potential inconsistency problems, and when used makes
it possible to live-migrate virtual machines. For information on setting cache to none, refer to
Section 13.3, “Adding Storage Devices to Guests”.
Make sure that the libvirtd service is enabled (# chkconfig libvirtd on) and running (#
service libvirtd start). It is also important to note that the ability to migrate effectively is
dependent on the parameter settings in the /etc/libvirt/libvirtd.conf configuration file.
# vim /etc/libvirt/libvirtd.conf
25
Virtualization Administration Guide
Forward Minor release 5.x → 5.y (y>x, Fully supported Any issues should
x>=4) be reported
Forward Minor release 6.x → 6.y (y>x, Fully supported Any issues should
x>=0) be reported
Issues with SPICE — It has been found that SPICE has an incompatible change when
migrating from Red Hat Enterprise Linux 6.0 → 6.1. In such cases, the client may disconnect and
then reconnect, causing a temporary loss of audio and video. This is only temporary and all
services will resume.
Issues with USB — Red Hat Enterprise Linux 6.2 added USB functionality which included
migration support, but not without certain caveats which reset USB devices and caused any
application running over the device to abort. This problem was fixed in Red Hat Enterprise Linux
6.4, and should not occur in future versions. To prevent this from happening in a version prior to
6.4, abstain from migrating while USB devices are in use.
26
CHAPTER 4. KVM LIVE MIGRATION
Issues with the migration protocol — If backward migration ends with "unknown section
error", repeating the migration process can repair the issue as it may be a transient error. If not,
please report the problem.
Alternatively, use the NFS example in Section 4.3, “Shared Storage Example: NFS for a Simple
Migration”
IMPORTANT
This example uses NFS to share guest virtual machine images with other KVM host
physical machines. Although not practical for large installations, it is presented to
demonstrate migration techniques only. Do not use this example for migrating or running
more than a few guest virtual machines. In addition, it is required that the sync parameter
is enabled. This is required for proper export of the NFS storage. In addition, it is strongly
recommended that the NFS is mounted on source host physical machine, and the guest
virtual machine's image needs to be created on the NFS mounted directory located on
source host physical machine. It should also be noted that NFS file locking must not be
used as it is not supported in KVM.
iSCSI storage is a better choice for large deployments. Refer to Section 12.5, “iSCSI-
based Storage Pools” for configuration details.
Also note, that the instructions provided in this section are not meant to replace the detailed instructions
found in Red Hat Linux Storage Administration Guide. Refer to this guide for information on configuring
NFS, opening IP tables, and configuring the firewall.
# mkdir /var/lib/libvirt-img/images
3. Start NFS
a. Make sure that the ports for NFS in iptables (2049, for example) are opened and add
NFS to the /etc/hosts.allow file.
27
Virtualization Administration Guide
4. Mount the shared storage on both the source and the destination
Mount the /var/lib/libvirt/images directory on both the source and destination system,
running the following command twice. Once on the source system and again on the destination
system.
# mount source_host:/var/lib/libvirt-img/images
/var/lib/libvirt/images
WARNING
Make sure that the directories you create in this procedure is compliant with
the requirements as outlined in Section 4.1, “Live Migration Requirements”.
In addition, the directory may need to be labeled with the correct SELinux
label. For more information consult the NFS chapter in the Red Hat
Enterprise Linux Storage Administration Guide.
Note that the --live option may be eliminated when live migration is not desired. Additional options are
listed in Section 4.4.2, “Additional Options for the virsh migrate Command”.
The GuestName parameter represents the name of the guest virtual machine which you want to migrate.
The DestinationURL parameter is the connection URL of the destination host physical machine. The
destination system must run the same version of Red Hat Enterprise Linux, be using the same hypervisor
and have libvirt running.
NOTE
The DestinationURL parameter for normal migration and peer-to-peer migration has
different semantics:
normal migration: the DestinationURL is the URL of the target host physical
machine as seen from the source guest virtual machine.
Once the command is entered, you will be prompted for the root password of the destination system.
28
CHAPTER 4. KVM LIVE MIGRATION
IMPORTANT
An entry for the destination host physical machine, in the /etc/hosts file on the source
server is required for migration to succeed. Enter the IP address and host name for the
destination host physical machine in this file as shown in the following example,
substituting your destination host physical machine's IP address and host name:
10.0.0.20 host2.example.com
This example assumes you have fully configured shared storage and meet all the prerequisites (listed
here: Migration requirements).
Once the command is entered you will be prompted for the root password of the destination
system.
3. Wait
The migration may take some time depending on load and the size of the guest virtual machine.
virsh only reports errors. The guest virtual machine continues to run on the source host
physical machine until fully migrated.
NOTE
4. Verify the guest virtual machine has arrived at the destination host
From the destination system, host2.example.com, verify guest1-rhel6-64 is running:
29
Virtualization Administration Guide
NOTE
libvirt supports a variety of networking methods including TLS/SSL, UNIX sockets, SSH,
and unencrypted TCP. Refer to Chapter 5, Remote Management of Guests for more
information on using other methods.
NOTE
Non-running guest virtual machines cannot be migrated with the virsh migrate
command. To migrate a non-running guest virtual machine, the following script should be
used:
#################################################################
#
# Processing controls
#
30
CHAPTER 4. KVM LIVE MIGRATION
#################################################################
3. Change the max_clients and max_workers parameters settings. It is recommended that the
number be the same in both parameters. The max_clients will use 2 clients per migration
(one per side) and max_workers will use 1 worker on the source and 0 workers on the
destination during the perform phase and 1 worker on the destination during the finish phase.
IMPORTANT
NOTE
There may be cases where a migration connection drops because there are too
many ssh sessions that have been started, but not yet authenticated. By default,
sshd allows only 10 sessions to be in a "pre-authenticated state" at any time.
This setting is controlled by the MaxStartups parameter in the sshd
configuration file (located here: /etc/ssh/sshd_config), which may require
some adjustment. Adjusting this parameter should be done with caution as the
limitation is put in place to prevent DoS attacks (and over-use of resources in
general). Setting this value too high will negate its purpose. To change this
parameter, edit the file /etc/ssh/sshd_config, remove the # from the
beginning of the MaxStartups line, and change the 10 (default value) to a
higher number. Remember to save the file and restart the sshd service. For more
information, refer to the sshd_config man page.
31
Virtualization Administration Guide
--persistent - leaves the domain in a persistent state on the destination host physical
machine
--undefinesource - removes the guest virtual machine on the source host physical machine
--suspend - leaves the domain in a paused state on the destination host physical machine
--abort-on-error - cancels the migration if a soft error (such as an I/O error) happens
during the migration process.
--desturi [string]- connection URI of the destination host physical machine as seen from the
client(normal migration) or source(p2p migration)
--timeout [seconds]- forces a guest virtual machine to suspend when the live migration
counter exceeds N seconds. It can only be used with a live migration. Once the timeout is
initiated, the migration continues on the suspended guest virtual machine.
--dname [string] - changes the name of the guest virtual machine to a new name during
migration (if supported)
--xml - the filename indicated can be used to supply an alternative XML file for use on the
destination to supply a larger set of changes to any host-specific portions of the domain XML,
such as accounting for naming differences between source and destination in accessing
underlying storage. This option is usually omitted.
32
CHAPTER 4. KVM LIVE MIGRATION
1. Open virt-manager
Open virt-manager. Choose Applications → System Tools → Virtual Machine Manager
from the main menu bar to launch virt-manager.
3. Add connection
The Add Connection window appears.
33
Virtualization Administration Guide
Username: Enter the user name for the remote host physical machine.
Hostname: Enter the host name for the remote host physical machine.
Click the Connect button. An SSH connection is used in this example, so the specified user's
password must be entered in the next step.
34
CHAPTER 4. KVM LIVE MIGRATION
Open the list of guests inside the source host physical machine (click the small triangle on the
left of the host name) and right click on the guest that is to be migrated (guest1-rhel6-64 in this
example) and click Migrate.
In the New Host field, use the drop-down list to select the host physical machine you wish to
migrate the guest virtual machine to and click Migrate.
35
Virtualization Administration Guide
Figure 4.6. Choosing the destination host physical machine and starting the migration
process
36
CHAPTER 4. KVM LIVE MIGRATION
virt-manager now displays the newly migrated guest virtual machine running in the destination
host. The guest virtual machine that was running in the source host physical machine is now
listed inthe Shutoff state.
Figure 4.8. Migrated guest virtual machine running in the destination host physical
machine
5. Optional - View the storage details for the host physical machine
In the Edit menu, click Connection Details, the Connection Details window appears.
Click the Storage tab. The iSCSI target details for the destination host physical machine is
shown. Note that the migrated guest virtual machine is listed as using the storage
37
Virtualization Administration Guide
<pool type='iscsi'>
<name>iscsirhel6guest</name>
<source>
<host name='virtlab22.example.com.'/>
<device path='iqn.2001-05.com.iscsivendor:0-8a0906-
fbab74a06-a700000017a4cc89-rhevh'/>
</source>
<target>
<path>/dev/disk/by-path</path>
</target>
</pool>
...
Figure 4.10. XML configuration for the destination host physical machine
38
CHAPTER 5. REMOTE MANAGEMENT OF GUESTS
Be aware of the issues with using SSH for remotely managing your virtual machines, including:
you require root log in access to the remote machine for managing virtual machines,
there is no standard or trivial way to revoke a user's key on all hosts or guests, and
ssh does not scale well with larger numbers of remote machines.
NOTE
Red Hat Virtualization enables remote management of large numbers of virtual machines.
Refer to the Red Hat Virtualization documentation for further details.
openssh
openssh-askpass
openssh-clients
openssh-server
IMPORTANT
SSH keys are user dependent and may only be used by their owners. A key's owner is
the one who generated it. Keys may not be shared.
virt-manager must be run by the user who owns the keys to connect to the remote
host. That means, if the remote systems are managed by a non-root user virt-manager
must be run in unprivileged mode. If the remote systems are managed by the local root
user then the SSH keys must be owned and created by root.
You cannot manage the local host as an unprivileged user with virt-manager.
39
Virtualization Administration Guide
$ su -
# ssh-keygen -t rsa
Now try logging into the machine, with the ssh [email protected] command and
check in the .ssh/authorized_keys file to make sure unexpected keys have not been
added.
Add the passphrase for the SSH key to the ssh-agent, if required. On the local host, use the
following command to add the passphrase (if there was one) to enable password-less login.
# ssh-add ~/.ssh/id_rsa
$ ssh root@somehost
# chkconfig libvirtd on
# service libvirtd start
After libvirtd and SSH are configured you should be able to remotely access and manage your virtual
machines. You should also be able to access your guests with VNC at this point.
40
CHAPTER 5. REMOTE MANAGEMENT OF GUESTS
Remote hosts can be managed with the virt-manager GUI tool. SSH keys must belong to the user
executing virt-manager for password-less login to work.
1. Start virt-manager.
3. Use the drop down menu to select hypervisor type, and click the Connect to remote host check
box to open the Connection Method (in this case Remote tunnel over SSH), and enter the
desired User name and Hostname, then click Connect.
Procedure 5.1. Creating a certificate authority (CA) key for TLS management
1. Before you begin, confirm that the certtool utility is installed. If not:
41
Virtualization Administration Guide
3. Once the key generates, the next step is to create a signature file so the key can be self-signed.
To do this, create a file with signature details and name it ca.info. This file should contain the
following:
# vim ca.info
Once the file generates, the ca.info file may be deleted using the rm command. The file that
results from the generation process is named cacert.pem. This file is the public key
(certificate). The loaded file cakey.pem is the private key. This file should not be kept in a
shared space. Keep this key private.
5. Install the cacert.pem Certificate Authority Certificate file on all clients and servers in the
/etc/pki/CA/cacert.pem directory to let them know that the certificate issued by your CA
can be trusted. To view the contents of this file, run:
This is all that is required to set up your CA. Keep the CA's private key safe as you will need it in
order to issue certificates for your clients and servers.
This procedure demonstrates how to issue a certificate with the X.509 CommonName (CN)field set to
the host name of the server. The CN must match the host name which clients will be using to connect to
the server. In this example, clients will be connecting to the server using the URI:
qemu://mycommonname/system, so the CN field should be identical, ie mycommoname.
2. Generate a signature for the CA's private key by first creating a template file called
server.info . Make sure that the CN is set to be the same as the server's host name:
42
CHAPTER 5. REMOTE MANAGEMENT OF GUESTS
Make sure to keep the location of the private key secret. To view the contents of the file, perform
the following command:
When opening this file the CN= parameter should be the same as the CN that you set earlier. For
example, mycommonname.
serverkey.pem - the server's private key. Place this file in the following location:
/etc/pki/libvirt/private/serverkey.pem
servercert.pem - the server's certificate. Install it in the following location on the server:
/etc/pki/libvirt/servercert.pem
1. For every client (ie. any program linked with libvirt, such as virt-manager), you need to issue a
certificate with the X.509 Distinguished Name (DN) set to a suitable name. This needs to be
decided on a corporate level.
This process is quite similar to Procedure 5.2, “Issuing a server certificate”, with the following
exceptions noted.
3. Generate a signature for the CA's private key by first creating a template file called
client.info . The file should contain the following (fields should be customized to reflect your
region/location):
country = USA
state = North Carolina
locality = Raleigh
organization = Red Hat
cn = client1
43
Virtualization Administration Guide
tls_www_client
encryption_key
signing_key
# cp clientkey.pem /etc/pki/libvirt/private/clientkey.pem
# cp clientcert.pem /etc/pki/libvirt/clientcert.pem
UNIX Sockets
UNIX domain sockets are only accessible on the local machine. Sockets are not encrypted, and use
UNIX permissions or SELinux for authentication. The standard socket names are
/var/run/libvirt/libvirt-sock and /var/run/libvirt/libvirt-sock-ro (for read-only
connections).
SSH
Transported over a Secure Shell protocol (SSH) connection. Requires Netcat (the nc package) installed.
The libvirt daemon (libvirtd) must be running on the remote machine. Port 22 must be open for SSH
access. You should use some sort of SSH key management (for example, the ssh-agent utility) or you
will be prompted for a password.
ext
The ext parameter is used for any external program which can make a connection to the remote
machine by means outside the scope of libvirt. This parameter is unsupported.
TCP
Unencrypted TCP/IP socket. Not recommended for production use, this is normally disabled, but an
administrator can enable it for testing or use over a trusted network. The default port is 16509.
Remote URIs
A Uniform Resource Identifier (URI) is used by virsh and libvirt to connect to a remote host. URIs can
also be used with the --connect parameter for the virsh command to execute single commands or
migrations on remote hosts. Remote URIs are formed by taking ordinary local URIs and adding a host
44
CHAPTER 5. REMOTE MANAGEMENT OF GUESTS
name or transport name. As a special case, using a URI scheme of 'remote', will tell the remote libvirtd
server to probe for the optimal hypervisor driver. This is equivalent to passing a NULL URI for a local
connection
libvirt URIs take the general form (content in square brackets, "[]", represents optional functions):
driver[+transport]://[username@][hostname][:port]/path[?extraparameters]
Note that if the hypervisor(driver) is QEMU, the path is mandatory. If it is XEN, it is optional.
qemu://hostname/
xen://hostname/
xen+ssh://hostname/
The transport method or the host name must be provided to target an external location. For more
information refer to http://libvirt.org/guide/html/Application_Development_Guide-Architecture-
Remote_URIs.html.
Connect to a remote KVM host named host2, using SSH transport and the SSH user name
virtuser.The connect command for each is connect [<name>] [--readonly], where
<name> is a valid URI as explained here. For more information about the virsh connect
command refer to Section 14.1.5, “connect”
qemu+ssh://virtuser@hot2/
Connect to a remote KVM hypervisor on the host named host2 using TLS.
qemu://host2/
Testing examples
Connect to the local KVM hypervisor with a non-standard UNIX socket. The full path to the UNIX
socket is supplied explicitly in this case.
qemu+unix:///system?socket=/opt/libvirt/run/libvirt/libvirt-sock
Connect to the libvirt daemon with an unencrypted TCP/IP connection to the server with the IP
address 10.1.1.10 on port 5000. This uses the test driver with default settings.
test+tcp://10.1.1.10:5000/default
45
Virtualization Administration Guide
netcat -U socket
46
CHAPTER 5. REMOTE MANAGEMENT OF GUESTS
47
Virtualization Administration Guide
As a result, under-utilized virtualized servers or desktops can run on fewer hosts, which saves a number
of system resources, with the net effect of less power, cooling, and investment in server hardware.
Overcommitting requires allotting sufficient swap space on the host physical machine to accommodate
all guest virtual machines as well as enough memory for the host physical machine's processes. As a
basic rule, the host physical machine's operating system requires a maximum of 4GB of memory along
with a minimum of 4GB of swap space. For advanced instructions on determining an appropriate size for
the swap partition, see the Red Hat KnowledgeBase.
IMPORTANT
Overcommitting is not an ideal solution for general memory issues. The recommended
methods to deal with memory shortage are to allocate less memory per guest, add more
physical memory to the host, or utilize swap space.
Overcommitting does not work with all guest virtual machines, but has been found to work in a desktop
virtualization setup with minimal intensive usage or running several identical guest virtual machines with
kernel same-page merging (KSM).
IMPORTANT
When device assignment is in use, all virtual machine memory must be statically pre-
allocated to enable direct memory access (DMA) with the assigned device. Memory
overcommit is therefore not supported with device assignment.
48
CHAPTER 6. OVERCOMMITTING WITH KVM
Virtualized CPUs (vCPUs) are overcommitted best when a single host physical machine has multiple
guest virtual machines that do not share the same vCPU. KVM should safely support guest virtual
machines with loads under 100% at a ratio of five VCPUs (on 5 virtual machines) to one physical CPU on
one single host physical machine. KVM will switch between all of the machines, making sure that the
load is balanced.
Do not overcommit guest virtual machines on more than the physical number of processing cores. For
example a guest virtual machine with four vCPUs should not be run on a host physical machine with a
dual core processor, but on a quad core host. In addition, it is not recommended to have more than 10
total allocated vCPUs per physical processor core.
IMPORTANT
For more information on how to get the best performance out of your virtual machine, refer to the Red Hat
Enterprise Linux 6 Virtualization Tuning and Optimization Guide.
49
Virtualization Administration Guide
CHAPTER 7. KSM
The concept of shared memory is common in modern operating systems. For example, when a program
is first started it shares all of its memory with the parent program. When either the child or parent
program tries to modify this memory, the kernel allocates a new memory region, copies the original
contents and allows the program to modify this new region. This is known as copy on write.
KSM is a new Linux feature which uses this concept in reverse. KSM enables the kernel to examine two
or more already running programs and compare their memory. If any memory regions or pages are
identical, KSM reduces multiple identical memory pages to a single page. This page is then marked copy
on write. If the contents of the page is modified by a guest virtual machine, a new page is created for that
guest virtual machine.
This is useful for virtualization with KVM. When a guest virtual machine is started, it inherits only the
memory from the parent qemu-kvm process. Once the guest virtual machine is running, the contents of
the guest virtual machine operating system image can be shared when guests are running the same
operating system or applications.
NOTE
The page deduplication technology (used also by the KSM implementation) may introduce
side channels that could potentially be used to leak information across multiple guests. In
case this is a concern, KSM can be disabled on a per-guest basis.
KSM provides enhanced memory speed and utilization. With KSM, common process data is stored in
cache or in main memory. This reduces cache misses for the KVM guests which can improve
performance for some applications and operating systems. Secondly, sharing memory reduces the
overall memory usage of guests which allows for higher densities and greater utilization of resources.
NOTE
Starting in Red Hat Enterprise Linux 6.5, KSM is NUMA aware. This allows it to take
NUMA locality into account while coalescing pages, thus preventing performance drops
related to pages being moved to a remote node. Red Hat recommends avoiding cross-
node memory merging when KSM is in use. If KSM is in use, change the
/sys/kernel/mm/ksm/merge_across_nodes tunable to 0 to avoid merging pages
across NUMA nodes. Kernel memory accounting statistics can eventually contradict each
other after large amounts of cross-node merging. As such, numad can become confused
after the KSM daemon merges large amounts of memory. If your system has a large
amount of free memory, you may achieve higher performance by turning off and disabling
the KSM daemon. Refer to the Red Hat Enterprise Linux Performance Tuning Guide for
more information on NUMA.
Red Hat Enterprise Linux uses two separate methods for controlling KSM:
The ksm service starts and stops the KSM kernel thread.
The ksmtuned service controls and tunes the ksm, dynamically managing same-page merging.
The ksmtuned service starts ksm and stops the ksm service if memory sharing is not
necessary. The ksmtuned service must be told with the retune parameter to run when new
guests are created or destroyed.
Both of these services are controlled with the standard service management tools.
50
CHAPTER 7. KSM
When the ksm service is not started, KSM shares only 2000 pages. This default is low and provides
limited memory saving benefits.
When the ksm service is started, KSM will share up to half of the host physical machine system's main
memory. Start the ksm service to enable KSM to share more memory.
The ksm service can be added to the default startup sequence. Make the ksm service persistent with the
chkconfig command.
# chkconfig ksm on
The ksmtuned service can be tuned with the retune parameter. The retune parameter instructs
ksmtuned to run tuning functions manually.
Before changing the parameters in the file, there are a few terms that need to be clarified:
thres - Activation threshold, in kbytes. A KSM cycle is triggered when the thres value added
to the sum of all qemu-kvm processes RSZ exceeds total system memory. This parameter is the
equivalent in kbytes of the percentage defined in KSM_THRES_COEF.
The /etc/ksmtuned.conf file is the configuration file for the ksmtuned service. The file output below
is the default ksmtuned.conf file.
51
Virtualization Administration Guide
# KSM_NPAGES_DECAY=-50
The variables in the list below are also configurable variables in the /etc/ksmtuned.conf file as
noted below.
full_scans
Full scans run.
pages_shared
Total pages shared.
pages_sharing
Pages presently shared.
pages_to_scan
Pages not scanned.
pages_unshared
Pages no longer shared.
pages_volatile
Number of volatile pages.
run
Whether the KSM process is running.
52
CHAPTER 7. KSM
sleep_millisecs
Sleep milliseconds.
KSM tuning activity is stored in the /var/log/ksmtuned log file if the DEBUG=1 line is added to the
/etc/ksmtuned.conf file. The log file location can be changed with the LOGFILE parameter.
Changing the log file location is not advised and may require special configuration of SELinux settings.
Deactivating KSM
KSM has a performance overhead which may be too large for certain environments or host physical
machine systems.
KSM can be deactivated by stopping the ksmtuned and the ksm service. Stopping the services
deactivates KSM but does not persist after restarting.
Persistently deactivate KSM with the chkconfig command. To turn off the services, run the following
commands:
IMPORTANT
Ensure the swap size is sufficient for the committed RAM even with KSM. KSM reduces
the RAM usage of identical or similar guests. Overcommitting guests with KSM without
sufficient swap space may be possible but is not recommended because guest virtual
machine memory use can result in pages becoming unshared.
53
Virtualization Administration Guide
The cgroup functionality is fully supported by libvirt. By default, libvirt puts each guest into a separate
control group for various controllers (such as memory, cpu, blkio, device).
When a guest is started, it is already in a cgroup. The only configuration that may be required is the
setting of policies on the cgroups. Refer to the Red Hat Enterprise Linux Resource Management Guide
for more information on cgroups.
Introduction
x86 CPUs usually address memory in 4kB pages, but they are capable of using larger pages known as
huge pages. KVM guests can be deployed with huge page memory support in order to improve
performance by increasing CPU cache hits against the Transaction Lookaside Buffer (TLB). Huge pages
can significantly increase performance, particularly for large memory and memory-intensive workloads.
Red Hat Enterprise Linux 6 is able to more effectively manage large amounts of memory by increasing
the page size through the use of huge pages.
By using huge pages for a KVM guest, less memory is used for page tables and TLB misses are
reduced, thereby significantly increasing performance, especially for memory-intensive situations.
To use transparent huge pages, no special configuration in the qemu.conf file is required. Huge pages
are used by default if /sys/kernel/mm/redhat_transparent_hugepage/enabled is set to
always.
Transparent huge pages do not prevent the use of the hugetlbfs feature. However, when hugetlbfs is
not used, KVM will use transparent huge pages instead of the regular 4kB page size.
NOTE
See the Red Hat Enterprise Linux 7 Virtualization Tuning and Optimization Guide for
instructions on tuning memory performance with huge pages.
54
CHAPTER 8. ADVANCED GUEST VIRTUAL MACHINE ADMINISTRATION
Upgraded VMBUS protocols - VMBUS protocols have been upgraded to Windows 8 level. As
part of this work, now VMBUS interrupts can be processed on all available virtual CPUs in the
guest. Furthermore, the signaling protocol between the Red Hat Enterprise Linux guest virtual
machine and the Windows host physical machine has been optimized.
Synthetic frame buffer driver - Provides enhanced graphics performance and superior resolution
for Red Hat Enterprise Linux desktop users.
Live Virtual Machine Backup support - Provisions uninterrupted backup support for live Red Hat
Enterprise Linux guest virtual machines.
Dynamic expansion of fixed size Linux VHDs - Allows expansion of live mounted fixed size Red
Hat Enterprise Linux VHDs.
For more information, refer to the following article: Enabling Linux Support on Windows Server 2012 R2
Hyper-V.
NOTE
55
Virtualization Administration Guide
Note that all values will be rounded up to the nearest kibibyte by libvirt, and may be further rounded to the
granularity supported by the hypervisor. Some hypervisors also enforce a minimum, such as 4000KiB (or
4000 x 210 or 4,096,000 bytes). The units for this value are determined by the optional attribute memory
unit, which defaults to the kibibytes (KiB) as a unit of measure where the value given is multiplied by
210 or blocks of 1024 bytes.
In the cases where the guest virtual machine crashes the optional attribute dumpCore can be used to
control whether the guest virtual machine's memory should be included in the generated coredump
(dumpCore='on') or not included (dumpCore='off'). Note that the default setting is on so if the
parameter is not set to off, the guest virtual machine memory will be included in the coredump file.
The currentMemory attribute determines the actual memory allocation for a guest virtual machine. This
value can be less than the maximum allocation, to allow for ballooning up the guest virtual machines
memory on the fly. If this is omitted, it defaults to the same value as the memory element. The unit
attribute behaves the same as for memory.
In all cases for this section, the domain XML needs to be altered as follows:
<domain>
This example uses virsh to set a guest virtual machine, TestServer, to automatically start when the
host physical machine boots.
The guest virtual machine now automatically starts with the host physical machine.
56
CHAPTER 8. ADVANCED GUEST VIRTUAL MACHINE ADMINISTRATION
To stop a guest virtual machine automatically booting use the --disable parameter
The guest virtual machine no longer automatically starts with the host physical machine.
If needed, Create and then Edit the ~/.vnc/xstartup file to start a GNOME session whenever
vncserver is started. The first time you run the vncserver script it will ask you for a password you want
to use for your VNC session. For more information on vnc server files refer to the Red Hat Enterprise
Linux Installation Guide.
$ ./macgen.py
00:16:3e:20:b0:11
#!/usr/bin/python
# macgen.py script to generate a MAC address for guest virtual machines
#
import random
#
def randomMAC():
mac = [ 0x00, 0x16, 0x3e,
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff) ]
return ':'.join(map(lambda x: "%02x" % x, mac))
#
print randomMAC()
57
Virtualization Administration Guide
8.8.1. Another Method to Generate a New MAC for Your Guest Virtual Machine
You can also use the built-in modules of python-virtinst to generate a new MAC address and UUID
for use in a guest virtual machine configuration file:
The script above can also be implemented as a script file as seen below.
#!/usr/bin/env python
# -*- mode: python; -*-
print ""
print "New UUID:"
import virtinst.util ; print
virtinst.util.uuidToString(virtinst.util.randomUUID())
print "New MAC:"
import virtinst.util ; print virtinst.util.randomMAC()
print ""
Other (not qemu-kvm processes) busy or stalled processes on the host physical machine.
KVM guest virtual machines function as Linux processes. Linux processes are not permanently kept in
main memory (physical RAM) and will be placed into swap space (virtual memory) especially if they are
not being used. If a guest virtual machine is inactive for long periods of time, the host physical machine
kernel may move the guest virtual machine into swap. As swap is slower than physical memory it may
appear that the guest is not responding. This changes once the guest is loaded into the main memory.
Note that the process of loading a guest virtual machine from swap to main memory may take several
seconds per gigabyte of RAM assigned to the guest virtual machine, depending on the type of storage
used for swap and the performance of the components.
KVM guest virtual machines processes may be moved to swap regardless of whether memory is
overcommitted or overall memory usage.
Using unsafe overcommit levels or overcommitting with swap turned off guest virtual machine processes
or other critical processes is not recommended. Always ensure the host physical machine has sufficient
swap space when overcommitting memory.
For more information on overcommitting with KVM, refer to Chapter 6, Overcommitting with KVM.
58
CHAPTER 8. ADVANCED GUEST VIRTUAL MACHINE ADMINISTRATION
WARNING
Virtual memory allows a Linux system to use more memory than there is physical
RAM on the system. Underused processes are swapped out which allows active
processes to use memory, improving memory utilization. Disabling swap reduces
memory utilization as all processes are stored in physical RAM.
The <clock> element is used to determine how the guest virtual machine clock is synchronized with the
host physical machine clock. The clock element has the following attributes:
offset determines how the guest virtual machine clock is offset from the host physical machine
clock. The offset attribute has the following possible values:
Value Description
59
Virtualization Administration Guide
NOTE
The value utc is set as the clock offset in a virtual machine by default. However, if
the guest virtual machine clock is run with the localtime value, the clock offset
needs to be changed to a different value in order to have the guest virtual
machine clock synchronized with the host physical machine clock.
The timezone attribute determines which timezone is used for the guest virtual machine clock.
The adjustment attribute provides the delta for guest virtual machine clock synchronization. In
seconds, relative to UTC.
The name attribute dictates the type of the time source to use, and can be one of the following:
Value Description
60
CHAPTER 8. ADVANCED GUEST VIRTUAL MACHINE ADMINISTRATION
Value Description
8.10.2. track
The track attribute specifies what is tracked by the timer. Only valid for a name value of rtc.
Value Description
8.10.3. tickpolicy
The tickpolicy attribute assigns the policy used to pass ticks on to the guest virtual machine. The
following values are accepted:
Value Description
61
Virtualization Administration Guide
mode determines how the time source is exposed to the guest virtual machine. This attribute is only
relevant for a name value of tsc. All other timers are always emulated. Command is as follows: <timer
name='tsc' frequency='NNN' mode='auto|native|emulate|smpsafe'/>. Mode definitions
are given in the table.
Value Description
present is used to override the default set of timers visible to the guest virtual machine..
Value Description
Example 8.5. Clock synchronizing to local time with RTC and PIT timers
In this example. the clock is synchronized to local time with RTC and PIT timers
<clock offset="localtime">
<timer name="rtc" tickpolicy="catchup" track="guest virtual machine" />
<timer name="pit" tickpolicy="delay" />
</clock>
62
CHAPTER 8. ADVANCED GUEST VIRTUAL MACHINE ADMINISTRATION
NOTE
The PIT clocksource can be used with a 32-bit guest running under a 64-bit host (which
cannot use PIT), with the following conditions:
APIC timer must be disabled (use the "noapictimer" command line option)
NoHZ mode must be disabled in the guest (use the "nohz=off" command line
option)
High resolution timer mode must be disabled in the guest (use the "highres=off"
command line option)
The PIT clocksource is not compatible with either high resolution timer mode, or
NoHz mode.
Using performance monitoring, allows developers to use the CPU's PMU counter while using the
performance tool for profiling. The virtual performance monitoring unit feature allows virtual machine
users to identify sources of possible performance problems in their guest virtual machines, thereby
improving the ability to profile a KVM guest virtual machine.
This feature is only supported with guest virtual machines running Red Hat Enterprise Linux 6 and is
disabled by default. This feature only works using the Linux perf tool. Make sure the perf package is
installed using the command:
See the man page on perf for more information on the perf commands.
...
<pm>
<suspend-to-disk enabled='no'/>
<suspend-to-mem enabled='yes'/>
</pm>
...
63
Virtualization Administration Guide
The element pm enables ('yes') or disables ('no') BIOS support for S3 (suspend-to-disk) and S4
(suspend-to-mem) ACPI sleep states. If nothing is specified, then the hypervisor will be left with its
default value.
64
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
Emulated devices are purely virtual devices that mimic real hardware, allowing unmodified guest
operating systems to work with them using their standard in-box drivers. Red Hat Enterprise
Linux 6 supports up to 216 virtio devices.
Virtio devices are purely virtual devices designed to work optimally in a virtual machine. Virtio
devices are similar to emulated devices, however, non-Linux virtual machines do not include the
drivers they require by default. Virtualization management software like the Virtual Machine
Manager (virt-manager) and the Red Hat Virtualization Hypervisor (RHV-H) install these drivers
automatically for supported non-Linux guest operating systems. Red Hat Enterprise Linux 6
supports up to 700 scsi disks.
Assigned devices are physical devices that are exposed to the virtual machine. This method is
also known as 'passthrough'. Device assignment allows virtual machines exclusive access to
PCI devices for a range of tasks, and allows PCI devices to appear and behave as if they were
physically attached to the guest operating system. Red Hat Enterprise Linux 6 supports up to 32
assigned devices per virtual machine.
Device assignment is supported on PCIe devices, including select graphics devices. Nvidia K-series
Quadro, GRID, and Tesla graphics card GPU functions are now supported with device assignment in
Red Hat Enterprise Linux 6. Parallel PCI devices may be supported as assigned devices, but they have
severe limitations due to security and system configuration conflicts.
NOTE
The number of devices that can be attached to a virtual machine depends on several
factors. One factor is the number of files open by the QEMU process (configured in
/etc/security/limits.conf, which can be overridden by
/etc/libvirt/qemu.conf). Other limitation factors include the number of slots
available on the virtual bus, as well as the system-wide limit on open files set by sysctl.
For more information on specific devices and for limitations refer to Section 20.16, “Devices”.
Red Hat Enterprise Linux 6 supports PCI hot plug of devices exposed as single function slots to the
virtual machine. Single function host devices and individual functions of multi-function host devices may
be configured to enable this. Configurations exposing devices as multi-function PCI slots to the virtual
machine are recommended only for non-hotplug applications.
65
Virtualization Administration Guide
NOTE
Platform support for interrupt remapping is required to fully isolate a guest with assigned
devices from the host. Without such support, the host may be vulnerable to interrupt
injection attacks from a malicious guest. In an environment where guests are trusted, the
admin may opt-in to still allow PCI device assignment using the
allow_unsafe_interrupts option to the vfio_iommu_type1 module. This may
either be done persistently by adding a .conf file (for example local.conf) to
/etc/modprobe.d containing the following:
# echo 1 >
/sys/module/vfio_iommu_type1/parameters/allow_unsafe_interrupts
The Intel VT-d specifications must be enabled in the BIOS. Some system manufacturers disable
these specifications by default. The terms used to refer to these specifications can differ
between manufacturers; consult your system manufacturer's documentation for the appropriate
terms.
The example below is a modified grub file with Intel VT-d activated.
GRUB_CMDLINE_LINUX="rd.lvm.lv=vg_VolGroup00/LogVol01
vconsole.font=latarcyrheb-sun16 rd.lvm.lv=vg_VolGroup_1/root
vconsole.keymap=us $([ -x /usr/sbin/rhcrashkernel-param ] &&
/usr/sbin/
rhcrashkernel-param || :) rhgb quiet intel_iommu=on"
grub2-mkconfig -o /etc/grub2.cfg
Note that if you are using a UEFI-based host, the target file should be /etc/grub2-efi.cfg.
66
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
4. Ready to use
Reboot the system to enable the changes. Your system is now capable of PCI device
assignment.
grub2-mkconfig -o /etc/grub2.cfg
Note that if you are using a UEFI-based host, the target file should be /etc/grub2-efi.cfg.
4. Ready to use
Reboot the system to enable the changes. Your system is now capable of PCI device
assignment.
This example uses a PCIe network controller with the PCI identifier code, pci_0000_01_00_0, and a
fully virtualized guest machine named guest1-rhel6-64.
Procedure 9.3. Assigning a PCI device to a guest virtual machine with virsh
This example uses the Ethernet controller highlighted in the following output:
This Ethernet controller is shown with the short identifier 00:19.0. We need to find out the full
identifier used by virsh in order to assign this PCI device to a virtual machine.
67
Virtualization Administration Guide
To do so, use the virsh nodedev-list command to list all devices of a particular type (pci)
that are attached to the host machine. Then look at the output for the string that maps to the
short identifier of the device you wish to use.
This example highlights the string that maps to the Ethernet controller with the short identifier
00:19.0. In this example, the : and . characters are replaced with underscores in the full
identifier.
Record the PCI device number that maps to the device you want to use; this is required in other
steps.
68
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
<driver>
<name>e1000e</name>
</driver>
<capability type='pci'>
<domain>0</domain>
<bus>0</bus>
<slot>25</slot>
<function>0</function>
<product id='0x1502'>82579LM Gigabit Network
Connection</product>
<vendor id='0x8086'>Intel Corporation</vendor>
<iommuGroup number='7'>
<address domain='0x0000' bus='0x00' slot='0x19'
function='0x0'/>
</iommuGroup>
</capability>
</device>
NOTE
$ ls
/sys/bus/pci/devices/0000:01:00.0/iommu_group/devices/
0000:01:00.0 0000:01:00.1
The example device has the following values: bus = 0, slot = 25 and function = 0. The decimal
configuration uses those three values:
69
Virtualization Administration Guide
bus='0'
slot='25'
function='0'
Alternately, run virsh attach-device, specifying the virtual machine name and the guest's
XML file:
The PCI device should now be successfully assigned to the virtual machine, and accessible to the guest
operating system.
Procedure 9.4. Assigning a PCI device to a guest virtual machine using virt-manager
70
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
Select an unused PCI device. If you select a PCI device that is in use by another guest an error
may result. In this example, a spare 82576 network device is used. Click Finish to complete
setup.
71
Virtualization Administration Guide
72
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
NOTE
If device assignment fails, there may be other endpoints in the same IOMMU group that
are still attached to the host. There is no way to retrieve group information using virt-
manager, but virsh commands can be used to analyze the bounds of the IOMMU group
and if necessary sequester devices.
Refer to the Note in Section 9.1.1, “Assigning a PCI Device with virsh” for more
information on IOMMU groups and how to detach endpoint devices using virsh.
73
Virtualization Administration Guide
The virsh nodedev-list command lists all devices attached to the system, and identifies
each PCI device with a string. To limit output to only PCI devices, run the following command:
Record the PCI device number; the number is needed in other steps.
Information on the domain, bus and function are available from output of the virsh nodedev-
dumpxml command:
74
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
<driver>
<name>igb</name>
</driver>
<capability type='pci'>
<domain>0</domain>
<bus>1</bus>
<slot>0</slot>
<function>0</function>
<product id='0x10c9'>82576 Gigabit Network Connection</product>
<vendor id='0x8086'>Intel Corporation</vendor>
<iommuGroup number='7'>
<address domain='0x0000' bus='0x00' slot='0x19'
function='0x0'/>
</iommuGroup>
</capability>
</device>
NOTE
If there are multiple endpoints in the IOMMU group and not all of them are
assigned to the guest, you will need to manually detach the other endpoint(s) from
the host by running the following command before you start the guest:
Refer to the Note in Section 9.1.1, “Assigning a PCI Device with virsh” for more
information on IOMMU groups.
virt-install \
--name=guest1-rhel6-64 \
--disk path=/var/lib/libvirt/images/guest1-rhel6-64.img,size=8 \
--nonsparse --graphics spice \
--vcpus=2 --ram=2048 \
--location=http://example1.com/installation_tree/RHEL6.0-Server-
x86_64/os \
--nonetworks \
--os-type=linux \
--os-variant=rhel6
--host-device=pci_0000_01_00_0
75
Virtualization Administration Guide
If the device is not using managed mode, use the following command to re-attach the PCI device
to the host machine:
76
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
Click the Remove button to confirm. The device is now available for host use.
NOTE
This action cannot be performed when the guest virtual machine is running. You must add
the PCI device on a guest virtual machine that is shutdown.
77
Virtualization Administration Guide
To use VFIO device assignment rather than traditional/legacy KVM device assignment (VFIO is a new
method of device assignment that is compatible with UEFI Secure Boot), a <type='hostdev'>
interface can have an optional driver sub-element with a name attribute set to "vfio". To use legacy KVM
device assignment you can set name to "kvm" (or simply omit the <driver> element, since
<driver='kvm'> is currently the default).
NOTE
<devices>
<interface type='hostdev'>
<driver name='vfio'/>
<source>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07'
function='0x0'/>
</source>
<mac address='52:54:00:6d:90:02'>
<virtualport type='802.1Qbh'>
<parameters profileid='finance'/>
</virtualport>
</interface>
</devices>
These VFs can be assigned to guest virtual machines in the traditional manner using the element
<hostdev>, but as SR-IOV VF network devices do not have permanent unique MAC addresses, it
causes issues where the guest virtual machine's network settings would have to be re-configured each
time the host physical machine is rebooted. To remedy this, you would need to set the MAC address
prior to assigning the VF to the host physical machine and you would need to set this each and every
time the guest virtual machine boots. In order to assign this MAC address as well as other options, refer
to the procedure described in Procedure 9.8, “Configuring MAC addresses, vLAN, and virtual ports for
assigning PCI devices on SR-IOV”.
Procedure 9.8. Configuring MAC addresses, vLAN, and virtual ports for assigning PCI devices on
SR-IOV
78
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
It is important to note that the <hostdev> element cannot be used for function-specific items like MAC
address assignment, vLAN tag ID assignment, or virtual port assignment because the <mac>, <vlan>,
and <virtualport> elements are not valid children for <hostdev>. As they are valid for
<interface>, support for a new interface type was added ( <interface type='hostdev'>). This
new interface device type behaves as a hybrid of an <interface> and <hostdev>. Thus, before
assigning the PCI device to the guest virtual machine, libvirt initializes the network-specific
hardware/switch that is indicated (such as setting the MAC address, setting a vLAN tag, or associating
with an 802.1Qbh switch) in the guest virtual machine's XML configuration file. For information on setting
the vLAN tag, refer to Section 18.14, “Setting vLAN Tags”.
2. Gather information
In order to use <interface type='hostdev'>, you must have an SR-IOV-capable network
card, host physical machine hardware that supports either the Intel VT-d or AMD IOMMU
extensions, and you must know the PCI address of the VF that you wish to assign.
79
Virtualization Administration Guide
<devices>
...
<interface type='hostdev' managed='yes'>
<source>
<address type='pci' domain='0x0' bus='0x00' slot='0x07'
function='0x0'/> <!--these values can be decimal as well-->
</source>
<mac address='52:54:00:6d:90:02'/>
<!--sets the mac address-->
<virtualport type='802.1Qbh'>
<!--sets the virtual port for the 802.1Qbh switch-->
<parameters profileid='finance'/>
</virtualport>
<vlan>
<!--sets the vlan tag-->
<tag id='42'/>
</vlan>
</interface>
...
</devices>
Note that if you do not provide a MAC address, one will be automatically generated, just as with
any other type of interface device. Also, the <virtualport> element is only used if you are
connecting to an 802.11Qgh hardware switch (802.11Qbg (a.k.a. "VEPA") switches are currently
not supported.
When the guest virtual machine starts, it sees the network device provided to it by the physical
host machine's adapter, with the configured MAC address. This MAC address will remain
unchanged across guest virtual machine and host physical machine reboots.
9.1.8. Setting PCI Device Assignment from a Pool of SR-IOV Virtual Functions
Hard coding the PCI addresses of a particular Virtual Functions (VFs) into a guest's configuration has
two serious limitations:
The specified VF must be available any time the guest virtual machine is started, implying that
the administrator must permanently assign each VF to a single guest virtual machine (or modify
the configuration file for every guest virtual machine to specify a currently unused VF's PCI
address each time every guest virtual machine is started).
If the guest virtual machine is moved to another host physical machine, that host physical
machine must have exactly the same hardware in the same location on the PCI bus (or, again,
the guest virtual machine configuration must be modified prior to start).
80
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
It is possible to avoid both of these problems by creating a libvirt network with a device pool containing
all the VFs of an SR-IOV device. Once that is done you would configure the guest virtual machine to
reference this network. Each time the guest is started, a single VF will be allocated from the pool and
assigned to the guest virtual machine. When the guest virtual machine is stopped, the VF will be returned
to the pool for use by another guest virtual machine.
The following is an example network definition that will make available a pool of all VFs for the
SR-IOV adapter with its physical function (PF) at "eth3' on the host physical machine:
<network>
<name>passthrough</name>
<!--This is the name of the file you created-->
<forward mode='hostdev' managed='yes'>
<pf dev='myNetDevName'/>
<!--Use the netdev name of your SR-IOV devices PF here-->
</forward>
</network>
81
Virtualization Administration Guide
<interface type='network'>
<source network='passthrough'>
</interface>
7. Verification
You can verify this by running virsh net-dumpxml passthrough command after starting
the first guest that uses the network; you will get output similar to the following:
<network connections='1'>
<name>passthrough</name>
<uuid>a6b49429-d353-d7ad-3185-4451cc786437</uuid>
<forward mode='hostdev' managed='yes'>
<pf dev='eth3'/>
<address type='pci' domain='0x0000' bus='0x02' slot='0x10'
function='0x1'/>
<address type='pci' domain='0x0000' bus='0x02' slot='0x10'
function='0x3'/>
<address type='pci' domain='0x0000' bus='0x02' slot='0x10'
function='0x5'/>
<address type='pci' domain='0x0000' bus='0x02' slot='0x10'
function='0x7'/>
<address type='pci' domain='0x0000' bus='0x02' slot='0x11'
function='0x1'/>
<address type='pci' domain='0x0000' bus='0x02' slot='0x11'
function='0x3'/>
<address type='pci' domain='0x0000' bus='0x02' slot='0x11'
function='0x5'/>
</forward>
</network>
82
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
Most devices such as web cameras, card readers, keyboards, or mice are connected to a computer
using a USB port and cable. There are two ways to pass such devices to a guest virtual machine:
Using USB passthrough - this requires the device to be physically connected to the host physical
machine that is hosting the guest virtual machine. SPICE is not needed in this case. USB
devices on the host can be passed to the guest using the command line or virt-manager. Refer
to Section 15.3.1, “Attaching USB Devices to a Guest Virtual Machine” for virt manager
directions.
NOTE
virt-manager should not be used for hot plugging or hot unplugging devices. If
you want to hot plug/or hot unplug a USB device, refer to Procedure 14.1, “Hot
plugging USB devices for use by the guest virtual machine”.
Using USB re-direction - USB re-direction is best used in cases where there is a host physical
machine that is running in a data center. The user connects to his/her guest virtual machine from
a local machine or thin client. On this local machine there is a SPICE client. The user can attach
any USB device to the thin client and the SPICE client will redirect the device to the host
physical machine on the data center so it can be used by the guest virtual machine that is
running on the thin client. For instructions on USB re-direction using the virt-manager, refer to
Section 15.3.1, “Attaching USB Devices to a Guest Virtual Machine” It should be noted that USB
redirection is not possible using the TCP protocol (Refer to BZ#1085318).
<class>:<vendor>:<product>:<version>:<allow>
Use the value -1 to designate it to accept any value for a particular field. You may use multiple rules
on the same command line using | as a separator.
IMPORTANT
If a device matches none of the rule filters, redirecting it will not be allowed!
Example 9.1. An example of limiting redirection with a windows guest virtual machine
2. Add the following code excerpt to the guest virtual machine's' domain xml file:
83
Virtualization Administration Guide
3. Start the guest virtual machine and confirm the setting changes by running the following:
-device usb-redir,chardev=charredir0,id=redir0,/
filter=0x08:0x1234:0xBEEF:0x0200:1|-1:-1:-1:-1:0,bus=usb.0,port=3
4. Plug a USB device into a host physical machine, and use virt-manager to connect to the
guest virtual machine.
5. Click Redirect USB Service in the menu, which will produce the following message: "Some
USB devices are blocked by host policy". Click OK to confirm and continue.
6. To make sure that the filter captures properly check the USB device vendor and product,
then make the following changes in the guest virtual machine's domain XML to allow for USB
redirection.
<redirfilter>
<usbdev class='0x08' vendor='0x0951' product='0x1625'
version='2.0' allow='yes'/>
<usbdev allow='no'/>
</redirfilter>
7. Restart the guest virtual machine, then use virt-viewer to connect to the guest virtual
machine. The USB device will now redirect traffic to the guest virtual machine.
84
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
...
<devices>
<controller type='ide' index='0'/>
<controller type='virtio-serial' index='0' ports='16' vectors='4'/>
<controller type='virtio-serial' index='1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x0a'
function='0x0'/>
</controller>
...
</devices>
...
Each controller has a mandatory attribute <controller type>, which must be one of:
ide
fdc
scsi
sata
usb
ccid
virtio-serial
pci
The <controller> element has a mandatory attribute <controller index> which is the decimal
integer describing in which order the bus controller is encountered (for use in controller attributes of
<address> elements). When <controller type ='virtio-serial'> there are two additional
optional attributes (named ports and vectors), which control how many devices can be connected
through the controller. Note that Red Hat Enterprise Linux 6 does not support the use of more than 32
vectors per device. Using more vectors will cause failures in migrating the guest virtual machine.
When <controller type ='scsi'>, there is an optional attribute model model, which can have the
following values:
auto
buslogic
ibmvscsi
lsilogic
lsisas1068
lsisas1078
virtio-scsi
85
Virtualization Administration Guide
vmpvscsi
When <controller type ='usb'>, there is an optional attribute model model, which can have the
following values:
piix3-uhci
piix4-uhci
ehci
ich9-ehci1
ich9-uhci1
ich9-uhci2
ich9-uhci3
vt82c686b-uhci
pci-ohci
nec-xhci
NOTE
If the USB bus needs to be explicitly disabled for the guest virtual machine,
<model='none'> may be used. .
For controllers that are themselves devices on a PCI or USB bus, an optional sub-element <address>
can specify the exact relationship of the controller to its master bus, with semantics as shown in
Section 9.4, “Setting Addresses for Devices”.
An optional sub-element <driver> can specify the driver specific options. Currently it only supports
attribute queues, which specifies the number of queues for the controller. For best performance, it is
recommended to specify a value matching the number of vCPUs.
USB companion controllers have an optional sub-element <master> to specify the exact relationship of
the companion to its master controller. A companion controller is on the same bus as its master, so the
companion index value should be equal.
86
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
...
<devices>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0' bus='0' slot='4' function='7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0' bus='0' slot='4' function='0'
multifunction='on'/>
</controller>
...
</devices>
...
PCI controllers have an optional model attribute with the following possible values:
pci-root
pcie-root
pci-bridge
dmi-to-pci-bridge
The root controllers (pci-root and pcie-root) have an optional pcihole64 element specifying how
big (in kilobytes, or in the unit specified by pcihole64's unit attribute) the 64-bit PCI hole should be.
Some guest virtual machines (such as Windows Server 2003) may cause a crash, unless unit is
disabled (set to 0 unit='0').
For machine types which provide an implicit PCI bus, the pci-root controller with index='0' is auto-
added and required to use PCI devices. pci-root has no address. PCI bridges are auto-added if there are
too many devices to fit on the one bus provided by model='pci-root', or a PCI bus number greater
than zero was specified. PCI bridges can also be specified manually, but their addresses should only
refer to PCI buses provided by already specified PCI controllers. Leaving gaps in the PCI controller
indexes might lead to an invalid configuration. The following XML example can be added to the
<devices> section:
...
<devices>
<controller type='pci' index='0' model='pci-root'/>
<controller type='pci' index='1' model='pci-bridge'>
<address type='pci' domain='0' bus='0' slot='5' function='0'
multifunction='off'/>
</controller>
</devices>
...
87
Virtualization Administration Guide
For machine types which provide an implicit PCI Express (PCIe) bus (for example, the machine types
based on the Q35 chipset), the pcie-root controller with index='0' is auto-added to the domain's
configuration. pcie-root has also no address, but provides 31 slots (numbered 1-31) and can only be
used to attach PCIe devices. In order to connect standard PCI devices on a system which has a pcie-
root controller, a pci controller with model='dmi-to-pci-bridge' is automatically added. A dmi-to-
pci-bridge controller plugs into a PCIe slot (as provided by pcie-root), and itself provides 31 standard PCI
slots (which are not hot-pluggable). In order to have hot-pluggable PCI slots in the guest system, a pci-
bridge controller will also be automatically created and connected to one of the slots of the auto-created
dmi-to-pci-bridge controller; all guest devices with PCI addresses that are auto-determined by libvirt will
be placed on this pci-bridge device.
...
<devices>
<controller type='pci' index='0' model='pcie-root'/>
<controller type='pci' index='1' model='dmi-to-pci-bridge'>
<address type='pci' domain='0' bus='0' slot='0xe' function='0'/>
</controller>
<controller type='pci' index='2' model='pci-bridge'>
<address type='pci' domain='0' bus='1' slot='1' function='0'/>
</controller>
</devices>
...
Every address has a mandatory attribute type that describes which bus the device is on. The choice of
which address to use for a given device is constrained in part by the device and the architecture of the
guest virtual machine. For example, a <disk> device uses type='drive', while a <console> device
would use type='pci' on i686 or x86_64 guest virtual machine architectures. Each address type has
further optional attributes that control where on the bus the device will be placed as described in the
table:
88
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
89
Virtualization Administration Guide
iobase
irq
pass-through a physical SCSI device from the host to the guest via the QEMU scsi-block device,
and allow the usage of hundreds of devices per guest; an improvement from the 28-device limit
of virtio-blk.
This section details the necessary steps to create a virtual SCSI controller (also known as "Host Bus
Adapter", or HBA) and to add SCSI storage to the guest virtual machine.
1. Display the configuration of the guest virtual machine (Guest1) and look for a pre-existing SCSI
controller:
If a device controller is present, the command will output one or more lines similar to the
following:
90
CHAPTER 9. GUEST VIRTUAL MACHINE DEVICE CONFIGURATION
2. If the previous step did not show a device controller, create the description for one in a new file
and add it to the virtual machine, using the following steps:
a. Create the device controller by writing a <controller> element in a new file and save this
file with an XML extension. virtio-scsi-controller.xml, for example.
In this example the --config option behaves the same as it does for disks. Refer to
Procedure 13.2, “Adding physical block devices to guests” for more information.
3. Add a new SCSI disk or CD-ROM. The new disk can be added using the methods in sections
Section 13.3.1, “Adding File-based Storage to a Guest” and Section 13.3.2, “Adding Hard Drives
and Other Block Devices to a Guest”. In order to create a SCSI disk, specify a target device
name that starts with sd.
Depending on the version of the driver in the guest virtual machine, the new disk may not be
detected immediately by a running guest virtual machine. Follow the steps in the Red Hat
Enterprise Linux Storage Administration Guide.
Using an RNG is particularly useful when a device such as a keyboard, mouse and other inputs are not
enough to generate sufficient entropy on the guest virtual machine. The virtio-rng device is available for
both Red Hat Enterprise Linux and Windows guest virtual machines. Refer to the Note for instructions on
installing the Windows requirements. Unless noted, the following descriptions are for both Red Hat
Enterprise Linux and Windows guest virtual machines.
When virtio-rng is enabled on a Linux guest virtual machine, a chardev is created in the guest virtual
machine at the location /dev/hwrng/. This chardev can then be opened and read to fetch entropy from
the host physical machine. In order for guest virtual machines' applications to benefit from using
randomness from the virtio-rng device transparently, the input from /dev/hwrng/ must be relayed to
the kernel entropy pool in the guest virtual machine. This can be accomplished if the information in this
location is coupled with the rgnd daemon (contained within the rng-tools).
This coupling results in the entropy to be routed to the guest virtual machine's /dev/random file. The
process is done manually in Red Hat Enterprise Linux 6 guest virtual machines.
Red Hat Enterprise Linux 6 guest virtual machines are coupled by running the following command:
91
Virtualization Administration Guide
For more assistance, run the man rngd command for an explanation of the command options shown
here. For further examples, refer to Procedure 9.11, “Implementing virtio-rng with the command line
tools” for configuring the virtio-rng device.
NOTE
Windows guest virtual machines require the driver viorng to be installed. Once installed,
the virtual RNG device will work using the CNG (crypto next generation) API provided by
Microsoft. Once the driver is installed, the virtrng device appears in the list of RNG
providers.
2. In a terminal window, using the virsh edit domain-name command, open the XML file for
the desired guest virtual machine.
...
<devices>
<rng model='virtio'>
<rate period="2000" bytes="1234"/>
<backend model='random'>/dev/random</backend>
<source mode='bind' service='1234'>
<source mode='connect' host='192.0.2.1' service='1234'>
</backend>
</rng>
</devices>
...
92
CHAPTER 10. QEMU-IMG AND QEMU GUEST AGENT
Check
Perform a consistency check on the disk image filename.
NOTE
Using the -r tries to repair any inconsistencies that are found during the check, but when used with -r
leaks cluster leaks are repaired and when used with -r all all kinds of errors are fixed. Note that this
has a risk of choosing the wrong fix or hiding corruption issues that may have already occurred.
Commit
Commits any changes recorded in the specified file (filename) to the file's base image with the qemu-
img commit command. Optionally, specify the file's format type (format).
Convert
The convert option is used to convert one recognized image format to another image format.
Command format:
# qemu-img convert [-c] [-p] [-f format] [-t cache] [-O output_format] [-
o options] [-S sparse_size] filename output_filename
The -p parameter shows the progress of the command (optional and not for every command) and -S
option allows for the creation of a sparse file, which is included within the disk image. Sparse files in all
purposes function like a standard file, except that the physical blocks that only contain zeros (nothing).
When the Operating System sees this file, it treats it as it exists and takes up actual disk space, even
though in reality it does not take any. This is particularly helpful when creating a disk for a guest virtual
machine as this gives the appearance that the disk has taken much more disk space than it has. For
example, if you set -S to 50Gb on a disk image that is 10Gb, then your 10Gb of disk space will appear to
be 60Gb in size even though only 10Gb is actually being used.
Convert the disk image filename to disk image output_filename using format output_format.
The disk image can be optionally compressed with the -c option, or encrypted with the -o option by
setting -o encryption. Note that the options available with the -o parameter differ with the selected
format.
93
Virtualization Administration Guide
Only the qcow2 format supports encryption or compression. qcow2 encryption uses the AES format with
secure 128-bit keys. qcow2 compression is read-only, so if a compressed sector is converted from
qcow2 format, it is written to the new format as uncompressed data.
Image conversion is also useful to get a smaller image when using a format which can grow, such as
qcow or cow. The empty sectors are detected and suppressed from the destination image.
Create
Create the new disk image filename of size size and format format.
If a base image is specified with -o backing_file=filename, the image will only record differences
between itself and the base image. The backing file will not be modified unless you use the commit
command. No size needs to be specified in this case.
Preallocation is an option that may only be used with creating qcow2 images. Accepted values include -
o preallocation=off|meta|full|falloc. Images with preallocated metadata are larger than
images without. However in cases where the image size increases, performance will improve as the
image grows.
It should be noted that using full allocation can take a long time with large images. In cases where you
want full allocation and time is of the essence, using falloc will save you time.
Info
The info parameter displays information about a disk image filename. The format for the info option is
as follows:
This command is often used to discover the size reserved on disk which can be different from the
displayed size. If snapshots are stored in the disk image, they are displayed also. This command will
show for example, how much space is being taken by a qcow2 image on a block device. This is done by
running the qemu-img. You can check that the image in use is the one that matches the output of the
qemu-img info command with the qemu-img check command. Refer to Section 10.1, “Using qemu-
img”.
Map
The # qemu-img map [-f format] [--output=output_format] filename command dumps
the metadata of the image filename and its backing file chain. Specifically, this command dumps the
allocation state of every sector of a specified file, together with the topmost file that allocates it in the
backing file chain. For example, if you have a chain such as c.qcow2 → b.qcow2 → a.qcow2, a.qcow is
the original file, b.qcow2 is the changes made to a.qcow2 and c.qcow2 is the delta file from b.qcow2.
When this chain is created the image files stores the normal image data, plus information about what is
94
CHAPTER 10. QEMU-IMG AND QEMU GUEST AGENT
in which file and where it is located within the file. This information is referred to as the image's
metadata. The -f format option is the format of the specified image file. Formats such as raw, qcow2,
vhdx and vmdk may be used. There are two output options possible: human and json.
human is the default setting. It is designed to be more readable to the human eye, and as such,
this format should not be parsed. For clarity and simplicity, the default human format only dumps
known-nonzero areas of the file. Known-zero parts of the file are omitted altogether, and likewise
for parts that are not allocated throughout the chain. When the command is executed, qemu-img
output will identify a file from where the data can be read, and the offset in the file. The output is
displayed as a table with four columns; the first three of which are hexadecimal numbers.
For more information on the JSON format, refer to the qemu-img(1) man page.
Rebase
Changes the backing file of an image.
# qemu-img rebase [-f format] [-t cache] [-p] [-u] -b backing_file [-F
backing_format] filename
The backing file is changed to backing_file and (if the format of filename supports the feature), the
backing file format is changed to backing_format.
NOTE
Only the qcow2 format supports changing the backing file (rebase).
There are two different modes in which rebase can operate: Safe and Unsafe.
Safe mode is used by default and performs a real rebase operation. The new backing file may differ from
the old one and the qemu-img rebase command will take care of keeping the guest virtual machine-
visible content of filename unchanged. In order to achieve this, any clusters that differ between
backing_file and old backing file of filename are merged into filename before making any changes to the
backing file.
95
Virtualization Administration Guide
Note that safe mode is an expensive operation, comparable to converting an image. The old backing file
is required for it to complete successfully.
Unsafe mode is used if the -u option is passed to qemu-img rebase. In this mode, only the backing
file name and format of filename is changed, without any checks taking place on the file contents. Make
sure the new backing file is specified correctly or the guest-visible content of the image will be corrupted.
This mode is useful for renaming or moving the backing file. It can be used without an accessible old
backing file. For instance, it can be used to fix an image whose backing file has already been moved or
renamed.
Resize
Change the disk image filename as if it had been created with size size. Only images in raw format can
be resized regardless of version. Red Hat Enterprise Linux 6.1 and later adds the ability to grow (but not
shrink) images in qcow2 format.
Use the following to set the size of the disk image filename to size bytes:
You can also resize relative to the current size of the disk image. To give a size relative to the current
size, prefix the number of bytes with + to grow, or - to reduce the size of the disk image by that number
of bytes. Adding a unit suffix allows you to set the image size in kilobytes (K), megabytes (M), gigabytes
(G) or terabytes (T).
WARNING
Before using this command to shrink a disk image, you must use file system and
partitioning tools inside the VM itself to reduce allocated file systems and partition
sizes accordingly. Failure to do so will result in data loss.
After using this command to grow a disk image, you must use file system and
partitioning tools inside the VM to actually begin using the new space on the device.
Snapshot
List, apply, create, or delete an existing snapshot (snapshot) of an image (filename).
-l lists all snapshots associated with the specified disk image. The apply option, -a, reverts the disk
image (filename) to the state of a previously saved snapshot. -c creates a snapshot (snapshot) of an
image (filename). -d deletes the specified snapshot.
Supported Formats
qemu-img is designed to convert files to one of the following formats:
96
CHAPTER 10. QEMU-IMG AND QEMU GUEST AGENT
raw
Raw disk image format (default). This can be the fastest file-based format. If your file system supports
holes (for example in ext2 or ext3 on Linux or NTFS on Windows), then only the written sectors will
reserve space. Use qemu-img info to obtain the real size used by the image or ls -ls on
Unix/Linux. Although Raw images give optimal performance, only very basic features are available
with a Raw image (for example, no snapshots are available).
qcow2
QEMU image format, the most versatile format with the best feature set. Use it to have optional AES
encryption, zlib-based compression, support of multiple VM snapshots, and smaller images, which
are useful on file systems that do not support holes (non-NTFS file systems on Windows). Note that
this expansive feature set comes at the cost of performance.
Although only the formats above can be used to run on a guest virtual machine or host physical machine
machine, qemu-img also recognizes and supports the following formats in order to convert from them
into either raw or qcow2 format. The format of an image is usually detected automatically. In addition to
converting these formats into raw or qcow2 , they can be converted back from raw or qcow2 to the
original format.
bochs
Bochs disk image format.
cloop
Linux Compressed Loop image, useful only to reuse directly compressed CD-ROM images present
for example in the Knoppix CD-ROMs.
cow
User Mode Linux Copy On Write image format. The cow format is included only for compatibility with
previous versions. It does not work with Windows.
dmg
Mac disk image format.
nbd
Network block device.
parallels
Parallels virtualization disk image format.
qcow
Old QEMU image format. Only included for compatibility with older versions.
vdi
Oracle VM VirtualBox hard disk image format.
vmdk
VMware compatible image format (read-write support for versions 1 and 2, and read-only support for
version 3).
97
Virtualization Administration Guide
vpc
Windows Virtual PC disk image format. Also referred to as vhd, or Microsoft virtual hard disk image
format.
vvfat
Virtual VFAT disk image format.
IMPORTANT
Note that it is only safe to rely on the guest agent when run by trusted guests. An
untrusted guest may maliciously ignore or abuse the guest agent protocol, and although
built-in safeguards exist to prevent a denial of service attack on the host, the host requires
guest co-operation for operations to run as expected.
Note that QEMU guest agent can be used to enable and disable virtual CPUs (vCPUs) while the guest is
running, thus adjusting the number of vCPUs without using the hot plug and hot unplug features. Refer to
Section 14.13.6, “Configuring Virtual CPU Count” for more information.
NOTE
For instructions on how to set up the QEMU guest agent on Windows guests, refer to the
instructions found here.
98
CHAPTER 10. QEMU-IMG AND QEMU GUEST AGENT
<channel type='unix'>
<source mode='bind' path='/var/lib/libvirt/qemu/rhel6.agent'/>
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
Figure 10.1. Editing the guest XML to configure the QEMU guest agent
You can now communicate with the guest by sending valid libvirt commands over the established
character device driver.
The qemu-guest-agent cannot detect whether or not a client has connected to the channel.
There is no way for a client to detect whether or not qemu-guest-agent has disconnected or
reconnected to the back-end.
If the virtio-serial device resets and qemu-guest-agent has not connected to the channel
(generally caused by a reboot or hot plug), data from the client will be dropped.
If qemu-guest-agent has connected to the channel following a virtio-serial device reset, data
from the client will be queued (and eventually throttled if available buffers are exhausted),
regardless of whether or not qemu-guest-agent is still running or connected.
virsh shutdown --mode=agent - This shutdown method is more reliable than virsh
shutdown --mode=acpi, as virsh shutdown used with the QEMU guest agent is
guaranteed to shut down a cooperative guest in a clean state. If the agent is not present, libvirt
has to instead rely on injecting an ACPI shutdown event, but some guests ignore that event and
thus will not shut down.
virsh snapshot-create --quiesce - Allows the guest to flush its I/O into a stable state
before the snapshot is created, which allows use of the snapshot without having to perform a
fsck or losing partial database transactions. The guest agent allows a high level of disk contents
99
Virtualization Administration Guide
virsh dompmsuspend - Suspends a running guest gracefully using the guest operating
system's power management functions.
File system applications / databases flush working buffers to the virtual disk and stop accepting
client connections
Snapshot is confirmed
Use the snapshot-create-as command to create a snapshot of the guest disk. See
Section 14.15.2.2, “Creating a snapshot for the current domain” for more details on this command.
NOTE
100
CHAPTER 10. QEMU-IMG AND QEMU GUEST AGENT
The main hook script, /usr/libexec/qemu-ga/fsfreeze-hook logs its own messages, as well as
the application-specific script's standard output and error messages, in the following log file:
/var/log/qemu-ga/fsfreeze-hook.log. For more information, refer to the qemu-guest-agent wiki
page at wiki.qemu.org or libvirt.org.
101
Virtualization Administration Guide
NOTE
Windows guest virtual machines require the QEMU guest agent package for Windows,
qemu-guest-agent-win. This agent is required for VSS (Volume Shadow Copy Service)
support for Windows guest virtual machines running on Red Hat Enterprise Linux. More
information can be found here.
Follow these steps for Windows guests running on a Red Hat Enterprise Linux host machine.
To copy the drivers in the Windows guest, make an *.iso file for the qxl driver using the
following command:
# mkisofs -o /var/lib/libvirt/images/virtiowin.iso
/usr/share/virtio-win/drivers
To install the drivers using the Windows Control Panel, navigate to the following menus:
To install the virtio-win driver - Select Hardware and Sound > Device manager > virtio-
serial driver.
Add the following elements to the XML file using the # virsh edit win7x86 command and
save the changes. Note that the source socket name must be unique in the host, named
win7x86.agent in this example:
102
CHAPTER 10. QEMU-IMG AND QEMU GUEST AGENT
...
<channel type='unix'>
<source mode='bind'
path='/var/lib/libvirt/qemu/win7x86.agent'/>
<target type='virtio' name='org.qemu.guest_agent.0'/>
<address type='virtio-serial' controller='0' bus='0'
port='1'/>
</channel>
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0'/>
<address type='virtio-serial' controller='0' bus='0'
port='2'/>
</channel>
...
Figure 10.2. Editing the Windows guest XML to configure the QEMU guest agent
# ls -l /usr/share/virtio-win/guest-agent/
total 1544
103
Virtualization Administration Guide
ga-x86.msi by double clicking on the file. Once installed, it will be shown as a qemu-ga
service in the Windows guest within the System Manager. This same manager can be used
to monitor the status of the service.
10.3.1. Using libvirt Commands with the QEMU Guest Agent on Windows Guests
The QEMU guest agent can use the following virsh commands with Windows guests:
virsh shutdown --mode=agent - This shutdown method is more reliable than virsh
shutdown --mode=acpi, as virsh shutdown used with the QEMU guest agent is
guaranteed to shut down a cooperative guest in a clean state. If the agent is not present, libvirt
has to instead rely on injecting an ACPI shutdown event, but some guests ignore that event and
thus will not shut down.
virsh snapshot-create --quiesce - Allows the guest to flush its I/O into a stable state
before the snapshot is created, which allows use of the snapshot without having to perform a
fsck or losing partial database transactions. The guest agent allows a high level of disk contents
stability by providing guest co-operation.
virsh dompmsuspend - Suspends a running guest gracefully using the guest operating
system's power management functions.
<class>:<vendor>:<product>:<version>:<allow>
Use the value -1 to designate it to accept any value for a particular field. You may use multiple rules
on the same command line using | as a separator. Note that if a device matches none of the filter rules,
the redirection will not be allowed.
2. Add the following code excerpt to the guest virtual machine's XML file:
3. Start the guest virtual machine and confirm the setting changes by running the following:
104
CHAPTER 10. QEMU-IMG AND QEMU GUEST AGENT
-device usb-redir,chardev=charredir0,id=redir0,/
filter=0x08:0x1234:0xBEEF:0x0200:1|-1:-1:-1:-1:0,bus=usb.0,port=3
4. Plug a USB device into a host physical machine, and use virt-viewer to connect to the guest
virtual machine.
5. Click USB device selection in the menu, which will produce the following message: "Some
USB devices are blocked by host policy". Click OK to confirm and continue.
6. To make sure that the filter captures properly check the USB device vendor and product,
then make the following changes in the host physical machine's domain XML to allow for
USB redirection.
<redirfilter>
<usbdev class='0x08' vendor='0x0951' product='0x1625'
version='2.0' allow='yes'/>
<usbdev allow='no'/>
</redirfilter>
7. Restart the guest virtual machine, then use virt-viewer to connect to the guest virtual
machine. The USB device will now redirect traffic to the guest virtual machine.
<interface type='bridge'>
<mac address='52:54:00:4a:c9:5e'/>
<source bridge='virbr0'/>
<model type='virtio'/>
</interface>
# cat br1.xml
<interface type='bridge'>
<mac address='52:54:00:4a:c9:5e'/>
<source bridge='virbr1'/>
<model type='virtio'/>
</interface>
105
Virtualization Administration Guide
3. Start the guest virtual machine, confirm the guest virtual machine's network functionality, and
check that the guest virtual machine's vnetX is connected to the bridge you indicated.
# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.5254007da9f2 yes
virbr0-nic
vnet0
virbr1 8000.525400682996 yes
virbr1-nic
4. Update the guest virtual machine's network with the new interface parameters with the following
command:
5. On the guest virtual machine, run service network restart. The guest virtual machine
gets a new IP address for virbr1. Check the guest virtual machine's vnet0 is connected to the
new bridge(virbr1)
# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.5254007da9f2 yes virbr0-nic
virbr1 8000.525400682996 yes virbr1-nic
vnet0
106
CHAPTER 11. STORAGE CONCEPTS
XFS = ~8 Exabytes
qcow2 and host file systems keep their own metadata and scalability should be evaluated/tuned
when trying very large image sizes. Using raw disks means fewer layers that could affect
scalability or max size.
libvirt uses a directory-based storage pool, the /var/lib/libvirt/images/ directory, as the default
storage pool. The default storage pool can be changed to another storage pool.
Local storage pools - Local storage pools are directly attached to the host physical machine
server. Local storage pools include: local directories, directly attached disks, physical partitions,
and LVM volume groups. These storage volumes store guest virtual machine images or are
attached to guest virtual machines as additional storage. As local storage pools are directly
attached to the host physical machine server, they are useful for development, testing and small
deployments that do not require migration or large numbers of guest virtual machines. Local
storage pools are not suitable for many production environments as local storage pools do not
support live migration.
Networked (shared) storage pools - Networked storage pools include storage devices shared
over a network using standard protocols. Networked storage is required when migrating virtual
machines between host physical machines with virt-manager, but is optional when migrating
with virsh. Networked storage pools are managed by libvirt. Supported protocols for networked
storage pools include:
iSCSI
NFS
GFS2
SCSI RDMA protocols (SCSI RCP), the block export protocol used in InfiniBand and 10GbE
iWARP adapters.
107
Virtualization Administration Guide
NOTE
Multi-path storage pools should not be created or used as they are not fully supported.
11.2. VOLUMES
Storage pools are divided into storage volumes. Storage volumes are an abstraction of physical
partitions, LVM logical volumes, file-based disk images and other storage types handled by libvirt.
Storage volumes are presented to guest virtual machines as local storage devices regardless of the
underlying hardware.
Referencing Volumes
To reference a specific volume, three approaches are possible:
virsh #
The full path to the storage on the host physical machine system
A volume may also be referred to by its full path on the file system. When using this approach, a pool
identifier does not need to be included.
For example, a volume named secondimage.img, visible to the host physical machine system as
/images/secondimage.img. The image can be referred to as /images/secondimage.img.
When used with block based storage such as LVM, the volume key may follow this format:
c3pKz4-qPVc-Xf7M-7WNM-WJc8-qSiz-mtvpGn
When used with file based storage, the volume key may instead be a copy of the full path to the
volume storage.
108
CHAPTER 11. STORAGE CONCEPTS
/images/secondimage.img
virsh provides commands for converting between a volume name, volume path, or volume key:
vol-name
Returns the volume name when provided with a volume path or volume key.
vol-path
Returns the volume path when provided with a volume key, or a storage pool identifier and volume
name.
109
Virtualization Administration Guide
Suppose a storage administrator responsible for an NFS server creates a share to store guest virtual
machines' data. The system administrator defines a pool on the host physical machine with the
details of the share (nfs.example.com:/path/to/share should be mounted on /vm_data). When
the pool is started, libvirt mounts the share on the specified directory, just as if the system
administrator logged in and executed mount nfs.example.com:/path/to/share /vmdata. If
the pool is configured to autostart, libvirt ensures that the NFS share is mounted on the directory
specified when libvirt is started.
Once the pool starts, the files that the NFS share, are reported as volumes, and the storage volumes'
paths are then queried using the libvirt APIs. The volumes' paths can then be copied into the section
of a guest virtual machine's XML definition file describing the source storage for the guest virtual
machine's block devices. With NFS, applications using the libvirt APIs can create and delete volumes
in the pool (files within the NFS share) up to the limit of the size of the pool (the maximum storage
capacity of the share). Not all pool types support creating and deleting volumes. Stopping the pool
negates the start operation, in this case, unmounts the NFS share. The data on the share is not
modified by the destroy operation, despite the name. See man virsh for more details.
NOTE
Storage pools and volumes are not required for the proper operation of guest virtual
machines. Pools and volumes provide a way for libvirt to ensure that a particular piece of
storage will be available for a guest virtual machine, but some administrators will prefer to
manage their own storage and guest virtual machines will operate properly without any
pools or volumes defined. On systems that do not use pools, system administrators must
ensure the availability of the guest virtual machines' storage using whatever tools they
prefer, for example, adding the NFS share to the host physical machine's fstab so that the
share is mounted at boot time.
WARNING
110
CHAPTER 12. STORAGE POOLS
WARNING
Guests should not be given write access to whole disks or block devices (for
example, /dev/sdb). Use partitions (for example, /dev/sdb1) or LVM volumes.
If you pass an entire block device to the guest, the guest will likely partition it or
create its own LVM groups on it. This can cause the host physical machine to detect
these partitions or LVM groups and cause errors.
WARNING
Dedicating a disk to a storage pool will reformat and erase all data presently stored
on the disk device. It is strongly recommended to back up the storage device before
commencing with the following procedure.
# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel
New disk label type? gpt
(parted) quit
Information: You may need to update /etc/fstab.
#
The file must be in the format shown below, and contain the following fields:
<name>guest_images_disk</name>
The name parameter determines the name of the storage pool. This example uses the name
guest_images_disk in the example below.
111
Virtualization Administration Guide
<device path='/dev/sdb'/>
The device parameter with the path attribute specifies the device path of the storage
device. This example uses the device /dev/sdb.
<target> <path>/dev</path></target>
The file system target parameter with the path sub-parameter determines the location on
the host physical machine file system to attach volumes created with this storage pool.
For example, sdb1, sdb2, sdb3. Using /dev/, as in the example below, means volumes
created from this storage pool can be accessed as /dev/sdb1, /dev/sdb2, /dev/sdb3.
<format type='gpt'/>
The format parameter specifies the partition table type. This example uses the gpt in the
example below, to match the GPT disk label type created in the previous step.
Create the XML file for the storage pool device with a text editor.
<pool type='disk'>
<name>guest_images_disk</name>
<source>
<device path='/dev/sdb'/>
<format type='gpt'/>
</source>
<target>
<path>/dev</path>
</target>
</pool>
112
CHAPTER 12. STORAGE POOLS
-----------------------------------------
default active yes
guest_images_disk active no
5. Turn on autostart
Turn on autostart for the storage pool. Autostart configures the libvirtd service to start the
storage pool when the service starts.
# rm ~/guest_images_disk.xml
1. To avoid any issues with other guest virtual machines using the same pool, it is best to stop the
storage pool and release any resources in use by it.
113
Virtualization Administration Guide
For the following examples, a host physical machine has a 500GB hard drive (/dev/sdc) partitioned
into one 500GB, ext4 formatted partition (/dev/sdc1). We set up a storage pool for it using the
procedure below.
a. In the virt-manager graphical interface, select the host physical machine from the main
window.
114
CHAPTER 12. STORAGE POOLS
Choose a Name for the storage pool. This example uses the name guest_images_fs.
Change the Type to fs: Pre-Formatted Block Device.
115
Virtualization Administration Guide
Target Path
Enter the location to mount the source device for the storage pool in the Target Path
field. If the location does not already exist, virt-manager will create the directory.
Format
Select a format from the Format list. The device is formatted with the selected format.
This example uses the ext4 file system, the default Red Hat Enterprise Linux file system.
Source Path
Enter the device in the Source Path field.
Verify the details and press the Finish button to create the storage pool.
Select the storage pool. In the Autostart field, click the On Boot check box. This will make
sure the storage device starts whenever the libvirtd service starts.
116
CHAPTER 12. STORAGE POOLS
The storage pool is now created, close the Connection Details window.
1. To avoid any issues with other guest virtual machines using the same pool, it is best to stop the
storage pool and release any resources in use by it. To do this, select the storage pool you want
to stop and click the red X icon at the bottom of the Storage window.
117
Virtualization Administration Guide
2. Delete the storage pool by clicking the Trash can icon. This icon is only enabled if you stop the
storage pool first.
WARNING
Do not use this procedure to assign an entire disk as a storage pool (for example,
/dev/sdb). Guests should not be given write access to whole disks or block
devices. Only use this method to assign partitions (for example, /dev/sdb1) to
storage pools.
Procedure 12.2. Creating pre-formatted block device storage pools using virsh
Partition name
The name parameter determines the name of the storage pool. This example uses the name
guest_images_fs in the example below.
118
CHAPTER 12. STORAGE POOLS
device
The device parameter with the path attribute specifies the device path of the storage
device. This example uses the partition /dev/sdc1.
mountpoint
The mountpoint on the local file system where the formatted device will be mounted. If the
mount point directory does not exist, the virsh command can create the directory.
119
Virtualization Administration Guide
5. Turn on autostart
By default, a storage pool defined with virsh, is not set to automatically start each time
libvirtd starts. To remedy this, enable the automatic start with the virsh pool-
autostart command. The storage pool is now automatically started each time libvirtd
starts.
2. Optionally, if you want to remove the directory where the storage pool resides use the following
command:
120
CHAPTER 12. STORAGE POOLS
Directory-based storage pools can be created with virt-manager or the virsh command line tools.
# mkdir /guest_images
# ls -la /guest_images
total 8
drwx------. 2 root root 4096 May 28 13:57 .
dr-xr-xr-x. 26 root root 4096 May 28 13:57 ..
a. In the virt-manager graphical interface, select the host physical machine from the main
window.
121
Virtualization Administration Guide
Choose a Name for the storage pool. This example uses the name guest_images. Change
the Type to dir: Filesystem Directory.
122
CHAPTER 12. STORAGE POOLS
Verify the details and press the Finish button to create the storage pool.
Select the storage pool. In the Autostart field, confirm that the On Boot check box is
checked. This will make sure the storage pool starts whenever the libvirtd service starts.
123
Virtualization Administration Guide
The storage pool is now created, close the Connection Details window.
1. To avoid any issues with other guest virtual machines using the same pool, it is best to stop the
storage pool and release any resources in use by it. To do this, select the storage pool you want
to stop and click the red X icon at the bottom of the Storage window.
124
CHAPTER 12. STORAGE POOLS
2. Delete the storage pool by clicking the Trash can icon. This icon is only enabled if you stop the
storage pool first.
This example uses the name guest_images. All further virsh commands used in this
example use this name.
The path to a file system directory for storing guest image files. If this directory does not
exist, virsh will create it.
125
Virtualization Administration Guide
5. Turn on autostart
Turn on autostart for the storage pool. Autostart configures the libvirtd service to start the
storage pool when the service starts.
126
CHAPTER 12. STORAGE POOLS
Persistent: yes
Autostart: yes
Capacity: 49.22 GB
Allocation: 12.80 GB
Available: 36.41 GB
# ls -la /guest_images
total 8
drwx------. 2 root root 4096 May 30 02:44 .
dr-xr-xr-x. 26 root root 4096 May 30 02:44 ..
#
1. To avoid any issues with other guest virtual machines using the same pool, it is best to stop the
storage pool and release any resources in use by it.
2. Optionally, if you want to remove the directory where the storage pool resides use the following
command:
NOTE
Thin provisioning is currently not possible with LVM based storage pools.
NOTE
Refer to the Red Hat Enterprise Linux Storage Administration Guide for more details on
LVM.
127
Virtualization Administration Guide
WARNING
WARNING
This procedure will remove all data from the selected storage device.
# fdisk /dev/sdb
Command (m for help):
Command action
e extended
p primary partition (1-4)
c. Choose an available partition number. In this example the first partition is chosen by entering
1.
128
CHAPTER 12. STORAGE POOLS
e. Select the size of the partition. In this example the entire disk is allocated by pressing
Enter.
g. Choose the partition you created in the previous steps. In this example, the partition number
is 1.
The new LVM volume group, guest_images_lvm, can now be used for an LVM-based storage
pool.
a. In the virt-manager graphical interface, select the host from the main window.
129
Virtualization Administration Guide
Choose a Name for the storage pool. We use guest_images_lvm for this example. Then
change the Type to logical: LVM Volume Group, and
130
CHAPTER 12. STORAGE POOLS
Now fill in the Target Path and Source Path fields, then tick the Build Pool check
box.
Use the Target Path field to either select an existing LVM volume group or as the
name for a new volume group. The default format is /dev/storage_pool_name.
The Source Path field is optional if an existing LVM volume group is used in the
Target Path.
For new LVM volume groups, input the location of a storage device in the Source
Path field. This example uses a blank partition /dev/sdc.
The Build Pool check box instructs virt-manager to create a new LVM volume
group. If you are using an existing volume group you should not select the Build Pool
check box.
This example is using a blank partition to create a new volume group so the Build
Pool check box must be selected.
131
Virtualization Administration Guide
Verify the details and press the Finish button format the LVM volume group and create the
storage pool.
Press the Yes button to proceed to erase all data on the storage device and create the
storage pool.
It is generally a good idea to have the Autostart check box enabled, to ensure the storage
pool starts automatically with libvirtd.
132
CHAPTER 12. STORAGE POOLS
1. To avoid any issues with other guest virtual machines using the same pool, it is best to stop the
storage pool and release any resources in use by it. To do this, select the storage pool you want
to stop and click the red X icon at the bottom of the Storage window.
133
Virtualization Administration Guide
2. Delete the storage pool by clicking the Trash can icon. This icon is only enabled if you stop the
storage pool first.
2. Build the pool according to the specified name. If you are using an already existing volume
group, skip this step.
134
CHAPTER 12. STORAGE POOLS
# vgs
VG #PV #LV #SN Attr VSize VFree
libvirt_lvm 1 0 0 wz--n- 465.76g 465.76g
7. The following commands demonstrate the creation of three volumes (volume1, volume2 and
volume3) within this pool.
8. List the available volumes in this pool with the virsh command.
9. The following two commands (lvscan and lvs) display further information about the newly
created volumes.
# lvscan
ACTIVE '/dev/libvirt_lvm/volume1' [8.00 GiB] inherit
ACTIVE '/dev/libvirt_lvm/volume2' [8.00 GiB] inherit
ACTIVE '/dev/libvirt_lvm/volume3' [8.00 GiB] inherit
# lvs
LV VG Attr LSize Pool Origin Data% Move Log
Copy% Convert
135
Virtualization Administration Guide
1. To avoid any issues with other guests using the same pool, it is best to stop the storage pool
and release any resources in use by it.
2. Optionally, if you want to remove the directory where the storage pool resides use the following
command:
iSCSI (Internet Small Computer System Interface) is a network protocol for sharing storage devices.
iSCSI connects initiators (storage clients) to targets (storage servers) using SCSI instructions over the IP
layer.
136
CHAPTER 12. STORAGE POOLS
beneficial for guest virtual machines. This example creates an LVM image named virtimage1 on
a new volume group named virtstore on a RAID5 array for hosting guest virtual machines with
iSCSI.
# mkdir -p /var/lib/tgtd/virtualization
# dd if=/dev/zero of=/var/lib/tgtd/virtualization/virtimage2.img
bs=1M seek=10000 count=0
# restorecon -R /var/lib/tgtd
5. Create targets
Targets can be created by adding a XML entry to the /etc/tgt/targets.conf file. The
target attribute requires an iSCSI Qualified Name (IQN). The IQN is in the format:
Where:
137
Virtualization Administration Guide
yyyy-mm represents the year and month the device was started (for example: 2010-05);
reversed domain name is the host physical machines domain name in reverse (for example
server1.example.com in an IQN would be com.example.server1); and
optional identifier text is any text string, without spaces, that assists the administrator in
identifying devices or hardware.
This example creates iSCSI targets for the two types of images created in the optional steps on
server1.example.com with an optional identifier trial. Add the following to the
/etc/tgt/targets.conf file.
<target iqn.2010-05.com.example.server1:iscsirhel6guest>
backing-store /dev/virtstore/virtimage1 #LUN 1
backing-store /var/lib/tgtd/virtualization/virtimage2.img #LUN 2
write-cache off
</target>
Ensure that the /etc/tgt/targets.conf file contains the default-driver iscsi line to
set the driver type as iSCSI. The driver uses iSCSI by default.
IMPORTANT
This example creates a globally accessible target without access control. Refer to
the scsi-target-utils for information on implementing secure access.
7. iptables configuration
Open port 3260 for iSCSI access with iptables.
# tgt-admin --show
Target 1: iqn.2010-05.com.example.server1:iscsirhel6guest
System information:
Driver: iscsi
State: ready
I_T nexus information:
LUN information:
LUN: 0
Type: controller
SCSI ID: IET 00010000
SCSI SN: beaf10
138
CHAPTER 12. STORAGE POOLS
Size: 0 MB
Online: Yes
Removable media: No
Backing store type: rdwr
Backing store path: None
LUN: 1
Type: disk
SCSI ID: IET 00010001
SCSI SN: beaf11
Size: 20000 MB
Online: Yes
Removable media: No
Backing store type: rdwr
Backing store path: /dev/virtstore/virtimage1
LUN: 2
Type: disk
SCSI ID: IET 00010002
SCSI SN: beaf12
Size: 10000 MB
Online: Yes
Removable media: No
Backing store type: rdwr
Backing store path: /var/lib/tgtd/virtualization/virtimage2.img
Account information:
ACL information:
ALL
WARNING
The ACL list is set to all. This allows all systems on the local network to
access this device. It is recommended to set host physical machine access
ACLs for production environments.
139
Virtualization Administration Guide
a. Open virt-manager.
b. Select a host physical machine from the main virt-manager window. Click Edit menu
and select Connection Details.
140
CHAPTER 12. STORAGE POOLS
Choose a name for the storage pool, change the Type to iscsi, and press Forward to continue.
141
Virtualization Administration Guide
You will need the information you used in Section 12.5, “iSCSI-based Storage Pools” and
Procedure 12.4, “Creating an iSCSI target” to complete the fields in this menu.
a. Enter the iSCSI source and target. The Format option is not available as formatting is
handled by the guest virtual machines. It is not advised to edit the Target Path. The
default target path value, /dev/disk/by-path/, adds the drive path to that directory. The
target path should be the same on all host physical machines for migration.
b. Enter the host name or IP address of the iSCSI target. This example uses
host1.example.com.
c. In the Source Pathfield, enter the iSCSI target IQN. If you look at Procedure 12.4,
“Creating an iSCSI target” in Section 12.5, “iSCSI-based Storage Pools”, this is the
information you added in the /etc/tgt/targets.conf file. This example uses
iqn.2010-05.com.example.server1:iscsirhel6guest.
d. Check the IQN check box to enter the IQN for the initiator. This example uses iqn.2010-
05.com.example.host1:iscsirhel6.
1. To avoid any issues with other guest virtual machines using the same pool, it is best to stop the
storage pool and release any resources in use by it. To do this, select the storage pool you want
to stop and click the red X icon at the bottom of the Storage window.
142
CHAPTER 12. STORAGE POOLS
2. Delete the storage pool by clicking the Trash can icon. This icon is only enabled if you stop the
storage pool first.
The virsh pool-define-as command has several parameters which are accepted in the
following format:
type
defines this pool as a particular type, iscsi for example
name
must be unique and sets the name for the storage pool
143
Virtualization Administration Guide
these parameters are not required for iSCSI-based pools, use a - character to leave the field
blank.
target
defines the location for mounting the iSCSI device on the host physical machine
The example below creates the same iSCSI-based storage pool as the previous step.
4. Turn on autostart
Turn on autostart for the storage pool. Autostart configures the libvirtd service to start the
storage pool when the service starts.
144
CHAPTER 12. STORAGE POOLS
Verify the storage pool was created correctly, the sizes reported correctly, and the state reports
as running.
1. To avoid any issues with other guest virtual machines using the same pool, it is best to stop the
storage pool and release any resources in use by it.
a. Open virt-manager.
b. Select a host physical machine from the main virt-manager window. Click Edit menu
and select Connection Details.
145
Virtualization Administration Guide
146
CHAPTER 12. STORAGE POOLS
Choose a name for the storage pool and press Forward to continue.
Enter the host name or IP address of the NFS server. This example uses
server1.example.com.
147
Virtualization Administration Guide
1. To avoid any issues with other guests using the same pool, it is best to stop the storage pool
and release any resources in use by it. To do this, select the storage pool you want to stop and
click the red X icon at the bottom of the Storage window.
148
CHAPTER 12. STORAGE POOLS
2. Delete the storage pool by clicking the Trash can icon. This icon is only enabled if you stop the
storage pool first.
IMPORTANT
Red Hat Red Hat Enterprise Linux 6 does not support the use of GlusterFS with storage
pools. However, Red Hat Enterprise Linux 6.5 and later includes native support for
creating virtual machines with GlusterFS using the libgfapi library.
This allows multiple guests to see the same storage from multiple physical hosts, and thus allows for
easier migration paths for the storage. As a result, there is no need for the migration to create or copy
storage, as long as the correct storage path is specified.
In virtualization, the virtual host bus adapter, or vHBA, controls the LUNs for virtual machines. Each
vHBA is identified by its own WWNN (World Wide Node Name) and WWPN (World Wide Port Name).
The path to the storage is determined by the WWNN and WWPN values.
149
Virtualization Administration Guide
This section provides instructions for configuring a vHBA on a virtual machine. Note that Red Hat
Enterprise Linux 6 does not support persistent vHBA configuration across host reboots; verify any vHBA-
related settings following a host reboot.
For each scsi_host, run the following command to examine the device XML for the line
<capability type='vport_ops'>, which indicates a scsi_host with vport capability.
The XML output from the virsh nodedev-dumpxml command will list the fields <name>,
<wwnn>, and <wwpn>, which are used to create a vHBA. The <max_vports> value shows the
maximum number of supported vHBAs.
<path>/sys/devices/pci0000:00/0000:00:04.0/0000:10:00.0/host3</path>
<parent>pci_0000_10_00_0</parent>
<capability type='scsi_host'>
<host>3</host>
<capability type='fc_host'>
<wwnn>20000000c9848140</wwnn>
<wwpn>10000000c9848140</wwpn>
<fabric_wwn>2002000573de9a81</fabric_wwn>
</capability>
<capability type='vport_ops'>
<max_vports>127</max_vports>
<vports>0</vports>
</capability>
</capability>
</device>
150
CHAPTER 12. STORAGE POOLS
In this example, the <max_vports> value shows there are a total 127 virtual ports available for
use in the HBA configuration. The <vports> value shows the number of virtual ports currently
being used. These values update after creating a vHBA.
# cat vhba_host3.xml
<device>
<parent>scsi_host3</parent>
<capability type='scsi_host'>
<capability type='fc_host'>
</capability>
</capability>
</device>
The <parent> field specifies the HBA device to associate with this vHBA device. The details in
the <device> tag are used in the next step to create a new vHBA device for the host. See
http://libvirt.org/formatnode.html for more information on the nodedev XML format.
<path>/sys/devices/pci0000:00/0000:00:04.0/0000:10:00.0/host3/vport-
3:0-0/host5</path>
<parent>scsi_host3</parent>
<capability type='scsi_host'>
<host>5</host>
<capability type='fc_host'>
<wwnn>5001a4a93526d0a1</wwnn>
<wwpn>5001a4ace3ee047d</wwpn>
<fabric_wwn>2002000573de9a81</fabric_wwn>
</capability>
</capability>
</device>
151
Virtualization Administration Guide
the libvirt code can easily find the LUN's path using the virsh command output, and
virtual machine migration requires only defining and starting a storage pool with the same vHBA
name on the target machine. To do this, the vHBA LUN, libvirt storage pool and volume name
must be specified in the virtual machine's XML configuration. Refer to Section 12.8.3,
“Configuring the Virtual Machine to Use a vHBA LUN” for an example.
NOTE
Ensure you use the vHBA created in Procedure 12.6, “Creating a vHBA” as the
host name, modifying the vHBA name scsi_hostN to hostN for the storage pool
configuration. In this example, the vHBA is named scsi_host5, which is specified
as <adapter name='host5'/> in a Red Hat Enterprise Linux 6 libvirt storage
pool.
It is recommended to use a stable location for the <path> value, such as one of the
/dev/disk/by-{path|id|uuid|label} locations on your system. More information on
<path> and the elements within <target> can be found at http://libvirt.org/formatstorage.html.
<pool type='scsi'>
<name>vhbapool_host3</name>
<uuid>e9392370-2917-565e-692b-d057f46512d6</uuid>
<capacity unit='bytes'>0</capacity>
<allocation unit='bytes'>0</allocation>
<available unit='bytes'>0</available>
<source>
<adapter name='host5'/>
</source>
<target>
<path>/dev/disk/by-path</path>
<permissions>
<mode>0700</mode>
<owner>0</owner>
<group>0</group>
</permissions>
</target>
</pool>
152
CHAPTER 12. STORAGE POOLS
4. Enable autostart
Finally, to ensure that subsequent host reboots will automatically define vHBAs for use in virtual
machines, set the storage pool autostart feature (in this example, for a pool named
vhbapool_host3):
The list of LUN names displayed will be available for use as disk volumes in virtual machine
configurations.
the source device in the <source> parameter. Note this can be entered as /dev/sdaN, or
as a symbolic link generated by udev in /dev/disk/by-path|by-id|by-uuid|by-
label, which can be found by running the virsh vol-list pool command.
For example:
153
Virtualization Administration Guide
154
CHAPTER 13. VOLUMES
155
Virtualization Administration Guide
volume3 /dev/sdb3
clone1 /dev/sdb4
1. Create a storage file or use an existing file (such as an IMG file). Note that both of the following
commands create a 4GB file which can be used as additional storage for a guest:
Pre-allocated files are recommended for file-based storage images. Create a pre-allocated
file using the following dd command as shown:
Alternatively, create a sparse file instead of a pre-allocated file. Sparse files are created
much faster and can be used for testing, but are not recommended for production
environments due to data integrity and performance issues.
2. Create the additional storage by writing a <disk> element in a new file. In this example, this file
will be known as NewStorage.xml.
A <disk> element describes the source of the disk, and a device name for the virtual block
device. The device name should be unique across all devices in the guest, and identifies the bus
on which the guest will find the virtual block device. The following example defines a virtio block
device whose source is a file-based storage container named FileName.img:
156
CHAPTER 13. VOLUMES
Device names can also start with "hd" or "sd", identifying respectively an IDE and a SCSI disk.
The configuration file can also contain an <address> sub-element that specifies the position on
the bus for the new device. In the case of virtio block devices, this should be a PCI address.
Omitting the <address> sub-element lets libvirt locate and assign the next available PCI slot.
NOTE
This change will only apply after the guest has been destroyed and restarted. In
addition, persistent devices can only be added to a persistent domain, that is a
domain whose configuration has been saved with virsh define command.
If the guest is running, and you want the new device to be added temporarily until the guest is
destroyed, omit the --config option:
NOTE
The virsh command allows for an attach-disk command that can set a
limited number of parameters with a simpler syntax and without the need to
create an XML file. The attach-disk command is used in a similar manner to
the attach-device command mentioned previously, as shown:
Note that the virsh attach-disk command also accepts the --config
option.
157
Virtualization Administration Guide
NOTE
The following steps are Linux guest specific. Other operating systems handle new
storage devices in different ways. For other systems, refer to that operating
system's documentation.
# fdisk /dev/vdb
Command (m for help):
Command action
e extended
p primary partition (1-4)
d. Choose an available partition number. In this example, the first partition is chosen by
entering 1.
f. Select the size of the partition. In this example the entire disk is allocated by pressing
Enter.
h. Select the partition you created in the previous steps. In this example, the partition number
is 1 as there was only one partition created and fdisk automatically selected partition 1.
158
CHAPTER 13. VOLUMES
# mke2fs -j /dev/vdb1
7. Create a mount directory, and mount the disk on the guest. In this example, the directory is
located in myfiles.
# mkdir /myfiles
# mount /dev/vdb1 /myfiles
The guest now has an additional virtualized file-based storage device. Note however, that this
storage will not mount persistently across reboot unless defined in the guest's /etc/fstab file:
1. This procedure describes how to add a hard drive on the host physical machine to a guest. It
applies to all physical block devices, including CD-ROM, DVD and floppy devices.
Physically attach the hard disk device to the host physical machine. Configure the host physical
machine if the drive is not accessible by default.
a. Create the additional storage by writing a disk element in a new file. In this example, this
file will be known as NewStorage.xml. The following example is a configuration file section
which contains an additional device-based storage container for the host physical machine
partition /dev/sr0:
b. Follow the instruction in the previous section to attach the device to the guest virtual
machine. Alternatively, you can use the virsh attach-disk command, as shown:
159
Virtualization Administration Guide
The virsh attach-disk command also accepts the --config, --type, and --
mode options, as shown:
Additionally, --type also accepts --type disk in cases where the device is a hard
drive.
3. The guest virtual machine now has a new hard disk device called /dev/vdc on Linux (or
something similar, depending on what the guest virtual machine OS chooses) or D: drive (for
example) on Windows. You can now initialize the disk from the guest virtual machine, following
the standard procedures for the guest virtual machine's operating system. Refer to
Procedure 13.1, “Adding file-based storage” and Procedure 13.1, “Adding file-based storage” for
an example.
WARNING
IMPORTANT
Guest virtual machines should not be given write access to whole disks or block
devices (for example, /dev/sdb). Guest virtual machines with access to whole
block devices may be able to modify volume labels, which can be used to
compromise the host physical machine system. Use partitions (for example,
/dev/sdb1) or LVM volumes to prevent this issue.
160
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
14.1.1. help
$ virsh help [command|group] The help command can be used with or without options. When
used without options, all commands are listed, one per line. When used with an option, it is grouped into
categories, displaying the keyword for each group.
To display the commands that are only for a specific option, you need to give the keyword for that group
as an option. For example:
Using the same command with a command option, gives the help information on that one specific
command. For example:
161
Virtualization Administration Guide
SYNOPSIS
vol-path <vol> [--pool <string>]
OPTIONS
[--vol] <string> volume name or key
--pool <string> pool name or uuid
$ virsh exit
$ virsh quit
14.1.3. version
The version command displays the current libvirt version and displays information about where the build
is from. For example:
$ virsh version
Compiled against library: libvirt 1.1.1
Using library: libvirt 1.1.1
Using API: QEMU 1.1.1
Running hypervisor: QEMU 1.5.3
14.1.5. connect
Connects to a hypervisor session. When the shell is first started this command runs automatically when
the URI parameter is requested by the -c command. The URI specifies how to connect to the
hypervisor. The most commonly used URIs are:
qemu:///system - connects locally as root to the daemon supervising QEMU and KVM
domains.
xen:///session - connects locally as a user to the user's set of QEMU and KVM domains.
162
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
Where {name} is the machine name (host name) or URL (the output of the virsh uri command) of
the hypervisor. To initiate a read-only connection, append the above command with --readonly. For
more information on URIs refer to Remote URIs. If you are unsure of the URI, the virsh uri command
will display it:
$ virsh uri
qemu:///session
$ sysinfo - displays the XML representation of the hypervisor's system information, if available
Procedure 14.1. Hot plugging USB devices for use by the guest virtual machine
The following procedure demonstrates how to attach USB devices to the guest virtual machine. This can
be done while the guest virtual machine is running as a hotplug procedure or it can be done while the
guest is shutoff. The device you want to emulate needs to be attached to the host physical machine.
1. Locate the USB device you want to attach with the following command:
# lsusb -v
2. Create an XML file and give it a logical name (usb_device.xml, for example). Make sure you
copy the vendor and product IDs exactly as was displayed in your search.
163
Virtualization Administration Guide
In this example [rhel6] is the name of your guest virtual machine and [usb_device.xml] is the file
you created in the previous step. If you want to have the change take effect in the next reboot,
use the --config option. If you want this change to be persistent, use the --persistent
option. If you want the change to take effect on the current domain, use the --current option.
See the Virsh man page for additional information.
4. If you want to detach the device (hot unplug), perform the following command:
In this example [rhel6] is the name of your guest virtual machine and [usb_device.xml] is the file
you attached in the previous step
--persistent - behaves like --config for an offline domain, and like --live for a running
domain.
--mac - use this to specify the MAC address of the network interface
--script - use this to specify a path to a script file handling a bridge instead of the default one.
--inbound - controls the inbound bandwidth of the interface. Acceptable values are average,
peak, and burst.
164
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
--outbound - controls the outbound bandwidth of the interface. Acceptable values are
average, peak, and burst.
The type can be either network to indicate a physical network device, or bridge to indicate a bridge to
a device. source is the source of the device. To remove the attached device, use the virsh detach-
device.
In the example above, the rhel6 guest virtual machine will automatically start when the host physical
machine boots
In the example above, the autostart function is disabled and the guest virtual machine will no longer start
automatically when the host physical machine boots.
165
Virtualization Administration Guide
14.5.2. Connecting the Serial Console for the Guest Virtual Machine
The $ virsh console <domain> [--devname <string>] [--force] [--safe] command
connects the virtual serial console for the guest virtual machine. The optional --devname <string>
parameter refers to the device alias of an alternate console, serial, or parallel device configured for the
guest virtual machine. If this parameter is omitted, the primary console will be opened. The --force
option will force the console connection or when used with disconnect, will disconnect connections.
Using the --safe option will only allow the guest to connect if safe console handling is supported.
These values are user fields that allow storage of arbitrary textual data to allow easy identification of
domains. Ideally, the title should be short, although this is not enforced by libvirt.
The options --live or --config select whether this command works on live or persistent definitions
of the domain. If both --live and --config are specified, the --config option will be implemented
first, where the description entered in the command becomes the new configuration setting which is
applied to both the live configuration and persistent configuration setting. The --current option will
modify or get the current state configuration and will not be persistent. The --current option will be
used if neither --live nor --config, nor --current are specified. The --edit option specifies that
an editor with the contents of current description or title should be opened and the contents saved back
afterwards. Using the --title option will show or modify the domain's title field only and not include its
description. In addition, if neither --edit nor --new-desc are used in the command, then only the
description is displayed and cannot be modified.
For example, the following command changes the guest virtual machine's title from testvm to TestVM-4F
and will change the description to Guest VM on fourth floor:
166
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
Target Source
------------------------------------------------
vda /VirtualMachines/rhel6.img
hdc -
# domif-setlink [domain][interface-device][state]{--config}
Using this modifies the status of the specified interface for the specified domain. Note that if you only
want the persistent configuration of the domain to be modified, you need to use the --configoption. It
should also be noted that for compatibility reasons, --persistent is an alias of --config. The
"interface device" can be the interface's target name or the MAC address.
167
Virtualization Administration Guide
The only required parameter is the domain name and interface device of the guest virtual machine, the -
-config, --live, and --current functions the same as in Section 14.19, “Setting Schedule
Parameters”. If no limit is specified, it will query current network interface setting. Otherwise, alter the
limits with the following options:
<interface-device> This is mandatory and it will set or query the domain’s network interface’s
bandwidth parameters. interface-device can be the interface’s target name (<target
dev=’name’/>), or the MAC address.
If no --inbound or --outbound is specified, this command will query and show the bandwidth
settings. Otherwise, it will set the inbound or outbound bandwidth. average,peak,burst is the
same as in attach-interface command. Refer to Section 14.3, “Attaching Interface Devices”
Both the --live and --config options may be used but --current is exclusive. If no option is
specified, the behavior will be different depending on the guest's state.
168
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
The domblklist domain --inactive --details displays a table of all block devices that are
associated with the specified domain.
If --inactive is specified, the result will show the devices that are to be used at the next boot and will
not show those that are currently running in use by the running domain. If --details is specified, the
disk type and device value will be included in the table. The information displayed in this table can be
used with the domblkinfo and snapshot-create.
If --inactive is specified, the result will show the devices that are to be used at the next boot and will
not show those that are currently running in use by the running domain.
Commands that require a MAC address of a virtual interface (such as detach-interface or domif-
setlink) will accept the output displayed by this command.
blockcommit copies data from one part of the chain down into a backing file, allowing you to pivot the
rest of the chain in order to bypass the committed portions. For example, suppose this is the current
state:
Using blockcommit moves the contents of snap2 into snap1, allowing you to delete snap2 from the
chain, making backups much quicker.
# virsh blockcommit $dom $disk -base snap1 -top snap2 -wait -verbose
169
Virtualization Administration Guide
WARNING
blockcommit will corrupt any file that depends on the -base option (other
than files that depend on the -top option, as those files now point to the
base). To prevent this, do not commit changes into files shared by more
than one guest. The -verbose option allows the progress to be printed on
the screen.
Flattens an image by populating it with data from its backing image chain. This makes the image
file self-contained so that it no longer depends on backing images and looks like this:
After: base.img is no longer used by the guest and Active contains all of the data.
Flattens part of the backing image chain. This can be used to flatten snapshots into the top-level
image and looks like this:
After: base.img ← active. Note that active now contains all data from sn1 and sn2 and
neither sn1 nor sn2 are used by the guest.
Moves the disk image to a new file system on the host. This is allows image files to be moved
while the guest is running and looks like this:
Useful in live migration with post-copy storage migration. The disk image is copied from the
source host to the destination host after live migration completes.
2. If the chain looks like this: base ← snap1 ← snap2 ← active run the following:
170
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
This command makes 'snap1' the backing file of active, by pulling data from snap2 into active
resulting in: base ← snap1 ← active.
3. Once the blockpull is complete, the libvirt tracking of the snapshot that created the extra
image in the chain is no longer useful. Delete the tracking on the outdated snapshot with this
command:
To flatten a single image and populate it with data from its backing image chain:# virsh
blockpull example-domain vda - wait
To flatten part of the backing image chain:# virsh blockpull example-domain vda -
base /path/to/base.img - wait
To move the disk image to a new file system on the host:# virsh snapshot-create
example-domaine - xmlfile /path/to/new.xml - disk-only followed by # virsh
blockpull example-domain vda - wait
NOTE
Live image re-sizing will always re-size the image, but may not immediately be picked up
by guests. With recent guest kernels, the size of virtio-blk devices is automatically
updated (older kernels require a guest reboot). With SCSI devices, it is required to
manually trigger a re-scan in the guest with the command, echo >
/sys/class/scsi_device/0:0:0:0/device/rescan. In addition, with IDE it is
required to reboot the guest before it picks up the new size.
171
Virtualization Administration Guide
Domain is the unique target name or source file of the domain whose size you want to
change
Path size is a scaled integer which defaults to KiB (blocks of 1024 bytes) if there is no suffix.
You must use a suffix of "B" to for bytes.
NOTE
Live block copy is a feature that is not supported with the version of KVM that is supplied
with Red Hat Enterprise Linux. Live block copy is available with the version of KVM that is
supplied with Red Hat Virtualization. This version of KVM must be running on your
physical host machine in order for the feature to be supported. Contact your
representative at Red Hat for more details.
Live block copy allows you to copy an in use guest disk image to a destination image and switches the
guest disk image to the destination guest image while the guest is running. Whilst live migration moves
the memory and registry state of the host, the guest is kept in shared storage. Live block copy allows
you to move the entire guest contents to another host on the fly while the guest is running. Live block
copy may also be used for live migration without requiring permanent share storage. In this method the
disk image is copied to the destination host after migration, but while the guest is running.
when maintenance is required, guests can be transferred to another location, with no loss of
performance
image format conversions can be done without having to shut down the guest
This example shows what happens when live block copy is performed. The example has a backing
file (base) that is shared between a source and destination. It also has two overlays (sn1 and sn2)
that are only present on the source and must be copied.
sn1 - the first snapshot that was taken of the base disk image
172
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
2. When a copy of the image is created as a new image on top of sn2 the result is this:
3. At this point the read permissions are all in the correct order and are set automatically. To
make sure write permissions are set properly, a mirror mechanism redirects all writes to both
sn2 and active, so that sn2 and active read the same at any time (and this mirror mechanism
is the essential difference between live block copy and image streaming).
4. A background task that loops over all disk clusters is executed. For each cluster, there are
the following possible cases and actions:
Use bdrv_is_allocated() to follow the backing file chain. If the cluster is read from
base (which is shared) there is nothing to do.
If bdrv_is_allocated() variant is not feasible, rebase the image and compare the
read data with write data in base in order to decide if a copy is needed.
5. When the copy has completed, the backing file of active is switched to base (similar to
rebase)
To reduce the length of a backing chain after a series of snapshots, the following commands are helpful:
blockcommit and blockpull. See Section 14.5.15, “Using blockcommit to Shorten a Backing Chain”
for more information.
virsh domhostname domain displays the host name of the specified domain provided the
hypervisor can publish it.
virsh domid domain|ID converts a given domain name or UUID into an ID.
virsh domjobabort domain aborts the currently running job on the specified domain.
virsh domjobinfo domain displays information about jobs running on the specified domain,
including migration statistics
virsh domname domain ID|UUID converts a given domain ID or UUID into a domain name.
173
Virtualization Administration Guide
virsh domstate domain displays the state of the given domain. Using the --reason option
will also display the reason for the displayed state.
virsh domcontrol domain displays the state of an interface to VMM that were used to
control a domain. For states that are not OK or Error, it will also print the number of seconds that
have elapsed since the control interface entered the displayed state.
In order to get information about the domain, run the following command:
Suppose you have a QEMU guest with the following args file:
$ cat demo.args
LC_ALL=C
PATH=/bin
HOME=/home/test
USER=test
LOGNAME=test /usr/bin/qemu -S -M pc -m 214 -smp 1 -nographic -monitor pty -
no-acpi -boot c -hda /dev/HostVG/QEMUGuest1 -net none -serial none -
parallel none -usb
To convert this to a domain XML file so that the guest can be managed by libvirt, run:
174
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
This command turns the args file above, into this domain XML file:
<domain type='qemu'>
<uuid>00000000-0000-0000-0000-000000000000</uuid>
<memory>219136</memory>
<currentMemory>219136</currentMemory>
<vcpu>1</vcpu>
<os>
<type arch='i686' machine='pc'>hvm</type>
<boot dev='hd'/>
</os>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/qemu</emulator>
<disk type='block' device='disk'>
<source dev='/dev/HostVG/QEMUGuest1'/>
<target dev='hda' bus='ide'/>
</disk>
</devices>
</domain>
--bypass-cache the file saved will not contain the file system cache. Note that selecting this
option may slow down dump operation.
--live will save the file as the domain continues to run and will not pause or stop the domain.
--crash puts the domain in a crashed status rather than leaving it in a paused state while the
dump file is saved.
--reset once the dump file is successfully saved, the domain will reset.
--memory-only the only information that will be saved in the dump file will be the domain's
memory and CPU common register file.
Note that the entire process can be monitored using the domjobinfo command and can be canceled
using the domjobabort command.
175
Virtualization Administration Guide
This command outputs the guest virtual machine's XML configuration file to standard out (stdout). You
can save the data by piping the output to a file. An example of piping the output to a file called guest.xml:
This file guest.xml can recreate the guest virtual machine (refer to Section 14.6, “Editing a Guest
Virtual Machine's configuration file”. You can edit this XML configuration file to configure additional
devices or to deploy additional guest virtual machines.
176
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='tablet' bus='usb'/>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes'/>
<sound model='ich6'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04'
function='0x0'/>
</sound>
<video>
<model type='cirrus' vram='9216' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02'
function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06'
function='0x0'/>
</memballoon>
</devices>
</domain>
Note that the <shareable/> flag is set. This indicates the device is expected to be shared between
domains (assuming the hypervisor and OS support this), which means that caching should be
deactivated for that device.
This opens a text editor. The default text editor is the $EDITOR shell parameter (set to vi by default).
1. Run the virsh edit [guestname] command to edit the XML configuration file for the guest
virtual machine.
177
Virtualization Administration Guide
This enables the guest virtual machine to use the multifunction PCI devices.
For a PCI device with two functions, amend the XML configuration file to include a second
device with the same slot number as the first device and a different function number, such as
function='0x1'.
For Example:
$ lspci
00:05.0 SCSI storage controller: Red Hat, Inc Virtio block device
00:05.1 SCSI storage controller: Red Hat, Inc Virtio block device
Under normal conditions, the managed save will decide between using the running or paused state as
determined by the state the domain is in when the save is done. However, this can be overridden by
using the --running option to indicate that it must be left in a running state or by using --paused
option which indicates it is to be left in a paused state.
178
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
To remove the managed save state, use the virsh managedsave-remove command which will force
the domain to do a full boot the next time it is started.
Note that the entire managed save process can be monitored using the domjobinfo command and can
also be canceled using the domjobabort command.
Each keycode can either be a numeric value or a symbolic name from the corresponding codeset. If
multiple keycodes are specified, thay are all sent simultaneously to the guest virtual machine and as
such may be received in random order. If you need distinct keycodes, you must send the send-key
command multiple times.
If a --holdtime is given, each keystroke will be held for the specified amount in milliseconds. The --
codeset allows you to specify a code set, the default being Linux, but the following options are
permitted:
linux - choosing this option causes the symbolic names to match the corresponding Linux key
constant macro names and the numeric values are those offered by the Linux generic input
event subsystems.
xt- this will send a value that is defined by the XT keyboard controller. No symbolic names are
provided.
atset1 - the numeric values are those that are defined by the AT keyboard controller, set1 (XT
compatible set). Extended keycodes from the atset1 may differ from extended keycodes in the
XT codeset. No symbolic names are provided.
atset2 - The numeric values are those defined by the AT keyboard controller, set 2. No
symbolic names are provided.
atset3 - The numeric values are those defined by the AT keyboard controller, set 3 (PS/2
compatible). No symbolic names are provided.
179
Virtualization Administration Guide
os_x - The numeric values are those defined by the OS-X keyboard input subsystem. The
symbolic names match the corresponding OS-X key constant macro names.
xt_kbd - The numeric values are those defined by the Linux KBD device. These are a variant
on the original XT codeset, but often with different encoding for extended keycodes. No symbolic
names are provided.
win32 - The numeric values are those defined by the Win32 keyboard input subsystem. The
symbolic names match the corresponding Win32 key constant macro names.
usb - The numeric values are those defined by the USB HID specification for keyboard input. No
symbolic names are provided.
rfb - The numeric values are those defined by the RFB extension for sending raw keycodes.
These are a variant on the XT codeset, but extended keycodes have the low bit of the second
bite set, instead of the high bit of the first byte. No symbolic names are provided.
Either an integer signal constant number or a symbolic signal name can be sent this way. Thus, for
example, both of the following commands send the kill signal to process ID 187 on the rhel6 domain:
For the full list of available signals and their uses, see the virsh(1) and signal(7) manual pages.
14.6.7. Displaying the IP Address and Port Number for the VNC Display
The virsh vncdisplay will print the IP address and port number of the VNC display for the specified
domain. If the information is unavailable the exit code 1 will be displayed.
$ virsh nodeinfo
CPU model: x86_64
CPU(s): 4
180
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
--mode - The mode can be set to either strict, interleave, or preferred. Running
domains cannot have their mode changed while live unless the domain was started within
strict mode.
--nodeset contains a list of NUMA nodes that are used by the host physical machine for
running the domain. The list contains nodes, each separated by a comma, with a dash - used
for node ranges and a caret ^ used for excluding a node.
Only one of the following three options can be used per instance:
--config will take effect on the next boot of a persistent guest virtual machine.
--live will set the scheduler information of a running guest virtual machine.
--current will affect the current state of the guest virtual machine.
$ virsh nodecpumap
CPUs present: 4
CPUs online: 1
CPU map: y
181
Virtualization Administration Guide
The nodecpustats command displays statistical information about the specified CPU, if the CPU is
given. If not, it will display the CPU status of the node. If a percent is specified, it will display the
percentage of each type of CPU statistics that were recorded over an one (1) second interval.
$ virsh nodecpustats
user: 1056442260000000
system: 401675280000000
idle: 7549613380000000
iowait: 94593570000000
You can control the behavior of the rebooting guest virtual machine by modifying the on_reboot
element in the guest virtual machine's configuration file.
shm-pages-to-scan - sets the number of pages to scan before the shared memory service
goes to sleep.
shm-sleep-milisecs - sets the number of milliseconds that the shared memory service will
sleep before the next scan
182
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
Note that different drivers expect the device to be bound to different dummy devices. Using the --
driver option allows you to specify the desired back-end driver.
183
Virtualization Administration Guide
--paused - If this is supported by the driver it will boot the domain and then put it into a paused
state
--autodestroy - the guest virtual machine is automatically destroyed when the virsh session
closes or the connection to libvirt closes, or it otherwise exits
--bypass-cache - used if the domain is in the managedsave state. If this is used, it will
restore the guest virtual machine, avoiding the system cache. Note this will slow down the
restore process.
--force-boot - discards any managedsave options and causes a fresh boot to occur
--pass-fds - is a list of additional options separated by commas, which are passed onto the
guest virtual machine.
When a guest virtual machine is in a suspended state, it consumes system RAM but not processor
resources. Disk and network I/O does not occur while the guest virtual machine is suspended. This
operation is immediate and the guest virtual machine can be restarted with the resume (Section 14.8.6,
“Resuming a Guest Virtual Machine”) option.
184
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
# dompmwakeup rhel6
This command requires the name of the domain, rhel6 for example as shown.
This command will undefine a domain. Although it can work on a running domain, it will convert the
running domain into a transient domain without stopping it. If the domain is inactive, the domain
configuration is removed.
--managed-save - this option guarantees that any managed save image is also cleaned up.
Without using this option, attempts to undefine a domain with a managed save image will fail.
--snapshots-metadata - this option guarantees that any snapshots (as shown with
snapshot-list) are also cleaned up when undefining an inactive domain. Note that any
attempts to undefine an inactive domain whose configuration file contains snapshot metadata
will fail. If this option is used and the domain is active, it is ignored.
--storage - using this option requires a comma separated list of volume target names or
source paths of storage volumes to be removed along with the undefined domain. This action
will undefine the storage volume before it is removed. Note that this can only be done with
inactive domains. Note too that this will only work with storage volumes that are managed by
libvirt.
--wipe-storage - in addition to deleting the storage volume, the contents are wiped.
This operation is immediate and the guest virtual machine parameters are preserved for suspend and
resume operations.
185
Virtualization Administration Guide
Save the current state of a guest virtual machine to a file using the virsh command:
This stops the guest virtual machine you specify and saves the data to a file, which may take some time
given the amount of memory in use by your guest virtual machine. You can restore the state of the guest
virtual machine with the restore (Section 14.8.11, “Restore a Guest Virtual Machine”) option. Save is
similar to pause, instead of just pausing a guest virtual machine the present state of the guest virtual
machine is saved.
--bypass-cache - causes the restore to avoid the file system cache but note that using this
option may slow down the restore operation.
--xml - this option must be used with an XML file name. Although this option is usually omitted,
it can be used to supply an alternative XML file for use on a restored guest virtual machine with
changes only in the host-specific portions of the domain XML. For example, it can be used to
account for the file naming differences in underlying storage due to disk snapshots taken after
the guest was saved.
--running - overrides the state recorded in the save image to start the domain as running.
--paused- overrides the state recorded in the save image to start the domain as paused.
If you want to restore the guest virtual machine directly from the XML file, the virsh restore
command will do just that. You can monitor the process with the domjobinfo and cancel it with the
domjobabort.
14.8.8. Updating the Domain XML File that will be Used for Restoring the Guest
The virsh save-image-define file xml --running|--paused command will update the
domain XML file that will be used when the specified file is later used during the virsh restore
command. The xml argument must be an XML file name containing the alternative XML with changes
only in the host physical machine specific portions of the domain XML. For example, it can be used to
account for the file naming differences resulting from creating disk snapshots of underlying storage after
the guest was saved. The save image records if the domain should be restored to a running or paused
state. Using the options --running or --paused dictates the state that is to be used.
186
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
Note that the save image records whether the domain should be restored to a --running or --paused
state. Without using these options the state is determined by the file itself. By selecting --running or -
-paused you can overwrite the state that virsh restore should use.
This restarts the saved guest virtual machine, which may take some time. The guest virtual machine's
name and UUID are preserved but are allocated for a new id.
The virsh restore state-file command can take the following options:
--bypass-cache - causes the restore to avoid the file system cache but note that using this
option may slow down the restore operation.
--xml - this option must be used with an XML file name. Although this option is usually omitted,
it can be used to supply an alternative XML file for use on a restored guest virtual machine with
changes only in the host-specific portions of the domain XML. For example, it can be used to
account for the file naming differences in underlying storage due to disk snapshots taken after
the guest was saved.
--running - overrides the state recorded in the save image to start the domain as running.
--paused- overrides the state recorded in the save image to start the domain as paused.
You can control the behavior of the rebooting guest virtual machine by modifying the on_shutdown
parameter in the guest virtual machine's configuration file.
14.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.
187
Virtualization Administration Guide
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 shutdown
command 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.
Log into the guest virtual machine and install the acpid package on the guest virtual machine:
# chkconfig acpid on
# service acpid start
<channel type='unix'>
<source mode='bind'
path='/var/lib/libvirt/qemu/{$guestname}.agent'/>
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
188
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
# virsh list --all - this command lists all of the known domains
Id Name State
----------------------------------
rhel6 running
c. Wait a few seconds for the guest virtual machine to shut down.
d. Start the domain named rhel6, with the XML file you edited.
f. List all the domains again, rhel6 should still be on the list, and it should indicate it is shut off.
g. Start the domain named rhel6, with the XML file you edited.
i. List the domains. rhel6 should still be on the list, and it should indicate it is shut off
The guest virtual machine will shut down using the virsh shutdown command for the consecutive
shutdowns, without using the workaround described above.
189
Virtualization Administration Guide
In addition to the method described above, a guest can be automatically shutdown, by stopping the
libvirt-guest service. Refer to Section 14.9.3, “Manipulating the libvirt-guests Configuration
Settings” for more information on this method.
Procedure 14.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.
$ vi /etc/sysconfig/libvirt-guests
boot, however,
# guests marked as autostart will still be automatically
started by
# libvirtd
#ON_BOOT=start
190
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
allow
# parallel startup.
#START_DELAY=0
# 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 libvirt
configuration 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
start option 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.
191
Virtualization Administration Guide
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
shutdown which 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.
You can control the behavior of the rebooting guest virtual machine by modifying the <on_reboot>
element in the guest virtual machine's configuration file. Refer to Section 20.12, “Events Configuration”
for more information.
By default, the hypervisor will try to pick a suitable shutdown method. To specify an alternative method,
the --mode option can specify a comma separated list which includes initctl, acpi, agent, and
signal. The order in which drivers will try each mode is not related to the order specified in the
command. For strict control over ordering, use a single mode at a time and repeat the command.
192
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
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 destroy option only
when the guest virtual machine is unresponsive. If you want to initiate a graceful shutdown, use the
virsh destroy --graceful command.
193
Virtualization Administration Guide
UUID: a03093a1-5da6-a2a2-3baf-a845db2f10b9
OS Type: hvm
State: running
CPU(s): 1
CPU time: 21.6s
Max memory: 2097152 kB
Used memory: 1025000 kB
Persistent: yes
Autostart: disable
Security model: selinux
Security DOI: 0
Security label: system_u:system_r:svirt_t:s0:c612,c921 (permissive)
The pool-info pool-or-uuid command will list the basic information about the specified storage
pool object. This command requires the name or UUID of the storage pool. To retrieve this information,
use the following coomand:
This lists all storage pool objects known to libvirt. By default, only active pools are listed; but using the -
-inactive option lists just the inactive pools, and using the --all option lists all of the storage pools.
In addition to those options there are several sets of filtering options that can be used to filter the content
of the list. --persistent restricts the list to persistent pools, --transient restricts the list to transient
pools, --autostart restricts the list to autostarting pools and finally --no-autostart restricts the list
to the storage pools that have autostarting disabled.
For all storage pool commands which require a type, the pool types must be separated by comma. The
valid pool types include: dir, fs, netfs, logical, disk, iscsi, scsi, mpath, rbd, and sheepdog.
The --details option instructs virsh to additionally display pool persistence and capacity related
information where available.
194
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
NOTE
When this command is used with older servers, it is forced to use a series of API calls
with an inherent race, where a pool might not be listed or might appear more than once if
it changed its state between calls while the list was being collected. Newer servers
however, do not have this problem.
If --no-overwrite is specified, it probes to determine if a file system already exists on the target
device, returning an error if it exists, or using mkfs to format the target device if it does not. If --
overwrite is specified, then the mkfs command is executed and any existing data on the target device
is overwritten.
The pool-create file creates and starts a storage pool from its associated XML file.
The pool-define file creates, but does not start, a storage pool object from the XML file.
This command creates and starts a pool object name from the raw parameters given.
If --print-xml is specified, then it prints the XML of the storage pool object without creating the pool.
Otherwise, the pool requires a type in order to be built. For all storage pool commands which require a
type, the pool types must be separated by comma. The valid pool types include: dir, fs, netfs,
logical, disk, iscsi, scsi, mpath, rbd, and sheepdog.
In contrast, the following command creates, but does not start, a pool object name from the raw
parameters given:
If --print-xml is specified, then it prints the XML of the pool object without defining the pool.
Otherwise, the pool has to have a specified type. For all storage pool commands which require a type,
the pool types must be separated by comma. The valid pool types include: dir, fs, netfs, logical,
disk, iscsi, scsi, mpath, rbd, and sheepdog.
195
Virtualization Administration Guide
The pool-start pool-or-uuid starts the specified storage pool, which was previously defined but
inactive.
The pool-delete pool-or-uuid destroys the resources used by the specified storage pool. It is
important to note that this operation is non-recoverable and non-reversible. However, the pool structure
will still exist after this command, ready to accept the creation of new storage volumes.
The pool-undefine pool-or-uuid command undefines the configuration for an inactive pool.
This method is the only method that should be used to edit an XML configuration file as it does error
checking before applying.
The pool-uuid pool command returns the UUID of the specified pool.
196
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
The file argument specifies the XML file and path containing the volume definition. The --inputpool
pool-or-uuid option specifies the name or uuid of the storage pool the source volume is in. The vol-
name-or-key-or-path argument specifies the name or key or path of the source volume. For some
examples, refer to Section 13.1, “Creating Volumes”.
The vol-create-as command creates a volume from a set of arguments. The pool-or-uuid argument
contains the name or UUID of the storage pool to create the volume in.
name is the name of the new volume. capacity is the size of the volume to be created, as a scaled
integer, defaulting to bytes if there is no suffix. --allocation <size> is the initial size to be allocated
in the volume, also as a scaled integer defaulting to bytes. --format <string> is used in file based
storage pools to specify the volume file format which is a string of acceptable formats separated by a
comma. Acceptable formats include raw, bochs, qcow, qcow2, vmdk, --backing-vol vol-name-
or-key-or-path is the source backing volume to be used if taking a snapshot of an existing volume.-
-backing-vol-format string is the format of the snapshot backing volume which is a string of
formats separated by a comma. Accepted values include: raw, bochs, qcow, qcow2, , vmdk, and
host_device. These are, however, only meant for file based storage pools.
The vol-create pool-or-uuid file creates a storage volume from a saved XML file. This
command also requires the pool-or-uuid, which is the name or UUID of the storage pool in which the
volume will be created. The file argument contains the path with the volume definition's XML file. An easy
way to create the XML file is to use the vol-dumpxml command to obtain the definition of a pre-existing
volume, modify it and then save it and then run the vol-create.
The --inactive option lists the inactive guest virtual machines (that is, guest virtual machines
that have been defined but are not currently active).
197
Virtualization Administration Guide
storage pool the volume is in. The vol-name-or-key-or-path option specifies the name or key or
path of the volume to delete.
nnsa - 4-pass NNSA Policy Letter NAP-14.1-C (XVI-8) for sanitizing removable and non-
removable hard disks: random x2, 0x00, verify.
dod - 4-pass DoD 5220.22-M section 8-306 procedure for sanitizing removable and non-
removable rigid disks: random, 0x00, 0xff, verify.
NOTE
The version of the scrub binary installed on the host will limit the algorithms that are
available.
This command requires a --pool pool-or-uuid, which is the name or UUID of the storage pool the
volume is in. vol-name-or-key-or-path is the name or key or path of the volume to place the resulting XML
file.
198
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
The vol-list --pool pool-or-uuid --details lists all of volumes in the specified storage pool.
This command requires --pool pool-or-uuid which is the name or UUID of the storage pool. The -
-details option instructs virsh to additionally display volume type and capacity related information
where available.
The vol-path --pool pool-or-uuid vol-name-or-key command returns the path for a given
volume. The command requires --pool pool-or-uuid, which is the name or UUID of the storage
pool the volume is in. It also requires vol-name-or-key which is the name or key of the volume for which
the path has been requested.
The vol-name vol-key-or-path command returns the name for a given volume, where vol-key-or-
path is the key or path of the volume to return the name for.
The vol-key --pool pool-or-uuid vol-name-or-path command returns the volume key for a
given volume where --pool pool-or-uuid is the name or UUID of the storage pool the volume is in
and vol-name-or-path is the name or path of the volume to return the volume key for.
This command command downloads the contents of local-file from a storage volume. It requires a --
pool pool-or-uuid which is the name or UUID of the storage pool that the volume is in. It also
requires vol-name-or-key-or-path which is the name or key or path of the volume to wipe. Using the
option --offset dictates the position in the storage volume at which to start reading the data. --
length length dictates an upper limit for the amount of data to be downloaded.
199
Virtualization Administration Guide
This command command re-sizes the capacity of the given volume, in bytes. The command requires --
pool pool-or-uuid which is the name or UUID of the storage pool the volume is in. This command
also requires vol-name-or-key-or-path is the name or key or path of the volume to re-size.
The new capacity may create a sparse file unless the --allocate option is specified. Normally,
capacity is the new size, but if --delta is present, then it is added to the existing size. Attempts to
shrink the volume will fail unless the --shrink option is present.
Note that capacity cannot be negative unless the --shrink option is provided and a negative sign is not
necessary. capacity is a scaled integer which defaults to bytes if there is no suffix. Note too that this
command is only safe for storage volumes not in use by an active guest. Refer to Section 14.5.17,
“Using blockresize to Change the Size of a Domain Path” for live re-sizing.
# virsh list
--inactive option lists the inactive guest virtual machines (that is, guest virtual machines that
have been defined but are not currently active)
There are seven states that can be visible using this command:
Running - The running state refers to guest virtual machines which are currently active on
a CPU.
Idle - The idle state indicates that the domain is idle, and may not be running or able to
run. This can be caused because the domain is waiting on IO (a traditional wait state) or has
gone to sleep because there was nothing else for it to do.
Paused - The paused state lists domains that are paused. This occurs if an administrator
uses the paused button in virt-manager or virsh suspend. When a guest virtual
machine is paused it consumes memory and other resources but it is ineligible for
scheduling and CPU resources from the hypervisor.
Shutdown - The shutdown state is for guest virtual machines in the process of shutting
200
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
down. The guest virtual machine is sent a shutdown signal and should be in the process of
stopping its operations gracefully. This may not work with all guest virtual machine operating
systems; some operating systems do not respond to these signals.
Shut off - The shut off state indicates that the domain is not running. This can be caused
when a domain completely shuts down or has not been started.
Crashed - The crashed state indicates that the domain has crashed and can only occur if
the guest virtual machine has been configured not to restart on crash.
Dying - Domains in the dying state are in is in process of dying, which is a state where the
domain has not completely shut-down or crashed.
--managed-save Although this option alone does not filter the domains, it will list the domains
that have managed save state enabled. In order to actually list the domains separately you will
need to use the --inactive option as well.
--name is specified domain names are printed in a list. If --uuid is specified the domain's
UUID is printed instead. Using the option --table specifies that a table style output should be
used. All three commands are mutually exclusive
--title This command must be used with --table output. --titlewill cause an extra
column to be created in the table with the short domain description (title).
--with-managed-save lists the domains that have been configured with managed save. To
list the commands without it, use the command --without-managed-save
--state-running filters out for the domains that are running, --state-paused for paused
domains, --state-shutoff for domains that are turned off, and --state-other lists all
states as a fallback.
--autostart this option will cause the auto-starting domains to be listed. To list domains with
this feature disabled, use the option --no-autostart.
--with-snapshot will list the domains whose snapshot images can be listed. To filter for the
domains without a snapshot, use the option --without-snapshot
Id Name State
Title
0 Domain-0 running
Mailserver1
2 rhelvm paused
For an example of virsh vcpuinfo output, refer to Section 14.13.2, “Displaying Virtual CPU
Information”
201
Virtualization Administration Guide
VCPU: 1
CPU: 2
State: running
CPU time: 10889.1s
CPU Affinity: yyyy
[--cpulist] >string< lists the host physical machine's CPU number(s) to set, or omit
an optional query
202
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
--config displays the value to be configured on guest virtual machine's next boot
--guest displays the count that is returned is from the perspective of the guest
The vcpu parameter denotes the number of virtualized CPUs allocated to the guest virtual machine.The
vcpu parameter must be provided.
The cpulist parameter is a list of physical CPU identifier numbers separated by commas. The
cpulist parameter determines which physical CPUs the VCPUs can run on.
Additional parameters such as --config affect the next boot, whereas --live affects the running
domain, and --current affects the current domain.
The following parameters may be set for the virsh setvcpus command:
NOTE
The count value cannot exceed the number of CPUs that were assigned to the
guest virtual machine when it was created. It may also be limited by the host or
the hypervisor. For Xen, you can only adjust the virtual CPUs of a running domain
if the domain is paravirtualized.
203
Virtualization Administration Guide
--live - The default option, used if none are specified. The configuration change takes effect
on the running guest virtual machine. This is referred to as a hot plug if the number of vCPUs is
increased, and hot unplug if it is reduced.
IMPORTANT
--config - The configuration change takes effect on the next reboot of the guest. Both the --
config and --live options may be specified together if supported by the hypervisor.
--current - Configuration change takes effect on the current state of the guest virtual
machine. If used on a running guest, it acts as --live, if used on a shut-down guest, it acts as
--config.
--maximum - Sets a maximum vCPU limit that can be hot-plugged on the next reboot of the
guest. As such, it must only be used with the --config option, and not with the --live option.
--guest - Instead of a hot plug or a hot unplug, the QEMU guest agent modifies the vCPU
count directly in the running guest by enabling or disabling vCPUs. This option cannot be used
with count value higher than the current number of vCPUs in the gueet, and configurations set
with --guest are reset when a guest is rebooted.
To hot-plug a vCPU, run the following command on a guest with a single vCPU:
This increases the number of vCPUs for guestVM1 to two. The change is performed while guestVM1
is running, as indicated by the --live option.
To hot-unplug one vCPU from the same running guest, run the following:
Be aware, however, that currently, using vCPU hot unplug can lead to problems with further
modifications of the vCPU count.
You must specify the count in kilobytes. The new count value cannot exceed the amount you specified
when you created the guest virtual machine. Values lower than 64 MB are unlikely to work with most
guest virtual machine operating systems. A higher maximum memory value does not affect active guest
204
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
virtual machines. If the new value is lower than the available memory, it will shrink possibly causing the
guest virtual machine to crash.
Note that all values will be rounded up to the nearest kibibyte by libvirt, and may be further
rounded to the granularity supported by the hypervisor. Some hypervisors also enforce a
minimum, such as 4000KiB (or 4000 x 2 10 or 4,096,000 bytes). The units for this value are
determined by the optional attribute memory unit, which defaults to the kibibytes (KiB) as a
unit of measure where the value given is multiplied by 210 or blocks of 1024 bytes.
The size that can be given for the maximum memory is a scaled integer that by default is expressed in
kibibytes, unless a supported suffix is provided. The following options can be used with this command:
205
Virtualization Administration Guide
--live - controls the memory of the running domain, providing the hypervisor supports this
action as not all hypervisors allow live changes of the maximum memory limit.
# virsh net-list
# virsh net-list
Name State Autostart
-----------------------------------------
default active yes
vnet1 active yes
vnet2 active yes
206
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
virsh net-create XMLfile — generates and starts a new network using an existing XML
file.
virsh net-define XMLfile — generates a new network device from an existing XML file
without starting it.
WARNING
The commands in this section are only supported if the machine has the
NetworkManager service disabled, and is using the network service instead.
Often, these host interfaces can then be used by name within domain <interface> elements (such as
a system-created bridge interface), but there is no requirement that host interfaces be tied to any
particular guest configuration XML at all. Many of the commands for host interfaces are similar to the
ones used for domains, and the way to name an interface is either by its name or its MAC address.
However, using a MAC address for an iface option only works when that address is unique (if an
interface and a bridge share the same MAC address, which is often the case, then using that MAC
address results in an error due to ambiguity, and you must resort to a name instead).
14.15.1.1. Defining and starting a host physical machine interface via an XML file
The virsh iface-define file command define a host interface from an XML file. This command
will only define the interface and will not start it.
207
Virtualization Administration Guide
To start an interface which has already been defined, run iface-start interface, where interface
is the interface name.
14.15.1.2. Editing the XML configuration file for the host interface
The command iface-edit interface edits the XML configuration file for a host interface. This is the
only recommended way to edit the XML configuration file. (Refer to Chapter 20, Manipulating the
Domain XML for more information about these files.)
The iface-list --inactive --all displays a list of active host interfaces. If --all is specified,
this list will also include interfaces that are defined but are inactive. If --inactive is specified only the
inactive interfaces will be listed.
The iface-name interface command converts a host interface MAC to an interface name, provided
the MAC address is unique among the host’s interfaces. This command requires interface which is the
interface's MAC address.
The iface-mac interface command will convert a host's interface name to MAC address where in
this case interface, is the interface name.
The virsh iface-destroy interface command destroys (stops) a given host interface, which is
the same as running if-down on the host. This command will disable that interface from active use and
takes effect immediately.
To undefine the interface, use the iface-undefine interface command along with the interface
name.
virsh iface-dumpxml interface --inactive displays the host interface information as an XML
dump to stdout. If the --inactive option is specified, then the output reflects the persistent state of the
interface that will be used the next time it is started.
The iface-bridge creates a bridge device named bridge, and attaches the existing network device
interface to the new bridge, which starts working immediately, with STP enabled and a delay of 0.
Note that these settings can be altered with --no-stp, --no-start, and an integer number of seconds for
delay. All IP address configuration of interface will be moved to the new bridge device. Refer to
Section 14.15.1.8, “Tearing down a bridge device” for information on tearing down the bridge.
The iface-unbridge bridge --no-start command tears down a specified bridge device named
208
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
bridge, releases its underlying interface back to normal usage, and moves all IP address configuration
from the bridge device to the underlying device. The underlying interface is restarted unless --no-
start option is used, but keep in mind not restarting is generally not recommended. Refer to
Section 14.15.1.7, “Creating bridge devices” for the command to use to create a bridge.
The iface-begin command creates a snapshot of current host interface settings, which can later be
committed (with iface-commit) or restored (iface-rollback). If a snapshot already exists, then
this command will fail until the previous snapshot has been committed or restored. Undefined behavior
will result if any external changes are made to host interfaces outside of the libvirt API between the time
of the creation of a snapshot and its eventual commit or rollback.
Use the iface-commit command to declare all changes made since the last iface-begin as
working, and then delete the rollback point. If no interface snapshot has already been started via iface-
begin, then this command will fail.
Use the iface-rollback to revert all host interface settings back to the state that recorded the last
time the iface-begin command was executed. If iface-begin command had not been previously
executed, then iface-rollback will fail. Note that rebooting the host physical machine also serves as
an implicit rollback point.
The virsh snapshot-create command creates a snapshot for domain with the properties specified
in the domain XML file (such as <name> and <description> elements, as well as <disks>).
The domain name, ID, or UID may be used as the domain requirement. The XML requirement is a string
must contain the <name>, <description> and <disks> elements.
NOTE
Live snapshots are not supported in Red Hat Enterprise Linux. There are additional
options available with the virsh snapshot-create command for use with live
snapshots which are visible in libvirt, but not supported in Red Hat Enterprise Linux 6.
--redefine specifies that if all XML elements produced by snapshot-dumpxml are valid; it
209
Virtualization Administration Guide
can be used to migrate snapshot hierarchy from one machine to another, to recreate hierarchy
for the case of a transient domain that goes away and is later recreated with the same name and
UUID, or to make slight alterations in the snapshot metadata (such as host-specific aspects of
the domain XML embedded in the snapshot). When this option is supplied, the xmlfile
argument is mandatory, and the domain’s current snapshot will not be altered unless the --
current option is also given.
--no-metadata creates the snapshot, but any metadata is immediately discarded (that is,
libvirt does not treat the snapshot as current, and cannot revert to the snapshot unless --
redefine is later used to teach libvirt about the metadata again).
--reuse-external, if used, this option specifies the location of an existing external XML
snapshot to use. If an existing external snapshot does not already exist, the command will fail to
take a snapshot to avoid losing contents of the existing files.
The virsh snapshot-create-as domain command creates a snapshot for the domain with the
properties specified in the domain XML file (such as <name> and <description> elements). If these
values are not included in the XML string, libvirt will choose a value. To create a snapshot run:
--print-xml creates appropriate XML for snapshot-create as output, rather than actually
creating a snapshot.
--diskspec option can be used to control how --disk-only and external checkpoints create
external files. This option can occur multiple times, according to the number of <disk> elements
in the domain XML. Each <diskspec> is in the form disk[,snapshot=type][,driver=type]
[,file=name]. To include a literal comma in disk or in file=name, escape it with a second
comma. A literal --diskspec must precede each diskspec unless all three of <domain>,
<name>, and <description> are also present. For example, a diskspec of
vda,snapshot=external,file=/path/to,,new results in the following XML:
--no-metadata creates snapshot data but any metadata is immediately discarded (that is,
libvirt does not treat the snapshot as current, and cannot revert to the snapshot unless snapshot-
create is later used to teach libvirt about the metadata again). This option is incompatible with -
-print-xml.
This command is used to query which snapshot is currently in use. To use, run:
210
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
If snapshotname is not used, snapshot XML for the domain’s current snapshot (if there is one) will be
displayed as output. If --name is specified, just the current snapshot name instead of the full XML will be
sent as output. If --security-info is supplied, security sensitive information will be included in the
XML. Using snapshotname, libvirt generates a request to make the existing named snapshot become
the current snapshot, without reverting it to the domain.
14.15.2.4. snapshot-edit-domain
This command is used to edit the snapshot that is currently in use. To use, run:
If both snapshotname and --current are specified, it forces the edited snapshot to become the
current snapshot. If snapshotname is omitted, then --current must be supplied, in order to edit the
current snapshot.
This is equivalent to the following command sequence below, but it also includes some error checking:
If --rename is specified, then the resulting edited file gets saved in a different file name. If --clone is
specified, then changing the snapshot name will create a clone of the snapshot metadata. If neither is
specified, then the edits will not change the snapshot name. Note that changing a snapshot name must
be done with care, since the contents of some snapshots, such as internal snapshots within a single
qcow2 file, are accessible only from the original snapshot filename.
14.15.2.5. snapshot-info-domain
Outputs basic information about a specified snapshot , or the current snapshot with --current.
14.15.2.6. snapshot-list-domain
List all of the available snapshots for the given domain, defaulting to show columns for the snapshot
name, creation time, and domain state. To use, run:
--parent adds a column to the output table giving the name of the parent of each snapshot.
This option may not be used with --roots or --tree.
211
Virtualization Administration Guide
--roots filters the list to show only the snapshots that have no parents. This option may not be
used with --parent or --tree.
--tree displays output in a tree format, listing just snapshot names. These three options are
mutually exclusive. This option may not be used with --roots or --parent.
--from filters the list to snapshots which are children of the given snapshot; or if--current is
provided, will cause the list to start at the current snapshot. When used in isolation or with --
parent, the list is limited to direct children unless --descendants is also present. When used
with --tree, the use of --descendants is implied. This option is not compatible with --
roots. Note that the starting point of --from or --current is not included in the list unless
the --tree option is also present.
--leaves is specified, the list will be filtered to just snapshots that have no children. Likewise, if
--no-leaves is specified, the list will be filtered to just snapshots with children. (Note that
omitting both options does no filtering, while providing both options will either produce the same
list or error out depending on whether the server recognizes the options) Filtering options are not
compatible with --tree..
--metadata is specified, the list will be filtered to just snapshots that involve libvirt metadata,
and thus would prevent the undefining of a persistent domain, or be lost on destroy of a transient
domain. Likewise, if --no-metadata is specified, the list will be filtered to just snapshots that
exist without the need for libvirt metadata.
--inactive is specified, the list will be filtered to snapshots that were taken when the domain
was shut off. If --active is specified, the list will be filtered to snapshots that were taken when
the domain was running, and where the snapshot includes the memory state to revert to that
running state. If --disk-only is specified, the list will be filtered to snapshots that were taken
when the domain was running, but where the snapshot includes only disk state.
--internal is specified, the list will be filtered to snapshots that use internal storage of existing
disk images. If --external is specified, the list will be filtered to snapshots that use external files
for disk images or memory state.
virsh snapshot-dumpxml domain snapshot outputs the snapshot XML for the domain’s
snapshot named snapshot. To use, run:
The --security-info option will also include security sensitive information. Use snapshot-
current to easily access the XML of the current snapshot.
Outputs the name of the parent snapshot, if any, for the given snapshot, or for the current snapshot with
--current. To use, run:
212
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
Reverts the given domain to the snapshot specified by snapshot, or to the current snapshot with --
current.
WARNING
Be aware that this is a destructive action; any changes in the domain since the last
snapshot was taken will be lost. Also note that the state of the domain after
snapshot-revert is complete will be the state of the domain at the time the
original snapshot was taken.
Normally, reverting to a snapshot leaves the domain in the state it was at the time the snapshot was
created, except that a disk snapshot with no guest virtual machine state leaves the domain in an inactive
state. Passing either the --running or --paused option will perform additional state changes (such
as booting an inactive domain, or pausing a running domain). Since transient domains cannot be
inactive, it is required to use one of these options when reverting to a disk snapshot of a transient
domain.
There are two cases where a snapshot revert involves extra risk, which requires the use of --
force to proceed. One is the case of a snapshot that lacks full domain information for reverting
configuration; since libvirt cannot prove that the current configuration matches what was in use at the
time of the snapshot, supplying --force assures libvirt that the snapshot is compatible with the current
configuration (and if it is not, the domain will likely fail to run). The other is the case of reverting from a
running domain to an active state where a new hypervisor has to be created rather than reusing the
existing hypervisor, because it implies drawbacks such as breaking any existing VNC or Spice
connections; this condition happens with an active snapshot that uses a provably incompatible
configuration, as well as with an inactive snapshot that is combined with the --start or --pause
option.
snapshot-delete domain deletes the snapshot for the specified domain. To do this, run:
This command Deletes the snapshot for the domain named snapshot, or the current snapshot with --
current. If this snapshot has child snapshots, changes from this snapshot will be merged into the
children. If the option --children is used, then it will delete this snapshot and any children of this
snapshot. If --children-only is used, then it will delete any children of this snapshot, but leave this
snapshot intact. These two options are mutually exclusive.
The --metadata is used it will delete the snapshot's metadata maintained by libvirt, while leaving the
snapshot contents intact for access by external tools; otherwise deleting a snapshot also removes its
data contents from that point in time.
213
Virtualization Administration Guide
14.16.1. Introduction
Every hypervisor has its own policy for what a guest virtual machine will see for its CPUs by default.
Whereas some hypervisors decide which CPU host physical machine features will be available for the
guest virtual machine, QEMU/KVM presents the guest virtual machine with a generic model named
qemu32 or qemu64. These hypervisors perform more advanced filtering, classifying all physical CPUs
into a handful of groups and have one baseline CPU model for each group that is presented to the guest
virtual machine. Such behavior enables the safe migration of guest virtual machines between host
physical machines, provided they all have physical CPUs that classify into the same group. libvirt does
not typically enforce policy itself, rather it provides the mechanism on which the higher layers define their
own desired policy. Understanding how to obtain CPU model information and define a suitable guest
virtual machine CPU model is critical to ensure guest virtual machine migration is successful between
host physical machines. Note that a hypervisor can only emulate features that it is aware of and features
that were created after the hypervisor was released may not be emulated.
It is not practical to have a database listing all known CPU models, so libvirt has a small list of baseline
CPU model names. It chooses the one that shares the greatest number of CPUID bits with the actual
host physical machine CPU and then lists the remaining bits as named features. Notice that libvirt does
not display which features the baseline CPU contains. This might seem like a flaw at first, but as will be
explained in this section, it is not actually necessary to know this information.
214
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
Here is an example of what libvirt reports as the capabilities on a basic workstation, when the virsh
capabilitiesis executed:
<capabilities>
<host>
<cpu>
<arch>i686</arch>
<model>pentium3</model>
<topology sockets='1' cores='2' threads='1'/>
<feature name='lahf_lm'/>
<feature name='lm'/>
<feature name='xtpr'/>
<feature name='cx16'/>
<feature name='ssse3'/>
<feature name='tm2'/>
<feature name='est'/>
<feature name='vmx'/>
<feature name='ds_cpl'/>
<feature name='monitor'/>
<feature name='pni'/>
<feature name='pbe'/>
<feature name='tm'/>
<feature name='ht'/>
<feature name='ss'/>
<feature name='sse2'/>
<feature name='acpi'/>
<feature name='ds'/>
<feature name='clflush'/>
<feature name='apic'/>
</cpu>
</host>
</capabilities>
Now compare that to any random server, with the same virsh capabilities command:
215
Virtualization Administration Guide
<capabilities>
<host>
<cpu>
<arch>x86_64</arch>
<model>phenom</model>
<topology sockets='2' cores='4' threads='1'/>
<feature name='osvw'/>
<feature name='3dnowprefetch'/>
<feature name='misalignsse'/>
<feature name='sse4a'/>
<feature name='abm'/>
<feature name='cr8legacy'/>
<feature name='extapic'/>
<feature name='cmp_legacy'/>
<feature name='lahf_lm'/>
<feature name='rdtscp'/>
<feature name='pdpe1gb'/>
<feature name='popcnt'/>
<feature name='cx16'/>
<feature name='ht'/>
<feature name='vme'/>
</cpu>
...snip...
To see if this CPU description is compatible with the previous workstation CPU description, use the
virsh cpu-compare command.
The reduced content was stored in a file named virsh-caps-workstation-cpu-only.xml and the
virsh cpu-compare command can be executed on this file:
As seen in this output, libvirt is correctly reporting that the CPUs are not strictly compatible. This is
because there are several features in the server CPU that are missing in the client CPU. To be able to
migrate between the client and the server, it will be necessary to open the XML file and comment out
some features. To determine which features need to be removed, run the virsh cpu-baseline
command, on the both-cpus.xml which contains the CPU information for both machines. Running #
virsh cpu-baseline both-cpus.xml, results in:
216
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
<cpu match='exact'>
<model>pentium3</model>
<feature policy='require' name='lahf_lm'/>
<feature policy='require' name='lm'/>
<feature policy='require' name='cx16'/>
<feature policy='require' name='monitor'/>
<feature policy='require' name='pni'/>
<feature policy='require' name='ht'/>
<feature policy='require' name='sse2'/>
<feature policy='require' name='clflush'/>
<feature policy='require' name='apic'/>
</cpu>
This composite file shows which elements are in common. Everything that is not in common should be
commented out.
match='minimum' - the host physical machine CPU must have at least the CPU features
described in the guest virtual machine XML. If the host physical machine has additional features
beyond the guest virtual machine configuration, these will also be exposed to the guest virtual
machine.
match='exact' - the host physical machine CPU must have at least the CPU features described
in the guest virtual machine XML. If the host physical machine has additional features beyond
the guest virtual machine configuration, these will be masked out from the guest virtual machine.
match='strict' - the host physical machine CPU must have exactly the same CPU features
described in the guest virtual machine XML.
The next enhancement is that the <feature> elements can each have an extra 'policy' attribute with
possible values of:
policy='force' - expose the feature to the guest virtual machine even if the host physical machine
does not have it. This is usually only useful in the case of software emulation.
policy='require' - expose the feature to the guest virtual machine and fail if the host physical
machine does not have it. This is the sensible default.
policy='optional' - expose the feature to the guest virtual machine if it happens to support it.
policy='disable' - if the host physical machine has this feature, then hide it from the guest virtual
machine.
217
Virtualization Administration Guide
policy='forbid' - if the host physical machine has this feature, then fail and refuse to start the
guest virtual machine.
The 'forbid' policy is for a niche scenario where an incorrectly functioning application will try to use a
feature even if it is not in the CPUID mask, and you wish to prevent accidentally running the guest virtual
machine on a host physical machine with that feature. The 'optional' policy has special behavior with
respect to migration. When the guest virtual machine is initially started the parameter is optional, but
when the guest virtual machine is live migrated, this policy turns into 'require', since you cannot have
features disappearing across migration.
memory - The memory controller allows for setting limits on RAM and swap usage and querying
cumulative usage of all processes in the group
cpuset - The CPU set controller binds processes within a group to a set of CPUs and controls
migration between CPUs.
cpuacct - The CPU accounting controller provides information about CPU usage for a group of
processes.
cpu -The CPU scheduler controller controls the prioritization of processes in the group. This is
similar to granting nice level privileges.
devices - The devices controller grants access control lists on character and block devices.
freezer - The freezer controller pauses and resumes execution of processes in the group. This
is similar to SIGSTOP for the whole group.
net_cls - The network class controller manages network utilization by associating processes
with a tc network class.
In creating a group hierarchy cgroup will leave mount point and directory setup entirely to the
administrators’ discretion and is more complex than just adding some mount points to /etc/fstab. It is
necessary to setup the directory hierarchy and decide how processes get placed within it. This can be
done with the following virsh commands:
218
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
--set - the string placed here is the controller or action that is to be called. Additional
parameters or values if required should be added as well.
--current - when used with --set, will use the specified set string as the current scheduler
information. When used without will display the current scheduler information.
--config - - when used with --set, will use the specified set string on the next reboot. When
used without will display the scheduler information that is saved in the configuration file.
--live - when used with --set, will use the specified set string on a guest virtual machine
that is currently running. When used without will display the configuration setting currently used
by the running virtual machine
The scheduler can be set with any of the following parameters: cpu_shares, vcpu_period and
vcpu_quota.
This example shows the shell guest virtual machine's schedule information
In this example, the cpu_shares is changed to 2046. This effects the current state and not the
configuration file.
219
Virtualization Administration Guide
More information on this command can be found in the Virtualization Tuning and Optimization Guide
This command accepts the --disable option which disables the autostart command.
# net-define file
# net-destroy network
220
CHAPTER 14. MANAGING GUEST VIRTUAL MACHINES WITH VIRSH
The editor used for editing the XML file can be supplied by the $VISUAL or $EDITOR environment
variables, and defaults to vi.
Note: When talking to older servers, this command is forced to use a series of API calls with an inherent
race, where a pool might not be listed or might appear more than once if it changed state between calls
while the list was being collected. Newer servers do not have this problem.
221
Virtualization Administration Guide
# net-undefine network
222
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
virt-manager provides a graphical view of hypervisors and guests on your host system and on remote
host systems. virt-manager can perform virtualization management tasks, including:
assigning memory,
saving and restoring, pausing and resuming, and shutting down and starting guests,
Alternatively, virt-manager can be started remotely using ssh as demonstrated in the following
command:
223
Virtualization Administration Guide
Using ssh to manage virtual machines and hosts is discussed further in Section 5.1, “Remote
Management with SSH”.
224
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
225
Virtualization Administration Guide
NOTE
In order to attach the USB device to the guest virtual machine, you first must attach it to
the host physical machine and confirm that the device is working. If the guest is running,
you need to shut it down before proceeding.
226
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
3. In the Add New Virtual Hardware popup, select USB Host Device, select the device you want
to attach from the list and Click Finish.
227
Virtualization Administration Guide
4. To use the USB device in the guest virtual machine, start the guest virtual machine.
228
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
NOTE
VNC is considered insecure by many security experts, however, several changes have
been made to enable the secure usage of VNC for virtualization on Red Hat enterprise
Linux. The guest machines only listen to the local host's loopback address (127.0.0.1).
This ensures only those with shell privileges on the host can access virt-manager and the
virtual machine through VNC. Although virt-manager is configured to listen to other public
network interfaces and alternative methods can be configured, it is not recommended.
Remote administration can be performed by tunneling over SSH which encrypts the
traffic. Although VNC can be configured to access remotely without tunneling over SSH,
for security reasons, it is not recommended. To remotely administer the guest follow the
instructions in: Chapter 5, Remote Management of Guests. TLS can provide enterprise
level security for managing guest and host systems.
Your local desktop can intercept key combinations (for example, Ctrl+Alt+F1) to prevent them from being
sent to the guest machine. You can use the Send key menu option to send these sequences. From the
guest machine window, click the Send key menu and select the key sequence to send. In addition, from
this menu you can also capture the screen output.
229
Virtualization Administration Guide
1. To create a new connection open the File menu and select the Add Connection... menu
item.
2. The Add Connection wizard appears. Select the hypervisor. For Red Hat Enterprise Linux 6
systems select QEMU/KVM. Select Local for the local system or one of the remote connection
options and click Connect. This example uses Remote tunnel over SSH which works on default
installations. For more information on configuring remote connections refer to Chapter 5, Remote
Management of Guests
3. Enter the root password for the selected host when prompted.
A remote host is now connected and appears in the main virt-manager window.
230
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
1. In the Virtual Machine Manager main window, highlight the virtual machine that you want to view.
2. From the Virtual Machine Manager Edit menu, select Virtual Machine Details.
231
Virtualization Administration Guide
When the Virtual Machine details window opens, there may be a console displayed. Should this
happen, click View and then select Details. The Overview window opens first by default. To
go back to this window, select Overview from the navigation pane on the left hand side.
The Overview view shows a summary of configuration details for the guest.
232
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
3. Select Performance from the navigation pane on the left hand side.
The Performance view shows a summary of guest performance, including CPU and Memory
usage.
233
Virtualization Administration Guide
4. Select Processor from the navigation pane on the left hand side. The Processor view allows
you to view the current processor allocation, as well as to change it.
It is also possible to change the number of virtual CPUs (vCPUs) while the virtual machine is
running, which is referred to as hot plugging and hot unplugging.
IMPORTANT
234
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
5. Select Memory from the navigation pane on the left hand side. The Memory view allows you to
view or change the current memory allocation.
235
Virtualization Administration Guide
6. Each virtual disk attached to the virtual machine is displayed in the navigation pane. Click on a
virtual disk to modify or remove it.
236
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
7. Each virtual network interface attached to the virtual machine is displayed in the navigation
pane. Click on a virtual network interface to modify or remove it.
237
Virtualization Administration Guide
2. From the Stats tab specify the time in seconds or stats polling options.
238
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
1. From the View menu, select Graph, then the Guest CPU Usage check box.
239
Virtualization Administration Guide
2. The Virtual Machine Manager shows a graph of CPU usage for all virtual machines on your
system.
1. From the View menu, select Graph, then the Host CPU Usage check box.
2. The Virtual Machine Manager shows a graph of host CPU usage on your system.
240
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
1. Make sure that the Disk I/O statistics collection is enabled. To do this, from the Edit menu,
select Preferences and click the Statstab.
241
Virtualization Administration Guide
3. To enable the Disk I.O display, from the View menu, select Graph, then the Disk I/O check
box.
242
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
4. The Virtual Machine Manager shows a graph of Disk I/O for all virtual machines on your system.
1. Make sure that the Network I/O statistics collection is enabled. To do this, from the Edit menu,
select Preferences and click the Statstab.
243
Virtualization Administration Guide
3. To display the Network I/O statistics, from the View menu, select Graph, then the Network
I/O check box.
244
CHAPTER 15. MANAGING GUESTS WITH THE VIRTUAL MACHINE MANAGER (VIRT-MANAGER)
4. The Virtual Machine Manager shows a graph of Network I/O for all virtual machines on your
system.
245
Virtualization Administration Guide
246
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
16.1. INTRODUCTION
Red Hat Enterprise Linux 6 comes with tools to access, edit and create host physical machine disks or
other disk images. There are several uses for these tools, including:
Preparing new disk images containing files, directories, file systems, partitions, logical volumes
and other options.
Rescuing and repairing host physical machines that fail to boot or those that need boot
configuration changes.
WARNING
You must never use these tools to write to a host physical machine or disk image
which is attached to a running virtual machine, not even to open such a disk image
in write mode. Doing so will result in disk corruption of the guest virtual machine.
The tools try to prevent you from doing this, however do not catch all cases. If there
is any suspicion that a guest virtual machine might be running, it is strongly
recommended that the tools not be used, or at least always use the tools in read-
only mode.
247
Virtualization Administration Guide
NOTE
Some virtualization commands in Red Hat Enterprise Linux 6 allow you to specify a
remote libvirt connection. For example:
However, libguestfs in Red Hat Enterprise Linux 6 cannot access remote guests, and
commands using remote URLs like this do not work as expected. This affects the
following Red Hat Enterprise Linux 6 commands:
guestfish
guestmount
virt-alignment-scan
virt-cat
virt-copy-in
virt-copy-out
virt-df
virt-edit
virt-filesystems
virt-inspector
virt-inspector2
virt-list-filesystems
virt-list-partitions
virt-ls
virt-rescue
virt-sysprep
virt-tar
virt-tar-in
virt-tar-out
virt-win-reg
16.2. TERMINOLOGY
This section explains the terms used throughout this chapter.
248
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
libguestfs (Guest file system library) - the underlying C library that provides the basic
functionality for opening disk images, reading and writing files and so on. You can write C
programs directly to this API, but it is quite low level.
guestfish (Guest file system interactive shell) is an interactive shell that you can use from the
command line or from shell scripts. It exposes all of the functionality of the libguestfs API.
Various virt tools are built on top of libguestfs, and these provide a way to perform specific single
tasks from the command line. Tools include virt-df, virt-rescue, virt-resize and virt-edit.
hivex and Augeas are libraries for editing the Windows Registry and Linux configuration files
respectively. Although these are separate from libguestfs, much of the value of libguestfs comes
from the combination of these tools.
guestmount is an interface between libguestfs and FUSE. It is primarily used to mount file
systems from disk images on your host physical machine. This functionality is not necessary, but
can be useful.
16.3. INSTALLATION
To install libguestfs, guestfish, the libguestfs tools, guestmount and support for Windows guest virtual
machines, subscribe to the Red Hat Enterprise Linux V2WIN channel, go to the Red Hat Website and run
the following command:
To install every libguestfs-related package including the language bindings, run the following command:
To begin viewing or editing a virtual machine disk image, run the following command, substituting the
path to your desired disk image:
--ro means that the disk image is opened read-only. This mode is always safe but does not allow write
access. Only omit this option when you are certain that the guest virtual machine is not running, or the
disk image is not attached to a live guest virtual machine. It is not possible to use libguestfs to edit a
live guest virtual machine, and attempting to will result in irreversible disk corruption.
/path/to/disk/image is the path to the disk. This can be a file, a host physical machine logical volume
(such as /dev/VG/LV), a host physical machine device (/dev/cdrom) or a SAN LUN (/dev/sdf3).
NOTE
libguestfs and guestfish do not require root privileges. You only need to run them as root if
the disk image being accessed needs root to read or write or both.
249
Virtualization Administration Guide
><fs>
At the prompt, type run to initiate the library and attach the disk image. This can take up to 30 seconds
the first time it is done. Subsequent starts will complete much faster.
NOTE
libguestfs will use hardware virtualization acceleration such as KVM (if available) to speed
up this process.
Once the run command has been entered, other commands can be used, as the following section
demonstrates.
The list-filesystems command will list file systems found by libguestfs. This output shows a Red
Hat Enterprise Linux 4 disk image:
><fs> run
><fs> list-filesystems
/dev/vda1: ext3
/dev/VolGroup00/LogVol00: ext3
/dev/VolGroup00/LogVol01: swap
><fs> run
><fs> list-filesystems
/dev/vda1: ntfs
/dev/vda2: ntfs
Other useful commands are list-devices, list-partitions, lvs, pvs, vfs-type and file. You
can get more information and help on any command by typing help command, as shown in the following
output:
250
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
SYNOPSIS
vfs-type device
DESCRIPTION
This command gets the file system type corresponding to the file
system on
"device".
For most file systems, the result is the name of the Linux VFS module
which would be used to mount this file system if you mounted it
without
specifying the file system type. For example a string such as "ext3"
or
"ntfs".
To view the actual contents of a file system, it must first be mounted. This example uses one of the
Windows partitions shown in the previous output (/dev/vda2), which in this case is known to
correspond to the C:\ drive:
You can use guestfish commands such as ls, ll, cat, more, download and tar-out to view and
download files and directories.
NOTE
There is no concept of a current working directory in this shell. Unlike ordinary shells, you
cannot for example use the cd command to change directories. All paths must be fully
qualified starting at the top with a forward slash (/) character. Use the Tab key to
complete paths.
Instead of listing and mounting file systems by hand, it is possible to let guestfish itself inspect the image
and mount the file systems as they would be in the guest virtual machine. To do this, add the -i option on
the command line:
251
Virtualization Administration Guide
><fs> ll /
total 210
drwxr-xr-x. 24 root root 4096 Oct 28 09:09 .
drwxr-xr-x 21 root root 4096 Nov 17 15:10 ..
drwxr-xr-x. 2 root root 4096 Oct 27 22:37 bin
drwxr-xr-x. 4 root root 1024 Oct 27 21:52 boot
drwxr-xr-x. 4 root root 4096 Oct 27 21:21 dev
drwxr-xr-x. 86 root root 12288 Oct 28 09:09 etc
[etc]
Because guestfish needs to start up the libguestfs back end in order to perform the inspection and
mounting, the run command is not necessary when using the -i option. The -i option works for many
common Linux and Windows guest virtual machines.
A guest virtual machine can be accessed from the command line when you specify its name as known to
libvirt (in other words, as it appears in virsh list --all). Use the -d option to access a guest
virtual machine by its name, with or without the -i option:
guestfish -d RHEL3 -i
Commands to edit files include edit, vi and emacs. Many commands also exist for creating files and
directories, such as write, mkdir, upload and tar-in.
252
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
#!/bin/bash -
set -e
guestname="$1"
#!/bin/bash -
set -e
guestname="$1"
Augeas can also be used to modify configuration files. You can modify the above script to change the
keyboard layout:
#!/bin/bash -
set -e
guestname="$1"
1. The --ro option has been removed in the second example, giving the ability to write to the
guest virtual machine.
253
Virtualization Administration Guide
2. The aug-get command has been changed to aug-set to modify the value instead of fetching
it. The new value will be "gb" (including the quotes).
3. The aug-save command is used here so Augeas will write the changes out to disk.
NOTE
guestfish can do much more than we can cover in this introductory document. For example, creating disk
images from scratch:
guestfish -N fs
virt-cat is similar to the guestfish download command. It downloads and displays a single
file to the guest virtual machine. For example:
virt-edit is similar to the guestfish edit command. It can be used to interactively edit a
single file within a guest virtual machine. For example, you may need to edit the grub.conf file
in a Linux-based guest virtual machine that will not boot:
virt-edit has another mode where it can be used to make simple non-interactive changes to
a single file. For this, the -e option is used. This command, for example, changes the root
password in a Linux guest virtual machine to having no password:
virt-ls is similar to the guestfish ls, ll and find commands. It is used to list a directory or
directories (recursively). For example, the following command would recursively list files and
directories under /home in a Linux guest virtual machine:
254
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
16.6.1. Introduction
This section describes virt-rescue, which can be considered analogous to a rescue CD for virtual
machines. It boots a guest virtual machine into a rescue shell so that maintenance can be performed to
correct errors and the guest virtual machine can be repaired.
There is some overlap between virt-rescue and guestfish. It is important to distinguish their differing
uses. virt-rescue is for making interactive, ad-hoc changes using ordinary Linux file system tools. It is
particularly suited to rescuing a guest virtual machine that has failed . virt-rescue cannot be scripted.
In contrast, guestfish is particularly useful for making scripted, structured changes through a formal set of
commands (the libguestfs API), although it can also be used interactively.
virt-rescue GuestName
virt-rescue /path/to/disk/image
(where the path can be any file, any logical volume, LUN, or so on) containing a guest virtual machine
disk.
You will first see output scroll past, as virt-rescue boots the rescue VM. In the end you will see:
bash: cannot set terminal process group (-1): Inappropriate ioctl for
device
bash: no job control in this shell
><rescue>
The shell prompt here is an ordinary bash shell, and a reduced set of ordinary Red Hat Enterprise Linux
commands is available. For example, you can enter:
The previous command will list disk partitions. To mount a file system, it is suggested that you mount it
under /sysroot, which is an empty directory in the rescue machine for the user to mount anything you
like. Note that the files under / are files from the rescue VM itself:
255
Virtualization Administration Guide
><rescue> ls -l /sysroot/grub/
total 324
-rw-r--r--. 1 root root 63 Sep 16 18:14 device.map
-rw-r--r--. 1 root root 13200 Sep 16 18:14 e2fs_stage1_5
-rw-r--r--. 1 root root 12512 Sep 16 18:14 fat_stage1_5
-rw-r--r--. 1 root root 11744 Sep 16 18:14 ffs_stage1_5
-rw-------. 1 root root 1503 Oct 15 11:19 grub.conf
[...]
When you are finished rescuing the guest virtual machine, exit the shell by entering exit or Ctrl+d.
virt-rescue has many command line options. The options most often used are:
--ro: Operate in read-only mode on the guest virtual machine. No changes will be saved. You
can use this to experiment with the guest virtual machine. As soon as you exit from the shell, all
of your changes are discarded.
--network: Enable network access from the rescue shell. Use this if you need to, for example,
download RPM or other files into the guest virtual machine.
16.7.1. Introduction
This section describes virt-df, which displays file system usage from a disk image or a guest virtual
machine. It is similar to the Linux df command, but for virtual machines.
# virt-df /dev/vg_guests/RHEL6
Filesystem 1K-blocks Used Available Use%
RHEL6:/dev/sda1 101086 10233 85634 11%
RHEL6:/dev/VolGroup00/LogVol00 7127864 2272744 4493036 32%
(Where /dev/vg_guests/RHEL6 is a Red Hat Enterprise Linux 6 guest virtual machine disk image.
The path in this case is the host physical machine logical volume where this disk image is located.)
You can also use virt-df on its own to list information about all of your guest virtual machines (ie.
those known to libvirt). The virt-df command recognizes some of the same options as the standard
df such as -h (human-readable) and -i (show inodes instead of blocks).
# virt-df -h
Filesystem Size Used Available Use%
F14x64:/dev/sda1 484.2M 66.3M 392.9M 14%
F14x64:/dev/vg_f14x64/lv_root 7.4G 3.0G 4.4G 41%
RHEL6brewx64:/dev/sda1 484.2M 52.6M 406.6M 11%
RHEL6brewx64:/dev/vg_rhel6brewx64/lv_root
256
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
NOTE
You can use virt-df safely on live guest virtual machines, since it only needs read-only
access. However, you should not expect the numbers to be precisely the same as those
from a df command running inside the guest virtual machine. This is because what is on
disk will be slightly out of synch with the state of the live guest virtual machine.
Nevertheless it should be a good enough approximation for analysis and monitoring
purposes.
virt-df is designed to allow you to integrate the statistics into monitoring tools, databases and so on. This
allows system administrators to generate reports on trends in disk usage, and alerts if a guest virtual
machine is about to run out of disk space. To do this you should use the --csv option to generate
machine-readable Comma-Separated-Values (CSV) output. CSV output is readable by most databases,
spreadsheet software and a variety of other tools and programming languages. The raw CSV looks like
the following:
For resources and ideas on how to process this output to produce trends and alerts, refer to the following
URL: http://libguestfs.org/virt-df.1.html.
16.8.1. Introduction
This section describes virt-resize, a tool for expanding or shrinking guest virtual machines. It only
works for guest virtual machines which are offline (shut down). It works by copying the guest virtual
machine image and leaving the original disk image untouched. This is ideal because you can use the
original image as a backup, however there is a trade-off as you need twice the amount of disk space.
1. Locate the disk image to be resized. You can use the command virsh dumpxml GuestName
for a libvirt guest virtual machine.
2. Decide on how you wish to expand the guest virtual machine. Run virt-df -h and virt-
list-partitions -lh on the guest virtual machine disk, as shown in the following output:
# virt-df -h /dev/vg_guests/RHEL6
Filesystem Size Used Available Use%
RHEL6:/dev/sda1 98.7M 10.0M 83.6M 11%
RHEL6:/dev/VolGroup00/LogVol00 6.8G 2.2G 4.3G 32%
257
Virtualization Administration Guide
Increase the size of the first (boot) partition, from approximately 100MB to 500MB.
2. Rename the original disk as the backup. How you do this depends on the host physical machine
storage environment for the original disk. If it is stored as a file, use the mv command. For logical
volumes (as demonstrated in this example), use lvrename:
3. Create the new disk. The requirements in this example are to expand the total disk size up to
16GB. Since logical volumes are used here, the following command is used:
# virt-resize \
/dev/vg_guests/RHEL6.backup /dev/vg_guests/RHEL6 \
--resize /dev/sda1=500M \
--expand /dev/sda2 \
--LV-expand /dev/VolGroup00/LogVol00
The first two arguments are the input disk and output disk. --resize /dev/sda1=500M
resizes the first partition up to 500MB. --expand /dev/sda2 expands the second partition to
fill all remaining space. --LV-expand /dev/VolGroup00/LogVol00 expands the guest
virtual machine logical volume to fill the extra space in the second partition.
Summary of changes:
/dev/sda1: partition will be resized from 101.9M to 500.0M
/dev/sda1: content will be expanded using the 'resize2fs' method
/dev/sda2: partition will be resized from 7.9G to 15.5G
/dev/sda2: content will be expanded using the 'pvresize' method
/dev/VolGroup00/LogVol00: LV will be expanded to maximum size
/dev/VolGroup00/LogVol00: content will be expanded using the
'resize2fs' method
Copying /dev/sda1 ...
[#####################################################]
258
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
5. Try to boot the virtual machine. If it works (and after testing it thoroughly) you can delete the
backup disk. If it fails, shut down the virtual machine, delete the new disk, and rename the
backup disk back to its original name.
# virt-df -h /dev/vg_pin/RHEL6
Filesystem Size Used Available
Use%
RHEL6:/dev/sda1 484.4M 10.8M 448.6M
3%
RHEL6:/dev/VolGroup00/LogVol00 14.3G 2.2G 11.4G 16%
Resizing guest virtual machines is not an exact science. If virt-resize fails, there are a number of tips
that you can review and attempt in the virt-resize(1) man page. For some older Red Hat Enterprise Linux
guest virtual machines, you may need to pay particular attention to the tip regarding GRUB.
16.9.1. Introduction
virt-inspector is a tool for inspecting a disk image to find out what operating system it contains.
NOTE
Red Hat Enterprise Linux 6.2 provides two variations of this program: virt-inspector
is the original program as found in Red Hat Enterprise Linux 6.0 and is now deprecated
upstream. virt-inspector2 is the same as the new upstream virt-inspector
program.
16.9.2. Installation
To install virt-inspector and the documentation, enter the following command:
To process Windows guest virtual machines you must also install libguestfs-winsupport. Refer to
Section 16.10.2, “Installation” for details. The documentation, including example XML output and a
Relax-NG schema for the output, will be installed in /usr/share/doc/libguestfs-devel-*/ where
"*" is replaced by the version number of libguestfs.
259
Virtualization Administration Guide
You can run virt-inspector against any disk image or libvirt guest virtual machine as shown in the
following example:
Or as shown here:
The result will be an XML report (report.xml). The main components of the XML file are a top-level
<operatingsystems> element containing usually a single <operatingsystem> element, similar to the
following:
<operatingsystems>
<operatingsystem>
260
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
</operatingsystem>
</operatingsystems>
Processing these reports is best done using W3C standard XPath queries. Red Hat Enterprise Linux 6
comes with a command line program (xpath) which can be used for simple instances; however, for
long-term and advanced usage, you should consider using an XPath library along with your favorite
programming language.
As an example, you can list out all file system devices using the following XPath query:
16.10.1. Introduction
virt-win-reg is a tool that manipulates the Registry in Windows guest virtual machines. It can be
used to read out registry keys. You can also use it to make changes to the Registry, but you must never
try to do this for live/running guest virtual machines, as it will result in disk corruption.
16.10.2. Installation
To use virt-win-reg you must run the following:
# virt-win-reg WindowsGuest \
'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall' \
| less
The output is in the standard text-based format used by .REG files on Windows.
261
Virtualization Administration Guide
NOTE
Hex-quoting is used for strings because the format does not properly define a portable
encoding method for strings. This is the only way to ensure fidelity when transporting
.REG files from one machine to another.
You can make hex-quoted strings printable by piping the output of virt-win-reg
through this simple Perl script:
To merge changes into the Windows Registry of an offline guest virtual machine, you must first prepare
a .REG file. There is a great deal of documentation about doing this available here. When you have
prepared a .REG file, enter the following:
262
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
The binding for each language is essentially the same, but with minor syntactic changes. A C statement:
guestfs_launch (g);
$g->launch ()
g#launch ()
In the C and C++ bindings, you must manually check for errors. In the other bindings, errors are
converted into exceptions; the additional error checks shown in the examples below are not necessary
for other languages, but conversely you may wish to add code to catch exceptions. Refer to the following
list for some points of interest regarding the architecture of the libguestfs API:
The libguestfs API is synchronous. Each call blocks until it has completed. If you want to make
calls asynchronously, you have to create a thread.
The libguestfs API is not thread safe: each handle should be used only from a single thread, or if
you want to share a handle between threads you should implement your own mutex to ensure
that two threads cannot execute commands on one handle at the same time.
You should not open multiple handles on the same disk image. It is permissible if all the handles
are read-only, but still not recommended.
You should not add a disk image for writing if anything else could be using that disk image (eg. a
live VM). Doing this will cause disk corruption.
Opening a read-only handle on a disk image which is currently in use (eg. by a live VM) is
possible; however, the results may be unpredictable or inconsistent particularly if the disk image
is being heavily written to at the time you are reading it.
#include <stdio.h>
#include <stdlib.h>
#include <guestfs.h>
int
main (int argc, char *argv[])
{
guestfs_h *g;
g = guestfs_create ();
if (g == NULL) {
perror ("failed to create libguestfs handle");
exit (EXIT_FAILURE);
}
263
Virtualization Administration Guide
/* ... */
guestfs_close (g);
exit (EXIT_SUCCESS);
}
Save this program to a file (test.c). Compile this program and run it with the following two commands:
At this stage it should print no output. The rest of this section demonstrates an example showing how to
extend this program to create a new disk image, partition it, format it with an ext4 file system, and create
some files in the file system. The disk image will be called disk.img and be created in the current
directory.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <guestfs.h>
int
main (int argc, char *argv[])
{
guestfs_h *g;
size_t i;
g = guestfs_create ();
if (g == NULL) {
perror ("failed to create libguestfs handle");
exit (EXIT_FAILURE);
}
264
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
/* Set the trace flag so that we can see each libguestfs call. */
guestfs_set_trace (g, 1);
265
Virtualization Administration Guide
exit (EXIT_FAILURE);
}
/* This uploads the local file /etc/resolv.conf into the disk image. */
if (guestfs_upload (g, "/etc/resolv.conf", "/foo/resolv.conf") == -1)
exit (EXIT_FAILURE);
/* Because 'autosync' was set (above) we can just close the handle
* and the disk contents will be synchronized. You can also do
* this manually by calling guestfs_umount_all and guestfs_sync.
*/
guestfs_close (g);
exit (EXIT_SUCCESS);
}
Compile and run this program with the following two commands:
If the program runs to completion successfully then you should be left with a disk image called
disk.img, which you can examine with guestfish:
266
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
By default (for C and C++ bindings only), libguestfs prints errors to stderr. You can change this behavior
by setting an error handler. The guestfs(3) man page discusses this in detail.
The term "sysprep" is derived from the System Preparation tool (sysprep.exe) which is used with the
Microsoft Windows systems. Despite this, the tool does not currently work on Windows guests.
NOTE
libguestfs and guestfish do not require root privileges. You only need to run them as root if
the disk image being accessed needs root access to read or write or both.
The virt-sysprep tool is part of the libguestfs-tools-c package, which is installed with the following
command:
Alternatively, just the virt-sysprep tool can be installed with the following command:
IMPORTANT
virt-sysprep modifies the guest or disk image in place. To use virt-sysprep, the
guest virtual machine must be offline, so you must shut it down before running the
commands. To preserve the existing contents of the guest virtual machine, you must
snapshot, copy or clone the disk first. Refer to libguestfs.org for more information on
copying and cloning disks.
267
Virtualization Administration Guide
-a [file] or --add [ file] Adds the specified file, which $ virt-sysprep --add
should be a disk image from a /dev/vms/disk.img
guest virtual machine. The format
of the disk image is auto-
detected. To override this and
force a particular format, use the
--format option.
-d [guest] or --domain [guest] Adds all the disks from the $ virt-sysprep --domain
specified guest virtual machine. 90df2f3f-8857-5ba9-
Domain UUIDs can be used 2714-7d95907b1c9e
instead of domain names.
--format [raw| qcow2| auto] The default for the -a option is to $ virt-sysprep --format
auto-detect the format of the disk raw -a disk.img forces raw
image. Using this forces the disk format (no auto-detection) for
format for -a options which follow disk.img, but virt-sysprep -
on the command line. Using -- -format raw -a disk.img
format auto switches back to auto- --format auto -a
detection for subsequent -a another.img forces raw format
options (see the -a command (no auto-detection) for
above). disk.img and reverts to auto-
detection for another.img . If
you have untrusted raw-format
guest disk images, you should use
this option to specify the disk
format. This avoids a possible
security problem with malicious
guests.
268
CHAPTER 16. GUEST VIRTUAL MACHINE DISK ACCESS WITH OFFLINE TOOLS
269
Virtualization Administration Guide
16.13. TROUBLESHOOTING
A test tool is available to check that libguestfs is working. Run the following command after installing
libguestfs (root access not required) to test for normal operation:
$ libguestfs-test-tool
This tool prints a large amount of text to test the operation of libguestfs. If the test is successful, the
following text will appear near the end of the output:
270
CHAPTER 17. GRAPHICAL USER INTERFACE TOOLS FOR GUEST VIRTUAL MACHINE MANAGEMENT
17.1. VIRT-VIEWER
virt-viewer is a minimalistic command-line utility for displaying the graphical console of a guest
virtual machine. The console is accessed using the VNC or SPICE protocol. The guest can be referred to
by its name, ID, or UUID. If the guest is not already running, the viewer can be set to wait until is starts
before attempting to connect to the console. The viewer can connect to remote hosts to get the console
information and then also connect to the remote console using the same network transport.
In comparison with virt-manager, virt-viewer offers a smaller set of features, but is less resource-
demanding. In addition, unlike virt-manager, virt-viewer in most cases does not require read-write
permissions to libvirt. Therefore, it can be used by non-privileged users who should be able to connect to
and display guests, but not to configure them.
Syntax
The basic virt-viewer command-line syntax is as follows:
# virt-viewer guest-name-or-UUID
To connect to a console on a remote host by using SSH, look up the guest configuration and then make
a direct non-tunneled connection to the console:
271
Virtualization Administration Guide
Interface
By default, the virt-viewer interface provides only the basic tools for interacting with the guest:
Setting hotkeys
To create a customized keyboard shortcut (also referred to as a hotkey) for the virt-viewer session, use
the --hotkeys option:
# virt-viewer --hotkeys=action1=key-combination1[,action2=key-
combination2] guest-name-or-UUID
toggle-fullscreen
release-cursor
smartcard-insert
smartcard-remove
Key-name combination hotkeys are not case-sensitive. Note that the hotkey setting does not carry over to
future virt-viewer sessions.
272
CHAPTER 17. GRAPHICAL USER INTERFACE TOOLS FOR GUEST VIRTUAL MACHINE MANAGEMENT
To add a hotkey to change to full screen mode when connecting to a KVM-QEMU guest called
testguest:
Kiosk mode
In kiosk mode, virt-viewer only allows the user to interact with the connected desktop, and does not
provide any options to interact with the guest settings or the host system unless the guest is shut down.
This can be useful for example when an administrator wants to restrict a user's range of actions to a
specified guest.
To connect to a KVM-QEMU virtual machine in kiosk mode that terminates after the machine is shut
down, use the following command:
Note, however, that kiosk mode alone cannot ensure that the user does not interact with the host system
or the guest settings after the guest is shut down. This would require further security measures, such as
disabling the window manager on the host.
17.2. REMOTE-VIEWER
The remote-viewer is a simple remote desktop display client that supports SPICE and VNC. It shares
most of the features and limitations with virt-viewer.
However, unlike virt-viewer, remote-viewer does not require libvirt to connect to the remote guest display.
As such, remote-viewer can be used for example to connect to a virtual machine on a remote host that
does not provide permissions to interact with libvirt or to use SSH connections.
Syntax
The basic remote-viewer command-line syntax is as follows:
To see the full list of options available for use with remote-viewer, use man remote-viewer.
273
Virtualization Administration Guide
To connect to a specific guest using remote-viewer, use the VNC/SPICE URI. For information about
obtaining the URI, see Section 14.5.19, “Displaying a URI for Connection to a Graphical Display”.
Use the following to connect to a SPICE server on a machine called "testguest" that uses port 5900
for SPICE communication:
# remote-viewer spice://testguest:5900
Use the following to connect to a VNC server on a machine called testguest2 that uses port 5900
for VNC communication:
# remote-viewer vnc://testguest2:5900
Interface
By default, the remote-viewer interface provides only the basic tools for interacting with the guest:
274
CHAPTER 18. VIRTUAL NETWORKING
Linux host physical machine servers represent a virtual network switch as a network interface. When the
libvirtd daemon (libvirtd) is first installed and started, the default network interface representing the
virtual network switch is virbr0.
This virbr0 interface can be viewed with the ip command like any other interface:
It is possible to use multiple physical interfaces on the hypervisor by joining them together with a bond.
The bond is then added to a bridge and then guest virtual machines are added onto the bridge as well.
However, the bonding driver has several modes of operation, and only a few of these modes work with a
bridge where virtual guest machines are in use.
275
Virtualization Administration Guide
WARNING
The only bonding modes that should be used with a guest virtual machine are Mode
1, Mode 2, and Mode 4. Under no circumstances should Modes 0, 3, 5, or 6 be
used. It should also be noted that mii-monitoring should be used to monitor bonding
modes as arp-monitoring does not work.
For more information on bonding modes, refer to the knowledgebase article on bonding modes, or The
Red Hat Enterprise Linux 6 Deployment Guide.
For a detailed explanation of bridge_opts parameters, see the Red Hat Virtualization Administration
Guide.
276
CHAPTER 18. VIRTUAL NETWORKING
Figure 18.3. Virtual network switch using NAT with two guests
WARNING
Virtual network switches use NAT configured by iptables rules. Editing these rules
while the switch is running is not recommended, as incorrect rules may result in the
switch being unable to communicate.
If the switch is not running, you can set th public IP range for forward mode NAT in order to create a port
masquerading range by running:
277
Virtualization Administration Guide
278
CHAPTER 18. VIRTUAL NETWORKING
for basic functionality such as DHCP. However, even if this network is isolated from any physical
network, DNS names are still resolved. Therefore a situation can arise when DNS names resolve but
ICMP echo request (ping) commands fail.
279
Virtualization Administration Guide
NOTE
A virtual network can be restricted to a specific physical interface. This may be useful on a
physical system that has several interfaces (for example, eth0, eth1 and eth2). This is
only useful in routed and NAT modes, and can be defined in the dev=<interface>
option, or in virt-manager when creating a new virtual network.
Deploying guest virtual machines in an existing network alongside host physical machines
making the difference between virtual and physical machines transparent to the end user.
Deploying guest virtual machines without making any changes to existing physical network
configuration settings.
Deploying guest virtual machines which must be easily accessible to an existing physical
network. Placing guest virtual machines on a physical network where they must access services
within an existing broadcast domain, such as DHCP.
Connecting guest virtual machines to an existing network where VLANs are used.
DMZ
Consider a network where one or more nodes are placed in a controlled subnetwork for security
reasons. The deployment of a special subnetwork such as this is a common practice, and the
subnetwork is known as a DMZ. Refer to the following diagram for more details on this layout:
280
CHAPTER 18. VIRTUAL NETWORKING
Host physical machines in a DMZ typically provide services to WAN (external) host physical machines
as well as LAN (internal) host physical machines. As this requires them to be accessible from multiple
locations, and considering that these locations are controlled and operated in different ways based on
their security and trust level, routed mode is the best configuration for this environment.
281
Virtualization Administration Guide
Isolated mode allows virtual machines to communicate with each other only. They are unable to interact
with the physical network.
2. This will open the Connection Details menu. Click the Virtual Networks tab.
3. All available virtual networks are listed on the left-hand box of the menu. You can edit the
configuration of a virtual network by selecting it from this box and editing as you see fit.
282
CHAPTER 18. VIRTUAL NETWORKING
1. Open the Virtual Networks tab from within the Connection Details menu. Click the Add
Network button, identified by a plus sign (+) icon. For more information, refer to Section 18.8,
“Managing a Virtual Network”.
This will open the Create a new virtual network window. Click Forward to continue.
283
Virtualization Administration Guide
2. Enter an appropriate name for your virtual network and click Forward.
284
CHAPTER 18. VIRTUAL NETWORKING
3. Enter an IPv4 address space for your virtual network and click Forward.
285
Virtualization Administration Guide
4. Define the DHCP range for your virtual network by specifying a Start and End range of IP
addresses. Click Forward to continue.
286
CHAPTER 18. VIRTUAL NETWORKING
5. Select how the virtual network should connect to the physical network.
287
Virtualization Administration Guide
6. You are now ready to create the network. Check the configuration of your network and click
Finish.
288
CHAPTER 18. VIRTUAL NETWORKING
7. The new virtual network is now available in the Virtual Networks tab of the Connection
Details window.
1. In the Virtual Machine Manager window, highlight the guest that will have the network
assigned.
289
Virtualization Administration Guide
2. From the Virtual Machine Manager Edit menu, select Virtual Machine Details.
3. Click the Add Hardware button on the Virtual Machine Details window.
290
CHAPTER 18. VIRTUAL NETWORKING
4. In the Add new virtual hardware window, select Network from the left pane, and select
your network name (network1 in this example) from the Host device menu and click Finish.
Figure 18.22. Select your network from the Add new virtual hardware window
5. The new network is now displayed as a virtual network interface that will be presented to the
guest upon launch.
291
Virtualization Administration Guide
VEPA
In virtual ethernet port aggregator (VEPA) mode, all packets from the guests are sent to the external
switch. This enables the user to force guest traffic through the switch. For VEPA mode to work
correctly, the external switch must also support hairpin mode, which ensures that packets whose
destination is a guest on the same host machine as their source guest are sent back to the host by
the external switch.
292
CHAPTER 18. VIRTUAL NETWORKING
bridge
Packets whose destination is on the same host machine as their source guest are directly delivered
to the target macvtap device. Both the source device and the destination device need to be in bridge
mode for direct delivery to succeed. If either one of the devices is in VEPA mode, a hairpin-capable
external switch is required.
private
All packets are sent to the external switch and will only be delivered to a target guest on the same
host machine if they are sent through an external router or gateway and these send them back to the
host. Private mode can be used to prevent the individual guests on the single host from
communicating with each other. This procedure is followed if either the source or destination device
is in private mode.
293
Virtualization Administration Guide
passthrough
This feature attaches a physical interface device or a SR-IOV Virtual Function (VF) directly to a guest
without losing the migration capability. All packets are sent directly to the designated network device.
Note that a single network device can only be passed through to a single guest, as a network device
cannot be shared between guests in passthrough mode.
Each of the four modes is configured by changing the domain xml file. Once this file is opened, change
the mode setting as shown:
<devices>
...
<interface type='direct'>
<source dev='eth0' mode='vepa'/>
</interface>
</devices>
The network access of direct attached guest virtual machines can be managed by the hardware switch
to which the physical interface of the host physical machine is connected to.
The interface can have additional parameters as shown below, if the switch is conforming to the IEEE
802.1Qbg standard. The parameters of the virtualport element are documented in more detail in the
IEEE 802.1Qbg standard. The values are network specific and should be provided by the network
294
CHAPTER 18. VIRTUAL NETWORKING
administrator. In 802.1Qbg terms, the Virtual Station Interface (VSI) represents the virtual interface of a
virtual machine.
Note that IEEE 802.1Qbg requires a non-zero value for the VLAN ID. Also if the switch is conforming to
the IEEE 802.1Qbh standard, the values are network specific and should be provided by the network
administrator.
managerid
The VSI Manager ID identifies the database containing the VSI type and instance definitions. This is
an integer value and the value 0 is reserved.
typeid
The VSI Type ID identifies a VSI type characterizing the network access. VSI types are typically
managed by network administrator. This is an integer value.
typeidversion
The VSI Type Version allows multiple versions of a VSI Type. This is an integer value.
instanceid
The VSI Instance ID Identifier is generated when a VSI instance (that is a virtual interface of a virtual
machine) is created. This is a globally unique identifier.
profileid
The profile ID contains the name of the port profile that is to be applied onto this interface. This name
is resolved by the port profile database into the network parameters from the port profile, and those
network parameters will be applied to this interface.
Each of the four types is configured by changing the domain xml file. Once this file is opened, change the
mode setting as shown:
<devices>
...
<interface type='direct'>
<source dev='eth0.2' mode='vepa'/>
<virtualport type="802.1Qbg">
<parameters managerid="11" typeid="1193047" typeidversion="2"
instanceid="09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f"/>
</virtualport>
</interface>
</devices>
<devices>
...
<interface type='direct'>
<source dev='eth0' mode='private'/>
<virtualport type='802.1Qbh'>
<parameters profileid='finance'/>
</virtualport>
295
Virtualization Administration Guide
</interface>
</devices>
...
18.12.1. Introduction
The goal of the network filtering, is to enable administrators of a virtualized system to configure and
enforce network traffic filtering rules on virtual machines and manage the parameters of network traffic
that virtual machines are allowed to send or receive. The network traffic filtering rules are applied on the
host physical machine when a virtual machine is started. Since the filtering rules cannot be circumvented
from within the virtual machine, it makes them mandatory from the point of view of a virtual machine user.
From the point of view of the guest virtual machine, the network filtering system allows each virtual
machine's network traffic filtering rules to be configured individually on a per interface basis. These rules
are applied on the host physical machine when the virtual machine is started and can be modified while
the virtual machine is running. The latter can be achieved by modifying the XML description of a network
filter.
Multiple virtual machines can make use of the same generic network filter. When such a filter is
modified, the network traffic filtering rules of all running virtual machines that reference this filter are
updated. The machines that are not running will update on start.
As previously mentioned, applying network traffic filtering rules can be done on individual network
interfaces that are configured for certain types of network configurations. Supported network types
include:
network
bridge
The interface XML is used to reference a top-level filter. In the following example, the interface
description references the filter clean-traffic.
<devices>
<interface type='bridge'>
<mac address='00:16:3e:5d:c7:9e'/>
<filterref filter='clean-traffic'/>
</interface>
</devices>
Network filters are written in XML and may either contain: references to other filters, rules for traffic
filtering, or hold a combination of both. The above referenced filter clean-traffic is a filter that only
contains references to other filters and no actual filtering rules. Since references to other filters can be
used, a tree of filters can be built. The clean-traffic filter can be viewed using the command: # virsh
nwfilter-dumpxml clean-traffic.
As previously mentioned, a single network filter can be referenced by multiple virtual machines. Since
296
CHAPTER 18. VIRTUAL NETWORKING
interfaces will typically have individual parameters associated with their respective traffic filtering
rules, the rules described in a filter's XML can be generalized using variables. In this case, the
variable name is used in the filter XML and the name and value are provided at the place where the
filter is referenced.
In the following example, the interface description has been extended with the parameter IP and a
dotted IP address as a value.
<devices>
<interface type='bridge'>
<mac address='00:16:3e:5d:c7:9e'/>
<filterref filter='clean-traffic'>
<parameter name='IP' value='10.0.0.1'/>
</filterref>
</interface>
</devices>
In this particular example, the clean-traffic network traffic filter will be represented with the IP address
parameter 10.0.0.1 and as per the rule dictates that all traffic from this interface will always be using
10.0.0.1 as the source IP address, which is one of the purpose of this particular filter.
Packets start their filter evaluation in the root chain and can then continue their evaluation in other
chains, return from those chains back into the root chain or be dropped or accepted by a filtering rule in
one of the traversed chains.
Libvirt's network filtering system automatically creates individual root chains for every virtual machine's
network interface on which the user chooses to activate traffic filtering. The user may write filtering rules
that are either directly instantiated in the root chain or may create protocol-specific filtering chains for
efficient evaluation of protocol-specific rules.
root
mac
vlan
ipv4
ipv6
297
Virtualization Administration Guide
Multiple chains evaluating the mac, stp, vlan, arp, rarp, ipv4, or ipv6 protocol can be created using the
protocol name only as a prefix in the chain's name.
This example allows chains with names arp-xyz or arp-test to be specified and have their ARP
protocol packets evaluated in those chains.
The following filter XML shows an example of filtering ARP traffic in the arp chain.
The consequence of putting ARP-specific rules in the arp chain, rather than for example in the root
chain, is that packets protocols other than ARP do not need to be evaluated by ARP protocol-specific
rules. This improves the efficiency of the traffic filtering. However, one must then pay attention to only
putting filtering rules for the given protocol into the chain since other rules will not be evaluated. For
example, an IPv4 rule will not be evaluated in the ARP chain since IPv4 protocol packets will not
traverse the ARP chain.
298
CHAPTER 18. VIRTUAL NETWORKING
stp -810
mac -800
vlan -750
ipv4 -700
ipv6 -600
arp -500
rarp -400
NOTE
A chain with a lower priority value is accessed before one with a higher value.
The chains listed in Table 18.1, “Filtering chain default priorities values” can be also be
assigned custom priorities by writing a value in the range [-1000 to 1000] into the priority
(XML) attribute in the filter node. Section 18.12.2, “Filtering Chains”filter shows the default
priority of -500 for arp chains, for example.
MAC is designated for the MAC address of the network interface. A filtering rule that references this
variable will automatically be replaced with the MAC address of the interface. This works without the user
having to explicitly provide the MAC parameter. Even though it is possible to specify the MAC parameter
similar to the IP parameter above, it is discouraged since libvirt knows what MAC address an interface
will be using.
The parameter IP represents the IP address that the operating system inside the virtual machine is
expected to use on the given interface. The IP parameter is special in so far as the libvirt daemon will try
to determine the IP address (and thus the IP parameter's value) that is being used on an interface if the
parameter is not explicitly provided but referenced. For current limitations on IP address detection,
consult the section on limitations Section 18.12.12, “Limitations” on how to use this feature and what to
expect when using it. The XML file shown in Section 18.12.2, “Filtering Chains” contains the filter no-
arp-spoofing, which is an example of using a network filter XML to reference the MAC and IP
variables.
Note that referenced variables are always prefixed with the character $. The format of the value of a
variable must be of the type expected by the filter attribute identified in the XML. In the above example,
the IP parameter must hold a legal IP address in standard format. Failure to provide the correct structure
will result in the filter variable not being replaced with a value and will prevent a virtual machine from
299
Virtualization Administration Guide
starting or will prevent an interface from attaching when hot plugging is being used. Some of the types
that are expected for each XML attribute are shown in the example Example 18.4, “Sample variable
types”.
As variables can contain lists of elements, (the variable IP can contain multiple IP addresses that are
valid on a particular interface, for example), the notation for providing multiple elements for the IP
variable is:
<devices>
<interface type='bridge'>
<mac address='00:16:3e:5d:c7:9e'/>
<filterref filter='clean-traffic'>
<parameter name='IP' value='10.0.0.1'/>
<parameter name='IP' value='10.0.0.2'/>
<parameter name='IP' value='10.0.0.3'/>
</filterref>
</interface>
</devices>
This XML file creates filters to enable multiple IP addresses per interface. Each of the IP addresses
will result in a separate filtering rule. Therefore using the XML above and the following rule, three
individual filtering rules (one for each IP address) will be created:
As it is possible to access individual elements of a variable holding a list of elements, a filtering rule
like the following accesses the 2nd element of the variable DSTPORTS.
As it is possible to create filtering rules that represent all possible combinations of rules from different
lists using the notation $VARIABLE[@<iterator id="x">]. The following rule allows a virtual
machine to receive traffic on a set of ports, which are specified in DSTPORTS, from the set of source
IP address specified in SRCIPADDRESSES. The rule generates all combinations of elements of the
variable DSTPORTS with those of SRCIPADDRESSES by using two independent iterators to access
their elements.
300
CHAPTER 18. VIRTUAL NETWORKING
Assigning values to the variables using $SRCIPADDRESSES[@1] and $DSTPORTS[@2] would then
result in all combinations of addresses and ports being created as shown:
10.0.0.1, 80
10.0.0.1, 8080
11.1.2.3, 80
11.1.2.3, 8080
Accessing the same variables using a single iterator, for example by using the notation
$SRCIPADDRESSES[@1] and $DSTPORTS[@1], would result in parallel access to both lists and
result in the following combination:
10.0.0.1, 80
11.1.2.3, 8080
NOTE
$VARIABLE is short-hand for $VARIABLE[@0]. The former notation always assumes the
role of iterator with iterator id="0" added as shown in the opening paragraph at the
top of this section.
18.12.5.1. Introduction
The detection of IP addresses used on a virtual machine's interface is automatically activated if the
variable IP is referenced but no value has been assigned to it. The variable CTRL_IP_LEARNING can be
used to specify the IP address learning method to use. Valid values include: any, dhcp, or none.
The value any instructs libvirt to use any packet to determine the address in use by a virtual machine,
which is the default setting if the variable TRL_IP_LEARNING is not set. This method will only detect a
single IP address per interface. Once a guest virtual machine's IP address has been detected, its IP
network traffic will be locked to that address, if for example, IP address spoofing is prevented by one of
its filters. In that case, the user of the VM will not be able to change the IP address on the interface inside
the guest virtual machine, which would be considered IP address spoofing. When a guest virtual
machine is migrated to another host physical machine or resumed after a suspend operation, the first
packet sent by the guest virtual machine will again determine the IP address that the guest virtual
machine can use on a particular interface.
The value of dhcp instructs libvirt to only honor DHCP server-assigned addresses with valid leases. This
method supports the detection and usage of multiple IP address per interface. When a guest virtual
machine resumes after a suspend operation, any valid IP address leases are applied to its filters.
Otherwise the guest virtual machine is expected to use DHCP to obtain a new IP addresses. When a
guest virtual machine migrates to another physical host physical machine, the guest virtual machine is
required to re-run the DHCP protocol.
301
Virtualization Administration Guide
If CTRL_IP_LEARNING is set to none, libvirt does not do IP address learning and referencing IP without
assigning it an explicit value is an error.
When DHCP snooping is enabled and the DHCP lease expires, the guest virtual machine will no longer
be able to use the IP address until it acquires a new, valid lease from a DHCP server. If the guest virtual
machine is migrated, it must get a new valid DHCP lease to use an IP address (for example, by bringing
the VM interface down and up again).
NOTE
Automatic DHCP detection listens to the DHCP traffic the guest virtual machine
exchanges with the DHCP server of the infrastructure. To avoid denial-of-service attacks
on libvirt, the evaluation of those packets is rate-limited, meaning that a guest virtual
machine sending an excessive number of DHCP packets per second on an interface will
not have all of those packets evaluated and thus filters may not get adapted. Normal
DHCP client behavior is assumed to send a low number of DHCP packets per second.
Further, it is important to setup appropriate filters on all guest virtual machines in the
infrastructure to avoid them being able to send DHCP packets. Therefore guest virtual
machines must either be prevented from sending UDP and TCP traffic from port 67 to port
68 or the DHCPSERVER variable should be used on all guest virtual machines to restrict
DHCP server messages to only be allowed to originate from trusted DHCP servers. At the
same time anti-spoofing prevention must be enabled on all guest virtual machines in the
subnet.
The following XML provides an example for the activation of IP address learning using the DHCP
snooping method:
<interface type='bridge'>
<source bridge='virbr0'/>
<filterref filter='clean-traffic'>
<parameter name='CTRL_IP_LEARNING' value='dhcp'/>
</filterref>
</interface>
302
CHAPTER 18. VIRTUAL NETWORKING
The following shows the XML of the clean-traffic network filter referencing several other filters.
<filter name='clean-traffic'>
<uuid>6ef53069-ba34-94a0-d33d-17751b9b8cb1</uuid>
<filterref filter='no-mac-spoofing'/>
<filterref filter='no-ip-spoofing'/>
<filterref filter='allow-incoming-ipv4'/>
<filterref filter='no-arp-spoofing'/>
<filterref filter='no-other-l2-traffic'/>
<filterref filter='qemu-announce-self'/>
</filter>
To reference another filter, the XML node filterref needs to be provided inside a filter node. This node
must have the attribute filter whose value contains the name of the filter to be referenced.
New network filters can be defined at any time and may contain references to network filters that are not
known to libvirt, yet. However, once a virtual machine is started or a network interface referencing a filter
is to be hotplugged, all network filters in the filter tree must be available. Otherwise the virtual machine
will not start or the network interface cannot be attached.
303
Virtualization Administration Guide
The traffic filtering rule starts with the rule node. This node may contain up to three of the following
attributes:
drop (matching the rule silently discards the packet with no further analysis)
reject (matching the rule generates an ICMP reject message with no further analysis)
accept (matching the rule accepts the packet with no further analysis)
return (matching the rule passes this filter, but returns control to the calling filter for further
analysis)
continue (matching the rule goes on to the next rule for further analysis)
priority is optional. The priority of the rule controls the order in which the rule will be instantiated
relative to other rules. Rules with lower values will be instantiated before rules with higher
values. Valid values are in the range of -1000 to 1000. If this attribute is not provided, priority 500
will be assigned by default. Note that filtering rules in the root chain are sorted with filters
connected to the root chain following their priorities. This allows to interleave filtering rules with
access to filter chains. Refer to Section 18.12.3, “Filtering Chain Priorities” for more information.
statematch is optional. Possible values are '0' or 'false' to turn the underlying connection state
matching off. The default setting is 'true' or 1
For more information see Section 18.12.11, “Advanced Filter Configuration Topics”.
The above example Example 18.7, “An Example of a clean traffic filter” indicates that the traffic of type ip
will be associated with the chain ipv4 and the rule will have priority=500. If for example another filter
is referenced whose traffic of type ip is also associated with the chain ipv4 then that filter's rules will be
ordered relative to the priority=500 of the shown rule.
304
CHAPTER 18. VIRTUAL NETWORKING
A rule may contain a single rule for filtering of traffic. The above example shows that traffic of type ip is to
be filtered.
IP_MASK: IP address mask in either dotted decimal format (255.255.248.0) or CIDR mask (0-
32)
STRING: A string
IPSETFLAGS: The source and destination flags of the ipset described by up to 6 'src' or 'dst'
elements selecting features from either the source or destination part of the packet header;
example: src,src,dst. The number of 'selectors' to provide here depends on the type of ipset that
is referenced
Every attribute except for those of type IP_MASK or IPV6_MASK can be negated using the match
attribute with value no. Multiple negated attributes may be grouped together. The following XML
fragment shows such an example using abstract attributes.
[...]
<rule action='drop' direction='in'>
<protocol match='no' attribute1='value1' attribute2='value2'/>
<protocol attribute3='value3'/>
</rule>
[...]
Rules behave evaluate the rule as well as look at it logically within the boundaries of the given protocol
attributes. Thus, if a single attribute's value does not match the one given in the rule, the whole rule will
be skipped during the evaluation process. Therefore, in the above example incoming traffic will only be
dropped if: the protocol property attribute1 does not match both value1 and the protocol property
attribute2 does not match value2 and the protocol property attribute3 matches value3.
305
Virtualization Administration Guide
[...]
<mac match='no' srcmacaddr='$MAC'/>
[...]
Rules of this type should go either into the root or vlan chain.
306
CHAPTER 18. VIRTUAL NETWORKING
Rules of this type should go either into the root or stp chain.
307
Virtualization Administration Guide
18.12.10.4. ARP/RARP
Rules of this type should either go into the root or arp/rarp chain.
308
CHAPTER 18. VIRTUAL NETWORKING
18.12.10.5. IPv4
Protocol ID: ip
Rules of this type should either go into the root or ipv4 chain.
309
Virtualization Administration Guide
18.12.10.6. IPv6
Rules of this type should either go into the root or ipv6 chain.
310
CHAPTER 18. VIRTUAL NETWORKING
18.12.10.7. TCP/UDP/SCTP
The chain parameter is ignored for this type of traffic and should either be omitted or set to root. .
311
Virtualization Administration Guide
18.12.10.8. ICMP
Note: The chain parameter is ignored for this type of traffic and should either be omitted or set to root.
312
CHAPTER 18. VIRTUAL NETWORKING
The chain parameter is ignored for this type of traffic and should either be omitted or set to root.
313
Virtualization Administration Guide
The chain parameter is ignored for this type of traffic and should either be omitted or set to root.
314
CHAPTER 18. VIRTUAL NETWORKING
18.12.10.11. ICMPv6
315
Virtualization Administration Guide
The chain parameter is ignored for this type of traffic and should either be omitted or set to root.
The chain parameter is ignored for this type of traffic and should either be omitted or set to root.
316
CHAPTER 18. VIRTUAL NETWORKING
Table 18.14. IGMP, ESP, AH, UDPLITE, 'ALL' over IPv protocol types
The network filtering subsystem (on Linux) makes use of the connection tracking support of IP tables.
This helps in enforcing the directionality of network traffic (state match) as well as counting and limiting
the number of simultaneous connections towards a guest virtual machine. As an example, if a guest
virtual machine has TCP port 8080 open as a server, clients may connect to the guest virtual machine on
port 8080. Connection tracking and enforcement of directionality then prevents the guest virtual machine
from initiating a connection from (TCP client) port 8080 to the host physical machine back to a remote
host physical machine. More importantly, tracking helps to prevent remote attackers from establishing a
317
Virtualization Administration Guide
connection back to a guest virtual machine. For example, if the user inside the guest virtual machine
established a connection to port 80 on an attacker site, then the attacker will not be able to initiate a
connection from TCP port 80 back towards the guest virtual machine. By default the connection state
match that enables connection tracking and then enforcement of directionality of traffic is turned on.
Example 18.9. XML example for turning off connections to the TCP port
The following shows an example XML fragment where this feature has been turned off for incoming
connections to TCP port 12345.
[...]
<rule direction='in' action='accept' statematch='false'>
<cp dstportstart='12345'/>
</rule>
[...]
This now allows incoming traffic to TCP port 12345, but would also enable the initiation from (client)
TCP port 12345 within the VM, which may or may not be desirable.
To limit the number of connections a guest virtual machine may establish, a rule must be provided that
sets a limit of connections for a given type of traffic. If for example a VM is supposed to be allowed to
only ping one other IP address at a time and is supposed to have only one active incoming ssh
connection at a time.
[...]
<rule action='drop' direction='in' priority='400'>
<tcp connlimit-above='1'/>
</rule>
<rule action='accept' direction='in' priority='500'>
<tcp dstportstart='22'/>
</rule>
<rule action='drop' direction='out' priority='400'>
<icmp connlimit-above='1'/>
</rule>
<rule action='accept' direction='out' priority='500'>
<icmp/>
</rule>
<rule action='accept' direction='out' priority='500'>
<udp dstportstart='53'/>
</rule>
<rule action='drop' direction='inout' priority='1000'>
<all/>
</rule>
[...]
318
CHAPTER 18. VIRTUAL NETWORKING
NOTE
Limitation rules must be listed in the XML prior to the rules for accepting traffic. According
to the XML file in Example 18.10, “XML sample file that sets limits to connections”, an
additional rule for allowing DNS traffic sent to port 22 go out the guest virtual machine,
has been added to avoid ssh sessions not getting established for reasons related to DNS
lookup failures by the ssh daemon. Leaving this rule out may result in the ssh client
hanging unexpectedly as it tries to connect. Additional caution should be used in regards
to handling timeouts related to tracking of traffic. An ICMP ping that the user may have
terminated inside the guest virtual machine may have a long timeout in the host physical
machine's connection tracking system and will therefore not allow another ICMP ping to
go through.
The best solution is to tune the timeout in the host physical machine's sysfs with the
following command:# echo 3 >
/proc/sys/net/netfilter/nf_conntrack_icmp_timeout. This command sets
the ICMP connection tracking timeout to 3 seconds. The effect of this is that once one
ping is terminated, another one can start after 3 seconds.
If for any reason the guest virtual machine has not properly closed its TCP connection,
the connection to be held open for a longer period of time, especially if the TCP timeout
value was set for a large amount of time on the host physical machine. In addition, any
idle connection may result in a time out in the connection tracking system which can be
re-activated once packets are exchanged.
However, if the limit is set too low, newly initiated connections may force an idle
connection into TCP backoff. Therefore, the limit of connections should be set rather high
so that fluctuations in new TCP connections do not cause odd traffic behavior in relation
to idle connections.
virsh has been extended with life-cycle support for network filters. All commands related to the network
filtering subsystem start with the prefix nwfilter. The following commands are available:
nwfilter-define : defines a new network filter or updates an existing one (must supply a
name)
nwfilter-undefine : deletes a specified network filter (must supply a name). Do not delete a
network filter currently in use.
The following is a list of example network filters that are automatically installed with libvirt:
319
Virtualization Administration Guide
These filters are only building blocks and require a combination with other filters to provide useful
network traffic filtering. The most used one in the above list is the clean-traffic filter. This filter itself can
for example be combined with the no-ip-multicast filter to prevent virtual machines from sending IP
multicast traffic on top of the prevention of packet spoofing.
Since libvirt only provides a couple of example networking filters, you may consider writing your own.
When planning on doing so there are a couple of things you may need to know regarding the network
filtering subsystem and how it works internally. Certainly you also have to know and understand the
protocols very well that you want to be filtering on so that no further traffic than what you want can pass
and that in fact the traffic you want to allow does pass.
The network filtering subsystem is currently only available on Linux host physical machines and only
works for Qemu and KVM type of virtual machines. On Linux, it builds upon the support for ebtables,
iptables and ip6tables and makes use of their features. Considering the list found in Section 18.12.10,
“Supported Protocols” the following protocols can be implemented using ebtables:
mac
vlan (802.1Q)
arp, rarp
320
CHAPTER 18. VIRTUAL NETWORKING
ipv4
ipv6
Any protocol that runs over IPv4 is supported using iptables, those over IPv6 are implemented using
ip6tables.
Using a Linux host physical machine, all traffic filtering rules created by libvirt's network filtering
subsystem first passes through the filtering support implemented by ebtables and only afterwards
through iptables or ip6tables filters. If a filter tree has rules with the protocols including: mac, stp, vlan
arp, rarp, ipv4, or ipv6; the ebtable rules and values listed will automatically be used first.
Multiple chains for the same protocol can be created. The name of the chain must have a prefix of one of
the previously enumerated protocols. To create an additional chain for handling of ARP traffic, a chain
with name arp-test, can for example be specified.
As an example, it is possible to filter on UDP traffic by source and destination ports using the IP protocol
filter and specifying attributes for the protocol, source and destination IP addresses and ports of UDP
packets that are to be accepted. This allows early filtering of UDP traffic with ebtables. However, once an
IP or IPv6 packet, such as a UDP packet, has passed the ebtables layer and there is at least one rule in
a filter tree that instantiates iptables or ip6tables rules, a rule to let the UDP packet pass will also be
necessary to be provided for those filtering layers. This can be achieved with a rule containing an
appropriate udp or udp-ipv6 traffic filtering node.
allows the VM to send ping traffic from an interface but not let the VM be pinged on the
interface
The requirement to prevent spoofing is fulfilled by the existing clean-traffic network filter, thus
the way to do this is to reference it from a custom filter.
To enable traffic for TCP ports 22 and 80, two rules are added to enable this type of traffic. To allow
the guest virtual machine to send ping traffic a rule is added for ICMP traffic. For simplicity reasons,
general ICMP traffic will be allowed to be initiated from the guest virtual machine, and will not be
specified to ICMP echo request and response messages. All other traffic will be prevented to reach or
be initiated by the guest virtual machine. To do this a rule will be added that drops all other traffic.
Assuming the guest virtual machine is called test and the interface to associate our filter with is
called eth0, a filter is created named test-eth0.
<filter name='test-eth0'>
<!- - This rule references the clean traffic filter to prevent MAC, IP
and ARP spoofing. By not providing an IP address parameter, libvirt will
detect the IP address the guest virtual machine is using. - ->
<filterref filter='clean-traffic'/>
321
Virtualization Administration Guide
reachable - ->
<rule action='accept' direction='in'>
<tcp dstportstart='22'/>
</rule>
<!- - This rule enables outgoing DNS lookups using UDP - ->
<rule action='accept' direction='out'>
<udp dstportstart='53'/>
</rule>
</filter>
Although one of the rules in the above XML contains the IP address of the guest virtual machine as
either a source or a destination address, the filtering of the traffic works correctly. The reason is that
whereas the rule's evaluation occurs internally on a per-interface basis, the rules are additionally
evaluated based on which (tap) interface has sent or will receive the packet, rather than what their
source or destination IP address may be.
An XML fragment for a possible network interface description inside the domain XML of the test guest
virtual machine could then look like this:
[...]
<interface type='bridge'>
<source bridge='mybridge'/>
<filterref filter='test-eth0'/>
</interface>
[...]
To more strictly control the ICMP traffic and enforce that only ICMP echo requests can be sent from
the guest virtual machine and only ICMP echo responses be received by the guest virtual machine,
the above ICMP rule can be replaced with the following two rules:
322
CHAPTER 18. VIRTUAL NETWORKING
</rule>
This example demonstrates how to build a similar filter as in the example above, but extends the list
of requirements with an ftp server located inside the guest virtual machine. The requirements for this
filter are:
prevents a guest virtual machine's interface from MAC, IP, and ARP spoofing
allows the guest virtual machine to send ping traffic from an interface but does not allow the
guest virtual machine to be pinged on the interface
allows the guest virtual machine to do DNS lookups (UDP towards port 53)
enables the ftp server (in active mode) so it can run inside the guest virtual machine
The additional requirement of allowing an FTP server to be run inside the guest virtual machine maps
into the requirement of allowing port 21 to be reachable for FTP control traffic as well as enabling the
guest virtual machine to establish an outgoing TCP connection originating from the guest virtual
machine's TCP port 20 back to the FTP client (FTP active mode). There are several ways of how this
filter can be written and two possible solutions are included in this example.
The first solution makes use of the state attribute of the TCP protocol that provides a hook into the
connection tracking framework of the Linux host physical machine. For the guest virtual machine-
initiated FTP data connection (FTP active mode) the RELATED state is used to enable detection that
the guest virtual machine-initiated FTP data connection is a consequence of ( or 'has a relationship
with' ) an existing FTP control connection, thereby allowing it to pass packets through the firewall.
The RELATED state, however, is only valid for the very first packet of the outgoing TCP connection
for the FTP data path. Afterwards, the state is ESTABLISHED, which then applies equally to the
incoming and outgoing direction. All this is related to the FTP data traffic originating from TCP port 20
of the guest virtual machine. This then leads to the following solution:
<filter name='test-eth0'>
<!- - This filter (eth0) references the clean traffic filter to
prevent MAC, IP, and ARP spoofing. By not providing an IP address
parameter, libvirt will detect the IP address the guest virtual machine
is using. - ->
<filterref filter='clean-traffic'/>
<!- - This rule enables TCP port 20 for guest virtual machine-
initiated FTP data connection related to an existing FTP control
323
Virtualization Administration Guide
connection - ->
<rule action='accept' direction='out'>
<tcp srcportstart='20' state='RELATED,ESTABLISHED'/>
</rule>
<!- - This rule accepts all packets from a client on the FTP data
connection - ->
<rule action='accept' direction='in'>
<tcp dstportstart='20' state='ESTABLISHED'/>
</rule>
<!- - This rule enables outgoing DNS lookups using UDP - ->
<rule action='accept' direction='out'>
<udp dstportstart='53'/>
</rule>
</filter>
Before trying out a filter using the RELATED state, you have to make sure that the appropriate
connection tracking module has been loaded into the host physical machine's kernel. Depending on
the version of the kernel, you must run either one of the following two commands before the FTP
connection with the guest virtual machine is established:
If protocols other than FTP are used in conjunction with the RELATED state, their corresponding
module must be loaded. Modules are available for the protocols: ftp, tftp, irc, sip, sctp, and amanda.
The second solution makes use of the state flags of connections more than the previous solution did.
This solution takes advantage of the fact that the NEW state of a connection is valid when the very
first packet of a traffic flow is detected. Subsequently, if the very first packet of a flow is accepted, the
flow becomes a connection and thus enters into the ESTABLISHED state. Therefore a general rule
can be written for allowing packets of ESTABLISHED connections to reach the guest virtual machine
324
CHAPTER 18. VIRTUAL NETWORKING
or be sent by the guest virtual machine. This is done writing specific rules for the very first packets
identified by the NEW state and dictates the ports that the data is acceptable. All packets meant for
ports that are not explicitly accepted are dropped, thus not reaching an ESTABLISHED state. Any
subsequent packets sent from that port are dropped as well.
<filter name='test-eth0'>
<!- - This filter references the clean traffic filter to prevent MAC,
IP and ARP spoofing. By not providing and IP address parameter, libvirt
will detect the IP address the VM is using. - ->
<filterref filter='clean-traffic'/>
<!- - This rule allows the packets of all previously accepted and
related connections be sent from the guest virtual machine - ->
<rule action='accept' direction='out'>
<all state='ESTABLISHED,RELATED'/>
</rule>
<!- - This rule enables traffic towards port 21 (FTP) and port 22
(SSH)- ->
<rule action='accept' direction='in'>
<tcp dstportstart='21' dstportend='22' state='NEW'/>
</rule>
<!- - This rule enables outgoing DNS lookups using UDP - ->
<rule action='accept' direction='out'>
<udp dstportstart='53' state='NEW'/>
</rule>
</filter>
18.12.12. Limitations
325
Virtualization Administration Guide
The following is a list of the currently known limitations of the network filtering subsystem.
VM migration is only supported if the whole filter tree that is referenced by a guest virtual
machine's top level filter is also available on the target host physical machine. The network filter
clean-traffic for example should be available on all libvirt installations and thus enable
migration of guest virtual machines that reference this filter. To assure version compatibility is not
a problem make sure you are using the most current version of libvirt by updating the package
regularly.
Migration must occur between libvirt installations of version 0.8.1 or later in order not to lose the
network traffic filters associated with an interface.
VLAN (802.1Q) packets, if sent by a guest virtual machine, cannot be filtered with rules for
protocol IDs arp, rarp, ipv4 and ipv6. They can only be filtered with protocol IDs, MAC and
VLAN. Therefore, the example filter clean-traffic Example 18.1, “An example of network filtering”
will not work as expected.
To create a multicast tunnel, specify the following XML details into the <devices> element:
...
<devices>
<interface type='mcast'>
<mac address='52:54:00:6d:90:01'>
<source address='230.0.0.1' port='5558'/>
</interface>
</devices>
...
326
CHAPTER 18. VIRTUAL NETWORKING
default DNS or DHCP support nor does it provide outgoing network access. To provide outgoing network
access, one of the guest virtual machines should have a second NIC which is connected to one of the
first four network types thus providing appropriate routing.
To create a TCP tunnel place the following XML details into the <devices> element:
...
<devices>
<interface type='server'>
<mac address='52:54:00:22:c9:42'>
<source address='192.168.0.1' port='5558'/>
</interface>
...
<interface type='client'>
<mac address='52:54:00:8b:c9:51'>
<source address='192.168.0.1' port='5558'/>
</interface>
</devices>
...
<network>
<name>ovs-net</name>
<forward mode='bridge'/>
<bridge name='ovsbr0'/>
<virtualport type='openvswitch'>
<parameters interfaceid='09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f'/>
</virtualport>
<vlan trunk='yes'>
<tag id='42' nativeMode='untagged'/>
<tag id='47'/>
</vlan>
<portgroup name='dontpanic'>
<vlan>
<tag id='42'/>
</vlan>
</portgroup>
</network>
Figure 18.30. vSetting VLAN tag (on supported network types only)
If (and only if) the network type supports vlan tagging transparent to the guest, an optional <vlan>
element can specify one or more vlan tags to apply to the traffic of all guests using this network.
327
Virtualization Administration Guide
(openvswitch and type='hostdev' SR-IOV networks do support transparent VLAN tagging of guest traffic;
everything else, including standard linux bridges and libvirt's own virtual networks, do not support it.
802.1Qbh (vn-link) and 802.1Qbg (VEPA) switches provide their own way (outside of libvirt) to tag guest
traffic onto specific vlans.) As expected, the tag attribute specifies which vlan tag to use. If a network has
more than one <vlan> element defined, it is assumed that the user wants to do VLAN trunking using all
the specified tags. In the case that VLAN trunking with a single tag is desired, the optional attribute
trunk='yes' can be added to the VLAN element.
For network connections using openvswitch it is possible to configure the 'native-tagged' and 'native-
untagged' VLAN modes. This uses the optional nativeMode attribute on the <tag> element: nativeMode
may be set to 'tagged' or 'untagged'. The id attribute of the element sets the native vlan.
<vlan> elements can also be specified in a <portgroup> element, as well as directly in a domain's
<interface> element. In the case that a vlan tag is specified in multiple locations, the setting in
<interface> takes precedence, followed by the setting in the <portgroup> selected by the interface
config. The <vlan> in <network> will be selected only if none is given in <portgroup> or
<interface>.
328
CHAPTER 19. QEMU-KVM COMMANDS, FLAGS, AND ARGUMENTS
19.1. INTRODUCTION
NOTE
The primary objective of this chapter is to provide a list of the qemu-kvm utility
commands, flags, and arguments that are used as an emulator and a hypervisor in Red
Hat Enterprise Linux 6. This is a comprehensive summary of the options that are known to
work but are to be used at your own risk. Red Hat Enterprise Linux 6 uses KVM as an
underlying virtualization technology. The machine emulator and hypervisor used is a
modified version of QEMU called qemu-kvm. This version does not support all
configuration options of the original QEMU and it also adds some additional options.
Whitelist Format
<name> - When used in a syntax description, this string should be replaced by user-defined
value.
[a|b|c] - When used in a syntax description, only one of the strings separated by | is used.
Emulated Machine
-M <machine-type>
-machine <machine-type>[,<property>[=<value>][,..]]
Processor Type
-cpu <model>[,<FEATURE>][...]
329
Virtualization Administration Guide
cpu64-rhel5 - Red Hat Enterprise Linux 5 supported QEMU Virtual CPU version
cpu64-rhel6 - Red Hat Enterprise Linux 6 supported QEMU Virtual CPU version
Processor Topology
-smp <n>[,cores=<ncores>][,threads=<nthreads>][,sockets=<nsocks>][,maxcpus=<maxcpus>]
NUMA System
-numa <nodes>[,mem=<size>][,cpus=<cpu[-cpu>]][,nodeid=<node>]
Memory Size
-m <megs>
Supported values are limited by guest minimal and maximal values and hypervisor limits.
Keyboard Layout
-k <language>
Guest Name
-name <name>
Guest UUID
-uuid <uuid>
Generic Drive
-drive <option>[,<option>[,<option>[,...]]]
readonly[on|off]
werror[enospc|report|stop|ignore]
rerror[report|stop|ignore]
id=<id>
330
CHAPTER 19. QEMU-KVM COMMANDS, FLAGS, AND ARGUMENTS
file=<file>
Passing cd-rom device as <file> is supported only with cdrom media type (media=cdrom)
and only as IDE drive (either if=ide or if=none + -device ide-drive).
If <file> is neither block nor character device, it must not contain ':'.
if=<interface>
index=<index>
media=<media>
cache=<cache>
copy-on-read=[on|off]
snapshot=[yes|no]
serial=<serial>
aio=<aio>
format=<format>
This option is not required and can be omitted. However, this is not recommended for raw
images because it represents security risk. Supported formats are:
qcow2
raw
Boot Option
-boot [order=<drives>][,menu=[on|off]]
Snapshot Mode
-snapshot
331
Virtualization Administration Guide
Disable Graphics
-nographic
Supported types:
VNC Display
-vnc <display>[,<option>[,<option>[,...]]]
[<host>]:<port>
unix:<path>
share[allow-exclusive|force-shared|ignore]
to=<port>
reverse
password
tls
sasl
acl
Spice Desktop
-spice option[,option[,...]]
port=<number>
addr=<addr>
ipv4
332
CHAPTER 19. QEMU-KVM COMMANDS, FLAGS, AND ARGUMENTS
ipv6
password=<secret>
disable-ticketing
disable-copy-paste
tls-port=<number>
x509-dir=</path/to/certificate/dir>
x509-key-file=<file>
x509-key-password=<file>
x509-cert-file=<file>
x509-cacert-file=<file>
x509-dh-key-file=<file>
tls-cipher=<list>
tls-channel[main|display|cursor|inputs|record|playback]
plaintext-channel[main|display|cursor|inputs|record|playback]
image-compression=<compress>
jpeg-wan-compression=<value>
zlib-glz-wan-compression=<value>
streaming-video=[off|all|filter]
agent-mouse=[on|off]
playback-compression=[on|off]
seamless-migratio=[on|off]
TAP network
-netdev tap,id=<id>][,<options>...]
ifname
fd
script
333
Virtualization Administration Guide
downscript
sndbuf
vnet_hdr
vhost
vhostfd
vhostforce
General Device
-device <driver>[,<prop>[=<value>][,...]]
id
bus
pci-assign
host
bootindex
configfd
addr
rombar
romfile
multifunction
If the device has multiple functions, all of them need to be assigned to the same guest.
rtl8139
mac
netdev
bootindex
addr
e1000
334
CHAPTER 19. QEMU-KVM COMMANDS, FLAGS, AND ARGUMENTS
mac
netdev
bootindex
addr
virtio-net-pci
ioeventfd
vectors
indirect
event_idx
csum
guest_csum
gso
guest_tso4
guest_tso6
guest_ecn
guest_ufo
host_tso4
host_tso6
host_ecn
host_ufo
mrg_rxbuf
status
ctrl_vq
ctrl_rx
ctrl_vlan
ctrl_rx_extra
mac
netdev
bootindex
335
Virtualization Administration Guide
x-txtimer
x-txburst
tx
addr
qxl
ram_size
vram_size
revision
cmdlog
addr
ide-drive
unit
drive
physical_block_size
bootindex
ver
wwn
virtio-blk-pci
class
drive
logical_block_size
physical_block_size
min_io_size
opt_io_size
bootindex
ioeventfd
vectors
indirect_desc
event_idx
336
CHAPTER 19. QEMU-KVM COMMANDS, FLAGS, AND ARGUMENTS
scsi
addr
For Windows guests, Windows Server 2003, which was Technology Preview, is no longer
supported since 6.5. However, Windows Server 2008 and 2012, and Windows desktop 7 and 8
are fully supported since 6.5.
vectors
indirect_desc
event_idx
num_queues
addr
isa-debugcon
isa-serial
index
iobase
irq
chardev
virtserialport
nr
chardev
name
virtconsole
nr
chardev
name
virtio-serial-pci
vectors
class
indirect_desc
event_idx
337
Virtualization Administration Guide
max_ports
flow_control
addr
ES1370
addr
AC97
addr
intel-hda
addr
hda-duplex
cad
hda-micro
cad
hda-output
cad
i6300esb
addr
ib700 - no properties
sga - no properties
virtio-balloon-pci
indirect_desc
event_idx
addr
usb-tablet
migrate
port
usb-kbd
migrate
port
338
CHAPTER 19. QEMU-KVM COMMANDS, FLAGS, AND ARGUMENTS
usb-mouse
migrate
port
port
slot
hostbus
hostaddr
hostport
vendorid
productid
isobufs
port
port
freq
maxframes
port
drive
bootindex
serial
removable
port
chardev
filter
339
Virtualization Administration Guide
drive
logical_block_size
physical_block_size
min_io_size
opt_io_size
bootindex
ver
serial
scsi-id
lun
channel-scsi
wwn
drive
logical_block_size
physical_block_size
min_io_size
opt_io_size
bootindex
ver
serial
scsi-id
lun
channel-scsi
wwn
drive
bootindex
340
CHAPTER 19. QEMU-KVM COMMANDS, FLAGS, AND ARGUMENTS
drive=drive
logical_block_size
physical_block_size
min_io_size
opt_io_size
bootindex
ver
serial
scsi-id
lun
channel-scsi
wwn
piix3-usb-uhci
piix4-usb-uhci
ccid-card-passthru
Supported devices and properties as in "General device" section with these additional devices:
isa-fdc
driveA
driveB
bootindexA
bootindexB
qxl-vga
ram_size
vram_size
revision
cmdlog
addr
341
Virtualization Administration Guide
Character Device
-chardev back end,id=<id>[,<options>]
socket,id=<id>,port=<port>[,host=<host>][,to=<to>][,ipv4][,ipv6][,nodelay][,server][,nowait]
[,telnet] - tcp socket
Enable USB
-usb
Kernel File
-kernel <bzImage>
Ram Disk
-initrd <file>
KVM Virtualization
-enable-kvm
QEMU-KVM supports only KVM virtualization and it is used by default if available. If -enable-kvm is used
and KVM is not available, qemu-kvm fails. However, if -enable-kvm is not used and KVM is not available,
qemu-kvm runs in TCG mode, which is not supported.
No Shutdown
-no-shutdown
No Reboot
-no-reboot
342
CHAPTER 19. QEMU-KVM COMMANDS, FLAGS, AND ARGUMENTS
-monitor <dev>
-qmp <dev>
Monitor Redirect
-mon <chardev_id>[,mode=[readline|control]][,default=[on|off]]
RTC
-rtc [base=utc|localtime|date][,clock=host|vm][,driftfix=none|slew]
Watchdog
-watchdog model
Watchdog Reaction
-watchdog-action <action>
SMBIOS Entry
-smbios type=0[,vendor=<str>][,<version=str>][,date=<str>][,release=%d.%d]
-smbios type=1[,manufacturer=<str>][,product=<str>][,version=<str>][,serial=<str>][,uuid=<uuid>][,sku=
<str>][,family=<str>]
Help
-h
343
Virtualization Administration Guide
-help
Version
-version
Audio Help
-audio-help
Migration
-incoming
No Default Configuration
-nodefconfig
-nodefaults
-writeconfig <file>
344
CHAPTER 20. MANIPULATING THE DOMAIN XML
Element Description
345
Virtualization Administration Guide
Element Description
<description> Different from the title, This data is not used by libvirt
in any way, it can contain any information the user
wants to display.
...
<os>
<type>hvm</type>
<loader>/usr/lib/xen/boot/hvmloader</loader>
<boot dev='hd'/>
<boot dev='cdrom'/>
<bootmenu enable='yes'/>
<smbios mode='sysinfo'/>
<bios useserial='yes' rebootTimeout='0'/>
</os>
...
Element Description
346
CHAPTER 20. MANIPULATING THE DOMAIN XML
Element Description
347
Virtualization Administration Guide
Element Description
...
<bootloader>/usr/bin/pygrub</bootloader>
<bootloader_args>--append single</bootloader_args>
...
Element Description
348
CHAPTER 20. MANIPULATING THE DOMAIN XML
...
<os>
<type>hvm</type>
<loader>/usr/lib/xen/boot/hvmloader</loader>
<kernel>/root/f8-i386-vmlinuz</kernel>
<initrd>/root/f8-i386-initrd</initrd>
<cmdline>console=ttyS0 ks=http://example.com/f8-i386/os/</cmdline>
<dtb>/root/ppc.dtb</dtb>
</os>
...
Element Description
349
Virtualization Administration Guide
...
<os>
<smbios mode='sysinfo'/>
...
</os>
<sysinfo type='smbios'>
<bios>
<entry name='vendor'>LENOVO</entry>
</bios>
<system>
<entry name='manufacturer'>Fedora</entry>
<entry name='vendor'>Virt-Manager</entry>
</system>
</sysinfo>
...
The <sysinfo> element has a mandatory attribute type that determines the layout of sub-elements,
and may be defined as follows:
smbios - Sub-elements call out specific SMBIOS values, which will affect the guest virtual
machine if used in conjunction with the smbios sub-element of the <os> element. Each sub-
element of sysinfo names a SMBIOS block, and within those elements can be a list of entry
elements that describe a field within the block. The following blocks and entries are recognized:
bios - This is block 0 of SMBIOS, with entry names drawn from vendor, version, date,
and release.
<system> - This is block 1 of SMBIOS, with entry names drawn from manufacturer,
product, version, serial, uuid, sku, and family. If a uuid entry is provided
alongside a top-level uuid element, the two values must match.
<domain>
...
<vcpu placement='static' cpuset="1-4,^3,6" current="1">2</vcpu>
...
</domain>
The <cpu> element defines the maximum number of virtual CPUs (vCPUs) allocated for the guest virtual
machine operating system, which must be between 1 and the maximum supported by the hypervisor.
This element can contain an optional cpuset attribute, which is a comma-separated list of physical CPU
numbers that domain processes and virtual CPUs can be pinned to by default.
Note that the pinning policy of domain processes and virtual CPUs can be specified separately by using
the cputune attribute. If the emulatorpin attribute is specified in <cputune>, the cpuset value
specified by <vcpu> will be ignored.
350
CHAPTER 20. MANIPULATING THE DOMAIN XML
Similarly, virtual CPUs that have set a value for vcpupin cause cpuset settings to be ignored. Virtual
CPUs where vcpupin is not specified will be pinned to the physical CPUs specified by cpuset. Each
element in the cpuset list is either a single CPU number, a range of CPU numbers, or a caret (^)
followed by a CPU number to be excluded from a previous range. The attribute current can be used to
specify whether fewer than the maximum number of virtual CPUs should be enabled.
The optional attribute placement can be used to specify the CPU placement mode for the domain
process. placement can be set as either static or auto. If you set <vcpu placement='auto'>,
the system will query numad and use the settings specified in the <numatune> tag, and ignore any
other settings in <vcpu> . If you set <vcpu placement='static'>, the system will use the settings
specified in the <vcpu placement> tag instead of the settings in <numatune>.
<domain>
...
<cputune>
<vcpupin vcpu="0" cpuset="1-4,^2"/>
<vcpupin vcpu="1" cpuset="0,1"/>
<vcpupin vcpu="2" cpuset="2,3"/>
<vcpupin vcpu="3" cpuset="0,4"/>
<emulatorpin cpuset="1-3"/>
<shares>2048</shares>
<period>1000000</period>
<quota>-1</quota>
<emulator_period>1000000</emulator_period>
<emulator_quota>-1</emulator_quota>
</cputune>
...
</domain>
Although all are optional, the components of this section of the domain XML are as follows:
Element Description
351
Virtualization Administration Guide
Element Description
352
CHAPTER 20. MANIPULATING THE DOMAIN XML
Element Description
The optional <memoryBacking> element may have an <hugepages> element set within it. This tells
the hypervisor that the guest virtual machine should have its memory allocated using hugepages instead
of the normal native page size.
<domain>
...
<memoryBacking>
<hugepages/>
</memoryBacking>
...
</domain>
<domain>
...
<memtune>
<hard_limit unit='G'>1</hard_limit>
<soft_limit unit='M'>128</soft_limit>
<swap_hard_limit unit='G'>2</swap_hard_limit>
<min_guarantee unit='bytes'>67108864</min_guarantee>
</memtune>
...
</domain>
Although all are optional, the components of this section of the domain XML are as follows:
353
Virtualization Administration Guide
Element Description
>
<domain>
...
<numatune>
<memory mode="strict" nodeset="1-4,^3"/>
</numatune>
...
</domain>
Although all are optional, the components of this section of the domain XML are as follows:
354
CHAPTER 20. MANIPULATING THE DOMAIN XML
Element Description
<domain>
...
<blkiotune>
<weight>800</weight>
<device>
<path>/dev/sda</path>
<weight>1000</weight>
</device>
<device>
<path>/dev/sdb</path>
<weight>500</weight>
</device>
</blkiotune>
...
</domain>
Although all are optional, the components of this section of the domain XML are as follows:
355
Virtualization Administration Guide
Element Description
<resource>
<partition>/virtualmachines/production</partition>
</resource>
Resource partitions are currently supported by the QEMU and LXC drivers, which map partition paths to
cgroups directories in all mounted controllers.
356
CHAPTER 20. MANIPULATING THE DOMAIN XML
<cpu match='exact'>
<model fallback='allow'>core2duo</model>
<vendor>Intel</vendor>
<topology sockets='1' cores='2' threads='1'/>
<feature policy='disable' name='lahf_lm'/>
</cpu>
<cpu mode='host-model'>
<model fallback='forbid'/>
<topology sockets='1' cores='2' threads='1'/>
</cpu>
<cpu mode='host-passthrough'/>
In cases where no restrictions are to be put on either the CPU model nor its features, a simpler cpu
element such as the following may be used.
<cpu>
<topology sockets='1' cores='2' threads='1'/>
</cpu>
Element Description
357
Virtualization Administration Guide
Element Description
358
CHAPTER 20. MANIPULATING THE DOMAIN XML
Element Description
359
Virtualization Administration Guide
Element Description
360
CHAPTER 20. MANIPULATING THE DOMAIN XML
Element Description
<cpu>
<numa>
<cell cpus='0-3' memory='512000'/>
<cell cpus='4-7' memory='512000'/>
</numa>
</cpu>
...
Each cell element specifies a NUMA cell or a NUMA node. cpus specifies the CPU or range of CPUs
that are part of the node. memory specifies the node memory in kibibytes (blocks of 1024 bytes). Each
cell or node is assigned cellid or nodeid in increasing order starting from 0.
361
Virtualization Administration Guide
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<on_lockfailure>poweroff</on_lockfailure>
The following collections of elements allow the actions to be specified when a guest virtual machine OS
triggers a life cycle operation. A common use case is to force a reboot to be treated as a poweroff when
doing the initial OS installation. This allows the VM to be re-configured for the first post-install bootup.
State Description
362
CHAPTER 20. MANIPULATING THE DOMAIN XML
State Description
363
Virtualization Administration Guide
It is possible to forcibly enable or disable BIOS advertisements to the guest virtual machine OS using
conventional management tools which effects the following section of the domain XML:
...
<pm>
<suspend-to-disk enabled='no'/>
<suspend-to-mem enabled='yes'/>
</pm>
...
The <pm> element can be enabled using the arguement yes or disabled using the argument no. BIOS
support can be implemented for S3 using the argument suspend-to-disk and S4 using the argument
suspend-to-mem ACPI sleep states. If nothing is specified, the hypervisor will be left with its default
value.
...
<features>
<pae/>
<acpi/>
<apic/>
<hap/>
<privnet/>
<hyperv>
<relaxed state='on'/>
</hyperv>
</features>
...
All features are listed within the <features> element, if a <state> is not specified it is disabled. The
available features can be found by calling the capabilities XML, but a common set for fully
virtualized domains are:
State Description
364
CHAPTER 20. MANIPULATING THE DOMAIN XML
State Description
20.15. TIMEKEEPING
The guest virtual machine clock is typically initialized from the host physical machine clock. Most
operating systems expect the hardware clock to be kept in UTC, which is the default setting. Note that for
Windows guest virtual machines the guest virtual machine must be set in localtime.
...
<clock offset='localtime'>
<timer name='rtc' tickpolicy='catchup' track='guest'>
<catchup threshold='123' slew='120' limit='10000'/>
</timer>
<timer name='pit' tickpolicy='delay'/>
</clock>
...
State Description
365
Virtualization Administration Guide
State Description
366
CHAPTER 20. MANIPULATING THE DOMAIN XML
State Description
NOTE
Each <timer> element must contain a name attribute, and may have the following
attributes depending on the name specified.
<name> - selects which timer is being modified. The following values are
acceptable:kvmclock (QEMU-KVM), pit(QEMU-KVM), or rtc(QEMU-KVM),
or tsc(libxl only). Note that platform is currently unsupported.
track - specifies the timer track. The following values are acceptable: boot,
guest, or wall. track is only valid for name="rtc".
tickpolicy - determines what happens whens the deadline for injecting a tick
to the guest virtual machine is missed. The following values can be assigned:
delay -will continue to deliver ticks at the normal rate. The guest virtual
machine time will be delayed due to the late tick
catchup - delivers ticks at a higher rate in order to catch up with the missed
tick. The guest virtual machine time is not displayed once catchup is
complete. In addition, there can be three optional attributes, each a positive
integer, as follows: threshold, slew, and limit.
merge - merges the missed tick(s) into one tick and injects them. The guest
virtual machine time may be delayed, depending on how the merge is done.
discard - throws away the missed tick(s) and continues with future injection
at its default interval setting. The guest virtual machine time may be delayed,
unless there is an explicit statement for handling lost ticks
20.16. DEVICES
This set of XML elements are all used to describe devices provided to the guest virtual machine domain.
All of the devices below are indicated as children of the main devices element.
367
Virtualization Administration Guide
IMPORTANT
If a virtio device is created where the number of vectors is set to a value higher than 32,
the device behaves as if it was set to a zero value on Red Hat Enterprise Linux 6, but not
on Enterprise Linux 7. The resulting vector setting mismatch causes a migration error if
the number of vectors on any virtio device on either platform is set to 33 or higher. It is
therefore not reccomended to set the vector value to be greater than 32. All virtio
devices with the exception of virtio-balloon-pci and virtio-rng-pci will accept a vector
argument.
...
<devices>
<emulator>/usr/lib/xen/bin/qemu-dm</emulator>
</devices>
...
The contents of the <emulator> element specify the fully qualified path to the device model emulator
binary. The capabilities XML specifies the recommended default emulator to use for each particular
domain type or architecture combination.
...
<devices>
<disk type='file' snapshot='external'>
<driver name="tap" type="aio" cache="default"/>
<source file='/var/lib/xen/images/fv0' startupPolicy='optional'>
<seclabel relabel='no'/>
</source>
<target dev='hda' bus='ide'/>
<iotune>
<total_bytes_sec>10000000</total_bytes_sec>
<read_iops_sec>400000</read_iops_sec>
<write_iops_sec>100000</write_iops_sec>
</iotune>
<boot order='2'/>
<encryption type='...'>
...
</encryption>
368
CHAPTER 20. MANIPULATING THE DOMAIN XML
<shareable/>
<serial>
...
</serial>
</disk>
...
<disk type='network'>
<driver name="qemu" type="raw" io="threads" ioeventfd="on"
event_idx="off"/>
<source protocol="sheepdog" name="image_name">
<host name="hostname" port="7000"/>
</source>
<target dev="hdb" bus="ide"/>
<boot order='1'/>
<transient/>
<address type='drive' controller='0' bus='1' unit='0'/>
</disk>
<disk type='network'>
<driver name="qemu" type="raw"/>
<source protocol="rbd" name="image_name2">
<host name="hostname" port="7000"/>
</source>
<target dev="hdd" bus="ide"/>
<auth username='myuser'>
<secret type='ceph' usage='mypassid'/>
</auth>
</disk>
<disk type='block' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hdc' bus='ide' tray='open'/>
<readonly/>
</disk>
<disk type='block' device='lun'>
<driver name='qemu' type='raw'/>
<source dev='/dev/sda'/>
<target dev='sda' bus='scsi'/>
<address type='drive' controller='0' bus='0' target='3' unit='0'/>
</disk>
<disk type='block' device='disk'>
<driver name='qemu' type='raw'/>
<source dev='/dev/sda'/>
<geometry cyls='16383' heads='16' secs='63' trans='lba'/>
<blockio logical_block_size='512' physical_block_size='4096'/>
<target dev='hda' bus='ide'/>
</disk>
<disk type='volume' device='disk'>
<driver name='qemu' type='raw'/>
<source pool='blk-pool0' volume='blk-pool0-vol0'/>
<target dev='hda' bus='ide'/>
</disk>
</devices>
...
369
Virtualization Administration Guide
The <disk> element is the main container for describing disks. The attribute type can be used with the
<disk> element. The following types are allowed:
file
block
dir
network
If the <disk type='file''>, then the file attribute specifies the fully-qualified path to the file holding
the disk. If the <disk type='block'>, then the dev attribute specifies the path to the host physical
machine device to serve as the disk. With both file and block, one or more optional sub-elements
seclabel, described below, can be used to override the domain security labeling policy for just that
source file. If the disk type is dir, then the dir attribute specifies the fully-qualified path to the directory
to use as the disk. If the disk type is network, then the protocol attribute specifies the protocol to access
to the requested image; possible values are nbd, rbd, sheepdog or gluster.
If the protocol attribute is rbd, sheepdog or gluster, an additional attribute name is mandatory to
specify which volume and or image will be used. When the disk type is network, the source may have
zero or more host sub-elements used to specify the host physical machines to connect, including:
type='dir' and type='network'. For a file disk type which represents a cdrom or floppy (the
device attribute), it is possible to define policy what to do with the disk if the source file is not accessible.
This is done by manipulating the startupPolicy attribute, with the following values:
mandatory causes a failure if missing for any reason. This is the default setting.
This element is present if the hypervisor has started a BlockCopy operation, where the <mirror>
location in the attribute file will eventually have the same contents as the source, and with the file format
in attribute format (which might differ from the format of the source). If an attribute ready is present, then
it is known the disk is ready to pivot; otherwise, the disk is probably still copying. For now, this element
only valid in output; it is ignored on input.
The <target> element controls the bus / device under which the disk is exposed to the guest virtual
machine OS. The dev attribute indicates the logical device name. The actual device name specified is
not guaranteed to map to the device name in the guest virtual machine OS. The optional bus attribute
specifies the type of disk device to emulate; possible values are driver specific, with typical values being
ide, scsi, virtio, xen, usb or sata. If omitted, the bus type is inferred from the style of the device
370
CHAPTER 20. MANIPULATING THE DOMAIN XML
name. eg, a device named 'sda' will typically be exported using a SCSI bus. The optional attribute
tray indicates the tray status of the removable disks (such as CD-ROM or Floppy disk), the value can
be either open or closed. The default setting is closed. For more information, see target Elements
20.16.1.5. iotune
The optional <iotune> element provides the ability to provide additional per-device I/O tuning, with
values that can vary for each device (contrast this to the blkiotune element, which applies globally to
the domain). This element has the following optional sub-elements. Note that any sub-element not
specified or at all or specified with a value of 0 implies no limit.
<total_bytes_sec> - the total throughput limit in bytes per second. This element cannot be
used with <read_bytes_sec> or <write_bytes_sec>.
<total_iops_sec> - the total I/O operations per second. This element cannot be used with
<read_iops_sec> or <write_iops_sec>.
20.16.1.6. driver
The optional <driver> element allows specifying further details related to the hypervisor driver that is
used to provide the disk. The following options may be used:
If the hypervisor supports multiple back-end drivers, then the name attribute selects the primary
back-end driver name, while the optional type attribute provides the sub-type. For a list of
possible types refer to Driver Elements
The optional cache attribute controls the cache mechanism, possible values are: default,
none, writethrough, writeback, directsync (similar to writethrough, but it bypasses
the host physical machine page cache) and unsafe (host physical machine may cache all disk
io, and sync requests from guest virtual machine virtual machines are ignored).
The optional error_policy attribute controls how the hypervisor behaves on a disk read or
write error, possible values are stop, report, ignore, and enospace. The default setting of
error_policy is report. There is also an optional rerror_policy that controls behavior
for read errors only. If no rerror_policy is given, error_policy is used for both read and
write errors. If rerror_policy is given, it overrides the error_policy for read errors. Also
note that enospace is not a valid policy for read errors, so if error_policy is set to
enospace and no rerror_policy is given, the read error the default setting, report will be
used.
The optional io attribute controls specific policies on I/O; qemu guest virtual machine virtual
machines support threads and native. The optional ioeventfd attribute allows users to set
domain I/O asynchronous handling for disk device. The default is left to the discretion of the
hypervisor. Accepted values are on and off. Enabling this allows the guest virtual machine
virtual machine to be executed while a separate thread handles I/O. Typically guest virtual
machine virtual machines experiencing high system CPU utilization during I/O will benefit from
371
Virtualization Administration Guide
this. On the other hand, an overloaded host physical machine can increase guest virtual
machine virtual machine I/O latency. Unless you are absolutely certian that the io needs to be
manipulated, it is highly recommended that you not change the default setting and allow the
hypervisor to dictate the setting.
The optional event_idx attribute controls some aspects of device event processing and can be
set to either on or off - if it is on, it will reduce the number of interrupts and exits for the guest
virtual machine virtual machine. The default is determined by the hypervisor and the default
setting is on. In cases that there is a situation where this behavior is suboptimal, this attribute
provides a way to force the feature off. Unless you are absolutely certian that the event_idx
needs to be manipulated, it is highly recommended that you not change the default setting and
allow the hypervisor to dictate the setting.
The optional copy_on_read attribute controls whether to copy the read backing file into the
image file. The accepted values can be either on or <off>. copy-on-read avoids accessing
the same backing file sectors repeatedly and is useful when the backing file is over a slow
network. By default copy-on-read is off.
<order> - Determines the order in which devices will be tried during boot sequence.
<per-device> boot elements cannot be used together with general boot elements in BIOS
boot loader section
<encryption> - Specifies how the volume is encrypted. See the Storage Encryption page for
more information.
<readonly> - Indicates the device cannot be modified by the guest virtual machine virtual
machine. This setting is the default for disks with attribute device='cdrom'.
shareable Indicates the device is expected to be shared between domains (as long as
hypervisor and OS support this). If shareable is used, cache='no' should be used for that
device.
<transient>- Indicates that changes to the device contents should be reverted automatically
when the guest virtual machine virtual machine exits. With some hypervisors, marking a disk
transient prevents the domain from participating in migration or snapshots.
<serial>- Specifies the serial number of guest virtual machine virtual machine virtual
machine's hard drive. For example, <serial>WD-WMAP9A966149</serial>.
<wwn> - Specifies the WWN (World Wide Name) of a virtual hard disk or CD-ROM drive. It must
be composed of 16 hexadecimal digits.
<vendor> - Specifies the vendor of a virtual hard disk or CD-ROM device. It must not be longer
than 8 printable characters.
372
CHAPTER 20. MANIPULATING THE DOMAIN XML
<product> - Specifies the product of a virtual hard disk or CD-ROM device. It must not be
longer than 16 printable characters
<host> - Supports 4 attributes: viz, name, port, transport and socket, which specify the
host name, the port number, transport type and path to socket, respectively. The meaning of this
element and the number of the elements depend on the protocol attribute as shown here:
nbd - Specifies a server running nbd-server and may only be used for only one host
physical machine
rbd - Monitors servers of RBD type and may be used for one or more host physical
machines
sheepdog - Specifies one of the sheepdog servers (default is localhost:7000) and can be
used one or none of the host physical machines
gluster - Specifies a server running a glusterd daemon and may be used for only only one
host physical machine. The valid values for transport attribute are tcp, rdma or unix. If
nothing is specified, tcp is assumed. If transport is unix, the socket attribute specifies
path to unix socket.
<address> - Ties the disk to a given slot of a controller. The actual <controller> device can
often be inferred by but it can also be explicitly specified. The type attribute is mandatory, and is
typically pci or drive. For a pci controller, additional attributes for bus, slot, and function
must be present, as well as optional domain and multifunction. multifun ction
defaults to off. For a drive controller, additional attributes controller, bus, target, and
unit are available, each with a default setting of 0.
auth - Provides the authentication credentials needed to access the source. It includes a
mandatory attribute username, which identifies the user name to use during authentication, as
well as a sub-element secret with mandatory attribute type. More information can be found
here at Device Elements
geometry - Provides the ability to override geometry settings. This mostly useful for S390
DASD-disks or older DOS-disks.
trans - Specifies the BIOS-Translation-Modus and can have the following values:none, lba or
auto
blockio - Allows the block device to be overridden with any of the block device properties
listed below:
blockio options
373
Virtualization Administration Guide
20.16.2. Filesystems
A filesystems directory on the host physical machine that can be accessed directly from the guest virtual
machine virtual machine
...
<devices>
<filesystem type='template'>
<source name='my-vm-template'/>
<target dir='/'/>
</filesystem>
<filesystem type='mount' accessmode='passthrough'>
<driver type='path' wrpolicy='immediate'/>
<source dir='/export/to/guest'/>
<target dir='/import/from/host'/>
<readonly/>
</filesystem>
...
</devices>
...
type='mount' - Specifies the host physical machine directory to mount in the guest virtual
machine. This is the default type if one is not specified. This mode also has an optional sub-
element driver, with an attribute type='path' or type='handle'. The driver block has an
optional attribute wrpolicy that further controls interaction with the host physical machine page
cache; omitting the attribute reverts to the default setting, while specifying a value immediate
means that a host physical machine writeback is immediately triggered for all pages touched
during a guest virtual machine file write operation
type='template' - Specifies the OpenVZ filesystem template and is only used by OpenVZ
driver.
type='file' - Specifies that a host physical machine file will be treated as an image and
mounted in the guest virtual machine. This filesystem format will be autodetected and is only
used by LXC driver.
type='block' - Specifies the host physical machine block device to mount in the guest virtual
machine. The filesystem format will be autodetected and is only used by LXC driver.
type='ram' - Specifies that an in-memory filesystem, using memory from the host physical
machine OS will be used. The source element has a single attribute usage which gives the
memory usage limit in kibibytes and is only used by LXC driver.
type='bind' - Specifies a directory inside the guest virtual machine which will be bound to
another directory inside the guest virtual machine. This element is only used by LXC driver.
374
CHAPTER 20. MANIPULATING THE DOMAIN XML
accessmode which specifies the security mode for accessing the source. Currently this only
works with type='mount' for the QEMU/KVM driver. The possible values are:
passthrough - Specifies that the source is accessed with the User's permission settings
that are set from inside the guest virtual machine. This is the default access mode if one is
not specified.
mapped - Specifies that the source is accessed with the permission settings of the
hypervisor.
<source> - Specifies that the resource on the host physical machine that is being accessed in
the guest virtual machine. The name attribute must be used with <type='template'>, and the
dir attribute must be used with <type='mount'>. The usage attribute is used with
<type='ram'> to set the memory limit in KB.
target - Dictates where the source drivers can be accessed in the guest virtual machine. For
most drivers this is an automatic mount point, but for QEMU-KVM this is merely an arbitrary
string tag that is exported to the guest virtual machine as a hint for where to mount.
readonly - Enables exporting the file sydtem as a readonly mount for guest virtual machine, by
default read-write access is given.
space_hard_limit - Specifies the maximum space available to this guest virtual machine's
filesystem
space_soft_limit - Specifies the maximum space available to this guest virtual machine's
filesystem. The container is permitted to exceed its soft limits for a grace period of time.
Afterwards the hard limit is enforced.
Every address has a mandatory attribute type that describes which bus the device is on. The choice of
which address to use for a given device is constrained in part by the device and the architecture of the
guest virtual machine. For example, a disk device uses type='disk', while a console device would
use type='pci' on the 32-bit AMD and Intel architecture or AMD64 and Intel 64 guest virtual
machines, or type='spapr-vio' on PowerPC64 pseries guest virtual machines. Each address
<type> has additional optional attributes that control where on the bus the device will be placed. The
additional attributes are as follows:
375
Virtualization Administration Guide
Also available is the multifunction attribute, which controls turning on the multifunction
bit for a particular slot/function in the PCI control register. This multifunction attribute
defaults to 'off', but should be set to 'on' for function 0 of a slot that will have multiple
functions used.
type='ccid' - A CCID address, used for smart-cards, has the following additional attributes:
20.16.4. Controllers
Depending on the guest virtual machine architecture, it is possible to assign many virtual devices to a
single bus. Under normal circumstances libvirt can automatically infer which controller to use for the bus.
However, it may be necessary to provide an explicit <controller> element in the guest virtual
machine XML:
376
CHAPTER 20. MANIPULATING THE DOMAIN XML
...
<devices>
<controller type='ide' index='0'/>
<controller type='virtio-serial' index='0' ports='16' vectors='4'/>
<controller type='virtio-serial' index='1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x0a'
function='0x0'/>
<controller type='scsi' index='0' model='virtio-scsi'
num_queues='8'/>
</controller>
...
</devices>
...
Each controller has a mandatory attribute type, which must be one of "ide", "fdc", "scsi",
"sata", "usb", "ccid", or "virtio-serial", and a mandatory attribute index which is the
decimal integer describing in which order the bus controller is encountered (for use in controller
attributes of address elements). The "virtio-serial" controller has two additional optional
attributes, ports and vectors, which control how many devices can be connected through the
controller.
A "usb" controller has an optional attribute model, which is one of "piix3-uhci", "piix4-uhci",
"ehci", "ich9-ehci1", "ich9-uhci1", "ich9-uhci2", "ich9-uhci3", "vt82c686b-uhci",
"pci-ohci" or "nec-xhci". Additionally, if the USB bus needs to be explicitly disabled for the guest
virtual machine, model='none' may be used. The PowerPC64 "spapr-vio" addresses do not have an
associated controller.
For controllers that are themselves devices on a PCI or USB bus, an optional sub-element address can
specify the exact relationship of the controller to its master bus, with semantics given above.
USB companion controllers have an optional sub-element master to specify the exact relationship of the
companion to its master controller. A companion controller is on the same bus as its master, so the
companion index value should be equal.
377
Virtualization Administration Guide
...
<devices>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0' bus='0' slot='4' function='7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0' bus='0' slot='4' function='0'
multifunction='on'/>
</controller>
...
</devices>
...
...
<devices>
...
<lease>
<lockspace>somearea</lockspace>
<key>somekey</key>
<target path='/some/lease/path' offset='1024'/>
</lease>
...
</devices>
...
lockspace - an arbitrary string that identifies lockspace within which the key is held. Lock
managers may impose extra restrictions on the format, or length of the lockspace name.
key - an arbitrary string, that uniquely identies the lease to be acquired. Lock managers may
impose extra restrictions on the format, or length of the key.
target - the fully qualified path of the file associated with the lockspace. The offset specifies
where the lease is stored within the file. If the lock manager does not require a offset, set this
value to 0.
378
CHAPTER 20. MANIPULATING THE DOMAIN XML
This section provides information about host physical machine device assignment.
The host physical machine's USB and PCI devices can be passed through to the guest virtual machine
using the hostdev element, by modifying the host physical machine using a management tool the
following section of the domain xml file is configured:
...
<devices>
<hostdev mode='subsystem' type='usb'>
<source startupPolicy='optional'>
<vendor id='0x1234'/>
<product id='0xbeef'/>
</source>
<boot order='2'/>
</hostdev>
</devices>
...
...
<devices>
<hostdev mode='subsystem' type='pci' managed='yes'>
<source>
<address bus='0x06' slot='0x02' function='0x0'/>
</source>
<boot order='1'/>
<rom bar='on' file='/etc/fake/boot.bin'/>
</hostdev>
</devices>
...
Parameter Description
379
Virtualization Administration Guide
Parameter Description
380
CHAPTER 20. MANIPULATING THE DOMAIN XML
Parameter Description
The host physical machine's block / character devices can be passed through to the guest virtual
machine by using management tools to modify the domain xml hostdev element. Note that this is only
possible with container based virtualization.
...
<hostdev mode='capabilities' type='storage'>
<source>
<block>/dev/sdf1</block>
</source>
</hostdev>
...
Figure 20.30. Devices - host physical machine device assignment block character devices
381
Virtualization Administration Guide
...
<hostdev mode='capabilities' type='misc'>
<source>
<char>/dev/input/event3</char>
</source>
</hostdev>
...
Figure 20.31. Devices - host physical machine device assignment block character devices
alternative 1
...
<hostdev mode='capabilities' type='net'>
<source>
<interface>eth0</interface>
</source>
</hostdev>
...
Figure 20.32. Devices - host physical machine device assignment block character devices
alternative 2
Parameter Description
382
CHAPTER 20. MANIPULATING THE DOMAIN XML
...
<devices>
<redirdev bus='usb' type='tcp'>
<source mode='connect' host='localhost' service='4000'/>
<boot order='1'/>
</redirdev>
<redirfilter>
<usbdev class='0x08' vendor='0x1234' product='0xbeef'
version='2.00' allow='yes'/>
<usbdev allow='no'/>
</redirfilter>
</devices>
...
Parameter Description
redirfilter This is used for creating the filter rule to filter out
certain devices from redirection. It uses sub-element
usbdev to define each filter rule. The class
attribute is the USB Class code.
383
Virtualization Administration Guide
USB smartcard reader device on the host machine cannot be used on a guest with simple device
passthrough, as it cannot be made available to both the host and guest, and can lock the host computer
when it is removed from the guest. Therefore, some hypervisors provide a specialized virtual device that
can present a smartcard interface to the guest virtual machine, with several modes for describing how
the credentials are obtained from the host machine, or from a channel created by a third-party smartcard
provider. To set parameters for USB device redirection through a character device, edit the following
section of the domain XML:
...
<devices>
<smartcard mode='host'/>
<smartcard mode='host-certificates'>
<certificate>cert1</certificate>
<certificate>cert2</certificate>
<certificate>cert3</certificate>
<database>/etc/pki/nssdb/</database>
</smartcard>
<smartcard mode='passthrough' type='tcp'>
<source mode='bind' host='127.0.0.1' service='2001'/>
<protocol type='raw'/>
<address type='ccid' controller='0' slot='0'/>
</smartcard>
<smartcard mode='passthrough' type='spicevmc'/>
</devices>
...
The smartcard element has a mandatory attribute mode. The following modes are supported; in each
mode, the guest virtual machine sees a device on its USB bus that behaves like a physical USB CCID
(Chip/Smart Card Interface Device) card.
Parameter Description
384
CHAPTER 20. MANIPULATING THE DOMAIN XML
Parameter Description
Each mode supports an optional sub-element address, which fine-tunes the correlation between the
smartcard and a ccid bus controller (Refer to Section 20.16.3, “Device Addresses”).
385
Virtualization Administration Guide
...
<devices>
<interface type='bridge'>
<source bridge='xenbr0'/>
<mac address='00:16:3e:5d:c7:9e'/>
<script path='vif-bridge'/>
<boot order='1'/>
<rom bar='off'/>
</interface>
</devices>
...
There are several possibilities for specifying a network interface visible to the guest virtual machine.
Each subsection below provides more details about common setup options. Additionally, each
<interface> element has an optional <address> sub-element that can tie the interface to a particular
pci slot, with attribute type='pci' (Refer to Section 20.16.3, “Device Addresses”).
This is the recommended configuration for general guest virtual machine connectivity on host physical
machines with dynamic / wireless networking configurations (or multi-host physical machine
environments where the host physical machine hardware details are described separately in a
<network> definition). In addition, it provides a connection whose details are described by the named
network definition. Depending on the virtual network's forward mode configuration, the network may be
totally isolated (no <forward> element given), NAT'ing to an explicit network device or to the default
route (forward mode='nat'), routed with no NAT (forward mode='route'/), or connected
directly to one of the host physical machine's network interfaces (using macvtap) or bridge devices
(forward mode=' bridge|private|vepa|passthrough'/)
For networks with a forward mode of bridge, private, vepa, and passthrough, it is assumed that the host
physical machine has any necessary DNS and DHCP services already setup outside the scope of libvirt.
In the case of isolated, nat, and routed networks, DHCP and DNS are provided on the virtual network by
libvirt, and the IP range can be determined by examining the virtual network config with virsh net-
dumpxml [networkname]. There is one virtual network called 'default' setup out of the box which does
NAT'ing to the default route and has an IP range of 192.168.122.0/255.255.255.0. Each guest virtual
machine will have an associated tun device created with a name of vnetN, which can also be overridden
with the <target> element (refer to Section 20.16.9.11, “Overriding the target element”).
When the source of an interface is a network, a portgroup can be specified along with the name of the
network; one network may have multiple portgroups defined, with each portgroup containing slightly
different configuration information for different classes of network connections. Also, similar to
<direct> network connections (described below), a connection of type network may specify a
<virtualport> element, with configuration data to be forwarded to a vepa (802.1Qbg) or 802.1Qbh-
compliant switch, or to an Open vSwitch virtual switch.
Since the actual type of switch may vary depending on the configuration in the <network> on the host
physical machine, it is acceptable to omit the virtualport type attribute, and specify attributes from
multiple different virtualport types (and also to leave out certain attributes); at domain startup time, a
complete <virtualport> element will be constructed by merging together the type and attributes
defined in the network and the portgroup referenced by the interface. The newly-constructed virtualport
is a combination of both. The attributes from lower virtualport cannot make changes on the ones defined
in higher virtualport. Interfaces take the highest priority, portgroup is lowest priority.
386
CHAPTER 20. MANIPULATING THE DOMAIN XML
For example, to create a properly working network with both an 802.1Qbh switch and an Open vSwitch
switch, you may choose to specify no type, but both profileid and an interfaceid must be
supplied. The other attributes to be filled in from the virtual port, such as such as managerid, typeid,
or profileid, are optional.
If you want to limit a guest virtual machine to connecting only to certain types of switches, you can
specify the virtualport type, and only switches with the specified port type will connect. You can also
further limit switch connectivity by specifying additional parameters. As a result, if the port was specified
and the host physical machine's network has a different type of virtualport, the connection of the interface
will fail. The virtual network parameters are defined using management tools that modify the following
part of the domain XML:
...
<devices>
<interface type='network'>
<source network='default'/>
</interface>
...
<interface type='network'>
<source network='default' portgroup='engineering'/>
<target dev='vnet7'/>
<mac address="00:11:22:33:44:55"/>
<virtualport>
<parameters instanceid='09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f'/>
</virtualport>
</interface>
</devices>
...
Note that this is the recommended configuration setting for general guest virtual machine connectivity on
host physical machines with static wired networking configurations.
Bridge to LAN provides a bridge from the guest virtual machine directly onto the LAN. This assumes
there is a bridge device on the host physical machine which has one or more of the host physical
machines physical NICs enslaved. The guest virtual machine will have an associated tun device
created with a name of <vnetN>, which can also be overridden with the <target> element (refer to
Section 20.16.9.11, “Overriding the target element”). The <tun> device will be enslaved to the bridge.
The IP range / network configuration is whatever is used on the LAN. This provides the guest virtual
machine full incoming and outgoing net access just like a physical machine.
On Linux systems, the bridge device is normally a standard Linux host physical machine bridge. On host
physical machines that support Open vSwitch, it is also possible to connect to an open vSwitch bridge
device by adding a virtualport type='openvswitch'/ to the interface definition. The Open
vSwitch type virtualport accepts two parameters in its parameters element - an interfaceid which
is a standard uuid used to uniquely identify this particular interface to Open vSwitch (if you do no specify
one, a random interfaceid will be generated for you when you first define the interface), and an
optional profileid which is sent to Open vSwitch as the interfaces <port-profile>. To set the
bridge to LAN settings, use a management tool that will configure the following part of the domain XML:
387
Virtualization Administration Guide
...
<devices>
...
<interface type='bridge'>
<source bridge='br0'/>
</interface>
<interface type='bridge'>
<source bridge='br1'/>
<target dev='vnet7'/>
<mac address="00:11:22:33:44:55"/>
</interface>
<interface type='bridge'>
<source bridge='ovsbr'/>
<virtualport type='openvswitch'>
<parameters profileid='menial' interfaceid='09b11c53-8b5c-4eeb-
8f00-d84eaa0aaa4f'/>
</virtualport>
</interface>
...
</devices>
In cases where you want to set the port masquerading range, the port can be set as follows:
<forward mode='nat'>
<address start='192.0.2.1' end='192.0.2.10'/>
</forward> ...
These values should be set using the iptables commands as shown in Section 18.3, “Network
Address Translation Mode”
Setting the user-space SLIRP stack parameters provides a virtual LAN with NAT to the outside world.
The virtual network has DHCP and DNS services and will give the guest virtual machine an IP addresses
starting from 10.0.2.15. The default router will be 10.0.2.2 and the DNS server will be 10.0.2.3. This
networking is the only option for unprivileged users who need their guest virtual machines to have
outgoing access.
The user-space SLIP stack parameters are defined in the following part of the domain XML::
388
CHAPTER 20. MANIPULATING THE DOMAIN XML
...
<devices>
<interface type='user'/>
...
<interface type='user'>
<mac address="00:11:22:33:44:55"/>
</interface>
</devices>
...
Provides a means for the administrator to execute an arbitrary script to connect the guest virtual
machine's network to the LAN. The guest virtual machine will have a tun device created with a name of
vnetN, which can also be overridden with the target element. After creating the tun device a shell
script will be run which is expected to do whatever host physical machine network integration is required.
By default this script is called /etc/qemu-ifup but can be overridden (refer to Section 20.16.9.11,
“Overriding the target element”).
The generic Ethernet connection parameters are defined in the following part of the domain XML:
...
<devices>
<interface type='ethernet'/>
...
<interface type='ethernet'>
<target dev='vnet7'/>
<script path='/etc/qemu-ifup-mynet'/>
</interface>
</devices>
...
This set up requires the Linux macvtap driver to be available. One of the following modes can be chosen
for the operation mode of the macvtap device: vepa ( 'Virtual Ethernet Port Aggregator'), which is the
default mode, bridge or private.
To set up direct attachment to physical interface, use the following parameters in the domain XML:
389
Virtualization Administration Guide
...
<devices>
...
<interface type='direct'>
<source dev='eth0' mode='vepa'/>
</interface>
</devices>
...
The individual modes cause the delivery of packets to behave as shown in Table 20.17, “Direct
attachment to physical interface elements”:
Element Description
private All packets are sent to the external bridge and will
only be delivered to a target VM on the same host
physical machine if they are sent through an external
router or gateway and that device sends them back to
the host physical machine. This procedure is followed
if either the source or destination device is in private
mode.
The network access of direct attached virtual machines can be managed by the hardware switch to
which the physical interface of the host physical machine machine is connected to.
390
CHAPTER 20. MANIPULATING THE DOMAIN XML
The interface can have additional parameters as shown below, if the switch is conforming to the IEEE
802.1Qbg standard. The parameters of the virtualport element are documented in more detail in the
IEEE 802.1Qbg standard. The values are network specific and should be provided by the network
administrator. In 802.1Qbg terms, the Virtual Station Interface (VSI) represents the virtual interface of a
virtual machine.
Note that IEEE 802.1Qbg requires a non-zero value for the VLAN ID.
Additional elements that can be manipulated are described in Table 20.18, “Direct attachment to physical
interface additional elements”:
Element Description
391
Virtualization Administration Guide
...
<devices>
...
<interface type='direct'>
<source dev='eth0.2' mode='vepa'/>
<virtualport type="802.1Qbg">
<parameters managerid="11" typeid="1193047" typeidversion="2"
instanceid="09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f"/>
</virtualport>
</interface>
</devices>
...
Figure 20.42. Devices - network interfaces- direct attachment to physical interfaces additional
parameters
The interface can have additional parameters as shown below if the switch is conforming to the IEEE
802.1Qbh standard. The values are network specific and should be provided by the network
administrator.
...
<devices>
...
<interface type='direct'>
<source dev='eth0' mode='private'/>
<virtualport type='802.1Qbh'>
<parameters profileid='finance'/>
</virtualport>
</interface>
</devices>
...
Figure 20.43. Devices - network interfaces- direct attachment to physical interfaces more
additional parameters
The profileid attribute, contains the name of the port profile that is to be applied to this interface. This
name is resolved by the port profile database into the network parameters from the port profile, and
those network parameters will be applied to this interface.
A PCI network device (specified by the source element) is directly assigned to the guest virtual
machine using generic device passthrough, after first optionally setting the device's MAC address to the
configured value, and associating the device with an 802.1Qbh capable switch using an optionally
specified virtualport element (see the examples of virtualport given above for type='direct' network
devices). Note that - due to limitations in standard single-port PCI ethernet card driver design - only SR-
IOV (Single Root I/O Virtualization) virtual function (VF) devices can be assigned in this manner; to
assign a standard single-port PCI or PCIe ethernet card to a guest virtual machine, use the traditional
hostdev device definition
Note that this "intelligent passthrough" of network devices is very similar to the functionality of a standard
392
CHAPTER 20. MANIPULATING THE DOMAIN XML
hostdev device, the difference being that this method allows specifying a MAC address and
virtualport for the passed-through device. If these capabilities are not required, if you have a
standard single-port PCI, PCIe, or USB network card that does not support SR-IOV (and hence would
anyway lose the configured MAC address during reset after being assigned to the guest virtual machine
domain), or if you are using a version of libvirt older than 0.9.11, you should use standard hostdev to
assign the device to the guest virtual machine instead of interface type='hostdev'/.
...
<devices>
<interface type='hostdev'>
<driver name='vfio'/>
<source>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07'
function='0x0'/>
</source>
<mac address='52:54:00:6d:90:02'>
<virtualport type='802.1Qbh'>
<parameters profileid='finance'/>
</virtualport>
</interface>
</devices>
...
A multicast group may be used to represent a virtual network. Any guest virtual machine whose network
devices are within the same multicast group will talk to each other, even if they reside across multiple
physical host physical machines. This mode may be used as an unprivileged user. There is no default
DNS or DHCP support and no outgoing network access. To provide outgoing network access, one of the
guest virtual machines should have a second NIC which is connected to one of the first 4 network types
in order to provide appropriate routing. The multicast protocol is compatible with protocols used by user
mode linux guest virtual machines as well. Note that the source address used must be from the multicast
address block. A multicast tunnel is created by manipulating the interface type using a management
tool and setting/changing it to mcast, and providing a mac and source address. The result is shown in
changes made to the domain XML:
...
<devices>
<interface type='mcast'>
<mac address='52:54:00:6d:90:01'>
<source address='230.0.0.1' port='5558'/>
</interface>
</devices>
...
Creating a TCP client/server architecture is another way to provide a virtual network where one guest
393
Virtualization Administration Guide
virtual machine provides the server end of the network and all other guest virtual machines are
configured as clients. All network traffic between the guest virtual machines is routed via the guest virtual
machine that is configured as the server. This model is also available for use to unprivileged users.
There is no default DNS or DHCP support and no outgoing network access. To provide outgoing network
access, one of the guest virtual machines should have a second NIC which is connected to one of the
first 4 network types thereby providing the appropriate routing. A TCP tunnel is created by manipulating
the interface type using a management tool and setting/changing it to server or client, and
providing a mac and source address. The result is shown in changes made to the domain XML:
...
<devices>
<interface type='server'>
<mac address='52:54:00:22:c9:42'>
<source address='192.168.0.1' port='5558'/>
</interface>
...
<interface type='client'>
<mac address='52:54:00:8b:c9:51'>
<source address='192.168.0.1' port='5558'/>
</interface>
</devices>
...
Some NICs may have tunable driver-specific options. These options are set as attributes of the driver
sub-element of the interface definition. These options are set by using management tools to configuring
the following sections of the domain XML:
<devices>
<interface type='network'>
<source network='default'/>
<target dev='vnet1'/>
<model type='virtio'/>
<driver name='vhost' txmode='iothread' ioeventfd='on'
event_idx='off'/>
</interface>
</devices>
...
Currently the following attributes are available for the "virtio" NIC driver:
Parameter Description
394
CHAPTER 20. MANIPULATING THE DOMAIN XML
Parameter Description
To override the target element, use a management tool to make the following changes to the domain
XML:
395
Virtualization Administration Guide
...
<devices>
<interface type='network'>
<source network='default'/>
<target dev='vnet1'/>
</interface>
</devices>
...
If no target is specified, certain hypervisors will automatically generate a name for the created tun device.
This name can be manually specified, however the name must not start with either 'vnet' or 'vif', which
are prefixes reserved by libvirt and certain hypervisors. Manually specified targets using these prefixes
will be ignored.
To specify the boot order, use a management tool to make the following changes to the domain XML:
...
<devices>
<interface type='network'>
<source network='default'/>
<target dev='vnet1'/>
<boot order='1'/>
</interface>
</devices>
...
For hypervisors which support it, you can set a specific NIC to be used for the network boot. The order of
attributes determine the order in which devices will be tried during boot sequence. Note that the per-
device boot elements cannot be used together with general boot elements in BIOS boot loader section.
To specify the ROM BIOS configuration settings, use a management tool to make the following changes
to the domain XML:
396
CHAPTER 20. MANIPULATING THE DOMAIN XML
...
<devices>
<interface type='network'>
<source network='default'/>
<target dev='vnet1'/>
<rom bar='on' file='/etc/fake/boot.bin'/>
</interface>
</devices>
...
For hypervisors which support it, you can change how a PCI Network device's ROM is presented to the
guest virtual machine. The bar attribute can be set to on or off, and determines whether or not the
device's ROM will be visible in the guest virtual machine's memory map. (In PCI documentation, the
"rombar" setting controls the presence of the Base Address Register for the ROM). If no rom bar is
specified, the qemu default will be used (older versions of qemu used a default of off, while newer
qemus have a default of on). The optional file attribute is used to point to a binary file to be presented
to the guest virtual machine as the device's ROM BIOS. This can be useful to provide an alternative boot
ROM for a network device.
This section of the domain XML provides setting quality of service. Incoming and outgoing traffic can be
shaped independently. The bandwidth element can have at most one inbound and at most one
outbound child elements. Leaving any of these children element out results in no QoS being applied on
that traffic direction. Therefore, when you want to shape only domain's incoming traffic, use inbound
only, and vice versa.
Each of these elements has one mandatory attribute average (or floor as described below). average
specifies average bit rate on the interface being shaped. Then there are two optional attributes: peak,
which specifies maximum rate at which interface can send data, and burst, which specifies the amount
of bytes that can be burst at peak speed. Accepted values for attributes are integer numbers.
The units for average and peak attributes are kilobytes per second, whereas burst is only set in
kilobytes. In addition, inbound traffic can optionally have a floor attribute. This guarantees minimal
throughput for shaped interfaces. Using the floor requires that all traffic goes through one point where
QoS decisions can take place. As such it may only be used in cases where the interface
type='network'/ with a forward type of route, nat, or no forward at all). It should be noted that
within a virtual network, all connected interfaces are required to have at least the inbound QoS set
(average at least) but the floor attribute does not require specifying average. However, peak and
burst attributes still require average. At the present time, ingress qdiscs may not have any classes,
and therefore floor may only be applied only on inbound and not outbound traffic.
To specify the QoS configuration settings, use a management tool to make the following changes to the
domain XML:
397
Virtualization Administration Guide
...
<devices>
<interface type='network'>
<source network='default'/>
<target dev='vnet0'/>
<bandwidth>
<inbound average='1000' peak='5000' floor='200' burst='1024'/>
<outbound average='128' peak='256' burst='256'/>
</bandwidth>
</interface>
<devices>
...
To specify the VLAN tag configuration settings, use a management tool to make the following changes to
the domain XML:
...
<devices>
<interface type='bridge'>
<vlan>
<tag id='42'/>
</vlan>
<source bridge='ovsbr0'/>
<virtualport type='openvswitch'>
<parameters interfaceid='09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f'/>
</virtualport>
</interface>
<devices>
...
Figure 20.52. Setting VLAN tag (on supported network types only)
If (and only if) the network connection used by the guest virtual machine supports vlan tagging
transparent to the guest virtual machine, an optional vlan element can specify one or more vlan tags to
apply to the guest virtual machine's network traffic (openvswitch and type='hostdev' SR-IOV
interfaces do support transparent vlan tagging of guest virtual machine traffic; everything else, including
standard Linux bridges and libvirt's own virtual networks, do not support it. 802.1Qbh (vn-link) and
802.1Qbg (VEPA) switches provide their own way (outside of libvirt) to tag guest virtual machine traffic
onto specific vlans.) To allow for specification of multiple tags (in the case of vlan trunking), a
subelement, tag, specifies which vlan tag to use (for example: tag id='42'/. If an interface has more
than one vlan element defined, it is assumed that the user wants to do VLAN trunking using all the
specified tags. In the case that vlan trunking with a single tag is desired, the optional attribute
trunk='yes' can be added to the toplevel vlan element.
398
CHAPTER 20. MANIPULATING THE DOMAIN XML
This element provides means of setting state of the virtual network link. Possible values for attribute
state are up and down. If down is specified as the value, the interface behaves as if it had the network
cable disconnected. Default behavior if this element is unspecified is to have the link state up.
To specify the virtual link state configuration settings, use a management tool to make the following
changes to the domain XML:
...
<devices>
<interface type='network'>
<source network='default'/>
<target dev='vnet0'/>
<link state='down'/>
</interface>
<devices>
...
To specify the input devices configuration settings, use a management tool to make the following
changes to the domain XML:
...
<devices>
<input type='mouse' bus='usb'/>
</devices>
...
The <input> element has one mandatory attribute: type which can be set to: mouse or tablet. The
latter provides absolute cursor movement, while the former uses relative movement. The optional bus
attribute can be used to refine the exact device type and can be set to: xen (paravirtualized), ps2, and
usb.
The input element has an optional sub-element <address>, which can tie the device to a particular PCI
slot, as documented above.
To specify the hub devices configuration settings, use a management tool to make the following changes
to the domain XML:
399
Virtualization Administration Guide
...
<devices>
<hub type='usb'/>
</devices>
...
The hub element has one mandatory attribute, the type whose value can only be usb. The hub element
has an optional sub-element address with type='usb'which can tie the device to a particular
controller.
To specify the graphical framebuffer devices configuration settings, use a management tool to make the
following changes to the domain XML:
...
<devices>
<graphics type='sdl' display=':0.0'/>
<graphics type='vnc' port='5904'>
<listen type='address' address='192.0.2.1'/>
</graphics>
<graphics type='rdp' autoport='yes' multiUser='yes' />
<graphics type='desktop' fullscreen='yes'/>
<graphics type='spice'>
<listen type='network' network='rednet'/>
</graphics>
</devices>
...
The graphics element has a mandatory type attribute which takes the value sdl, vnc, rdp or
desktop as explained below:
Parameter Description
400
CHAPTER 20. MANIPULATING THE DOMAIN XML
Parameter Description
When SPICE has both a normal and TLS secured TCP port configured, it may be desirable to restrict
what channels can be run on each port. This is achieved by adding one or more channel elements
inside the main graphics element. Valid channel names include main, display, inputs, cursor,
playback, record; smartcard; and usbredir.
To specify the SPICE configuration settings, use a mangement tool to make the following changes to the
domain XML:
401
Virtualization Administration Guide
SPICE supports variable compression settings for audio, images and streaming. These settings are
accessible by using the compression attribute in all following elements: image to set image compression
(accepts auto_glz, auto_lz, quick, glz, lz, off), jpeg for JPEG compression for images over wan (accepts
auto, never, always), zlib for configuring wan image compression (accepts auto, never, always) and
playback for enabling audio stream compression (accepts on or off).
Streaming mode is set by the streaming element, settings its mode attribute to one of filter, all or
off.
In addition, Copy and paste functionality (using the SPICE agent) is set by the clipboard element. It is
enabled by default, and can be disabled by setting the copypaste property to no.
Mouse mode is set by the mouse element, setting its mode attribute to one of server or client. If no
mode is specified, the qemu default will be used (client mode).
Parameter Description
402
CHAPTER 20. MANIPULATING THE DOMAIN XML
Parameter Description
To specify the video devices configuration settings, use a management tool to make the following
changes to the domain XML:
403
Virtualization Administration Guide
...
<devices>
<video>
<model type='vga' vram='8192' heads='1'>
<acceleration accel3d='yes' accel2d='yes'/>
</model>
</video>
</devices>
...
The graphics element has a mandatory type attribute which takes the value "sdl", "vnc", "rdp" or
"desktop" as explained below:
Parameter Description
To specify the consols, channel and other devices configuration settings, use a management tool to
make the following changes to the domain XML:
404
CHAPTER 20. MANIPULATING THE DOMAIN XML
...
<devices>
<parallel type='pty'>
<source path='/dev/pts/2'/>
<target port='0'/>
</parallel>
<serial type='pty'>
<source path='/dev/pts/3'/>
<target port='0'/>
</serial>
<console type='pty'>
<source path='/dev/pts/4'/>
<target port='0'/>
</console>
<channel type='unix'>
<source mode='bind' path='/tmp/guestfwd'/>
<target type='guestfwd' address='10.0.2.1' port='4600'/>
</channel>
</devices>
...
In each of these directives, the top-level element name (parallel, serial, console, channel) describes how
the device is presented to the guest virtual machine. The guest virtual machine interface is configured by
the target element. The interface presented to the host physical machine is given in the type attribute of
the top-level element. The host physical machine interface is configured by the source element. The
source element may contain an optional seclabel to override the way that labelling is done on the socket
path. If this element is not present, the security label is inherited from the per-domain setting. Each
character device element has an optional sub-element address which can tie the device to a particular
controller or PCI slot.
To set the parallel port, use a management tool to make the following change to the domain XML
...
<devices>
<parallel type='pty'>
<source path='/dev/pts/2'/>
<target port='0'/>
</parallel>
</devices>
...
<target> can have a port attribute, which specifies the port number. Ports are numbered starting
from 0. There are usually 0, 1 or 2 parallel ports.
To set the serial port use a management tool to make the following change to the domain XML:
405
Virtualization Administration Guide
...
<devices>
<serial type='pty'>
<source path='/dev/pts/3'/>
<target port='0'/>
</serial>
</devices>
...
<target> can have a port attribute, which specifies the port number. Ports are numbered starting
from 0. There are usually 0, 1 or 2 serial ports. There is also an optional type attribute, which has two
choices for its value, one is isa-serial, the other is usb-serial. If type is missing, isa-serial
will be used by default. For usb-serial an optional sub-element <address> with type='usb' can tie the
device to a particular controller, documented above.
The <console> element is used to represent interactive consoles. Depending on the type of guest
virtual machine in use, the consoles might be paravirtualized devices, or they might be a clone of a serial
device, according to the following rules:
If no targetType attribute is set, then the default device type is according to the hypervisor's
rules. The default type will be added when re-querying the XML fed into libvirt. For fully
virtualized guest virtual machines, the default device type will usually be a serial port.
If the targetType attribute is serial, and if no <serial> element exists, the console
element will be copied to the <serial> element. If a <serial> element does already exist, the
console element will be ignored.
Only the first <console> element may use a targetType of serial. Secondary consoles
must all be paravirtualized.
On s390, the console element may use a targetType of sclp or sclplm (line mode). SCLP is the
native console type for s390. There's no controller associated to SCLP consoles.
In the example below, a virtio console device is exposed in the guest virtual machine as /dev/hvc[0-7]
(for more information, see http://fedoraproject.org/wiki/Features/VirtioSerial):
406
CHAPTER 20. MANIPULATING THE DOMAIN XML
...
<devices>
<console type='pty'>
<source path='/dev/pts/4'/>
<target port='0'/>
</console>
...
<devices>
<!-- KVM s390 sclp console -->
<console type='pty'>
<source path='/dev/pts/1'/>
<target type='sclp' port='0'/>
</console>
</devices>
...
If the console is presented as a serial port, the <target> element has the same attributes as for a serial
port. There is usually only one console.
20.16.16. Channel
This represents a private communication channel between the host physical machine and the guest
virtual machine and is manipulated by making changes to your guest virtual machine virtual machine
using a management tool that results in changes made to the following section of the domain xml
407
Virtualization Administration Guide
...
<devices>
<channel type='unix'>
<source mode='bind' path='/tmp/guestfwd'/>
<target type='guestfwd' address='10.0.2.1' port='4600'/>
</channel>
This can be implemented in a variety of ways. The specific type of <channel> is given in the type
attribute of the <target> element. Different channel types have different target attributes as follows:
guestfwd - Dictates that TCP traffic sent by the guest virtual machine to a given IP address and
port is forwarded to the channel device on the host physical machine. The target element
must have address and port attributes.
virtio - Paravirtualized virtio channel. <channel> is exposed in the guest virtual machine
under /dev/vport*, and if the optional element nameis specified, /dev/virtio-
ports/$name (for more info, see http://fedoraproject.org/wiki/Features/VirtioSerial). The
optional element address can tie the channel to a particular type='virtio-serial'
controller, documented above. With QEMU, if name is "org.qemu.guest_agent.0", then libvirt
can interact with a guest virtual machine agent installed in the guest virtual machine, for actions
such as guest virtual machine shutdown or file system quiescing.
spicevmc - Paravirtualized SPICE channel. The domain must also have a SPICE server as a
graphics device, at which point the host physical machine piggy-backs messages across the
main channel. The target element must be present, with attribute type='virtio'; an
optional attribute name controls how the guest virtual machine will have access to the channel,
and defaults to name='com.redhat.spice.0'. The optional <address> element can tie the
channel to a particular type='virtio-serial' controller.
408
CHAPTER 20. MANIPULATING THE DOMAIN XML
<console
type='stdio'>
<target port='1'/>
</console>
</devices>
<serial type="file">
<source
path="/var/log/vm/vm
-serial.log"/>
<target port="1"/>
</serial>
</devices>
<target port="1"/>
</serial>
</devices>
409
Virtualization Administration Guide
<target port="1"/>
</serial>
</devices>
<source
path="/dev/pts/3"/>
<target port="1"/>
</serial>
</devices>
410
CHAPTER 20. MANIPULATING THE DOMAIN XML
</serial>
</devices>
<serial type="pipe">
<source
path="/tmp/mypipe"/>
<target port="1"/>
</serial>
</devices>
<serial type="tcp">
<source
mode="connect"
host="0.0.0.0"
service="2445"/>
<protocol
type="raw"/>
<target port="1"/>
</serial>
</devices>
411
Virtualization Administration Guide
<devices>
<serial type="tcp">
<source mode="bind"
host="127.0.0.1"
service="2445"/>
<protocol
type="raw"/>
<target port="1"/>
</serial>
</devices>
<devices>
<serial type="tcp">
<source
mode="connect"
host="0.0.0.0"
service="2445"/>
<protocol
type="telnet"/>
<target port="1"/>
</serial>
<serial type="tcp">
<source mode="bind"
host="127.0.0.1"
service="2445"/>
<protocol
type="telnet"/>
<target port="1"/>
</serial>
</devices>
412
CHAPTER 20. MANIPULATING THE DOMAIN XML
Parameter
UDP network console Description
The character device acts as a XML snippet
UDP netconsole service, sending
and receiving packets. This is a <devices>
lossy service.
<serial type="udp">
<source mode="bind"
host="0.0.0.0"
service="2445"/>
<source
mode="connect"
host="0.0.0.0"
service="2445"/>
<target port="1"/>
</serial>
</devices>
<source mode="bind"
path="/tmp/foo"/>
<target port="1"/>
</serial>
</devices>
...
<devices>
<sound model='es1370'/>
</devices>
...
413
Virtualization Administration Guide
The sound element has one mandatory attribute, model, which specifies what real sound device is
emulated. Valid values are specific to the underlying hypervisor, though typical choices are 'es1370',
'sb16', 'ac97', and 'ich6'. In addition, a sound element with ich6 model can have optional sub-
elements codec to attach various audio codecs to the audio device. If not specified, a default codec will
be attached to allow playback and recording. Valid values are 'duplex' (advertises a line-in and a line-
out) and 'micro' (advertises a speaker and a microphone).
...
<devices>
<sound model='ich6'>
<codec type='micro'/>
<sound/>
</devices>
...
Each sound element has an optional sub-element <address> which can tie the device to a particular
PCI slot, documented above.
...
<devices>
<watchdog model='i6300esb'/>
</devices>
...
...
<devices>
<watchdog model='i6300esb' action='poweroff'/>
</devices>
</domain>
model - The required model attribute specifies what real watchdog device is emulated. Valid
values are specific to the underlying hypervisor.
414
CHAPTER 20. MANIPULATING THE DOMAIN XML
action - The optional action attribute describes what action to take when the watchdog
expires. Valid values are specific to the underlying hypervisor. The action attribute can have
the following values:
shutdown — gracefully shuts down the guest virtual machine (not recommended)
Note that the 'shutdown' action requires that the guest virtual machine is responsive to ACPI signals. In
the sort of situations where the watchdog has expired, guest virtual machines are usually unable to
respond to ACPI signals. Therefore using 'shutdown' is not recommended. In addition, the directory to
save dump files can be configured by auto_dump_path in file /etc/libvirt/qemu.conf.
...
<devices>
<memballoon model='virtio'/>
</devices>
...
Here is an example where the device is added manually with static PCI slot 2 requested
...
<devices>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02'
function='0x0'/>
</memballoon>
</devices>
</domain>
415
Virtualization Administration Guide
The required model attribute specifies what type of balloon device is provided. Valid values are specific
to the virtualization platform are: 'virtio' which is the default setting with the KVM hypervisor or
'xen' which is the default setting with the Xen hypervisor.
If more than one security driver is used by libvirt, multiple seclabel tags can be used, one for each driver
and the security driver referenced by each tag can be defined using the attribute model Valid input XML
configurations for the top-level security label are:
<seclabel type='none'/>
If no 'type' attribute is provided in the input XML, then the security driver default setting will be used,
which may be either 'none' or 'dynamic'. If a <baselabel> is set but no 'type' is set, then the
type is presumed to be 'dynamic'. When viewing the XML for a running guest virtual machine with
automatic resource relabeling active, an additional XML element, imagelabel, will be included. This is
an output-only element, so will be ignored in user supplied XML documents.
type - Either static, dynamic or none to determine whether libvirt automatically generates a
unique security label or not.
model - A valid security model name, matching the currently activated security model
relabel - Either yes or no. This must always be yes if dynamic label assignment is used.
With static label assignment it will default to no.
416
CHAPTER 20. MANIPULATING THE DOMAIN XML
<label> - If static labelling is used, this must specify the full security label to assign to the
virtual domain. The format of the content depends on the security driver in use:
DAC: owner and group separated by colon. They can be defined both as user/group names
or uid/gid. The driver will first try to parse these values as names, but a leading plus sign can
used to force the driver to parse them as uid or gid.
<baselabel> - If dynamic labelling is used, this can optionally be used to specify the base
security label. The format of the content depends on the security driver in use.
<imagelabel> - This is an output only element, which shows the security label used on
resources associated with the virtual domain. The format of the content depends on the security
driver in use When relabeling is in effect, it is also possible to fine-tune the labeling done for
specific source file names, by either disabling the labeling (useful if the file lives on NFS or other
file system that lacks security labeling) or requesting an alternate label (useful when a
management application creates a special label to allow sharing of some, but not all, resources
between domains). When a seclabel element is attached to a specific path rather than the top-
level domain assignment, only the attribute relabel or the sub-element label are supported.
417
Virtualization Administration Guide
<domain type='qemu'>
<name>QEmu-fedora-i686</name>
<uuid>c7a5fdbd-cdaf-9455-926a-d65c16db1809</uuid>
<memory>219200</memory>
<currentMemory>219200</currentMemory>
<vcpu>2</vcpu>
<os>
<type arch='i686' machine='pc'>hvm</type>
<boot dev='cdrom'/>
</os>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type='file' device='cdrom'>
<source file='/home/user/boot.iso'/>
<target dev='hdc'/>
<readonly/>
</disk>
<disk type='file' device='disk'>
<source file='/home/user/fedora.img'/>
<target dev='hda'/>
</disk>
<interface type='network'>
<source network='default'/>
</interface>
<graphics type='vnc' port='-1'/>
</devices>
</domain>
418
CHAPTER 20. MANIPULATING THE DOMAIN XML
<domain type='kvm'>
<name>demo2</name>
<uuid>4dea24b3-1d52-d8f3-2516-782e98a23fa0</uuid>
<memory>131072</memory>
<vcpu>1</vcpu>
<os>
<type arch="i686">hvm</type>
</os>
<clock sync="localtime"/>
<devices>
<emulator>/usr/bin/qemu-kvm</emulator>
<disk type='file' device='disk'>
<source file='/var/lib/libvirt/images/demo2.img'/>
<target dev='hda'/>
</disk>
<interface type='network'>
<source network='default'/>
<mac address='24:42:53:21:52:45'/>
</interface>
<graphics type='vnc' port='-1' keymap='de'/>
</devices>
</domain>
419
Virtualization Administration Guide
Read this chapter to develop an understanding of some of the common problems associated with
virtualization technologies. Troubleshooting takes practice and experience which are difficult to learn
from a book. It is recommended that you experiment and test virtualization on Red Hat Enterprise Linux 6
to develop your troubleshooting skills.
If you cannot find the answer in this document there may be an answer online from the virtualization
community. Refer to Section B.1, “Online Resources” for a list of Linux virtualization websites.
trace-cmd
vmstat
iostat
lsof
systemtap
crash
sysrq
sysrq t
sysrq w
These networking tools can assist with troubleshooting virtualization networking problems:
ifconfig
tcpdump
The tcpdump command 'sniffs' network packets. tcpdump is useful for finding network
abnormalities and problems with network authentication. There is a graphical version of
tcpdump named wireshark.
brctl
brctl is a networking tool that inspects and configures the Ethernet bridge configuration in the
Linux kernel. You must have root access before performing these example commands:
420
CHAPTER 21. TROUBLESHOOTING
# brctl show
bridge-name bridge-id STP enabled interfaces
----------------------------------------------------------------
-------------
virtbr0 8000.feffffff yes eth0
Listed below are some other useful commands for troubleshooting virtualization.
strace is a command which traces system calls and events received and used by another
process.
vncviewer: connect to a VNC server running on your server or a virtual machine. Install
vncviwer using the yum install tigervnc command.
vncserver: start a remote desktop on your server. Gives you the ability to run graphical user
interfaces such as virt-manager via a remote session. Install vncserver using the yum install
tigervnc-server command.
Guest virtual machine files created by virt-manager when saving the current state of a
421
Virtualization Administration Guide
The guest virtual machine snapshot files, created by the qemu-img create and virsh
snapshot-create commands and found in the locations specified for the commands by
the user.
Guest virtual machine disk images created by virt-manager, if any, which can be found in
the /var/lib/libvirt/images/ directory. If the virsh pool-define command was
used to create virtual storage, the image file can be found in the location specified for virsh
pool-define by the user. For instructions on how to back up the guest image files, use the
steps described in Procedure 21.1, “Creating a backup of the guest virtual machine's disk
image for disaster recovery purposes”.
If you are using bridges, you will also need to back up the files located in
/etc/sysconfig/network-scripts/ifcfg-<bridge_name>
Procedure 21.1. Creating a backup of the guest virtual machine's disk image for disaster recovery
purposes
This procedure will cover how to back up several different disk image types.
1. To back up only the guest virtual machine disk image, back up the files located in
/var/lib/libvirt/images. To back up guest virtual machine disk images with LVM logical
volumes, run the following command:
This command creates a snapshot volume named snap with a size of 8G as part of a 64G
volume.
2. Create a file for the snapshots using a command similar to this one:
# mkdir /mnt/virt.snapshot
3. Mount the directory you created and the snapshot volume using the following command:
422
CHAPTER 21. TROUBLESHOOTING
While the domain (guest virtual machine domain name) and corefilepath (location of the newly created
core dump file) are mandatory, the following arguments are optional:
--live creates a dump file on a running machine and does not pause it.
--crash stops the guest virtual machine and generates the dump file. The main difference is
that the guest virtual machine will not be listed as Stopped, with the reason as Crashed. Note
that in virt-manager the status will be listed as Paused.
--reset will reset the guest virtual machine following a successful dump. Note, these three
switches are mutually exclusive.
--memory-only the dump file will be saved as an elf file, and will only include domain’s
memory and cpu common register value. This option is very useful if the domain uses host
devices directly.
The entire dump process may be monitored using virsh domjobinfo command and can be canceled
by running virsh domjobabort.
21.4. KVM_STAT
The kvm_stat command is a python script which retrieves runtime statistics from the kvm kernel
module. The kvm_stat command can be used to diagnose guest behavior visible to kvm. In particular,
performance related issues with guests. Currently, the reported statistics are for the entire system; the
behavior of all running guests is reported. To run this script you need to install the qemu-kvm-tools
package.
The kvm_stat command requires that the kvm kernel module is loaded and debugfs is mounted. If
either of these features are not enabled, the command will output the required steps to enable debugfs
or the kvm module. For example:
# kvm_stat
Please mount debugfs ('mount -t debugfs debugfs /sys/kernel/debug')
and ensure the kvm modules are loaded
423
Virtualization Administration Guide
kvm_stat Output
The kvm_stat command outputs statistics for all guests and the host. The output is updated until the
command is terminated (using Ctrl+c or the q key).
# kvm_stat
kvm statistics
efer_reload 94 0
exits 4003074 31272
fpu_reload 1313881 10796
halt_exits 14050 259
halt_wakeup 4496 203
host_state_reload 1638354 24893
hypercalls 0 0
insn_emulation 1093850 1909
insn_emulation_fail 0 0
invlpg 75569 0
io_exits 1596984 24509
irq_exits 21013 363
irq_injections 48039 1222
irq_window 24656 870
largepages 0 0
mmio_exits 11873 0
mmu_cache_miss 42565 8
mmu_flooded 14752 0
mmu_pde_zapped 58730 0
mmu_pte_updated 6 0
mmu_pte_write 138795 0
mmu_recycled 0 0
mmu_shadow_zapped 40358 0
mmu_unsync 793 0
nmi_injections 0 0
nmi_window 0 0
pf_fixed 697731 3150
pf_guest 279349 0
remote_tlb_flush 5 0
request_irq 0 0
signal_exits 1 0
tlb_flush 200190 0
Explanation of variables:
efer_reload
The number of Extended Feature Enable Register (EFER) reloads.
exits
The count of all VMEXIT calls.
fpu_reload
The number of times a VMENTRY reloaded the FPU state. The fpu_reload is incremented when a
guest is using the Floating Point Unit (FPU).
424
CHAPTER 21. TROUBLESHOOTING
halt_exits
Number of guest exits due to halt calls. This type of exit is usually seen when a guest is idle.
halt_wakeup
Number of wakeups from a halt.
host_state_reload
Count of full reloads of the host state (currently tallies MSR setup and guest MSR reads).
hypercalls
Number of guest hypervisor service calls.
insn_emulation
Number of guest instructions emulated by the host.
insn_emulation_fail
Number of failed insn_emulation attempts.
io_exits
Number of guest exits from I/O port accesses.
irq_exits
Number of guest exits due to external interrupts.
irq_injections
Number of interrupts sent to guests.
irq_window
Number of guest exits from an outstanding interrupt window.
largepages
Number of large pages currently in use.
mmio_exits
Number of guest exits due to memory mapped I/O (MMIO) accesses.
mmu_cache_miss
Number of KVM MMU shadow pages created.
mmu_flooded
Detection count of excessive write operations to an MMU page. This counts detected write operations
not of individual write operations.
mmu_pde_zapped
Number of page directory entry (PDE) destruction operations.
425
Virtualization Administration Guide
mmu_pte_updated
Number of page table entry (PTE) destruction operations.
mmu_pte_write
Number of guest page table entry (PTE) write operations.
mmu_recycled
Number of shadow pages that can be reclaimed.
mmu_shadow_zapped
Number of invalidated shadow pages.
mmu_unsync
Number of non-synchronized pages which are not yet unlinked.
nmi_injections
Number of Non-maskable Interrupt (NMI) injections to the guest.
nmi_window
Number of guest exits from (outstanding) Non-maskable Interrupt (NMI) windows.
pf_fixed
Number of fixed (non-paging) page table entry (PTE) maps.
pf_guest
Number of page faults injected into guests.
remote_tlb_flush
Number of remote (sibling CPU) Translation Lookaside Buffer (TLB) flush requests.
request_irq
Number of guest interrupt window request exits.
signal_exits
Number of guest exits due to pending signals from the host.
tlb_flush
Number of tlb_flush operations performed by the hypervisor.
NOTE
The output information from the kvm_stat command is exported by the KVM hypervisor
as pseudo files located in the /sys/kernel/debug/kvm/ directory.
426
CHAPTER 21. TROUBLESHOOTING
Traditionally, executing a virsh shutdown command causes a power button ACPI event to be sent,
thus copying the same action as when someone presses a power button on a physical machine. Within
every physical machine, it is up to the OS to handle this event. In the past operating systems would just
silently shutdown. Today, the most usual action is to show a dialog asking what should be done. Some
operating systems even ignore this event completely, especially when no users are logged in. When
such operating systems are installed on a guest virtual machine, running virsh shutdown just does
not work (it is either ignored or a dialog is shown on a virtual display). However, if a qemu-guest-agent
channel is added to a guest virtual machine and this agent is running inside the guest virtual machine's
OS, the virsh shutdown command will ask the agent to shutdown the guest OS instead of sending the
ACPI event. The agent will call for a shutdown from inside the guest virtual machine OS and everything
works as expected.
Procedure 21.2. Configuring the guest agent channel in a guest virtual machine
2. Open the Domain XML for the guest virtual machine and add the following snippet:
<channel type='unix'>
<source mode='bind'/>
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
This section covers how to enable serial console output for fully virtualized guests.
Fully virtualized guest serial console output can be viewed with the virsh console command.
Be aware fully virtualized guest serial consoles have some limitations. Present limitations include:
You must configure the virtualized operating system to output information to the virtual serial port.
To output kernel information from a fully virtualized Linux guest into the domain, modify the
/boot/grub/grub.conf file. Append the following to the kernel line: console=tty0
console=ttyS0,115200.
427
Virtualization Administration Guide
root (hd0,0)
kernel /vmlinuz-2.6.32-36.x86-64 ro root=/dev/volgroup00/logvol00 \
console=tty0 console=ttyS0,115200
initrd /initrd-2.6.32-36.x86-64.img
On the host, access the serial console with the following command:
# virsh console
You can also use virt-manager to display the virtual text console. In the guest console window, select
Serial 1 in Text Consoles from the View menu.
If you encounter any errors with the Virtual Machine Manager, you can review the generated data in the
virt-manager.log file that resides in the $HOME/.virt-manager directory.
This example uses 64 but you can specify another number to set the maximum loop value. You may also
have to implement loop device backed guests on your system. To use a loop device backed guests for a
full virtualized system, use the phy: device or file: file commands.
The current live-migration implementation has a default migration time configured to 30ms. This value
determines the guest pause time at the end of the migration in order to transfer the leftovers. Higher
values increase the odds that live migration will converge
428
CHAPTER 21. TROUBLESHOOTING
NOTE
To expand your expertise, you might also be interested in the Red Hat Virtualization
(RH318) training course.
This section describes how to identify hardware virtualization extensions and enable them in your BIOS if
they are disabled.
The Intel VT-x extensions can be disabled in the BIOS. Certain laptop vendors have disabled the Intel
VT-x extensions by default in their CPUs.
Refer to the following section for instructions on enabling disabled virtualization extensions.
Verify the virtualization extensions are enabled in BIOS. The BIOS settings for Intel VT or AMD-V are
usually in the Chipset or Processor menus. The menu names may vary from this guide, the
virtualization extension settings may be found in Security Settings or other non standard menu
names.
1. Reboot the computer and open the system's BIOS menu. This can usually be done by pressing
the delete key, the F1 key or Alt and F4 keys depending on the system.
NOTE
Many of the steps below may vary depending on your motherboard, processor
type, chipset and OEM. Refer to your system's accompanying documentation for
the correct information on configuring your system.
a. Open the Processor submenu The processor settings menu may be hidden in the
Chipset, Advanced CPU Configuration or Northbridge.
c. Enable Intel VT-d or AMD IOMMU, if the options are available. Intel VT-d and AMD IOMMU
are used for PCI device assignment.
4. When the machine has booted, run cat /proc/cpuinfo |grep -E "vmx|svm".
Specifying --color is optional, but useful if you want the search term highlighted. If the
command outputs, the virtualization extensions are now enabled. If there is no output your
system may not have the virtualization extensions or the correct BIOS setting enabled.
429
Virtualization Administration Guide
By default, KVM virtual machines are assigned a virtual Realtek 8139 (rtl8139) NIC (network interface
controller). Whereas Red Hat Enterprise Linux guests are assigned a virtio NIC by default, Windows
guests or the guest type is not specified.
The rtl8139 virtualized NIC works fine in most environments,but this device can suffer from performance
degradation problems on some networks, such as a 10 Gigabit Ethernet.
NOTE
Note that the virtualized Intel PRO/1000 (e1000) driver is also supported as an emulated
driver choice. To use the e1000 driver, replace virtio in the procedure below with
e1000. For the best performance it is recommended to use the virtio driver.
2. Edit the guest's configuration file with the virsh command (where GUEST is the guest's name):
The virsh edit command uses the $EDITOR shell variable to determine which editor to use.
3. Find the network interface section of the configuration. This section resembles the snippet
below:
<interface type='network'>
[output truncated]
<model type='rtl8139' />
</interface>
4. Change the type attribute of the model element from 'rtl8139' to 'virtio'. This will change
the driver from the rtl8139 driver to the e1000 driver.
<interface type='network'>
[output truncated]
<model type='virtio' />
</interface>
1. Create an XML template from an existing guest (in this example, named Guest1):
430
CHAPTER 21. TROUBLESHOOTING
2. Copy and edit the XML file and update the unique fields: virtual machine name, UUID, disk
image, MAC address, and any other unique parameters. Note that you can delete the UUID and
MAC address lines and virsh will generate a UUID and MAC address.
# cp /tmp/guest-template.xml /tmp/new-guest.xml
# vi /tmp/new-guest.xml
<interface type='network'>
[output truncated]
<model type='virtio' />
</interface>
External snapshots, on the other hand work with any type of original disk image, can be taken with no
guest downtime, and are able to receive active improvements from QEMU. In libvirt, they are created
when using the --disk-only option to snapshot-create-as (or when specifying an explicit XML file
to snapshot-create that does the same). At the moment external snapshots are a one-way operation
as libvirt can create them but cannot do anything further with them.
With Red Hat Enterprise Linux 3 and Red Hat Enterprise Linux 6 guests, there is usually no error
message produced when pressing the associated key. However, Red Hat Enterprise Linux 4 and Red
Hat Enterprise Linux 5 guests may display an error similar to the following:
431
Virtualization Administration Guide
Alternatively, to fix this issue using the virsh edit command on the target guest:
Add the following attribute to the <graphics> tag: keymap='ja'. For example:
1. Run the following command to verify the CPU virtualization extensions are available:
The following output contains a vmx entry indicating an Intel processor with the Intel VT-x
extension:
flags : fpu tsc msr pae mce cx8 apic mtrr mca cmov pat pse36
clflush
dts acpi mmx fxsr sse sse2 ss ht tm syscall lm constant_tsc pni
monitor ds_cpl
vmx est tm2 cx16 xtpr lahf_lm
The following output contains an svm entry indicating an AMD processor with the AMD-V
extensions:
flags : fpu tsc msr pae mce cx8 apic mtrr mca cmov pat pse36
clflush
mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt lm 3dnowext
3dnow pni cx16
lahf_lm cmp_legacy svm cr8legacy ts fid vid ttp tm stc
If any output is received, the processor has the hardware virtualization extensions. However in
some circumstances manufacturers disable the virtualization extensions in BIOS.
The "flags:" output content may appear multiple times, once for each hyperthread, core or
CPU on the system.
432
CHAPTER 21. TROUBLESHOOTING
The virtualization extensions may be disabled in the BIOS. If the extensions do not appear or full
virtualization does not work refer to Procedure 21.3, “Enabling virtualization extensions in BIOS”.
If the output includes kvm_intel or kvm_amd then the kvm hardware virtualization modules are
loaded and your system meets requirements.
NOTE
If the libvirt package is installed, the virsh command can output a full list of virtualization
system capabilities. Run virsh capabilities as root to receive the complete list.
433
Virtualization Administration Guide
In the host, a daemon (vhostmd) runs which writes metrics periodically into a disk image. This disk
image is exported read-only to guest virtual machines. Guest virtual machines can read the disk image to
see metrics. Simple synchronization stops guest virtual machines from seeing out of date or corrupt
metrics.
The system administrator chooses which metrics are available for use on a per guest virtual machine
basis. In addition, the system administrator may block one or more guest virtual machines from having
any access to metric configurations.
Customers who want to use vhostmd and vm-dump-metrics therefore need subscriptions for "RHEL
for SAP Business Applications" to be able to subscribe their RHEL systems running SAP to the "RHEL
for SAP" channel on the Customer Portal or Red Hat Subscription Management to install the packages.
The following kbase article in the customer portal describes the setup of vhostmd on RHEL:
https://access.redhat.com/knowledge/solutions/41566
434
APPENDIX B. ADDITIONAL RESOURCES
https://virt-manager.org/ is the project website for the Virtual Machine Manager (virt-manager),
the graphical application for managing virtual machines.
435
Virtualization Administration Guide
436