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