| 1 | [[TOC(heading=KVM tutorial TOC, Tutorials/KVM)]] |
| 2 | |
| 3 | = KVM Tutorial = |
| 4 | == Installing KVM == |
| 5 | |
| 6 | Check if your CPU supports hardware virtualization via command: |
| 7 | {{{ |
| 8 | egrep '(vmx|svm)' --color=always /proc/cpuinfo |
| 9 | }}} |
| 10 | |
| 11 | If nothing is displayed CPU doesn't support virtualization. |
| 12 | |
| 13 | Instal packages ubuntu-virt-server, python-vm-builder and kvm-pxe via command (but just in case, first update the package repository on the node): |
| 14 | {{{ |
| 15 | apt-get update |
| 16 | apt-get install ubuntu-virt-server python-vm-builder kvm-pxe |
| 17 | }}} |
| 18 | |
| 19 | Add the current user to the group: |
| 20 | {{{ |
| 21 | adduser `id -un` libvirtd |
| 22 | adduser `id -un` kvm |
| 23 | }}} |
| 24 | |
| 25 | Logout and login again: |
| 26 | {{{ |
| 27 | logout |
| 28 | ssh root@node1-1 |
| 29 | }}} |
| 30 | |
| 31 | Check users group by comamand: |
| 32 | {{{ |
| 33 | groups |
| 34 | }}} |
| 35 | |
| 36 | Output should be similar to: |
| 37 | {{{ |
| 38 | root kvm libvirtd |
| 39 | }}} |
| 40 | |
| 41 | To list all running KVMs use: |
| 42 | {{{ |
| 43 | virsh -c qemu:///system list |
| 44 | }}} |
| 45 | |
| 46 | == Setup network bridge on the host == |
| 47 | |
| 48 | Install the package bridge-utils: |
| 49 | {{{ |
| 50 | apt-get install bridge-utils |
| 51 | }}} |
| 52 | |
| 53 | Create the bridge interface named br0: |
| 54 | {{{ |
| 55 | brctl addbr br0 |
| 56 | }}} |
| 57 | |
| 58 | Edit the network interfaces configuration with editor (for example nano): |
| 59 | {{{ |
| 60 | nano /etc/network/interfaces |
| 61 | }}} |
| 62 | |
| 63 | and add persistent configuration for br0 interface: |
| 64 | {{{ |
| 65 | # This file describes the network interfaces available on your system |
| 66 | # and how to activate them. For more information, see interfaces(5). |
| 67 | |
| 68 | # The loopback network interface |
| 69 | auto lo |
| 70 | iface lo inet loopback |
| 71 | |
| 72 | # The primary network interface |
| 73 | auto eth1 |
| 74 | iface eth1 inet dhcp |
| 75 | |
| 76 | # The secondary network interface |
| 77 | auto eth0 |
| 78 | iface eth0 inet manual |
| 79 | |
| 80 | #Bridge 0 interface |
| 81 | auto br0 |
| 82 | iface br0 inet static |
| 83 | address 10.28.1.1 |
| 84 | netmask 255.255.0.0 |
| 85 | broadcast 10.28.255.255 |
| 86 | network 10.28.0.0 |
| 87 | bridge_ports eth0 |
| 88 | bridge_fd 9 |
| 89 | bridge_hello 2 |
| 90 | bridge_maxage 12 |
| 91 | bridge_stp off |
| 92 | }}} |
| 93 | |
| 94 | Restart networking with command: |
| 95 | {{{ |
| 96 | /etc/init.d/networking restart |
| 97 | }}} |
| 98 | |
| 99 | For bridge verification open the new ssh window and run: |
| 100 | {{{ |
| 101 | tcpdump -i br0 |
| 102 | }}} |
| 103 | |
| 104 | and then, in the other window, try to ping some adress in same subnet, for example: |
| 105 | {{{ |
| 106 | ping 10.28.0.1 |
| 107 | }}} |
| 108 | |
| 109 | In the second windows you should see logging ofrequest/reply messages. |
| 110 | |
| 111 | It is recommended to reboot the machine once the bridge is configured. |
| 112 | |
| 113 | == Create and run KVM image using vmbuilder == |
| 114 | |
| 115 | Folder /var/lib/libvirt/images/ will be created for storing images. Each image will have |
| 116 | separate folder for example /var/lib/libvirt/images/virtual-machine1 |
| 117 | |
| 118 | Create necessary folders: |
| 119 | {{{ |
| 120 | mkdir -p /var/lib/libvirt/images/vm1/mytemplates/libvirt |
| 121 | }}} |
| 122 | '-p' option of mkdir command means that all parent folders will be created if they do not exist. |
| 123 | |
| 124 | vmbuilder tool uses templates for creating of images so copy the template: |
| 125 | {{{ |
| 126 | cp /etc/vmbuilder/libvirt/* /var/lib/libvirt/images/vm1/mytemplates/libvirt/ |
| 127 | }}} |
| 128 | |
| 129 | Open partition configuration file with command: |
| 130 | {{{ |
| 131 | nano /var/lib/libvirt/images/vm1/vmbuilder.partition |
| 132 | }}} |
| 133 | |
| 134 | and add the following lines: |
| 135 | {{{ |
| 136 | root 8000 |
| 137 | swap 4000 |
| 138 | --- |
| 139 | /var 20000 |
| 140 | }}} |
| 141 | |
| 142 | Copied text defines three partitions - root, swap and var and their sizes. |
| 143 | "---" means that var partition will be separate image file. |
| 144 | |
| 145 | Go to folder vm1: |
| 146 | {{{ |
| 147 | cd /var/lib/libvirt/images/vm1/ |
| 148 | }}} |
| 149 | |
| 150 | Execute vmbuilder command: |
| 151 | {{{ |
| 152 | vmbuilder kvm ubuntu --suite=oneiric --flavour=virtual --arch=amd64 --mirror=http://de.archive.ubuntu.com/ubuntu -o --libvirt=qemu:///system --ip=10.28.4.4 --gw=10.28.0.1 --mask=255.255.0.0 --bcast=10.28.255.255 --net=10.28.0.0 --dns=10.0.0.9 --part=vmbuilder.partition --templates=mytemplates --user=guest --name=Guest --pass=guest --addpkg=vim-nox --addpkg=unattended-upgrades --addpkg=acpid --mem=256 --hostname=vm1 --bridge=br0 --addpkg openssh-server --addpkg nano |
| 153 | }}} |
| 154 | |
| 155 | == Create and run KVM image using KVM directly == |
| 156 | |
| 157 | Download ubuntu server ISO file: |
| 158 | {{{ |
| 159 | wget -O ubuntu-server.iso http://www.ubuntu.com/start-download?distro=server&bits=64&release=latest |
| 160 | }}} |
| 161 | |
| 162 | Create Disk: |
| 163 | {{{ |
| 164 | qemu-img create -f qcow2 vm-oneric-amd64-serial 10G |
| 165 | }}} |
| 166 | |
| 167 | Setup network bridge (see part III). |
| 168 | |
| 169 | Install tool for Tun: |
| 170 | {{{ |
| 171 | apt-get install uml-utilities |
| 172 | }}} |
| 173 | |
| 174 | Create the tap interface: |
| 175 | {{{ |
| 176 | tunctl |
| 177 | }}} |
| 178 | |
| 179 | Output of the name is (tap0). |
| 180 | |
| 181 | Add tap interface to the bridge as a port: |
| 182 | {{{ |
| 183 | brctl addif br0 tap0 |
| 184 | }}} |
| 185 | |
| 186 | Bring up tap0 interface: |
| 187 | {{{ |
| 188 | ifconfig tap0 up |
| 189 | }}} |
| 190 | |
| 191 | Boot and Install Ubuntu using VNC: |
| 192 | {{{ |
| 193 | kvm -daemonize -m 1024 -hda vm-oneric-amd64-serial -cdrom ubuntu-server.iso -boot d -smp 2 -cpu qemu64 -net nic,model=virtio -net tap,ifname=tap0,script=no -vnc :0 |
| 194 | }}} |
| 195 | |
| 196 | To run already created image you can enter: |
| 197 | {{{ |
| 198 | kvm -daemonize -m 1024 -hda vm-oneric-amd64-serial -smp 2 -cpu qemu64 -net nic,model=virtio -net tap,ifname=tap0,script=no -vnc :0 |
| 199 | }}} |
| 200 | |
| 201 | Use VNC client to connect to virtual machine. |
| 202 | |
| 203 | Kill Virtual machine (KVM process): |
| 204 | {{{ |
| 205 | pkill -9 kvm |
| 206 | }}} |
| 207 | |
| 208 | Remove tap0 in as a bridge interface: |
| 209 | {{{ |
| 210 | brctl delif br0 tap0 |
| 211 | }}} |
| 212 | |
| 213 | Delete tap interface: |
| 214 | {{{ |
| 215 | tunctl -d tap0 |
| 216 | }}} |
| 217 | |
| 218 | {{{ |
| 219 | kvm -daemonize -m 1024 -hda vm-oneric-amd64-serial -smp 2 -cpu qemu64 -net nic,model=virtio -net tap,ifname=tap0,script=no -vnc :0 |
| 220 | }}} |
| 221 | |
| 222 | {{{ |
| 223 | kvm -daemonize -m 1024 -hda vm-oneric-amd64-serial -cdrom ubuntu-11.10-server-amd64.iso -boot d -smp 2 -cpu qemu64 -net nic,model=virtio -net tap,ifname=tap0,script=no -vnc :0 |
| 224 | }}} |
| 225 | |