| mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. Released under the GPL v2 |
| 4 | |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 5 | """ |
| 6 | This module defines the KVM class |
| mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 8 | KVM: a KVM virtual machine monitor |
| mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 9 | """ |
| 10 | |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 11 | __author__ = """ |
| 12 | mbligh@google.com (Martin J. Bligh), |
| mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 13 | poirier@google.com (Benjamin Poirier), |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 14 | stutsman@google.com (Ryan Stutsman) |
| 15 | """ |
| mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 16 | |
| 17 | import os |
| 18 | |
| 19 | import hypervisor |
| mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 20 | import utils |
| 21 | import hosts |
| 22 | |
| mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 23 | from common.error import * |
| 24 | |
| mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 25 | |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 26 | _qemu_ifup_script= """\ |
| 27 | #!/bin/sh |
| 28 | # $1 is the name of the new qemu tap interface |
| 29 | |
| 30 | ifconfig $1 0.0.0.0 promisc up |
| 31 | brctl addif br0 $1 |
| 32 | """ |
| 33 | |
| 34 | _check_process_script= """\ |
| 35 | if [ -f "%(pid_file_name)s" ] |
| 36 | then |
| 37 | pid=$(cat "%(pid_file_name)s") |
| 38 | if [ -L /proc/$pid/exe ] && stat /proc/$pid/exe | |
| 39 | grep -q -- "-> \`%(qemu_binary)s\'\$" |
| 40 | then |
| 41 | echo "process present" |
| 42 | else |
| 43 | rm -f "%(pid_file_name)s" |
| 44 | rm -f "%(monitor_file_name)s" |
| 45 | fi |
| 46 | fi |
| 47 | """ |
| 48 | |
| 49 | _hard_reset_script= """\ |
| 50 | import socket |
| 51 | |
| 52 | monitor_socket= socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 53 | monitor_socket.connect("%(monitor_file_name)s") |
| 54 | monitor_socket.send("system_reset\\n")\n') |
| 55 | """ |
| 56 | |
| 57 | _remove_modules_script= """\ |
| 58 | if $(grep -q "^kvm_intel [[:digit:]]\+ 0" /proc/modules) |
| 59 | then |
| 60 | rmmod kvm-intel |
| 61 | fi |
| 62 | |
| 63 | if $(grep -q "^kvm_amd [[:digit:]]\+ 0" /proc/modules) |
| 64 | then |
| 65 | rmmod kvm-amd |
| 66 | fi |
| 67 | |
| 68 | if $(grep -q "^kvm [[:digit:]]\+ 0" /proc/modules) |
| 69 | then |
| 70 | rmmod kvm |
| 71 | fi |
| 72 | """ |
| 73 | |
| 74 | |
| mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 75 | class KVM(hypervisor.Hypervisor): |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 76 | """ |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 77 | This class represents a KVM virtual machine monitor. |
| 78 | |
| 79 | Implementation details: |
| 80 | This is a leaf class in an abstract class hierarchy, it must |
| 81 | implement the unimplemented methods in parent classes. |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 82 | """ |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 83 | |
| 84 | build_dir= None |
| 85 | pid_dir= None |
| 86 | support_dir= None |
| 87 | addresses= [] |
| 88 | insert_modules= True |
| mbligh | 7af0552 | 2007-12-04 22:48:35 +0000 | [diff] [blame] | 89 | modules= {} |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 90 | |
| 91 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 92 | def __del__(self): |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 93 | """ |
| 94 | Destroy a KVM object. |
| 95 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 96 | Guests managed by this hypervisor that are still running will |
| 97 | be killed. |
| 98 | """ |
| 99 | self.deinitialize() |
| 100 | |
| 101 | |
| 102 | def _insert_modules(self): |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 103 | """ |
| 104 | Insert the kvm modules into the kernel. |
| 105 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 106 | The modules inserted are the ones from the build directory, NOT |
| 107 | the ones from the kernel. |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 108 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 109 | This function should only be called after install(). It will |
| 110 | check that the modules are not already loaded before attempting |
| 111 | to insert them. |
| 112 | """ |
| 113 | cpu_flags= self.host.run('cat /proc/cpuinfo | ' |
| 114 | 'grep -e "^flags" | head -1 | cut -d " " -f 2-' |
| 115 | ).stdout.strip() |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 116 | |
| mbligh | 770e3d7 | 2007-11-02 01:33:52 +0000 | [diff] [blame] | 117 | if cpu_flags.find('vmx') != -1: |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 118 | module_type= "intel" |
| 119 | elif cpu_flags.find('svm') != -1: |
| 120 | module_type= "amd" |
| 121 | else: |
| mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 122 | raise AutoservVirtError("No harware " |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 123 | "virtualization extensions found, " |
| 124 | "KVM cannot run") |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 125 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 126 | self.host.run('if ! $(grep -q "^kvm " /proc/modules); ' |
| 127 | 'then insmod "%s"; fi' % (utils.sh_escape( |
| 128 | os.path.join(self.build_dir, "kernel/kvm.ko")),)) |
| 129 | if module_type == "intel": |
| 130 | self.host.run('if ! $(grep -q "^kvm_intel " ' |
| 131 | '/proc/modules); then insmod "%s"; fi' % |
| 132 | (utils.sh_escape(os.path.join(self.build_dir, |
| 133 | "kernel/kvm-intel.ko")),)) |
| 134 | elif module_type == "amd": |
| 135 | self.host.run('if ! $(grep -q "^kvm_amd " ' |
| 136 | '/proc/modules); then insmod "%s"; fi' % |
| 137 | (utils.sh_escape(os.path.join(self.build_dir, |
| 138 | "kernel/kvm-amd.ko")),)) |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 139 | |
| 140 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 141 | def _remove_modules(self): |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 142 | """ |
| 143 | Remove the kvm modules from the kernel. |
| 144 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 145 | This function checks that they're not in use before trying to |
| 146 | remove them. |
| 147 | """ |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 148 | self.host.run(_remove_modules_script) |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 149 | |
| 150 | |
| mbligh | 7af0552 | 2007-12-04 22:48:35 +0000 | [diff] [blame] | 151 | def install(self, addresses, build=True, insert_modules=True, syncdir=None): |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 152 | """ |
| 153 | Compile the kvm software on the host that the object was |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 154 | initialized with. |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 155 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 156 | The kvm kernel modules are compiled, for this, the kernel |
| 157 | sources must be available. A custom qemu is also compiled. |
| 158 | Note that 'make install' is not run, the kernel modules and |
| 159 | qemu are run from where they were built, therefore not |
| 160 | conflicting with what might already be installed. |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 161 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 162 | Args: |
| 163 | addresses: a list of dict entries of the form |
| 164 | {"mac" : "xx:xx:xx:xx:xx:xx", |
| 165 | "ip" : "yyy.yyy.yyy.yyy"} where x and y |
| 166 | are replaced with sensible values. The ip |
| 167 | address may be a hostname or an IPv6 instead. |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 168 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 169 | When a new virtual machine is created, the |
| 170 | first available entry in that list will be |
| 171 | used. The network card in the virtual machine |
| 172 | will be assigned the specified mac address and |
| 173 | autoserv will use the specified ip address to |
| 174 | connect to the virtual host via ssh. The virtual |
| 175 | machine os must therefore be configured to |
| 176 | configure its network with the ip corresponding |
| 177 | to the mac. |
| 178 | build: build kvm from the source material, if False, |
| 179 | it is assumed that the package contains the |
| 180 | source tree after a 'make'. |
| 181 | insert_modules: build kvm modules from the source |
| 182 | material and insert them. Otherwise, the |
| 183 | running kernel is assumed to already have |
| 184 | kvm support and nothing will be done concerning |
| 185 | the modules. |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 186 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 187 | TODO(poirier): check dependencies before building |
| 188 | kvm needs: |
| 189 | libasound2-dev |
| 190 | libsdl1.2-dev (or configure qemu with --disable-gfx-check, how?) |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 191 | bridge-utils |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 192 | """ |
| 193 | self.addresses= [ |
| 194 | {"mac" : address["mac"], |
| 195 | "ip" : address["ip"], |
| 196 | "is_used" : False} for address in addresses] |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 197 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 198 | self.build_dir = self.host.get_tmp_dir() |
| 199 | self.support_dir= self.host.get_tmp_dir() |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 200 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 201 | self.host.run('echo "%s" > "%s"' % ( |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 202 | utils.sh_escape(_qemu_ifup_script), |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 203 | utils.sh_escape(os.path.join(self.support_dir, |
| 204 | "qemu-ifup.sh")),)) |
| 205 | self.host.run('chmod a+x "%s"' % ( |
| 206 | utils.sh_escape(os.path.join(self.support_dir, |
| 207 | "qemu-ifup.sh")),)) |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 208 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 209 | self.host.send_file(self.source_material, self.build_dir) |
| 210 | remote_source_material= os.path.join(self.build_dir, |
| 211 | os.path.basename(self.source_material)) |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 212 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 213 | self.build_dir= utils.unarchive(self.host, |
| 214 | remote_source_material) |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 215 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 216 | if insert_modules: |
| 217 | configure_modules= "" |
| 218 | self.insert_modules= True |
| 219 | else: |
| 220 | configure_modules= "--with-patched-kernel " |
| 221 | self.insert_modules= False |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 222 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 223 | # build |
| 224 | if build: |
| 225 | try: |
| 226 | self.host.run('make -C "%s" clean' % ( |
| 227 | utils.sh_escape(self.build_dir),)) |
| mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 228 | except AutoservRunError: |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 229 | # directory was already clean and contained |
| 230 | # no makefile |
| 231 | pass |
| 232 | self.host.run('cd "%s" && ./configure %s' % ( |
| 233 | utils.sh_escape(self.build_dir), |
| 234 | configure_modules,)) |
| mbligh | 7af0552 | 2007-12-04 22:48:35 +0000 | [diff] [blame] | 235 | if syncdir: |
| 236 | cmd = 'cd "%s/kernel" && make sync LINUX=%s' % ( |
| 237 | utils.sh_escape(self.build_dir), |
| 238 | utils.sh_escape(syncdir)) |
| 239 | self.host.run(cmd) |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 240 | self.host.run('make -j%d -C "%s"' % ( |
| 241 | self.host.get_num_cpu() * 2, |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 242 | utils.sh_escape(self.build_dir),)) |
| mbligh | 7af0552 | 2007-12-04 22:48:35 +0000 | [diff] [blame] | 243 | # remember path to modules |
| 244 | self.modules['kvm'] = "%s" %( |
| 245 | utils.sh_escape(os.path.join(self.build_dir, |
| 246 | "kernel/kvm.ko"))) |
| 247 | self.modules['kvm-intel'] = "%s" %( |
| 248 | utils.sh_escape(os.path.join(self.build_dir, |
| 249 | "kernel/kvm-intel.ko"))) |
| 250 | self.modules['kvm-amd'] = "%s" %( |
| 251 | utils.sh_escape(os.path.join(self.build_dir, |
| 252 | "kernel/kvm-amd.ko"))) |
| 253 | print self.modules |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 254 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 255 | self.initialize() |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 256 | |
| 257 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 258 | def initialize(self): |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 259 | """ |
| 260 | Initialize the hypervisor. |
| 261 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 262 | Loads needed kernel modules and creates temporary directories. |
| 263 | The logic is that you could compile once and |
| 264 | initialize - deinitialize many times. But why you would do that |
| 265 | has yet to be figured. |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 266 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 267 | Raises: |
| 268 | AutoservVirtError: cpuid doesn't report virtualization |
| mbligh | 770e3d7 | 2007-11-02 01:33:52 +0000 | [diff] [blame] | 269 | extentions (vmx for intel or svm for amd), in |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 270 | this case, kvm cannot run. |
| 271 | """ |
| 272 | self.pid_dir= self.host.get_tmp_dir() |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 273 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 274 | if self.insert_modules: |
| 275 | self._remove_modules() |
| 276 | self._insert_modules() |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 277 | |
| 278 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 279 | def deinitialize(self): |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 280 | """ |
| 281 | Terminate the hypervisor. |
| 282 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 283 | Kill all the virtual machines that are still running and |
| 284 | unload the kernel modules. |
| 285 | """ |
| 286 | self.refresh_guests() |
| 287 | for address in self.addresses: |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 288 | if address["is_used"]: |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 289 | self.delete_guest(address["ip"]) |
| 290 | self.pid_dir= None |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 291 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 292 | if self.insert_modules: |
| 293 | self._remove_modules() |
| 294 | |
| 295 | |
| 296 | def new_guest(self, qemu_options): |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 297 | """ |
| 298 | Start a new guest ("virtual machine"). |
| 299 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 300 | Returns: |
| 301 | The ip that was picked from the list supplied to |
| 302 | install() and assigned to this guest. |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 303 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 304 | Raises: |
| 305 | AutoservVirtError: no more addresses are available. |
| 306 | """ |
| 307 | for address in self.addresses: |
| 308 | if not address["is_used"]: |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 309 | break |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 310 | else: |
| mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 311 | raise AutoservVirtError( |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 312 | "No more addresses available") |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 313 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 314 | retval= self.host.run( |
| mbligh | 74b22c3 | 2008-01-22 16:14:48 +0000 | [diff] [blame^] | 315 | '%s' |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 316 | # this is the line of options that can be modified |
| 317 | ' %s ' |
| mbligh | 74b22c3 | 2008-01-22 16:14:48 +0000 | [diff] [blame^] | 318 | '-pidfile "%s" -daemonize -nographic ' |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 319 | #~ '-serial telnet::4444,server ' |
| 320 | '-monitor unix:"%s",server,nowait ' |
| mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 321 | '-net nic,macaddr="%s" -net tap,script="%s" -L "%s"' % ( |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 322 | utils.sh_escape(os.path.join( |
| 323 | self.build_dir, |
| 324 | "qemu/x86_64-softmmu/qemu-system-x86_64")), |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 325 | qemu_options, |
| 326 | utils.sh_escape(os.path.join( |
| 327 | self.pid_dir, |
| 328 | "vhost%s_pid" % (address["ip"],))), |
| 329 | utils.sh_escape(os.path.join( |
| 330 | self.pid_dir, |
| 331 | "vhost%s_monitor" % (address["ip"],))), |
| 332 | utils.sh_escape(address["mac"]), |
| 333 | utils.sh_escape(os.path.join( |
| 334 | self.support_dir, |
| mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 335 | "qemu-ifup.sh")), |
| 336 | utils.sh_escape(os.path.join( |
| 337 | self.build_dir, |
| 338 | "qemu/pc-bios")),)) |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 339 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 340 | address["is_used"]= True |
| 341 | return address["ip"] |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 342 | |
| 343 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 344 | def refresh_guests(self): |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 345 | """ |
| 346 | Refresh the list of guests addresses. |
| 347 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 348 | The is_used status will be updated according to the presence |
| 349 | of the process specified in the pid file that was written when |
| 350 | the virtual machine was started. |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 351 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 352 | TODO(poirier): there are a lot of race conditions in this code |
| 353 | because the process might terminate on its own anywhere in |
| 354 | between |
| 355 | """ |
| 356 | for address in self.addresses: |
| mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 357 | if address["is_used"]: |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 358 | pid_file_name= utils.sh_escape(os.path.join( |
| 359 | self.pid_dir, |
| 360 | "vhost%s_pid" % (address["ip"],))) |
| 361 | monitor_file_name= utils.sh_escape(os.path.join( |
| 362 | self.pid_dir, |
| 363 | "vhost%s_monitor" % (address["ip"],))) |
| 364 | retval= self.host.run( |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 365 | _check_process_script % { |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 366 | "pid_file_name" : pid_file_name, |
| 367 | "monitor_file_name" : monitor_file_name, |
| 368 | "qemu_binary" : utils.sh_escape( |
| 369 | os.path.join(self.build_dir, |
| 370 | "qemu/x86_64-softmmu/" |
| 371 | "qemu-system-x86_64")),}) |
| 372 | if (retval.stdout.strip() != |
| 373 | "process present"): |
| 374 | address["is_used"]= False |
| 375 | |
| 376 | |
| 377 | def delete_guest(self, guest_hostname): |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 378 | """ |
| 379 | Terminate a virtual machine. |
| 380 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 381 | Args: |
| 382 | guest_hostname: the ip (as it was specified in the |
| 383 | address list given to install()) of the guest |
| 384 | to terminate. |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 385 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 386 | Raises: |
| 387 | AutoservVirtError: the guest_hostname argument is |
| 388 | invalid |
| 389 | |
| 390 | TODO(poirier): is there a difference in qemu between |
| 391 | sending SIGTEM or quitting from the monitor? |
| 392 | TODO(poirier): there are a lot of race conditions in this code |
| 393 | because the process might terminate on its own anywhere in |
| 394 | between |
| 395 | """ |
| 396 | for address in self.addresses: |
| 397 | if address["ip"] == guest_hostname: |
| 398 | if address["is_used"]: |
| 399 | break |
| 400 | else: |
| 401 | # Will happen if deinitialize() is |
| 402 | # called while guest objects still |
| 403 | # exit and these are del'ed after. |
| 404 | # In that situation, nothing is to |
| 405 | # be done here, don't throw an error |
| 406 | # either because it will print an |
| 407 | # ugly message during garbage |
| 408 | # collection. The solution would be to |
| 409 | # delete the guest objects before |
| 410 | # calling deinitialize(), this can't be |
| 411 | # done by the KVM class, it has no |
| 412 | # reference to those objects and it |
| 413 | # cannot have any either. The Guest |
| 414 | # objects already need to have a |
| 415 | # reference to their managing |
| 416 | # hypervisor. If the hypervisor had a |
| 417 | # reference to the Guest objects it |
| 418 | # manages, it would create a circular |
| 419 | # reference and those objects would |
| 420 | # not be elligible for garbage |
| 421 | # collection. In turn, this means that |
| 422 | # the KVM object would not be |
| 423 | # automatically del'ed at the end of |
| 424 | # the program and guests that are still |
| 425 | # running would be left unattended. |
| 426 | # Note that this circular reference |
| 427 | # problem could be avoided by using |
| 428 | # weakref's in class KVM but the |
| 429 | # control file will most likely also |
| 430 | # have references to the guests. |
| 431 | return |
| 432 | else: |
| mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 433 | raise AutoservVirtError("Unknown guest hostname") |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 434 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 435 | pid_file_name= utils.sh_escape(os.path.join(self.pid_dir, |
| 436 | "vhost%s_pid" % (address["ip"],))) |
| 437 | monitor_file_name= utils.sh_escape(os.path.join(self.pid_dir, |
| 438 | "vhost%s_monitor" % (address["ip"],))) |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 439 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 440 | retval= self.host.run( |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 441 | _check_process_script % { |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 442 | "pid_file_name" : pid_file_name, |
| 443 | "monitor_file_name" : monitor_file_name, |
| 444 | "qemu_binary" : utils.sh_escape(os.path.join( |
| 445 | self.build_dir, |
| 446 | "qemu/x86_64-softmmu/qemu-system-x86_64")),}) |
| 447 | if retval.stdout.strip() == "process present": |
| 448 | self.host.run('kill $(cat "%s")' %( |
| 449 | pid_file_name,)) |
| 450 | self.host.run('rm -f "%s"' %( |
| 451 | pid_file_name,)) |
| 452 | self.host.run('rm -f "%s"' %( |
| 453 | monitor_file_name,)) |
| 454 | address["is_used"]= False |
| 455 | |
| 456 | |
| 457 | def reset_guest(self, guest_hostname): |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 458 | """ |
| 459 | Perform a hard reset on a virtual machine. |
| 460 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 461 | Args: |
| 462 | guest_hostname: the ip (as it was specified in the |
| 463 | address list given to install()) of the guest |
| 464 | to terminate. |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 465 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 466 | Raises: |
| 467 | AutoservVirtError: the guest_hostname argument is |
| 468 | invalid |
| 469 | """ |
| 470 | for address in self.addresses: |
| 471 | if address["ip"] is guest_hostname: |
| 472 | if address["is_used"]: |
| 473 | break |
| 474 | else: |
| mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 475 | raise AutoservVirtError("guest " |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 476 | "hostname not in use") |
| 477 | else: |
| mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 478 | raise AutoservVirtError("Unknown guest hostname") |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 479 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 480 | monitor_file_name= utils.sh_escape(os.path.join(self.pid_dir, |
| 481 | "vhost%s_monitor" % (address["ip"],))) |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 482 | |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 483 | self.host.run('python -c "%s"' % (utils.sh_escape( |
| mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 484 | _hard_reset_script % { |
| mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 485 | "monitor_file_name" : monitor_file_name,}),)) |