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"> |
---|
3 | <head> |
---|
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>libvirt and vmbuilder exercise</title> |
---|
8 | <style type="text/css">code{white-space: pre;}</style> |
---|
9 | <link rel="stylesheet" href="../../style.css" type="text/css" /> |
---|
10 | </head> |
---|
11 | <body> |
---|
12 | <div id="header"> |
---|
13 | <h1 class="title">libvirt and vmbuilder exercise</h1> |
---|
14 | </div> |
---|
15 | <div id="TOC"> |
---|
16 | <ul> |
---|
17 | <li><a href="#objective"><span class="toc-section-number">1</span> Objective</a></li> |
---|
18 | <li><a href="#using-vmbuilder"><span class="toc-section-number">2</span> Using vmbuilder</a><ul> |
---|
19 | <li><a href="#check-the-configuration"><span class="toc-section-number">2.1</span> Check the configuration</a></li> |
---|
20 | <li><a href="#build-a-vm"><span class="toc-section-number">2.2</span> Build a VM</a></li> |
---|
21 | </ul></li> |
---|
22 | <li><a href="#managing-your-vm"><span class="toc-section-number">3</span> Managing your VM</a><ul> |
---|
23 | <li><a href="#start"><span class="toc-section-number">3.1</span> Start</a></li> |
---|
24 | <li><a href="#attach-to-the-console"><span class="toc-section-number">3.2</span> Attach to the console</a></li> |
---|
25 | <li><a href="#set-up-networking"><span class="toc-section-number">3.3</span> Set up networking</a></li> |
---|
26 | <li><a href="#shutdown-and-restart"><span class="toc-section-number">3.4</span> Shutdown and restart</a></li> |
---|
27 | <li><a href="#ssh-directly-into-the-vm"><span class="toc-section-number">3.5</span> ssh directly into the VM</a></li> |
---|
28 | <li><a href="#changing-vm-parameters"><span class="toc-section-number">3.6</span> Changing VM parameters</a></li> |
---|
29 | <li><a href="#set-up-ssh-key-access"><span class="toc-section-number">3.7</span> Set up ssh key access</a></li> |
---|
30 | </ul></li> |
---|
31 | <li><a href="#optional-exercises"><span class="toc-section-number">4</span> OPTIONAL EXERCISES</a><ul> |
---|
32 | <li><a href="#virt-manager-and-vnc"><span class="toc-section-number">4.1</span> virt-manager and VNC</a></li> |
---|
33 | <li><a href="#graphical-vnc-console-onto-your-vm"><span class="toc-section-number">4.2</span> Graphical VNC console onto your VM</a></li> |
---|
34 | <li><a href="#discarding-a-virtual-machine"><span class="toc-section-number">4.3</span> Discarding a virtual machine</a></li> |
---|
35 | </ul></li> |
---|
36 | </ul> |
---|
37 | </div> |
---|
38 | <h1 id="objective"><a href="#objective"><span class="header-section-number">1</span> Objective</a></h1> |
---|
39 | <p>You are going to use vmbuilder to build an Ubuntu VM image automatically, and manage it using the "virsh" component of libvirt.</p> |
---|
40 | <h1 id="using-vmbuilder"><a href="#using-vmbuilder"><span class="header-section-number">2</span> Using vmbuilder</a></h1> |
---|
41 | <h2 id="check-the-configuration"><a href="#check-the-configuration"><span class="header-section-number">2.1</span> Check the configuration</a></h2> |
---|
42 | <p>The package "python-vmbuilder" should already be installed on your machine. Have a look at its top-level configuration file:</p> |
---|
43 | <pre><code>cat /etc/vmbuilder.cfg</code></pre> |
---|
44 | <p>This defines how the VM will be built:</p> |
---|
45 | <ul> |
---|
46 | <li>what architecture we are building (i386 = 32-bit, x86_64 = 64-bit)</li> |
---|
47 | <li>where to download packages from (mirror, security-mirror). Note that we are pointing to apt.ws.nsrc.org:3142 which is the apt-cacher running on your server.</li> |
---|
48 | <li>which version of Ubuntu we are building (suite, flavour)</li> |
---|
49 | <li>which extra packages to include</li> |
---|
50 | <li>ssh key to include</li> |
---|
51 | <li>initial username and password. Notice that these are different to the credentials you use to login to your server. Generally, the host server password is private and not known to the users of the VMs.</li> |
---|
52 | </ul> |
---|
53 | <p>There are also file snippets and templates under <code>/etc/vmbuilder/</code> which are used during the build. You normally do not need to change these.</p> |
---|
54 | <h2 id="build-a-vm"><a href="#build-a-vm"><span class="header-section-number">2.2</span> Build a VM</a></h2> |
---|
55 | <p>Everyone in your group will ssh into the server and build a VM; it's fine for you to do them at the same time. Use the following name for your VM:</p> |
---|
56 | <ul> |
---|
57 | <li>1st person in the group: noc.ws.nsrc.org</li> |
---|
58 | <li>2nd person in the group: noc2.ws.nsrc.org</li> |
---|
59 | <li>3rd person in the group: noc3.ws.nsrc.org</li> |
---|
60 | <li>...etc</li> |
---|
61 | </ul> |
---|
62 | <p>To build your VM, run the following command. If you are not the first person in the group then modify the two places where "noc.ws.nsrc.org" appears.</p> |
---|
63 | <pre><code>sudo vmbuilder kvm ubuntu --hostname noc.ws.nsrc.org --mem 512 --debug \ |
---|
64 | --rootsize 20480 --dest /var/lib/libvirt/images/noc.ws.nsrc.org</code></pre> |
---|
65 | <p>This builds a VM which will initially have 512MB RAM allocated when it runs, and a root disk which can grow up to 20GB.</p> |
---|
66 | <p>You should see progress messages scrolling up the screen. The first build may be slow as the packages are being downloaded for the first time.</p> |
---|
67 | <blockquote> |
---|
68 | <p>If you want to confirm that your apt-cacher is being used, you can do <code>tail -f /var/log/apt-cacher-ng/apt-cacher.log</code> in another session (ctrl-C to stop)</p> |
---|
69 | <p>If vmbuilder is running very slowly, you can do the "virt-manager and VNC" exercise at the end of the handout while you wait.</p> |
---|
70 | </blockquote> |
---|
71 | <p>Check that no errors are shown at the end of the build. You should see something like:</p> |
---|
72 | <pre><code>... |
---|
73 | 2013-11-11 10:30:21,390 DEBUG : Calling deploy method in VMBuilder.plugins.ubuntu.distro plugin. |
---|
74 | 2013-11-11 10:30:21,391 DEBUG : No such method |
---|
75 | 2013-11-11 10:30:21,391 DEBUG : Calling deploy method in context plugin VMBuilder.plugins.kvm.vm. |
---|
76 | 2013-11-11 10:30:21,391 DEBUG : ['rm', '-rf', '--one-file-system', '/tmp/tmpjC8QLO']</code></pre> |
---|
77 | <p>If there is a problem, try to work out what is wrong from the error message, and ask for help if you need it.</p> |
---|
78 | <h1 id="managing-your-vm"><a href="#managing-your-vm"><span class="header-section-number">3</span> Managing your VM</a></h1> |
---|
79 | <h2 id="start"><a href="#start"><span class="header-section-number">3.1</span> Start</a></h2> |
---|
80 | <p>Try the following commands</p> |
---|
81 | <pre><code>virsh list # shows running VMs (should be empty) |
---|
82 | virsh list --all # shows all VMs, including stopped ones</code></pre> |
---|
83 | <p>Now start the VM that you built. Remember to change "noc.ws.nsrc.org" to the name of your VM.</p> |
---|
84 | <pre><code>virsh start noc.ws.nsrc.org |
---|
85 | virsh list # show running VMs</code></pre> |
---|
86 | <p>Is your virtual machine running? A low-level way of checking is to see if libvirt has started a "kvm" process</p> |
---|
87 | <pre><code>ps auxwww | grep kvm # look for your KVM process</code></pre> |
---|
88 | <h2 id="attach-to-the-console"><a href="#attach-to-the-console"><span class="header-section-number">3.2</span> Attach to the console</a></h2> |
---|
89 | <p>Since you don't know what IP address your VM has got (if any), you need another way to connect to it. KVM provides two different ways:</p> |
---|
90 | <ul> |
---|
91 | <li>an emulated serial port</li> |
---|
92 | <li>an emulated VGA screen</li> |
---|
93 | </ul> |
---|
94 | <p>We are going to use the serial port, since you can easily do this using your ssh session.</p> |
---|
95 | <pre><code>virsh console noc.ws.nsrc.org</code></pre> |
---|
96 | <p>Hit Enter a few times. You should get a login prompt. Login using the username and password which the VM was built with.</p> |
---|
97 | <p>Type "ifconfig eth0" to see what IP address has been picked up by the VM.</p> |
---|
98 | <h2 id="set-up-networking"><a href="#set-up-networking"><span class="header-section-number">3.3</span> Set up networking</a></h2> |
---|
99 | <p>While you are still attached to the console, edit <code>/etc/network/interfaces</code> to replace DHCP with a static IP address.</p> |
---|
100 | <pre><code>auto eth0 |
---|
101 | iface eth0 inet static |
---|
102 | address 10.10.0.XXX |
---|
103 | netmask 255.255.255.0 |
---|
104 | gateway 10.10.0.254 |
---|
105 | dns-nameservers 10.10.0.241 |
---|
106 | # Disable UDP checksum offloading on virtio; it breaks when |
---|
107 | # packets traverse Dynamips |
---|
108 | post-up ethtool --offload eth0 tx off</code></pre> |
---|
109 | <p>Choose your IP address as follows:</p> |
---|
110 | <ul> |
---|
111 | <li>noc.ws.nsrc.org: 10.10.0.250</li> |
---|
112 | <li>noc2.ws.nsrc.org: 10.10.0.249</li> |
---|
113 | <li>noc3.ws.nsrc.org: 10.10.0.248</li> |
---|
114 | <li>...etc</li> |
---|
115 | </ul> |
---|
116 | <p>Note that the cursor keys in the editor may not work exactly as expected over a serial console - try to manage. Alternatively, since you know the machine's temporary (DHCP-assigned) IP address, you can ssh to that.</p> |
---|
117 | <h2 id="shutdown-and-restart"><a href="#shutdown-and-restart"><span class="header-section-number">3.4</span> Shutdown and restart</a></h2> |
---|
118 | <p>One way to shutdown the VM is to type <code>halt -p</code> within the VM. But you need to be very sure you are typing this in the VM and not the host server!</p> |
---|
119 | <p>A safer way is to send a shutdown signal from the host.</p> |
---|
120 | <ul> |
---|
121 | <li><p>If you are still attached to the virtual console, disconnect by pressing <code>ctrl</code> and <code>]</code></p></li> |
---|
122 | <li><p>Now issue the shutdown command to your VM</p> |
---|
123 | <pre><code>virsh shutdown noc.ws.nsrc.org |
---|
124 | virsh list # keep repeating this until your VM is no longer shown as running</code></pre></li> |
---|
125 | <li><p>Now restart your VM</p> |
---|
126 | <pre><code>virsh start noc.ws.nsrc.org</code></pre></li> |
---|
127 | </ul> |
---|
128 | <p>Check you can ping your VM on the static IP address you configured. It will probably take around 10-15 seconds before it starts to respond.</p> |
---|
129 | <p>If your machine does not respond, then go back in using the virtual console and correct the problem.</p> |
---|
130 | <h2 id="ssh-directly-into-the-vm"><a href="#ssh-directly-into-the-vm"><span class="header-section-number">3.5</span> ssh directly into the VM</a></h2> |
---|
131 | <p>Now you can confirm that you have ssh access into your VM. You should be able to ssh directly from your laptop to 10.10.0.XXX, where this is the IP address you assigned to your VM.</p> |
---|
132 | <p>Once logged in, you are going to do some basic system administration on the VM.</p> |
---|
133 | <pre><code>sudo apt-get update |
---|
134 | sudo apt-get install apache2</code></pre> |
---|
135 | <p>Edit <code>/var/www/index.html</code> and put your own "hello world" type message in it.</p> |
---|
136 | <p>Now point your laptop's web browser at http://10.10.0.XXX/ and check you can view this page. Check the pages for the other users in your group too.</p> |
---|
137 | <p>Congratulations, you have installed a complete Ubuntu virtual machine with webserver!</p> |
---|
138 | <h2 id="changing-vm-parameters"><a href="#changing-vm-parameters"><span class="header-section-number">3.6</span> Changing VM parameters</a></h2> |
---|
139 | <p>Let's say you decided that your noc.ws.nsrc.org needs more than 512MB of RAM to run. You can change the memory allocation in libvirt's XML configuration file for that VM.</p> |
---|
140 | <p>First, have a look at the existing XML:</p> |
---|
141 | <pre><code>virsh dumpxml noc.ws.nsrc.org</code></pre> |
---|
142 | <p>Now use the command to edit it:</p> |
---|
143 | <pre><code>virsh edit noc.ws.nsrc.org</code></pre> |
---|
144 | <p>Look for this section near the top:</p> |
---|
145 | <pre><code> <memory unit='KiB'>524288</memory> |
---|
146 | <currentMemory unit='KiB'>524288</currentMemory></code></pre> |
---|
147 | <p>The first number is the <em>maximum</em> amount of memory which this VM can use, and the second is the <em>current</em> amount of memory allocated to this VM.</p> |
---|
148 | <p>Change both these numbers to 1048576 (1GB), then shutdown and restart the VM.</p> |
---|
149 | <p>ssh into the VM and type "free" to see how much memory is visible.</p> |
---|
150 | <p>You can even change the memory used by a VM while it is running - this is called "hot-plug memory" - up to the maximum configured. The command is:</p> |
---|
151 | <pre><code>virsh setmem noc.ws.nsrc.org --size 400000</code></pre> |
---|
152 | <p>Type "free" again within the VM to see the change. It happens immediately.</p> |
---|
153 | <h2 id="set-up-ssh-key-access"><a href="#set-up-ssh-key-access"><span class="header-section-number">3.7</span> Set up ssh key access</a></h2> |
---|
154 | <p>Of course, you don't want to type your password every time you connect to the VM using ssh.</p> |
---|
155 | <p>To prevent this, you can put your public key into <code>/home/sysadm/.ssh/authorized_keys</code> in the VM. You'll have to create the .ssh directory if it doesn't already exist.</p> |
---|
156 | <p>You can also allow root login by putting your public key into <code>/root/.ssh/authorized_keys</code> in the VM. This is very convenient for managing the VM, especially using automated scripts. Then try ssh'ing in as root.</p> |
---|
157 | <p>To make this easier in future, put your public key into <code>/etc/vmbuilder/misc/authorized_keys</code> on the host. Then vmbuilder will include it in every VM it builds from now on.</p> |
---|
158 | <h1 id="optional-exercises"><a href="#optional-exercises"><span class="header-section-number">4</span> OPTIONAL EXERCISES</a></h1> |
---|
159 | <h2 id="virt-manager-and-vnc"><a href="#virt-manager-and-vnc"><span class="header-section-number">4.1</span> virt-manager and VNC</a></h2> |
---|
160 | <p>You can try out the graphical interface which virt-manager provides.</p> |
---|
161 | <ul> |
---|
162 | <li>Login to your server as the "nsrc" user</li> |
---|
163 | <li>Type "vncserver". If you have never run this before, you will be prompted to choose a password to protect your desktop. Use the instructor password.</li> |
---|
164 | <li>On your laptop, download and run a vnc viewer application. |
---|
165 | <ul> |
---|
166 | <li>For Windows: <a href="http://www.realvnc.com/download/viewer/">real VNC</a></li> |
---|
167 | <li>For Mac: <a href="http://sourceforge.net/projects/chicken/files/">chicken VNC</a></li> |
---|
168 | <li>For Linux desktop: gvncviewer</li> |
---|
169 | </ul></li> |
---|
170 | <li>Using vnc viewer, connect to your server (10.10.0.241) on port 5901. You should get a desktop with a terminal window.</li> |
---|
171 | <li>Type "virt-manager" into the terminal window. This should start the manager which shows your VMs and allows you to control them. Move and resize it to fit better.</li> |
---|
172 | <li>Disconnect your VNC session and reconnect; you should get the desktop exactly as you left it.</li> |
---|
173 | </ul> |
---|
174 | <h2 id="graphical-vnc-console-onto-your-vm"><a href="#graphical-vnc-console-onto-your-vm"><span class="header-section-number">4.2</span> Graphical VNC console onto your VM</a></h2> |
---|
175 | <p>For some VMs, like Windows VMs, a serial console may not be sufficient.</p> |
---|
176 | <p>You can get a graphical console indirectly by using virt-manager. Alternatively, you can expose the VM's graphical console directly to the network. You need to configure libvirt to allow VNC to listen on external interfaces, not just the loopback interface.</p> |
---|
177 | <p>To do this:</p> |
---|
178 | <ul> |
---|
179 | <li>virsh shutdown VNMAME</li> |
---|
180 | <li>wait for it to stop running (virsh list)</li> |
---|
181 | <li>virsh edit VMNAME</li> |
---|
182 | </ul> |
---|
183 | <p>This will open up the XML definition. Find this section:</p> |
---|
184 | <p>Find this section:</p> |
---|
185 | <pre><code> <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'> |
---|
186 | <listen type='address' address='127.0.0.1'/> |
---|
187 | </graphics></code></pre> |
---|
188 | <p>Change "127.0.0.1" to "0.0.0.0" and set a password, e.g.</p> |
---|
189 | <pre><code> <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' passwd='secret'> |
---|
190 | <listen type='address' address='0.0.0.0'/> |
---|
191 | </graphics></code></pre> |
---|
192 | <ul> |
---|
193 | <li>virsh start VMNAME</li> |
---|
194 | <li>virsh vncdisplay VMNAME</li> |
---|
195 | </ul> |
---|
196 | <p>This will return colon and a number, e.g. ":0". Add 5900 to this number to get the VNC port number.</p> |
---|
197 | <ul> |
---|
198 | <li>Use your laptop VNC client to connect to your server (10.10.0.241) on the port you have calculated. Enter the VNC password.</li> |
---|
199 | </ul> |
---|
200 | <h2 id="discarding-a-virtual-machine"><a href="#discarding-a-virtual-machine"><span class="header-section-number">4.3</span> Discarding a virtual machine</a></h2> |
---|
201 | <p>For reference: if you wanted to discard a virtual machine permanently, these are the steps you would have to take.</p> |
---|
202 | <pre><code>virsh destroy VMNAME # (if it's currently running) |
---|
203 | virsh undefine VMNAME # delete the XML |
---|
204 | sudo rm -rf /var/lib/libvirt/images/VMNAME # delete the disk image</code></pre> |
---|
205 | </body> |
---|
206 | </html> |
---|