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 | # TODO(poirier): uses start-stop-daemon until qemu -pidfile |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 315 | # and -daemonize can work together, this 'hides' the return |
| 316 | # code of qemu (bad) |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 317 | retval= self.host.run( |
| 318 | 'start-stop-daemon -S --exec "%s" --pidfile "%s" -b -- ' |
| 319 | # this is the line of options that can be modified |
| 320 | ' %s ' |
| 321 | '-pidfile "%s" -nographic ' |
| 322 | #~ '-serial telnet::4444,server ' |
| 323 | '-monitor unix:"%s",server,nowait ' |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 324 | '-net nic,macaddr="%s" -net tap,script="%s" -L "%s"' % ( |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 325 | utils.sh_escape(os.path.join( |
| 326 | self.build_dir, |
| 327 | "qemu/x86_64-softmmu/qemu-system-x86_64")), |
| 328 | utils.sh_escape(os.path.join( |
| 329 | self.pid_dir, |
| 330 | "vhost%s_pid" % (address["ip"],))), |
| 331 | qemu_options, |
| 332 | utils.sh_escape(os.path.join( |
| 333 | self.pid_dir, |
| 334 | "vhost%s_pid" % (address["ip"],))), |
| 335 | utils.sh_escape(os.path.join( |
| 336 | self.pid_dir, |
| 337 | "vhost%s_monitor" % (address["ip"],))), |
| 338 | utils.sh_escape(address["mac"]), |
| 339 | utils.sh_escape(os.path.join( |
| 340 | self.support_dir, |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 341 | "qemu-ifup.sh")), |
| 342 | utils.sh_escape(os.path.join( |
| 343 | self.build_dir, |
| 344 | "qemu/pc-bios")),)) |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 345 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 346 | address["is_used"]= True |
| 347 | return address["ip"] |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 348 | |
| 349 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 350 | def refresh_guests(self): |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 351 | """ |
| 352 | Refresh the list of guests addresses. |
| 353 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 354 | The is_used status will be updated according to the presence |
| 355 | of the process specified in the pid file that was written when |
| 356 | the virtual machine was started. |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 357 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 358 | TODO(poirier): there are a lot of race conditions in this code |
| 359 | because the process might terminate on its own anywhere in |
| 360 | between |
| 361 | """ |
| 362 | for address in self.addresses: |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 363 | if address["is_used"]: |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 364 | pid_file_name= utils.sh_escape(os.path.join( |
| 365 | self.pid_dir, |
| 366 | "vhost%s_pid" % (address["ip"],))) |
| 367 | monitor_file_name= utils.sh_escape(os.path.join( |
| 368 | self.pid_dir, |
| 369 | "vhost%s_monitor" % (address["ip"],))) |
| 370 | retval= self.host.run( |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 371 | _check_process_script % { |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 372 | "pid_file_name" : pid_file_name, |
| 373 | "monitor_file_name" : monitor_file_name, |
| 374 | "qemu_binary" : utils.sh_escape( |
| 375 | os.path.join(self.build_dir, |
| 376 | "qemu/x86_64-softmmu/" |
| 377 | "qemu-system-x86_64")),}) |
| 378 | if (retval.stdout.strip() != |
| 379 | "process present"): |
| 380 | address["is_used"]= False |
| 381 | |
| 382 | |
| 383 | def delete_guest(self, guest_hostname): |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 384 | """ |
| 385 | Terminate a virtual machine. |
| 386 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 387 | Args: |
| 388 | guest_hostname: the ip (as it was specified in the |
| 389 | address list given to install()) of the guest |
| 390 | to terminate. |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 391 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 392 | Raises: |
| 393 | AutoservVirtError: the guest_hostname argument is |
| 394 | invalid |
| 395 | |
| 396 | TODO(poirier): is there a difference in qemu between |
| 397 | sending SIGTEM or quitting from the monitor? |
| 398 | TODO(poirier): there are a lot of race conditions in this code |
| 399 | because the process might terminate on its own anywhere in |
| 400 | between |
| 401 | """ |
| 402 | for address in self.addresses: |
| 403 | if address["ip"] == guest_hostname: |
| 404 | if address["is_used"]: |
| 405 | break |
| 406 | else: |
| 407 | # Will happen if deinitialize() is |
| 408 | # called while guest objects still |
| 409 | # exit and these are del'ed after. |
| 410 | # In that situation, nothing is to |
| 411 | # be done here, don't throw an error |
| 412 | # either because it will print an |
| 413 | # ugly message during garbage |
| 414 | # collection. The solution would be to |
| 415 | # delete the guest objects before |
| 416 | # calling deinitialize(), this can't be |
| 417 | # done by the KVM class, it has no |
| 418 | # reference to those objects and it |
| 419 | # cannot have any either. The Guest |
| 420 | # objects already need to have a |
| 421 | # reference to their managing |
| 422 | # hypervisor. If the hypervisor had a |
| 423 | # reference to the Guest objects it |
| 424 | # manages, it would create a circular |
| 425 | # reference and those objects would |
| 426 | # not be elligible for garbage |
| 427 | # collection. In turn, this means that |
| 428 | # the KVM object would not be |
| 429 | # automatically del'ed at the end of |
| 430 | # the program and guests that are still |
| 431 | # running would be left unattended. |
| 432 | # Note that this circular reference |
| 433 | # problem could be avoided by using |
| 434 | # weakref's in class KVM but the |
| 435 | # control file will most likely also |
| 436 | # have references to the guests. |
| 437 | return |
| 438 | else: |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 439 | raise AutoservVirtError("Unknown guest hostname") |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 440 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 441 | pid_file_name= utils.sh_escape(os.path.join(self.pid_dir, |
| 442 | "vhost%s_pid" % (address["ip"],))) |
| 443 | monitor_file_name= utils.sh_escape(os.path.join(self.pid_dir, |
| 444 | "vhost%s_monitor" % (address["ip"],))) |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 445 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 446 | retval= self.host.run( |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 447 | _check_process_script % { |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 448 | "pid_file_name" : pid_file_name, |
| 449 | "monitor_file_name" : monitor_file_name, |
| 450 | "qemu_binary" : utils.sh_escape(os.path.join( |
| 451 | self.build_dir, |
| 452 | "qemu/x86_64-softmmu/qemu-system-x86_64")),}) |
| 453 | if retval.stdout.strip() == "process present": |
| 454 | self.host.run('kill $(cat "%s")' %( |
| 455 | pid_file_name,)) |
| 456 | self.host.run('rm -f "%s"' %( |
| 457 | pid_file_name,)) |
| 458 | self.host.run('rm -f "%s"' %( |
| 459 | monitor_file_name,)) |
| 460 | address["is_used"]= False |
| 461 | |
| 462 | |
| 463 | def reset_guest(self, guest_hostname): |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 464 | """ |
| 465 | Perform a hard reset on a virtual machine. |
| 466 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 467 | Args: |
| 468 | guest_hostname: the ip (as it was specified in the |
| 469 | address list given to install()) of the guest |
| 470 | to terminate. |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 471 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 472 | Raises: |
| 473 | AutoservVirtError: the guest_hostname argument is |
| 474 | invalid |
| 475 | """ |
| 476 | for address in self.addresses: |
| 477 | if address["ip"] is guest_hostname: |
| 478 | if address["is_used"]: |
| 479 | break |
| 480 | else: |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 481 | raise AutoservVirtError("guest " |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 482 | "hostname not in use") |
| 483 | else: |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 484 | raise AutoservVirtError("Unknown guest hostname") |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 485 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 486 | monitor_file_name= utils.sh_escape(os.path.join(self.pid_dir, |
| 487 | "vhost%s_monitor" % (address["ip"],))) |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 488 | |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 489 | self.host.run('python -c "%s"' % (utils.sh_escape( |
mbligh | b24d12d | 2007-08-10 19:30:18 +0000 | [diff] [blame] | 490 | _hard_reset_script % { |
mbligh | 890fc5b | 2007-08-02 18:03:36 +0000 | [diff] [blame] | 491 | "monitor_file_name" : monitor_file_name,}),)) |