lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 1 | import sys, os, time, commands, re, logging, signal, glob, threading, shutil |
lmr | 47a853b | 2010-02-04 13:56:48 +0000 | [diff] [blame] | 2 | from autotest_lib.client.bin import test, utils |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 3 | from autotest_lib.client.common_lib import error |
lmr | 9e964a0 | 2010-06-18 03:46:21 +0000 | [diff] [blame] | 4 | import kvm_vm, kvm_utils, kvm_subprocess, kvm_monitor, ppm_utils |
lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 5 | try: |
| 6 | import PIL.Image |
| 7 | except ImportError: |
| 8 | logging.warning('No python imaging library installed. PPM image ' |
| 9 | 'conversion to JPEG disabled. In order to enable it, ' |
| 10 | 'please install python-imaging or the equivalent for your ' |
| 11 | 'distro.') |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 12 | |
| 13 | |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 14 | _screendump_thread = None |
| 15 | _screendump_thread_termination_event = None |
| 16 | |
| 17 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 18 | def preprocess_image(test, params): |
| 19 | """ |
| 20 | Preprocess a single QEMU image according to the instructions in params. |
| 21 | |
| 22 | @param test: Autotest test object. |
| 23 | @param params: A dict containing image preprocessing parameters. |
| 24 | @note: Currently this function just creates an image if requested. |
| 25 | """ |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 26 | image_filename = kvm_vm.get_image_filename(params, test.bindir) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 27 | |
| 28 | create_image = False |
| 29 | |
| 30 | if params.get("force_create_image") == "yes": |
| 31 | logging.debug("'force_create_image' specified; creating image...") |
| 32 | create_image = True |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 33 | elif (params.get("create_image") == "yes" and not |
| 34 | os.path.exists(image_filename)): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 35 | logging.debug("Creating image...") |
| 36 | create_image = True |
| 37 | |
lmr | 0294de3 | 2009-09-09 22:44:28 +0000 | [diff] [blame] | 38 | if create_image and not kvm_vm.create_image(params, test.bindir): |
| 39 | raise error.TestError("Could not create image") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def preprocess_vm(test, params, env, name): |
| 43 | """ |
| 44 | Preprocess a single VM object according to the instructions in params. |
| 45 | Start the VM if requested and get a screendump. |
| 46 | |
| 47 | @param test: An Autotest test object. |
| 48 | @param params: A dict containing VM preprocessing parameters. |
| 49 | @param env: The environment (a dict-like object). |
| 50 | @param name: The name of the VM object. |
| 51 | """ |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 52 | logging.debug("Preprocessing VM '%s'..." % name) |
| 53 | vm = kvm_utils.env_get_vm(env, name) |
| 54 | if vm: |
| 55 | logging.debug("VM object found in environment") |
| 56 | else: |
| 57 | logging.debug("VM object does not exist; creating it") |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 58 | vm = kvm_vm.VM(name, params, test.bindir, env.get("address_cache")) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 59 | kvm_utils.env_register_vm(env, name, vm) |
| 60 | |
| 61 | start_vm = False |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 62 | |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame^] | 63 | if params.get("restart_vm") == "yes": |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 64 | logging.debug("'restart_vm' specified; (re)starting VM...") |
| 65 | start_vm = True |
| 66 | elif params.get("start_vm") == "yes": |
| 67 | if not vm.is_alive(): |
| 68 | logging.debug("VM is not alive; starting it...") |
| 69 | start_vm = True |
| 70 | elif vm.make_qemu_command() != vm.make_qemu_command(name, params, |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 71 | test.bindir): |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 72 | logging.debug("VM's qemu command differs from requested one; " |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 73 | "restarting it...") |
| 74 | start_vm = True |
| 75 | |
lmr | e9a9cf3 | 2010-06-18 12:15:25 +0000 | [diff] [blame] | 76 | if start_vm: |
| 77 | # Start the VM (or restart it if it's already up) |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame^] | 78 | if not vm.create(name, params, test.bindir): |
lmr | e9a9cf3 | 2010-06-18 12:15:25 +0000 | [diff] [blame] | 79 | raise error.TestError("Could not start VM") |
| 80 | else: |
| 81 | # Don't start the VM, just update its params |
| 82 | vm.params = params |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 83 | |
| 84 | scrdump_filename = os.path.join(test.debugdir, "pre_%s.ppm" % name) |
lmr | 9e964a0 | 2010-06-18 03:46:21 +0000 | [diff] [blame] | 85 | try: |
| 86 | if vm.monitor: |
| 87 | vm.monitor.screendump(scrdump_filename) |
| 88 | except kvm_monitor.MonitorError, e: |
| 89 | logging.warn(e) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | def postprocess_image(test, params): |
| 93 | """ |
| 94 | Postprocess a single QEMU image according to the instructions in params. |
| 95 | Currently this function just removes an image if requested. |
| 96 | |
| 97 | @param test: An Autotest test object. |
| 98 | @param params: A dict containing image postprocessing parameters. |
| 99 | """ |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 100 | if params.get("remove_image") == "yes": |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 101 | kvm_vm.remove_image(params, test.bindir) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def postprocess_vm(test, params, env, name): |
| 105 | """ |
| 106 | Postprocess a single VM object according to the instructions in params. |
| 107 | Kill the VM if requested and get a screendump. |
| 108 | |
| 109 | @param test: An Autotest test object. |
| 110 | @param params: A dict containing VM postprocessing parameters. |
| 111 | @param env: The environment (a dict-like object). |
| 112 | @param name: The name of the VM object. |
| 113 | """ |
| 114 | logging.debug("Postprocessing VM '%s'..." % name) |
| 115 | vm = kvm_utils.env_get_vm(env, name) |
| 116 | if vm: |
| 117 | logging.debug("VM object found in environment") |
| 118 | else: |
| 119 | logging.debug("VM object does not exist in environment") |
| 120 | return |
| 121 | |
| 122 | scrdump_filename = os.path.join(test.debugdir, "post_%s.ppm" % name) |
lmr | 9e964a0 | 2010-06-18 03:46:21 +0000 | [diff] [blame] | 123 | try: |
| 124 | if vm.monitor: |
| 125 | vm.monitor.screendump(scrdump_filename) |
| 126 | except kvm_monitor.MonitorError, e: |
| 127 | logging.warn(e) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 128 | |
| 129 | if params.get("kill_vm") == "yes": |
lmr | adbfb1a | 2009-10-13 13:44:14 +0000 | [diff] [blame] | 130 | kill_vm_timeout = float(params.get("kill_vm_timeout", 0)) |
| 131 | if kill_vm_timeout: |
| 132 | logging.debug("'kill_vm' specified; waiting for VM to shut down " |
| 133 | "before killing it...") |
| 134 | kvm_utils.wait_for(vm.is_dead, kill_vm_timeout, 0, 1) |
| 135 | else: |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 136 | logging.debug("'kill_vm' specified; killing VM...") |
| 137 | vm.destroy(gracefully = params.get("kill_vm_gracefully") == "yes") |
| 138 | |
| 139 | |
lmr | 12b98a7 | 2010-02-09 11:13:57 +0000 | [diff] [blame] | 140 | def process_command(test, params, env, command, command_timeout, |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 141 | command_noncritical): |
| 142 | """ |
| 143 | Pre- or post- custom commands to be executed before/after a test is run |
| 144 | |
| 145 | @param test: An Autotest test object. |
| 146 | @param params: A dict containing all VM and image parameters. |
| 147 | @param env: The environment (a dict-like object). |
lmr | 12b98a7 | 2010-02-09 11:13:57 +0000 | [diff] [blame] | 148 | @param command: Command to be run. |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 149 | @param command_timeout: Timeout for command execution. |
| 150 | @param command_noncritical: If True test will not fail if command fails. |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 151 | """ |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 152 | # Export environment vars |
lmr | 0ce407d | 2010-03-23 15:46:30 +0000 | [diff] [blame] | 153 | for k in params: |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 154 | os.putenv("KVM_TEST_%s" % k, str(params[k])) |
lmr | b1ef847 | 2010-02-05 11:21:13 +0000 | [diff] [blame] | 155 | # Execute commands |
lmr | 12b98a7 | 2010-02-09 11:13:57 +0000 | [diff] [blame] | 156 | try: |
| 157 | utils.system("cd %s; %s" % (test.bindir, command)) |
| 158 | except error.CmdError, e: |
lmr | 1084553 | 2010-06-18 12:54:46 +0000 | [diff] [blame] | 159 | if command_noncritical: |
| 160 | logging.warn(e) |
| 161 | else: |
| 162 | raise |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 163 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 164 | def process(test, params, env, image_func, vm_func): |
| 165 | """ |
| 166 | Pre- or post-process VMs and images according to the instructions in params. |
| 167 | Call image_func for each image listed in params and vm_func for each VM. |
| 168 | |
| 169 | @param test: An Autotest test object. |
| 170 | @param params: A dict containing all VM and image parameters. |
| 171 | @param env: The environment (a dict-like object). |
| 172 | @param image_func: A function to call for each image. |
| 173 | @param vm_func: A function to call for each VM. |
| 174 | """ |
| 175 | # Get list of VMs specified for this test |
| 176 | vm_names = kvm_utils.get_sub_dict_names(params, "vms") |
| 177 | for vm_name in vm_names: |
| 178 | vm_params = kvm_utils.get_sub_dict(params, vm_name) |
| 179 | # Get list of images specified for this VM |
| 180 | image_names = kvm_utils.get_sub_dict_names(vm_params, "images") |
| 181 | for image_name in image_names: |
| 182 | image_params = kvm_utils.get_sub_dict(vm_params, image_name) |
| 183 | # Call image_func for each image |
| 184 | image_func(test, image_params) |
| 185 | # Call vm_func for each vm |
| 186 | vm_func(test, vm_params, env, vm_name) |
| 187 | |
| 188 | |
| 189 | def preprocess(test, params, env): |
| 190 | """ |
| 191 | Preprocess all VMs and images according to the instructions in params. |
| 192 | Also, collect some host information, such as the KVM version. |
| 193 | |
| 194 | @param test: An Autotest test object. |
| 195 | @param params: A dict containing all VM and image parameters. |
| 196 | @param env: The environment (a dict-like object). |
| 197 | """ |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 198 | # Start tcpdump if it isn't already running |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 199 | if "address_cache" not in env: |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 200 | env["address_cache"] = {} |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 201 | if "tcpdump" in env and not env["tcpdump"].is_alive(): |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 202 | env["tcpdump"].close() |
| 203 | del env["tcpdump"] |
lmr | b1f7646 | 2010-05-25 23:44:03 +0000 | [diff] [blame] | 204 | if "tcpdump" not in env and params.get("run_tcpdump", "yes") == "yes": |
lmr | 3eb39c7 | 2010-07-08 23:34:05 +0000 | [diff] [blame] | 205 | cmd = "%s -npvi any 'dst port 68'" % kvm_utils.find_command("tcpdump") |
| 206 | logging.debug("Starting tcpdump (%s)...", cmd) |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 207 | env["tcpdump"] = kvm_subprocess.kvm_tail( |
lmr | 3eb39c7 | 2010-07-08 23:34:05 +0000 | [diff] [blame] | 208 | command=cmd, |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 209 | output_func=_update_address_cache, |
| 210 | output_params=(env["address_cache"],)) |
| 211 | if kvm_utils.wait_for(lambda: not env["tcpdump"].is_alive(), |
| 212 | 0.1, 0.1, 1.0): |
| 213 | logging.warn("Could not start tcpdump") |
| 214 | logging.warn("Status: %s" % env["tcpdump"].get_status()) |
| 215 | logging.warn("Output:" + kvm_utils.format_str_for_message( |
| 216 | env["tcpdump"].get_output())) |
| 217 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 218 | # Destroy and remove VMs that are no longer needed in the environment |
| 219 | requested_vms = kvm_utils.get_sub_dict_names(params, "vms") |
lmr | 0ce407d | 2010-03-23 15:46:30 +0000 | [diff] [blame] | 220 | for key in env.keys(): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 221 | vm = env[key] |
| 222 | if not kvm_utils.is_vm(vm): |
| 223 | continue |
| 224 | if not vm.name in requested_vms: |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 225 | logging.debug("VM '%s' found in environment but not required for " |
| 226 | "test; removing it..." % vm.name) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 227 | vm.destroy() |
| 228 | del env[key] |
| 229 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 230 | # Get the KVM kernel module version and write it as a keyval |
| 231 | logging.debug("Fetching KVM module version...") |
| 232 | if os.path.exists("/dev/kvm"): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 233 | try: |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 234 | kvm_version = open("/sys/module/kvm/version").read().strip() |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 235 | except: |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 236 | kvm_version = os.uname()[2] |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 237 | else: |
| 238 | kvm_version = "Unknown" |
| 239 | logging.debug("KVM module not loaded") |
| 240 | logging.debug("KVM version: %s" % kvm_version) |
| 241 | test.write_test_keyval({"kvm_version": kvm_version}) |
| 242 | |
| 243 | # Get the KVM userspace version and write it as a keyval |
| 244 | logging.debug("Fetching KVM userspace version...") |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 245 | qemu_path = kvm_utils.get_path(test.bindir, params.get("qemu_binary", |
| 246 | "qemu")) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 247 | version_line = commands.getoutput("%s -help | head -n 1" % qemu_path) |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 248 | matches = re.findall("[Vv]ersion .*?,", version_line) |
| 249 | if matches: |
| 250 | kvm_userspace_version = " ".join(matches[0].split()[1:]).strip(",") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 251 | else: |
| 252 | kvm_userspace_version = "Unknown" |
| 253 | logging.debug("Could not fetch KVM userspace version") |
| 254 | logging.debug("KVM userspace version: %s" % kvm_userspace_version) |
| 255 | test.write_test_keyval({"kvm_userspace_version": kvm_userspace_version}) |
| 256 | |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 257 | # Execute any pre_commands |
| 258 | if params.get("pre_command"): |
| 259 | process_command(test, params, env, params.get("pre_command"), |
| 260 | int(params.get("pre_command_timeout", "600")), |
| 261 | params.get("pre_command_noncritical") == "yes") |
| 262 | |
| 263 | # Preprocess all VMs and images |
| 264 | process(test, params, env, preprocess_image, preprocess_vm) |
| 265 | |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 266 | # Start the screendump thread |
| 267 | if params.get("take_regular_screendumps") == "yes": |
| 268 | logging.debug("Starting screendump thread") |
| 269 | global _screendump_thread, _screendump_thread_termination_event |
| 270 | _screendump_thread_termination_event = threading.Event() |
| 271 | _screendump_thread = threading.Thread(target=_take_screendumps, |
| 272 | args=(test, params, env)) |
| 273 | _screendump_thread.start() |
| 274 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 275 | |
| 276 | def postprocess(test, params, env): |
| 277 | """ |
| 278 | Postprocess all VMs and images according to the instructions in params. |
| 279 | |
| 280 | @param test: An Autotest test object. |
| 281 | @param params: Dict containing all VM and image parameters. |
| 282 | @param env: The environment (a dict-like object). |
| 283 | """ |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 284 | # Postprocess all VMs and images |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 285 | process(test, params, env, postprocess_image, postprocess_vm) |
| 286 | |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 287 | # Terminate the screendump thread |
| 288 | global _screendump_thread, _screendump_thread_termination_event |
| 289 | if _screendump_thread: |
| 290 | logging.debug("Terminating screendump thread...") |
| 291 | _screendump_thread_termination_event.set() |
| 292 | _screendump_thread.join(10) |
lmr | 327729b | 2010-06-14 17:14:00 +0000 | [diff] [blame] | 293 | _screendump_thread = None |
| 294 | _screendump_thread_termination_event = None |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 295 | |
lmr | 665975c | 2009-12-27 20:01:41 +0000 | [diff] [blame] | 296 | # Warn about corrupt PPM files |
| 297 | for f in glob.glob(os.path.join(test.debugdir, "*.ppm")): |
| 298 | if not ppm_utils.image_verify_ppm_file(f): |
| 299 | logging.warn("Found corrupt PPM file: %s", f) |
| 300 | |
lmr | 0ee0a9c | 2009-07-24 19:23:29 +0000 | [diff] [blame] | 301 | # Should we convert PPM files to PNG format? |
| 302 | if params.get("convert_ppm_files_to_png") == "yes": |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 303 | logging.debug("'convert_ppm_files_to_png' specified; converting PPM " |
| 304 | "files to PNG format...") |
lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 305 | try: |
| 306 | for f in glob.glob(os.path.join(test.debugdir, "*.ppm")): |
lmr | 15c4486 | 2009-09-10 03:23:45 +0000 | [diff] [blame] | 307 | if ppm_utils.image_verify_ppm_file(f): |
| 308 | new_path = f.replace(".ppm", ".png") |
| 309 | image = PIL.Image.open(f) |
| 310 | image.save(new_path, format='PNG') |
lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 311 | except NameError: |
| 312 | pass |
lmr | 0ee0a9c | 2009-07-24 19:23:29 +0000 | [diff] [blame] | 313 | |
| 314 | # Should we keep the PPM files? |
| 315 | if params.get("keep_ppm_files") != "yes": |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 316 | logging.debug("'keep_ppm_files' not specified; removing all PPM files " |
| 317 | "from debug dir...") |
lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 318 | for f in glob.glob(os.path.join(test.debugdir, '*.ppm')): |
| 319 | os.unlink(f) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 320 | |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 321 | # Should we keep the screendump dirs? |
| 322 | if params.get("keep_screendumps") != "yes": |
| 323 | logging.debug("'keep_screendumps' not specified; removing screendump " |
| 324 | "dirs...") |
| 325 | for d in glob.glob(os.path.join(test.debugdir, "screendumps_*")): |
| 326 | if os.path.isdir(d) and not os.path.islink(d): |
| 327 | shutil.rmtree(d, ignore_errors=True) |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 328 | |
lmr | 588f497 | 2009-10-13 13:43:10 +0000 | [diff] [blame] | 329 | # Kill all unresponsive VMs |
| 330 | if params.get("kill_unresponsive_vms") == "yes": |
| 331 | logging.debug("'kill_unresponsive_vms' specified; killing all VMs " |
| 332 | "that fail to respond to a remote login request...") |
| 333 | for vm in kvm_utils.env_get_all_vms(env): |
| 334 | if vm.is_alive(): |
| 335 | session = vm.remote_login() |
| 336 | if session: |
| 337 | session.close() |
| 338 | else: |
| 339 | vm.destroy(gracefully=False) |
| 340 | |
lmr | ee4338e | 2010-07-08 23:40:10 +0000 | [diff] [blame] | 341 | # Kill all kvm_subprocess tail threads |
| 342 | kvm_subprocess.kill_tail_threads() |
lmr | a419700 | 2009-08-13 05:00:51 +0000 | [diff] [blame] | 343 | |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 344 | # Terminate tcpdump if no VMs are alive |
| 345 | living_vms = [vm for vm in kvm_utils.env_get_all_vms(env) if vm.is_alive()] |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 346 | if not living_vms and "tcpdump" in env: |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 347 | env["tcpdump"].close() |
| 348 | del env["tcpdump"] |
| 349 | |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 350 | # Execute any post_commands |
| 351 | if params.get("post_command"): |
| 352 | process_command(test, params, env, params.get("post_command"), |
| 353 | int(params.get("post_command_timeout", "600")), |
| 354 | params.get("post_command_noncritical") == "yes") |
| 355 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 356 | |
| 357 | def postprocess_on_error(test, params, env): |
| 358 | """ |
| 359 | Perform postprocessing operations required only if the test failed. |
| 360 | |
| 361 | @param test: An Autotest test object. |
| 362 | @param params: A dict containing all VM and image parameters. |
| 363 | @param env: The environment (a dict-like object). |
| 364 | """ |
| 365 | params.update(kvm_utils.get_sub_dict(params, "on_error")) |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 366 | |
| 367 | |
| 368 | def _update_address_cache(address_cache, line): |
| 369 | if re.search("Your.IP", line, re.IGNORECASE): |
| 370 | matches = re.findall(r"\d*\.\d*\.\d*\.\d*", line) |
| 371 | if matches: |
| 372 | address_cache["last_seen"] = matches[0] |
| 373 | if re.search("Client.Ethernet.Address", line, re.IGNORECASE): |
| 374 | matches = re.findall(r"\w*:\w*:\w*:\w*:\w*:\w*", line) |
| 375 | if matches and address_cache.get("last_seen"): |
| 376 | mac_address = matches[0].lower() |
| 377 | logging.debug("(address cache) Adding cache entry: %s ---> %s", |
| 378 | mac_address, address_cache.get("last_seen")) |
| 379 | address_cache[mac_address] = address_cache.get("last_seen") |
| 380 | del address_cache["last_seen"] |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 381 | |
| 382 | |
| 383 | def _take_screendumps(test, params, env): |
| 384 | global _screendump_thread_termination_event |
| 385 | temp_dir = test.debugdir |
| 386 | if params.get("screendump_temp_dir"): |
| 387 | temp_dir = kvm_utils.get_path(test.bindir, |
| 388 | params.get("screendump_temp_dir")) |
| 389 | try: |
| 390 | os.makedirs(temp_dir) |
| 391 | except OSError: |
| 392 | pass |
| 393 | temp_filename = os.path.join(temp_dir, "scrdump-%s.ppm" % |
| 394 | kvm_utils.generate_random_string(6)) |
| 395 | delay = float(params.get("screendump_delay", 5)) |
| 396 | quality = int(params.get("screendump_quality", 30)) |
| 397 | |
| 398 | cache = {} |
| 399 | |
| 400 | while True: |
| 401 | for vm in kvm_utils.env_get_all_vms(env): |
lmr | 7fa9fe7 | 2010-06-22 01:46:47 +0000 | [diff] [blame] | 402 | if not vm.is_alive(): |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 403 | continue |
lmr | 9e964a0 | 2010-06-18 03:46:21 +0000 | [diff] [blame] | 404 | try: |
| 405 | vm.monitor.screendump(temp_filename) |
| 406 | except kvm_monitor.MonitorError, e: |
| 407 | logging.warn(e) |
| 408 | continue |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 409 | if not os.path.exists(temp_filename): |
| 410 | logging.warn("VM '%s' failed to produce a screendump", vm.name) |
| 411 | continue |
| 412 | if not ppm_utils.image_verify_ppm_file(temp_filename): |
| 413 | logging.warn("VM '%s' produced an invalid screendump", vm.name) |
| 414 | os.unlink(temp_filename) |
| 415 | continue |
| 416 | screendump_dir = os.path.join(test.debugdir, |
| 417 | "screendumps_%s" % vm.name) |
| 418 | try: |
| 419 | os.makedirs(screendump_dir) |
| 420 | except OSError: |
| 421 | pass |
| 422 | screendump_filename = os.path.join(screendump_dir, |
| 423 | "%s_%s.jpg" % (vm.name, |
| 424 | time.strftime("%Y-%m-%d_%H-%M-%S"))) |
| 425 | hash = utils.hash_file(temp_filename) |
| 426 | if hash in cache: |
| 427 | try: |
| 428 | os.link(cache[hash], screendump_filename) |
| 429 | except OSError: |
| 430 | pass |
| 431 | else: |
| 432 | try: |
| 433 | image = PIL.Image.open(temp_filename) |
| 434 | image.save(screendump_filename, format="JPEG", quality=quality) |
| 435 | cache[hash] = screendump_filename |
| 436 | except NameError: |
| 437 | pass |
| 438 | os.unlink(temp_filename) |
| 439 | if _screendump_thread_termination_event.isSet(): |
| 440 | break |
| 441 | _screendump_thread_termination_event.wait(delay) |