# WikiStart: ex-debian-kvm-libvirt.htm

File ex-debian-kvm-libvirt.htm, 52.8 KB (added by hans, 4 years ago)
Line
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml">
4  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5  <meta http-equiv="Content-Style-Type" content="text/css" />
6  <meta name="generator" content="pandoc" />
7  <title>Debian, KVM and libvirt</title>
8  <style type="text/css">code{white-space: pre;}</style>
11<body>
13<h1 class="title">Debian, KVM and libvirt</h1>
14</div>
15<div id="TOC">
16<ul>
17<li><a href="#objectives"><span class="toc-section-number">1</span> Objectives</a></li>
18<li><a href="#install-debian"><span class="toc-section-number">2</span> Install Debian</a><ul>
19<li><a href="#partitioning"><span class="toc-section-number">2.1</span> Partitioning</a></li>
20<li><a href="#software-selection"><span class="toc-section-number">2.2</span> Software selection</a></li>
21<li><a href="#basic-configuration"><span class="toc-section-number">2.3</span> Basic configuration</a></li>
22<li><a href="#network-configuration"><span class="toc-section-number">2.4</span> Network configuration</a><ul>
23<li><a href="#edit-etchostname"><span class="toc-section-number">2.4.1</span> Edit <code>/etc/hostname</code></a></li>
24<li><a href="#edit-etchosts-and-change-it-to-look-like-this"><span class="toc-section-number">2.4.2</span> Edit <code>/etc/hosts</code>, and change it to look like this:</a></li>
26<li><a href="#edit-etcnetworkinterfaces"><span class="toc-section-number">2.4.4</span> Edit <code>/etc/network/interfaces</code></a></li>
27<li><a href="#edit-etcresolv.conf"><span class="toc-section-number">2.4.5</span> Edit <code>/etc/resolv.conf</code></a></li>
28<li><a href="#reboot"><span class="toc-section-number">2.4.6</span> Reboot</a></li>
29<li><a href="#test-ssh"><span class="toc-section-number">2.4.7</span> Test ssh</a></li>
30</ul></li>
31</ul></li>
32<li><a href="#virtual-machine-tools"><span class="toc-section-number">3</span> Virtual machine tools</a><ul>
33<li><a href="#install-kvm-and-libvirt"><span class="toc-section-number">3.1</span> Install KVM and <code>libvirt</code></a></li>
34<li><a href="#check-for-hardware-virtualization-support"><span class="toc-section-number">3.2</span> Check for hardware virtualization support</a></li>
35<li><a href="#desktop-environment"><span class="toc-section-number">3.3</span> Desktop environment</a></li>
36<li><a href="#install-virt-manager-gui"><span class="toc-section-number">3.4</span> Install virt-manager GUI</a></li>
37</ul></li>
38<li><a href="#create-a-vm"><span class="toc-section-number">4</span> Create a VM</a><ul>
39<li><a href="#copy-ubuntu-iso-image"><span class="toc-section-number">4.1</span> Copy Ubuntu ISO image</a></li>
40<li><a href="#start-the-virt-manager-gui"><span class="toc-section-number">4.2</span> Start the virt-manager GUI</a></li>
41<li><a href="#create-a-vm-1"><span class="toc-section-number">4.3</span> Create a VM</a></li>
42<li><a href="#look-at-the-kvm-process"><span class="toc-section-number">4.4</span> Look at the KVM process</a></li>
43<li><a href="#find-the-disk-image-file"><span class="toc-section-number">4.5</span> Find the disk image file</a></li>
44</ul></li>
45<li><a href="#using-virsh-cli"><span class="toc-section-number">5</span> Using virsh CLI</a><ul>
46<li><a href="#setup"><span class="toc-section-number">5.1</span> Setup</a></li>
47<li><a href="#simple-commands"><span class="toc-section-number">5.2</span> Simple commands</a></li>
49</ul></li>
52<li><a href="#libvirt-storage-pools"><span class="toc-section-number">6.2</span> libvirt storage pools</a></li>
53<li><a href="#virt-install"><span class="toc-section-number">6.3</span> virt-install</a></li>
54<li><a href="#install-vm-with-qcow2-disk-iamge"><span class="toc-section-number">6.4</span> Install VM with QCOW2 disk iamge</a></li>
55<li><a href="#libvirt-snapshots"><span class="toc-section-number">6.5</span> libvirt snapshots</a></li>
56</ul></li>
57</ul>
58</div>
60<p>Working in pairs, you will build a Linux-based virtualization host server on hardware provided in the class.</p>
61<ul>
62<li>Install Debian Linux. (This is because Ganeti, which we will be using later, is better supported under Debian than Ubuntu)</li>
63<li>Configure LVM during installation with manual partitioning so that most of the volume group is unused</li>
64<li>Use KVM with the <code>virt-manager</code> GUI and the <code>virsh</code> command-line interface to install and manage a VM.</li>
65</ul>
66<p>NOTE: please take care to install LVM as shown, creating some small logical volumes for the system but leaving most of the volume group unused, because later exercises will need to create more logical volumes from the free space.</p>
67<p>Please also name your volume group &quot;ganeti&quot;, because this has to be consistent throughout a cluster of machines.</p>
69<p>It may be necessary to press a key (e.g. F12) to get the option to boot from the USB stick.</p>
70<p>If you get the Debian splash screen, select &quot;install&quot; instead of &quot;graphical install&quot;.</p>
71<ul>
72<li>Language: English</li>
73<li>Country: as appropriate</li>
74<li>Locale settings: United States</li>
75<li>Keymap: American English</li>
76<li>Hostname: as allocated by instructor (hostX where X is a number)</li>
77<li>Domain name: ws.nsrc.org</li>
79<li>Full name for new user: NSRC</li>
82</ul>
84<ul>
85<li>Select &quot;Manual&quot; (and hit Enter)
86<ul>
88<li>(If there is no partition table, you'll be prompted to create one)</li>
89</ul></li>
90<li>Delete existing partition(s)
91<ul>
92<li>Instructors will tell you the best approach</li>
93<li>If there's a large Windows partition, delete it</li>
94<li>If there's a Recovery partition, keep it</li>
95</ul></li>
96<li>Select &quot;pri/log FREE SPACE&quot;
97<ul>
98<li>Press ENTER to create a new partition</li>
99<li>New partition size: 1G (this is the /boot filesystem)</li>
100<li>Primary</li>
101<li>Beginning</li>
102<li>Use as: Ext4 journaling file system</li>
103<li>Mount point: /boot - static files of the boot loader</li>
104<li>Bootable flag: on</li>
105<li>Done setting up the partition</li>
106</ul></li>
107<li>Select &quot;pri/log FREE SPACE&quot;
108<ul>
109<li>Press ENTER to create a new partition</li>
110<li>Accept whatever size is shown (the rest of the disk)</li>
111<li>Primary</li>
112<li>Use as: physical volume for LVM</li>
113<li>Done setting up the partition</li>
114</ul></li>
115<li>Configure the Logical Volume Manager
116<ul>
117<li>Write changes to disks and configure LVM? Yes</li>
118<li>Create volume group
119<ul>
120<li>Volume group name: ganeti</li>
121<li>Devices for the new volume group: select only the LVM partition (probably /dev/sda2) and hit SPACE to display '[*]', then hit Enter to continue.</li>
122</ul></li>
123<li>Create logical volume
124<ul>
125<li>Volume group: ganeti</li>
126<li>Logical volume name: root</li>
127<li>Logical volume size: 4GB</li>
128</ul></li>
129<li>Create logical volume
130<ul>
131<li>Volume group: ganeti</li>
132<li>Logical volume name: swap</li>
133<li>Logical volume size: 4GB</li>
134</ul></li>
135<li>Create logical volume
136<ul>
137<li>Volume group: ganeti</li>
138<li>Logical volume name: var</li>
139<li>Logical volume size: 20GB</li>
140</ul></li>
141<li>Finish</li>
142</ul></li>
143<li><p>Select the entry #1 underneath LV <em>root</em></p>
144<pre><code>LVM VG ganeti, LV root - 4.0 GB Linux device-mapper (linear)
145     #1             4.0 GB   &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; select this &lt;&lt;&lt;&lt;&lt;&lt;&lt;</code></pre>
146<ul>
147<li>Use as: Ext4 journaling file system</li>
148<li>Mount point: / - the root filesystem</li>
149<li>Done setting up the partition</li>
150</ul></li>
151<li>Select the entry #1 underneath LV <em>swap</em>
152<ul>
153<li>Use as: swap area</li>
154<li>Done setting up the partition</li>
155</ul></li>
156<li>Select the entry #1 underneath LV <em>var</em>
157<ul>
158<li>Use as: Ext4 journalling file system</li>
159<li>Mount point: /var - variable data</li>
160<li>Done setting up the partition</li>
161</ul></li>
162<li>Finish partitioning and write changes to disk</li>
163<li><p>Write the changes to disks? Yes</p></li>
164</ul>
166<ul>
167<li>Use a network mirror? Yes
168<ul>
169<li>Country: Japan</li>
170<li>Mirror: ftp.jp.debian.net</li>
171<li>HTTP proxy: <code>http://apt.ws.nsrc.org:3142/</code></li>
172</ul></li>
173<li>Participate in the package usage survey? No</li>
174<li>Software to install. Select only these:
175<ul>
176<li>[X] SSH server</li>
177<li>[X] Standard system utilities</li>
178<li>(Do not select &quot;Debian desktop environment&quot; or you will get tons of applications including LibreOffice, Iceweasel, The Gimp etc)</li>
179<li>(To save time, do not select &quot;Laptop&quot; even if this is a laptop. You can install package &quot;task-laptop&quot; later if you wish)</li>
180</ul></li>
181<li>Install the GRUB boot loader to the master boot record? Yes</li>
182<li>Installation complete - hit Continue</li>
183</ul>
184<p>You should now boot into the system.</p>
187<p>The first thing you will need to do is to get the installer to forget about the CD-ROM.</p>
188<p>As root, edit the file <code>/etc/apt/sources.list</code>, for example using vi:</p>
189<pre><code># editor /etc/apt/sources.list</code></pre>
190<p>Comment out any line which begins &quot;deb cdrom:...&quot; by inserting a hash (<code>#</code>) in front of it, changing it from this:</p>
191<pre><code>deb cdrom:[Debian GNU/Linux 7.7.0 [...]</code></pre>
192<p>To this:</p>
193<pre><code># deb cdrom:[Debian GNU/Linux 7.7.0 [...]</code></pre>
194<p>Then save the file.</p>
195<p>Install <code>sudo</code>, and add the 'nsrc' user into the 'sudo' group</p>
196<pre><code># apt-get update
197# apt-get install sudo
198# usermod -G sudo -a nsrc</code></pre>
199<p>Also install <code>vim</code> which is an improved version of the vi editor</p>
200<pre><code># apt-get install vim</code></pre>
202<p>Your server may have picked up a dynamic IP address via DHCP, which you can find out using the <code>ifconfig</code> command:</p>
203<pre><code># ifconfig eth0</code></pre>
204<p>Now you are going to give it a static IP address and also configure bridging so that your virtual machines can share the same network interface.</p>
207<pre><code>hostX.ws.nsrc.org</code></pre>
208<h3 id="edit-etchosts-and-change-it-to-look-like-this"><span class="header-section-number">2.4.2</span> Edit <code>/etc/hosts</code>, and change it to look like this:</h3>
209<pre><code>127.0.0.1       localhost
21010.10.0.X       hostX.ws.nsrc.org       hostX</code></pre>
212<pre><code># apt-get install bridge-utils vlan</code></pre>
214<p>Change the file so that it looks like this. This removes the (dynamic) IP address from eth0, and instead creates a bridge interface &quot;br-lan&quot; with a static IP address, and eth0 a member of the bridge.</p>
215<pre><code># The loopback network interface
216auto lo
217iface lo inet loopback
218
219# Management interface
220auto eth0
221iface eth0 inet manual
222
223auto br-lan
224iface br-lan inet static
227        gateway         10.10.0.254
228        bridge_ports    eth0
229        bridge_stp      off
230        bridge_fd       0
231        bridge_maxwait  0</code></pre>
232<p>You can activate your changes like this:</p>
233<pre><code># ifdown eth0
234# killall dhclient
235# ifup br-lan
236# brctl show
237
238bridge name bridge id       STP enabled interfaces
239br-lan      8000.xxxxxxxxxxxx   no      eth0</code></pre>
240<p>You should see your new IP address on <code>ifconfig br-lan</code>, and you should still be able to ping out (e.g. <code>ping 8.8.8.8</code>)</p>
242<pre><code>domain ws.nsrc.org
243nameserver 10.10.0.241</code></pre>
244<p>Check you can still resolve names (e.g. <code>ping apt.ws.nsrc.org</code>)</p>
246<p>As a final check, reboot your server.</p>
247<pre><code># reboot</code></pre>
248<p>It should come up on the same IP address again.</p>
250<p>You should now be able to ssh into your server as user &quot;nsrc&quot; (e.g. using putty under Windows). Check you are able to do this. ssh to either <code>hostX.ws.nsrc.org</code> or <code>10.10.0.X</code></p>
251<p>Both people in the pair should be able to connect to the server from their laptop, so they don't need to share the console.</p>
252<p>NOTE: get into the habit of logging into the server as a normal user, and try to avoid logging in as &quot;root&quot;. You will see a prompt which ends with <code>$</code>. It's advisable to disable logging in as root directly over SSH (not covered here).</p> 253<p>If there is a single command you wish to run as root, prefix it with <code>sudo</code>. If you have a series of commands to run as root, start a root shell with <code>sudo -s</code>, and the prompt will change to <code>#</code>. When you have finished, type <code>exit</code> to leave the root shell.</p> 254<h1 id="virtual-machine-tools"><span class="header-section-number">3</span> Virtual machine tools</h1> 255<h2 id="install-kvm-and-libvirt"><span class="header-section-number">3.1</span> Install KVM and <code>libvirt</code></h2> 256<pre><code># apt-get install qemu-kvm libvirt-bin</code></pre> 257<p>Now add the 'nsrc' user into the 'libvirt' group.</p> 258<pre><code># usermod -G libvirt -a nsrc</code></pre> 259<p>This is needed so you can talk to the libvirt daemon. If you are currently logged in as 'nsrc' you will need to logout and login again to pick up this group. The command &quot;id&quot; will show you what groups you are a member of.</p> 260<h2 id="check-for-hardware-virtualization-support"><span class="header-section-number">3.2</span> Check for hardware virtualization support</h2> 261<p>At a command line prompt, type the following:</p> 262<pre><code>$ egrep '(vmx|svm)' /proc/cpuinfo</code></pre>
263<p>If you get one or more lines containing &quot;vmx&quot; or &quot;svm&quot; then the processor has hardware virtualization capabilities. However this doesn't tell you if it's enabled in the BIOS.</p>
264<p>To verify, do:</p>
265<pre><code>$ls -l /dev/kvm</code></pre> 266<p>If you see:</p> 267<pre><code>crw-rw---- 1 root kvm 10, 232 Jan 17 21:24 /dev/kvm</code></pre> 268<p>Then it's OK <a href="#fn1" class="footnoteRef" id="fnref1"><sup>1</sup></a></p> 269<p>If KVM acceleration is not available, then you may need to reboot into the BIOS settings and enable VT-x (Intel) or AMD-V (AMD).</p> 270<h2 id="desktop-environment"><span class="header-section-number">3.3</span> Desktop environment</h2> 271<p>Install an X11 desktop (which we want just so that we can run virt-manager)</p> 272<pre><code># apt-get install xorg lxde lightdm ssh-askpass --no-install-recommends</code></pre> 273<p>This will take a while - be patient!</p> 274<p>Once the install is finished, start the graphical environment:</p> 275<pre><code># service lightdm start</code></pre> 276<p>You should now be able to login to the graphical environment as the &quot;nsrc&quot; user. You can get a shell window using <code>Start &gt; Accessories &gt; LXTerminal</code>.</p> 277<p>You can also switch between text and graphical consoles using Ctrl-Alt-F1 and Ctrl-Alt-F7 respectively.</p> 278<h2 id="install-virt-manager-gui"><span class="header-section-number">3.4</span> Install virt-manager GUI</h2> 279<pre><code># apt-get install virt-manager</code></pre> 280<p>This installs a lot of extra packages because it depends on a lot of graphics libraries.</p> 281<h1 id="create-a-vm"><span class="header-section-number">4</span> Create a VM</h1> 282<p>On the virtualization host you have just built, you are going to install Ubuntu Server in a VM, using the virt-manager GUI which is similar to the VirtualBox GUI.</p> 283<h2 id="copy-ubuntu-iso-image"><span class="header-section-number">4.1</span> Copy Ubuntu ISO image</h2> 284<p>First, copy the file <code>ubuntu-14.04.1-server-i386.iso</code> into the directory <code>/var/lib/libvirt/images</code> on your host.</p> 285<p>How to do this depends on where you are copying the image from.</p> 286<ul> 287<li>If you are copying from a USB stick: insert the stick. If you have a GUI it will ask you if you want to open with the File Manager. Say yes, and it should mount the USB stick automatically (on <code>/media/XXX</code>), and open a window.</li> 288</ul> 289<p>(XXX will be a random number, look at the title of the window)</p> 290<p>You can then copy the ISO:</p> 291<pre><code>~~~ 292# cp /media/XXX/iso/ubuntu-14.04.1-server-i386.iso /var/lib/libvirt/images/ 293~~~</code></pre> 294<p>If it doesn't work, you may need to mount it by hand:</p> 295<pre><code>~~~ 296# dmesg | tail # find the ID of the device just inserted, e.g. sdb 297# mount /dev/sdb1 /mnt 298# ls /mnt # find the file 299# ls /mnt/iso # find the file 300# cp /mnt/iso/ubuntu-14.04.1-server-i386.iso /var/lib/libvirt/images/ 301# umount /mnt 302~~~</code></pre> 303<ul> 304<li><p>If you are copying from a web server:</p> 305<pre><code># cd /var/lib/libvirt/images 306# wget http://www.ws.nsrc.org/downloads/ubuntu-14.04.1-server-i386.iso</code></pre></li> 307</ul> 308<h2 id="start-the-virt-manager-gui"><span class="header-section-number">4.2</span> Start the virt-manager GUI</h2> 309<p>Use <code>Start &gt; System Tools &gt; Virtual Machine Manager</code>, or just type <code>virt-manager</code> at a terminal prompt while in the GUI.</p> 310<p>NOTE: to release the keyboard and mouse, press left-CTRL and left-ALT together.</p> 311<h2 id="create-a-vm-1"><span class="header-section-number">4.3</span> Create a VM</h2> 312<p>In the Virtual Machine Manager window:</p> 313<ul> 314<li>Click &quot;New&quot; (this is the PC-shaped button under the File menu)</li> 315<li>Step 1 of 5 316<ul> 317<li>Enter a name, e.g. &quot;ubuntu1&quot;</li> 318<li>Select &quot;Local install media (ISO image or CDROM)&quot;</li> 319<li>Click Forward</li> 320</ul></li> 321<li>Step 2 of 5 322<ul> 323<li>Check &quot;Use ISO image&quot; is selected</li> 324<li>Click &quot;Browse&quot;, and select <code>ubuntu-14.04.1-server-i386.iso</code> (if it's not there, click &quot;Browse Local&quot;, choose <code>File System</code> on the left, and browse to where you copied/downloaded the Ubuntu ISO image.</li> 325<li>OS type: Linux</li> 326<li>Version: select the nearest, which may be &quot;Ubuntu 11.10&quot;</li> 327<li>Click Forward</li> 328</ul></li> 329<li>Step 3 of 5 330<ul> 331<li>Memory: 512 MB</li> 332<li>CPUs: 1</li> 333<li>Click Forward</li> 334</ul></li> 335<li>Step 4 of 5 336<ul> 337<li>Create a disk image on the computer's hard drive</li> 338<li>4.0 GB</li> 339<li>Uncheck &quot;Allocate entire disk now&quot;</li> 340<li>Click Forward</li> 341</ul></li> 342<li>Step 5 of 5 343<ul> 344<li>Open &quot;Advanced options&quot;</li> 345<li>Check that the interface selected is &quot;Host device eth0 (Bridge 'br-lan')&quot;</li> 346<li>Click Finish</li> 347</ul></li> 348</ul> 349<p>At this point the VM will start and the console should appear. Continue to install your Ubuntu virtual machine however you like. Follow the instructions from the VirtualBox exercise if you wish. We recommend using &quot;Guided - use entire disk&quot; for partitioning, and please enter <code>http://apt.ws.nsrc.org:3142/</code> for the HTTP proxy. Install the &quot;OpenSSH Server&quot; package.</p> 350<p>When it has finished and rebooted, your VM should come back up. Login, type &quot;ifconfig&quot; to find what IP address it has come up on. This should be an address on 10.10.0.X which has been picked dynamically via DHCP.</p> 351<p>SSH into this address from your laptop (e.g. using Putty) and login. If you didn't select OpenSSH Server during the installation, then you can add it using <code>apt-get install openssh-server</code></p> 352<p>Congratulations: you have a created a working VM, it is on the network, and you have remote access to it!</p> 353<h2 id="look-at-the-kvm-process"><span class="header-section-number">4.4</span> Look at the KVM process</h2> 354<p>You should be able to find the running <code>kvm</code> (or qemu-system) process like this:</p> 355<pre><code>$ ps auxwww | egrep '(kvm|qemu-system)'</code></pre>
356<p>Note the very large number of command line parameters given to kvm. Now you can see why we need <code>libvirt</code> to manage this for us :-)</p>
357<h2 id="find-the-disk-image-file"><span class="header-section-number">4.5</span> Find the disk image file</h2>
358<p>The disk image file is in the directory <code>/var/lib/libvirt/images</code> and you can find it like this:</p>
359<pre><code># cd /var/lib/libvirt/images
360# ls -slh</code></pre>
361<p>Note that the total size of the file is 4.0GB, but the disk space used (the left-hand column on the line) is less than this. This means it is a &quot;sparse&quot; file, because we de-selected &quot;allocate entire disk now&quot;.</p>
362<p>To view how much space is really used, you can use the <code>du</code> (disk use) command, like so:</p>
363<pre><code># du -m *</code></pre>
364<p>-m = megabytes</p>
365<h1 id="using-virsh-cli"><span class="header-section-number">5</span> Using virsh CLI</h1>
366<p>When you are logged into the VM host platform remotely, e.g. over ssh, it's often far easier and quicker to interact using the command line rather than trying to pull back a graphical desktop and GUI.</p>
368<p>You should be able to run virsh as a non-root user (remember, it's always a good idea to run as few commands as root as possible). Under Debian you need to create a config file to make this work.</p>
369<p>When logged in as user &quot;nsrc&quot;, do the following to create a config file <code>.libvirt/libvirt.conf</code> in your home directory, containing one line:</p>
370<pre><code>$cd 371$ mkdir -p .libvirt     # it may already exist
372$editor .libvirt/libvirt.conf 373uri_default = &quot;qemu:///system&quot;</code></pre> 374<h2 id="simple-commands"><span class="header-section-number">5.2</span> Simple commands</h2> 375<p>On the host server (either a terminal window or logged in using ssh), try the following commands:</p> 376<pre><code>$ virsh list
377$virsh list --all</code></pre> 378<p>The first shows only running VMs, the second shows all defined VMs (including halted ones).</p> 379<p>You can send a shutdown signal like this:</p> 380<pre><code>$ virsh shutdown ubuntu1</code></pre>
381<p>After a few seconds, try <code>virsh list</code> again to see if it has shut down. (If it hasn't - this means <code>acpid</code> is not running inside the guest)</p>
382<p>To restart the VM:</p>
383<pre><code>$virsh start ubuntu1</code></pre> 384<p>You can find all the parameters of the VM, such as how much memory and CPU cores it has, and what disk images are attached, by looking at the XML:</p> 385<pre><code>$ virsh dumpxml ubuntu1 | less</code></pre>
386<p>See if you can find the path to the disk image file in the XML.</p>
387<p>(Hit space to advance to next page, 'b' to go back, and 'q' to quit)</p>
388<p>The XML files are actually stored under <code>/etc/libvirt/qemu/</code>, but it is safer to manipulate them using the <code>virsh</code> commands, which test the XML file for errors before saving it.</p>
390<p>On your host server, try connecting to the serial port of your VM:</p>
391<pre><code>$virsh console ubuntu1</code></pre> 392<p>Hit Enter a few times and you should find nothing happens. This is because the VM isn't configured to give a login prompt on the serial port. Hit ctrl and <code>]</code> (right-hand square bracket) to exit.</p> 393<p>Firstly, let's check that VM does have an emulated serial port, by editing the XML:</p> 394<pre><code>$ virsh edit ubuntu1</code></pre>
395<p>Scroll down and check that it contains the following 6 lines:</p>
396<pre><code>    &lt;serial type='pty'&gt;
397      &lt;target port='0'/&gt;
398    &lt;/serial&gt;
399    &lt;console type='pty'&gt;
400      &lt;target type='serial' port='0'/&gt;
401    &lt;/console&gt;</code></pre>
402<p>If it does, you can exit the editor without saving. If not, then scroll down until you find the section defining the mouse:</p>
403<pre><code>    &lt;input type='mouse' bus='ps2'/&gt;</code></pre>
404<p>and insert the 6 lines of serial and console XML from above, <em>before</em> this line. Exit the editor, then reboot the virtual machine.</p>
405<p>Now login to the virtual machine itself (using the graphical console as you have been doing so far). Create a file <code>/etc/init/ttyS0.conf</code> (as root) with the following contents:</p>
406<pre><code>start on stopped rc RUNLEVEL=[2345]
407stop on runlevel [!2345]
408
409respawn
410exec /sbin/getty -L 115200 ttyS0 xterm</code></pre>
411<p>Once you have created this, you should be able to check the status of the console getty process and start it like this:</p>
412<pre><code># initctl status ttyS0
413# initctl start ttyS0
414# initctl status ttyS0</code></pre>
415<p>Check that there is now a &quot;getty&quot; process running on ttyS0:</p>
416<pre><code># ps auxwww | grep ttyS0</code></pre>
417<p>Now go back to a command line on the <em>host</em> server. On there, type:</p>
418<pre><code>$virsh console ubuntu1</code></pre> 419<p>and then hit Enter. You should this time get a login prompt. Congratulations, you are now using the emulated serial port (which doesn't require any GUI to get into). Why use a serial console ? In case you lose network access to your virtual machine, and you cannot easily get access to the graphical desktop of your Host machine (if it is for example in a server room somewhere), then the virtual console will let you access your Linux server.</p> 420<p>Login to test, then hit Ctrl and <code>]</code> (right-hand square bracket) to disconnect from the emulated serial console.</p> 421<h1 id="additional-exercises"><span class="header-section-number">6</span> Additional exercises</h1> 422<p>Please feel free to try these if time is available, or use them as reference material.</p> 423<h2 id="vnc-remote-access-to-virt-manager-desktop"><span class="header-section-number">6.1</span> VNC remote access to virt-manager desktop</h2> 424<p>Unfortunately, virt-manager can only run under Linux. However it is possible to create and access a remote Linux desktop so you can use virt-manager remotely.</p> 425<p>Firstly, install the required package:</p> 426<pre><code># apt-get install vnc4server</code></pre> 427<p>(If you have not installed a desktop environment like LXDE, then you should also install a simple window manager like &quot;openbox&quot; or &quot;fluxbox&quot;)</p> 428<p>Now as the &quot;nsrc&quot; user - not as root! - type &quot;vncserver&quot; to start a server.</p> 429<pre><code>nsrc@hostX:~$ vncserver
430
432
435
436New 'hostX.ws.nsrc.org:1 (nsrc)' desktop is hostX.ws.nsrc.org:1
437
438Creating default startup script /home/nsrc/.vnc/xstartup
439Starting applications specified in /home/nsrc/.vnc/xstartup
440Log file is /home/nsrc/.vnc/hostX.ws.nsrc.org:1.log</code></pre>
441<p>Note the VNC screen number, in the above example it is :1. (The actual TCP port number is this value plus 5900).</p>
442<p>Now connect using a VNC client on your laptop to <code>&lt;yourhost&gt;:&lt;screen&gt;</code>, which would be <code>hostX:1</code> in the above example. You should be prompted for the password.</p>
443<p>If you just get a blank screen, then you are probably in the 'openbox' window manager. Right-click, select Terminal from the menu, and in the terminal type 'virt-manager'.</p>
444<p>Note that you can disconnect and reconnect from the desktop with VNC, and it will be just as you left it.</p>
445<p>If you want the VNC desktop to have the full LXDE desktop environment, you need to edit a config file, comment out a couple of lines and add a new line.</p>
446<pre><code>$cd 447$ editor .vnc/xstartup
448...
449#x-terminal-emulator -geometry 80x24+10+10 -ls -title &quot;$VNCDESKTOP Desktop&quot; &amp; 450#x-window-manager &amp; 451x-session-manager &amp;</code></pre> 452<p>Now restart your VNC server:</p> 453<pre><code>$ vncserver -kill :1
454\$ vncserver</code></pre>
455<p>and reconnect.</p>
456<p>Note 1: If you reboot your VM server, then you will have to login over ssh and type &quot;vncserver&quot; again to restart the VNC server. However it will remember the password you used before.</p>
457<p>Note 2: This is <em>not</em> the way we recommend to manage Linux systems, for many reasons. However it does give you a usable way to run virt-manager over the network.</p>
458<p>If you had a cluster of machines, you can run virt-manager on one machine, and then use <code>File &gt; Add Connection</code> to get it to communicate with libvirt on other machines. Then only the manager machine needs to have a graphical desktop. <a href="#fn2" class="footnoteRef" id="fnref2"><sup>2</sup></a></p>
459<h2 id="libvirt-storage-pools"><span class="header-section-number">6.2</span> libvirt storage pools</h2>
460<p>You can create 'storage pools' to make them available to libvirt. Here is how to set up an LVM pool.</p>
461<pre><code># editor ganeti.xml
462&lt;pool type=&quot;logical&quot;&gt;
463  &lt;name&gt;ganeti&lt;/name&gt;
464  &lt;target&gt;
465    &lt;path&gt;/dev/ganeti&lt;/path&gt;
466  &lt;/target&gt;
467&lt;/pool&gt;
468
469# virsh pool-define ganeti.xml
470# virsh pool-list --all
471# virsh pool-start ganeti
472# virsh pool-autostart ganeti
473# virsh pool-list</code></pre>
474<p>Now you have the option of creating VMs within the &quot;ganeti&quot; pool, which will create logical volumes in that volume group.</p>
476<p>The virt-install tool allows you to create and start a VM, allocate disk and attach CD image, without having to write XML and without having to use the virt-manager GUI.</p>
477<p>This is very useful if you want to do all your management using virsh CLI.</p>
478<pre><code># apt-get install virtinst
479# virt-install --name foo --ram 256 \
480   --cdrom /var/lib/libvirt/images/ubuntu-14.04.1-server-i386.iso \
481   --disk pool=default,size=4 --network=bridge:br-lan \
483<p>(Use <code>pool=ganeti,size=4</code> if you have set up an LVM storage pool and want to use a logical volume rather than an image file)</p>
484<p>With <code>--noautoconsole</code> it does not attempt to start the <code>virt-viewer</code> X11 application to attach to the console. Instead, you can use a VNC viewer on your laptop to connect to the console.</p>
485<p>To find out which VNC port to connect to, type:</p>
486<pre><code># virsh vncdisplay foo</code></pre>
487<p>This will return something like <code>:0</code> which is the VNC display number. (Add 5900 to get the VNC TCP port number).</p>
488<p>Start your VNC viewer, tell it to connect to <code>hostX.ws.nsrc.org:&lt;N&gt;</code> and you should have a password-protected graphical console onto the VM.</p>
489<h2 id="install-vm-with-qcow2-disk-iamge"><span class="header-section-number">6.4</span> Install VM with QCOW2 disk iamge</h2>
490<p>By default, virt-manager creates raw files (sparse or pre-allocated)</p>
491<p>If you want to create a different format, e.g. QCOW2, or create an image in a different storage pool, this is what you do.</p>
492<p>When creating a virtual machine, at step 4 of 5 (&quot;Enable storage for this virtual machine&quot;) click &quot;Select managed or other existing storage&quot;, then &quot;Browse&quot;.</p>
493<p>At this point you can select a storage pool from the left-hand pane and click New Volume. This allows you to choose a name for the volume and its format (e.g. QCOW2) and size. Note that you cannot choose the format if you choose an LVM based storage pool - therefore choose the <code>default</code> pool to create a disk image in QCOW2 format.</p>
494<p>When the volume has been create, select it in the right-hand pane, then click &quot;Choose Volume&quot; to continue with the installation using this volume.</p>
496<p>Snaphots are supported using the qcow2 format; the disk image file contains both the disk snapshots and the CPU/RAM state. You can try them out.</p>
497<p>virt-manager GUI support for snapshots is <a href="https://fedoraproject.org/wiki/Changes/Virt_Manager_Snapshots">planned</a> but for now use the command line:</p>
498<pre><code>virsh snapshot-create &lt;vmname&gt;
499virsh snapshot-list &lt;vmname&gt;
500virsh snapshot-revert &lt;vnmame&gt; &lt;snapshotID&gt;</code></pre>
501<p>You can try creating temporary files in the '/run' directory (which is a RAM disk) and watching how they change back when you revert a snapshot.</p>
502<p>If a machine is shutdown, you can use <code>snapshot-revert</code> to start it immediately at the point where the snapshot was taken.</p>
503<div class="footnotes">
504<hr />
505<ol>
506<li id="fn1"><p>On Ubuntu, The <code>kvm-ok</code> utility, included in the package <code>cpu-checker</code>, will tell you.<a href="#fnref1"></a></p></li>
507<li id="fn2"><p>If you're running Linux on your own computer, you could manage libvirt on other servers from your own machine. More on this <a href="http://libvirt.org/uri.html">here</a>.<a href="#fnref2"></a></p></li>
508</ol>
509</div>
510</body>
511</html>