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