1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head xmlns="http://www.w3.org/1999/xhtml">
5
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6
<title xmlns="">libvirt</title>
7
<link rel="stylesheet" href="../../libs/ubuntu-book.css" type="text/css" />
8
<link rel="home" href="index.html" title="Ubuntu Server Guide" />
9
<link rel="up" href="virtualization.html" title="Chapter 19. Virtualization" />
10
<link rel="prev" href="virtualization.html" title="Chapter 19. Virtualization" />
11
<link rel="next" href="jeos-and-vmbuilder.html" title="JeOS and vmbuilder" />
12
<link rel="copyright" href="legal.html" title="Credits and License" />
16
<img id="topcap" alt="" src="https://help.ubuntu.com/htdocs/ubuntunew/img/cap-top.png" />
17
<div id="layout" class="container clear-block">
18
<script xmlns="" src="https://ssl.google-analytics.com/urchin.js" type="text/javascript"></script>
19
<script xmlns="" type="text/javascript">
20
_uacct = "UA-1018242-8";
24
<div id="logo-floater">
26
<a href="https://help.ubuntu.com" title="Ubuntu Documentation">
27
<img alt="Ubuntu" id="logo" src="https://help.ubuntu.com/htdocs/ubuntunew/img/logo.png" />
32
<form action="http://www.google.com/cse" id="cse-search-box">
34
<input type="hidden" name="cx" value="003883529982892832976:e2vwumte3fq" />
35
<input type="hidden" name="ie" value="UTF-8" />
36
<input type="text" name="q" size="27" />
37
<input type="submit" name="sa" value="Search" />
42
document.write('<form action="https://help.ubuntu.com/search.html" id="cse-search-box">');
43
document.write(' <div>');
44
document.write(' <input type="hidden" name="cof" value="FORID:9" />');
45
document.write(' <input type="hidden" name="cx" value="003883529982892832976:e2vwumte3fq" />');
46
document.write(' <input type="hidden" name="ie" value="UTF-8" />');
47
document.write(' <input type="text" name="q" size="27" />');
48
document.write(' <input type="submit" name="sa" value="Search" />');
49
document.write(' </div>');
50
document.write('</form>');
53
<a href="https://help.ubuntu.com/">
54
<img alt="Official Documentation" src="https://help.ubuntu.com/htdocs/ubuntunew/img/help-about.png" />
55
<span>Official Documentation</span>
61
<div class="breadcrumbs"><a href="https://help.ubuntu.com/">Ubuntu Documentation</a> > <a href="https://help.ubuntu.com/11.04">Ubuntu 11.04</a> > <span class="breadcrumb-link"><a href="index.html">Ubuntu Server Guide</a></span> > <span class="breadcrumb-link"><a href="virtualization.html">Virtualization</a></span> > <span class="breadcrumb-node">libvirt</span></div>
62
<div xmlns="http://www.w3.org/1999/xhtml" class="sect1" title="libvirt">
63
<div class="titlepage">
66
<h2 class="title" style="clear: both"><a id="libvirt"></a>libvirt</h2>
71
The <span class="application"><strong>libvirt</strong></span> library is used to interface with different
72
virtualization technologies.
73
Before getting started with <span class="application"><strong>libvirt</strong></span> it is best to make sure your hardware supports the necessary
74
virtualization extensions for <span class="application"><strong>KVM</strong></span>. Enter the following from a terminal prompt:
77
<span class="command"><strong>kvm-ok</strong></span>
80
A message will be printed informing you if your CPU <span class="emphasis"><em>does</em></span> or <span class="emphasis"><em>does not</em></span> support hardware virtualization.
82
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
83
<table border="0" summary="Note">
85
<td rowspan="2" align="center" valign="top" width="25">
86
<img alt="[Note]" src="../../libs/admon/note.png" />
88
<th align="left"></th>
91
<td align="left" valign="top">
93
On most computer whose processor supports virtualization, it is
94
necessary to activate an option in the BIOS to enable it.
100
<div class="sect2" title="Virtual Networking">
101
<div class="titlepage">
104
<h3 class="title"><a id="virtual-networking"></a>Virtual Networking</h3>
109
There are a few different ways to allow a virtual machine access to the external network. The default virtual network configuration is
110
<span class="emphasis"><em>usermode</em></span> networking, which uses the SLIRP protocol and traffic is NATed through the host interface to the outside network.
113
To enable external hosts to directly access services on virtual machines a <span class="emphasis"><em>bridge</em></span> needs to be configured. This allows
114
the virtual interfaces to connect to the outside network through the physical interface, making them appear as normal hosts to the rest
115
of the network. For information on setting up a bridge see <a class="xref" href="network-configuration.html#bridging" title="Bridging">the section called “Bridging”</a>.
118
<div class="sect2" title="Installation">
119
<div class="titlepage">
122
<h3 class="title"><a id="libvirt-installation"></a>Installation</h3>
127
To install the necessary packages, from a terminal prompt enter:
130
<span class="command"><strong>sudo apt-get install kvm libvirt-bin</strong></span>
133
After installing <span class="application"><strong>libvirt-bin</strong></span>, the user used to manage virtual machines will need to be added to
134
the <span class="emphasis"><em>libvirtd</em></span> group. Doing so will grant the user access to the advanced networking options.
140
<span class="command"><strong>sudo adduser $USER libvirtd</strong></span>
142
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
143
<table border="0" summary="Note">
145
<td rowspan="2" align="center" valign="top" width="25">
146
<img alt="[Note]" src="../../libs/admon/note.png" />
148
<th align="left"></th>
151
<td align="left" valign="top">
153
If the user chosen is the current user, you will need to log out and back in for the new group membership to take effect.
160
You are now ready to install a <span class="emphasis"><em>Guest</em></span> operating system. Installing a virtual machine follows the same
161
process as installing the operating system directly on the hardware. You either need a way to automate the installation, or
162
a keyboard and monitor will need to be attached to the physical machine.
165
In the case of virtual machines a Graphical User Interface (GUI) is analogous to using a physical keyboard and mouse. Instead of installing
166
a GUI the <span class="application"><strong>virt-viewer</strong></span> application can be used to connect to a virtual machine's console using <span class="application"><strong>VNC</strong></span>.
167
See <a class="xref" href="libvirt.html#libvirt-virt-viewer" title="Virtual Machine Viewer">the section called “Virtual Machine Viewer”</a> for more information.
170
There are several ways to automate the Ubuntu installation process, for example using
171
preseeds, kickstart, etc. Refer to the <a class="ulink" href="https://help.ubuntu.com/11.04/installation-guide/" target="_top">Ubuntu Installation Guide</a> for details.
174
Yet another way to install an Ubuntu virtual machine is to use <span class="application"><strong>ubuntu-vm-builder</strong></span>. <span class="application"><strong>ubuntu-vm-builder</strong></span>
175
allows you to setup advanced partitions, execute custom post-install scripts, etc. For details see <a class="xref" href="jeos-and-vmbuilder.html" title="JeOS and vmbuilder">the section called “JeOS and vmbuilder”</a>
178
<div class="sect2" title="virt-install">
179
<div class="titlepage">
182
<h3 class="title"><a id="libvirt-virt-install"></a>virt-install</h3>
187
<span class="application"><strong>virt-install</strong></span> is part of the <span class="application"><strong>virtinst</strong></span> package. To install it, from a terminal prompt enter:
190
<span class="command"><strong>sudo apt-get install virtinst</strong></span>
193
There are several options available when using <span class="application"><strong>virt-install</strong></span>. For example:
196
<span class="command"><strong>sudo virt-install -n web_devel -r 256 \
197
--disk path=/var/lib/libvirt/images/web_devel.img,size=4 -c jeos.iso --accelerate \
198
--connect=qemu:///system --vnc \
199
--noautoconsole -v</strong></span>
201
<div class="itemizedlist">
202
<ul class="itemizedlist" type="disc">
203
<li class="listitem">
205
<span class="emphasis"><em>-n web_devel:</em></span> the name of the new virtual machine will be <span class="emphasis"><em>web_devel</em></span> in this example.
208
<li class="listitem">
210
<span class="emphasis"><em>-r 256:</em></span> specifies the amount of memory the virtual machine will use in megabytes.
213
<li class="listitem">
215
<span class="emphasis"><em>--disk path=/var/lib/libvirt/images/web_devel.img,size=4:</em></span> indicates the path to the virtual disk
216
which can be a file, partition, or logical volume. In this example a file named <code class="filename">web_devel.img</code> in the
217
/var/lib/libvirt/images/ directory with a size of 4 gigabytes.
220
<li class="listitem">
222
<span class="emphasis"><em>-c jeos.iso:</em></span> file to be used as a virtual CDROM. The file can be either an ISO file or the path to the
226
<li class="listitem">
228
<span class="emphasis"><em>--accelerate:</em></span> enables the kernel's acceleration technologies.
231
<li class="listitem">
233
<span class="emphasis"><em>--vnc:</em></span> exports the guest's virtual console using VNC.
236
<li class="listitem">
238
<span class="emphasis"><em>--noautoconsole:</em></span> will not automatically connect to the virtual machine's console.
241
<li class="listitem">
243
<span class="emphasis"><em>-v:</em></span> creates a fully virtualized guest.
249
After launching <span class="application"><strong>virt-install</strong></span> you can connect to the virtual machine's console either locally
250
using a GUI or with the <span class="application"><strong>virt-viewer</strong></span> utility.
253
<div class="sect2" title="virt-clone">
254
<div class="titlepage">
257
<h3 class="title"><a id="libvirt-virt-clone"></a>virt-clone</h3>
262
The <span class="application"><strong>virt-clone</strong></span> application can be used to copy one virtual machine to another. For example:
265
<span class="command"><strong>sudo virt-clone -o web_devel -n database_devel -f /path/to/database_devel.img --connect=qemu:///system</strong></span>
267
<div class="itemizedlist">
268
<ul class="itemizedlist" type="disc">
269
<li class="listitem">
271
<span class="emphasis"><em>-o:</em></span> original virtual machine.
274
<li class="listitem">
276
<span class="emphasis"><em>-n:</em></span> name of the new virtual machine.
279
<li class="listitem">
281
<span class="emphasis"><em>-f:</em></span> path to the file, logical volume, or partition to be used by the new virtual machine.
284
<li class="listitem">
286
<span class="emphasis"><em>--connect:</em></span> specifies which hypervisor to connect to.
292
Also, use <span class="emphasis"><em>-d</em></span> or <span class="emphasis"><em>--debug</em></span> option to help troubleshoot problems with
293
<span class="application"><strong>virt-clone</strong></span>.
295
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
296
<table border="0" summary="Note">
298
<td rowspan="2" align="center" valign="top" width="25">
299
<img alt="[Note]" src="../../libs/admon/note.png" />
301
<th align="left"></th>
304
<td align="left" valign="top">
306
Replace <span class="emphasis"><em>web_devel</em></span> and <span class="emphasis"><em>database_devel</em></span> with appropriate virtual machine names.
313
<div class="sect2" title="Virtual Machine Management">
314
<div class="titlepage">
317
<h3 class="title"><a id="libvirt-management"></a>Virtual Machine Management</h3>
321
<div class="sect3" title="virsh">
322
<div class="titlepage">
325
<h4 class="title"><a id="virsh"></a>virsh</h4>
330
There are several utilities available to manage virtual machines and <span class="application"><strong>libvirt</strong></span>.
331
The <span class="application"><strong>virsh</strong></span> utility can be used from the command line. Some examples:
333
<div class="itemizedlist">
334
<ul class="itemizedlist" type="disc">
335
<li class="listitem">
337
To list running virtual machines:
340
<span class="command"><strong>virsh -c qemu:///system list</strong></span>
343
<li class="listitem">
345
To start a virtual machine:
348
<span class="command"><strong>virsh -c qemu:///system start web_devel</strong></span>
351
<li class="listitem">
353
Similarly, to start a virtual machine at boot:
356
<span class="command"><strong>virsh -c qemu:///system autostart web_devel</strong></span>
359
<li class="listitem">
361
Reboot a virtual machine with:
364
<span class="command"><strong>virsh -c qemu:///system reboot web_devel</strong></span>
367
<li class="listitem">
369
The <span class="emphasis"><em>state</em></span> of virtual machines can be saved to a file in order to be restored later. The
370
following will save the virtual machine state into a file named according to the date:
373
<span class="command"><strong>virsh -c qemu:///system save web_devel web_devel-022708.state</strong></span>
376
Once saved the virtual machine will no longer be running.
379
<li class="listitem">
381
A saved virtual machine can be restored using:
384
<span class="command"><strong>virsh -c qemu:///system restore web_devel-022708.state</strong></span>
387
<li class="listitem">
389
To shutdown a virtual machine do:
392
<span class="command"><strong>virsh -c qemu:///system shutdown web_devel</strong></span>
395
<li class="listitem">
397
A CDROM device can be mounted in a virtual machine by entering:
400
<span class="command"><strong>virsh -c qemu:///system attach-disk web_devel /dev/cdrom /media/cdrom</strong></span>
405
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
406
<table border="0" summary="Note">
408
<td rowspan="2" align="center" valign="top" width="25">
409
<img alt="[Note]" src="../../libs/admon/note.png" />
411
<th align="left"></th>
414
<td align="left" valign="top">
416
In the above examples replace <span class="emphasis"><em>web_devel</em></span> with the appropriate virtual machine name, and
417
<code class="filename">web_devel-022708.state</code> with a descriptive file name.
424
<div class="sect3" title="Virtual Machine Manager">
425
<div class="titlepage">
428
<h4 class="title"><a id="virt-manager"></a>Virtual Machine Manager</h4>
433
The <span class="application"><strong>virt-manager</strong></span> package contains a graphical utility to manage local and remote virtual machines.
434
To install virt-manager enter:
437
<span class="command"><strong>sudo apt-get install virt-manager</strong></span>
440
Since <span class="application"><strong>virt-manager</strong></span> requires a Graphical User Interface (GUI) environment it is recommended to be
441
installed on a workstation or test machine instead of a production server. To connect to the local
442
<span class="application"><strong>libvirt</strong></span> service enter:
445
<span class="command"><strong>virt-manager -c qemu:///system</strong></span>
448
You can connect to the <span class="application"><strong>libvirt</strong></span> service running on another host by entering the following in a terminal prompt:
451
<span class="command"><strong>virt-manager -c qemu+ssh://virtnode1.mydomain.com/system</strong></span>
453
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
454
<table border="0" summary="Note">
456
<td rowspan="2" align="center" valign="top" width="25">
457
<img alt="[Note]" src="../../libs/admon/note.png" />
459
<th align="left"></th>
462
<td align="left" valign="top">
464
The above example assumes that <span class="application"><strong>SSH</strong></span> connectivity between the management system and virtnode1.mydomain.com
465
has already been configured, and uses SSH keys for authentication. SSH <span class="emphasis"><em>keys</em></span> are needed because
466
<span class="application"><strong>libvirt</strong></span> sends the password prompt to another process. For details on configuring
467
<span class="application"><strong>SSH</strong></span> see <a class="xref" href="openssh-server.html" title="OpenSSH Server">the section called “OpenSSH Server”</a>
475
<div class="sect2" title="Virtual Machine Viewer">
476
<div class="titlepage">
479
<h3 class="title"><a id="libvirt-virt-viewer"></a>Virtual Machine Viewer</h3>
484
The <span class="application"><strong>virt-viewer</strong></span> application allows you to connect to a virtual machine's console.
485
<span class="application"><strong>virt-viewer</strong></span> does require a Graphical User Interface (GUI) to interface with the virtual machine.
488
To install <span class="application"><strong>virt-viewer</strong></span> from a terminal enter:
491
<span class="command"><strong>sudo apt-get install virt-viewer</strong></span>
494
Once a virtual machine is installed and running you can connect to the virtual machine's console by using:
497
<span class="command"><strong>virt-viewer -c qemu:///system web_devel</strong></span>
500
Similar to <span class="application"><strong>virt-manager</strong></span>, <span class="application"><strong>virt-viewer</strong></span> can connect to a remote host using
501
<span class="emphasis"><em>SSH</em></span> with key authentication, as well:
504
<span class="command"><strong>virt-viewer -c qemu+ssh://virtnode1.mydomain.com/system web_devel</strong></span>
507
Be sure to replace <span class="italic">web_devel</span> with the appropriate virtual machine name.
510
If configured to use a <span class="emphasis"><em>bridged</em></span> network interface you can also setup <span class="application"><strong>SSH</strong></span> access
511
to the virtual machine. See <a class="xref" href="openssh-server.html" title="OpenSSH Server">the section called “OpenSSH Server”</a> and <a class="xref" href="network-configuration.html#bridging" title="Bridging">the section called “Bridging”</a> for more details.
514
<div class="sect2" title="Resources">
515
<div class="titlepage">
518
<h3 class="title"><a id="libvirt-resources"></a>Resources</h3>
522
<div class="itemizedlist">
523
<ul class="itemizedlist" type="disc">
524
<li class="listitem">
526
See the <a class="ulink" href="http://kvm.qumranet.com/kvmwiki" target="_top">KVM</a> home page for more details.
529
<li class="listitem">
531
For more information on <span class="application"><strong>libvirt</strong></span> see the <a class="ulink" href="http://libvirt.org/" target="_top">libvirt home page</a>
534
<li class="listitem">
536
The <a class="ulink" href="http://virt-manager.et.redhat.com/" target="_top">Virtual Machine Manager</a> site has more information on
537
<span class="application"><strong>virt-manager</strong></span> development.
540
<li class="listitem">
542
Also, stop by the <span class="emphasis"><em>#ubuntu-virt</em></span> IRC channel on <a class="ulink" href="http://freenode.net/" target="_top">freenode</a> to
543
discuss virtualization technology in Ubuntu.
546
<li class="listitem">
548
Another good resource is the <a class="ulink" href="https://help.ubuntu.com/community/KVM" target="_top">Ubuntu Wiki KVM</a> page.
556
<div xmlns="http://www.w3.org/1999/xhtml" class="navfooter">
558
<table width="100%" summary="Navigation footer">
560
<td width="40%" align="left"><a accesskey="p" href="virtualization.html"><img src="../../libs/navig/prev.png" alt="Prev" /></a> </td>
561
<td width="20%" align="center">
562
<a accesskey="u" href="virtualization.html">
563
<img src="../../libs/navig/up.png" alt="Up" />
566
<td width="40%" align="right"> <a accesskey="n" href="jeos-and-vmbuilder.html"><img src="../../libs/navig/next.png" alt="Next" /></a></td>
569
<td width="40%" align="left" valign="top">Chapter 19. Virtualization </td>
570
<td width="20%" align="center">
571
<a accesskey="h" href="index.html">
572
<img src="../../libs/navig/home.png" alt="Home" />
575
<td width="40%" align="right" valign="top"> JeOS and vmbuilder</td>
581
<div id="ubuntulinks">
582
<p>The material in this document is available under a free license, see <a href="/legal.html">Legal</a> for details<br />
583
For information on contributing see the <a href="https://wiki.ubuntu.com/DocumentationTeam">Ubuntu Documentation Team wiki page</a>. To report a problem, visit the <a href="https://bugs.launchpad.net/ubuntu/+source/ubuntu-docs">bug page for Ubuntu Documentation</a></p>
587
<img src="https://help.ubuntu.com/htdocs/ubuntunew/img/cap-bottom.png" alt="" />