lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 1 | import sys, os, time, commands, re, logging, signal, glob |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 2 | from autotest_lib.client.bin import test |
| 3 | from autotest_lib.client.common_lib import error |
lmr | 15c4486 | 2009-09-10 03:23:45 +0000 | [diff] [blame] | 4 | import kvm_vm, kvm_utils, kvm_subprocess, 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 | |
| 14 | def preprocess_image(test, params): |
| 15 | """ |
| 16 | Preprocess a single QEMU image according to the instructions in params. |
| 17 | |
| 18 | @param test: Autotest test object. |
| 19 | @param params: A dict containing image preprocessing parameters. |
| 20 | @note: Currently this function just creates an image if requested. |
| 21 | """ |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 22 | image_filename = kvm_vm.get_image_filename(params, test.bindir) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 23 | |
| 24 | create_image = False |
| 25 | |
| 26 | if params.get("force_create_image") == "yes": |
| 27 | logging.debug("'force_create_image' specified; creating image...") |
| 28 | create_image = True |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 29 | elif (params.get("create_image") == "yes" and not |
| 30 | os.path.exists(image_filename)): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 31 | logging.debug("Creating image...") |
| 32 | create_image = True |
| 33 | |
lmr | 0294de3 | 2009-09-09 22:44:28 +0000 | [diff] [blame] | 34 | if create_image and not kvm_vm.create_image(params, test.bindir): |
| 35 | raise error.TestError("Could not create image") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | def preprocess_vm(test, params, env, name): |
| 39 | """ |
| 40 | Preprocess a single VM object according to the instructions in params. |
| 41 | Start the VM if requested and get a screendump. |
| 42 | |
| 43 | @param test: An Autotest test object. |
| 44 | @param params: A dict containing VM preprocessing parameters. |
| 45 | @param env: The environment (a dict-like object). |
| 46 | @param name: The name of the VM object. |
| 47 | """ |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 48 | logging.debug("Preprocessing VM '%s'..." % name) |
| 49 | vm = kvm_utils.env_get_vm(env, name) |
| 50 | if vm: |
| 51 | logging.debug("VM object found in environment") |
| 52 | else: |
| 53 | logging.debug("VM object does not exist; creating it") |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 54 | vm = kvm_vm.VM(name, params, test.bindir, env.get("address_cache")) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 55 | kvm_utils.env_register_vm(env, name, vm) |
| 56 | |
| 57 | start_vm = False |
| 58 | for_migration = False |
| 59 | |
| 60 | if params.get("start_vm_for_migration") == "yes": |
| 61 | logging.debug("'start_vm_for_migration' specified; (re)starting VM with" |
| 62 | " -incoming option...") |
| 63 | start_vm = True |
| 64 | for_migration = True |
| 65 | elif params.get("restart_vm") == "yes": |
| 66 | logging.debug("'restart_vm' specified; (re)starting VM...") |
| 67 | start_vm = True |
| 68 | elif params.get("start_vm") == "yes": |
| 69 | if not vm.is_alive(): |
| 70 | logging.debug("VM is not alive; starting it...") |
| 71 | start_vm = True |
| 72 | elif vm.make_qemu_command() != vm.make_qemu_command(name, params, |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 73 | test.bindir): |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 74 | logging.debug("VM's qemu command differs from requested one; " |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 75 | "restarting it...") |
| 76 | start_vm = True |
| 77 | |
lmr | 0294de3 | 2009-09-09 22:44:28 +0000 | [diff] [blame] | 78 | if start_vm and not vm.create(name, params, test.bindir, for_migration): |
| 79 | raise error.TestError("Could not start VM") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 80 | |
| 81 | scrdump_filename = os.path.join(test.debugdir, "pre_%s.ppm" % name) |
| 82 | vm.send_monitor_cmd("screendump %s" % scrdump_filename) |
| 83 | |
| 84 | |
| 85 | def postprocess_image(test, params): |
| 86 | """ |
| 87 | Postprocess a single QEMU image according to the instructions in params. |
| 88 | Currently this function just removes an image if requested. |
| 89 | |
| 90 | @param test: An Autotest test object. |
| 91 | @param params: A dict containing image postprocessing parameters. |
| 92 | """ |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 93 | if params.get("remove_image") == "yes": |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 94 | kvm_vm.remove_image(params, test.bindir) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | def postprocess_vm(test, params, env, name): |
| 98 | """ |
| 99 | Postprocess a single VM object according to the instructions in params. |
| 100 | Kill the VM if requested and get a screendump. |
| 101 | |
| 102 | @param test: An Autotest test object. |
| 103 | @param params: A dict containing VM postprocessing parameters. |
| 104 | @param env: The environment (a dict-like object). |
| 105 | @param name: The name of the VM object. |
| 106 | """ |
| 107 | logging.debug("Postprocessing VM '%s'..." % name) |
| 108 | vm = kvm_utils.env_get_vm(env, name) |
| 109 | if vm: |
| 110 | logging.debug("VM object found in environment") |
| 111 | else: |
| 112 | logging.debug("VM object does not exist in environment") |
| 113 | return |
| 114 | |
| 115 | scrdump_filename = os.path.join(test.debugdir, "post_%s.ppm" % name) |
| 116 | vm.send_monitor_cmd("screendump %s" % scrdump_filename) |
| 117 | |
| 118 | if params.get("kill_vm") == "yes": |
lmr | adbfb1a | 2009-10-13 13:44:14 +0000 | [diff] [blame] | 119 | kill_vm_timeout = float(params.get("kill_vm_timeout", 0)) |
| 120 | if kill_vm_timeout: |
| 121 | logging.debug("'kill_vm' specified; waiting for VM to shut down " |
| 122 | "before killing it...") |
| 123 | kvm_utils.wait_for(vm.is_dead, kill_vm_timeout, 0, 1) |
| 124 | else: |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 125 | logging.debug("'kill_vm' specified; killing VM...") |
| 126 | vm.destroy(gracefully = params.get("kill_vm_gracefully") == "yes") |
| 127 | |
| 128 | |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 129 | def process_command(test, params, env, command, command_timeout, |
| 130 | command_noncritical): |
| 131 | """ |
| 132 | Pre- or post- custom commands to be executed before/after a test is run |
| 133 | |
| 134 | @param test: An Autotest test object. |
| 135 | @param params: A dict containing all VM and image parameters. |
| 136 | @param env: The environment (a dict-like object). |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 137 | @param command: Command to be run. |
| 138 | @param command_timeout: Timeout for command execution. |
| 139 | @param command_noncritical: If True test will not fail if command fails. |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 140 | """ |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 141 | # Export environment vars |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 142 | for k in params.keys(): |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 143 | os.putenv("KVM_TEST_%s" % k, str(params[k])) |
| 144 | # Execute command |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 145 | logging.info("Executing command '%s'..." % command) |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 146 | (status, output) = kvm_subprocess.run_fg("cd %s; %s" % (test.bindir, |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 147 | command), |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 148 | logging.debug, "(command) ", |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 149 | timeout=command_timeout) |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 150 | if status != 0: |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 151 | logging.warn("Custom processing command failed: '%s'" % command) |
| 152 | if not command_noncritical: |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 153 | raise error.TestError("Custom processing command failed") |
| 154 | |
| 155 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 156 | def process(test, params, env, image_func, vm_func): |
| 157 | """ |
| 158 | Pre- or post-process VMs and images according to the instructions in params. |
| 159 | Call image_func for each image listed in params and vm_func for each VM. |
| 160 | |
| 161 | @param test: An Autotest test object. |
| 162 | @param params: A dict containing all VM and image parameters. |
| 163 | @param env: The environment (a dict-like object). |
| 164 | @param image_func: A function to call for each image. |
| 165 | @param vm_func: A function to call for each VM. |
| 166 | """ |
| 167 | # Get list of VMs specified for this test |
| 168 | vm_names = kvm_utils.get_sub_dict_names(params, "vms") |
| 169 | for vm_name in vm_names: |
| 170 | vm_params = kvm_utils.get_sub_dict(params, vm_name) |
| 171 | # Get list of images specified for this VM |
| 172 | image_names = kvm_utils.get_sub_dict_names(vm_params, "images") |
| 173 | for image_name in image_names: |
| 174 | image_params = kvm_utils.get_sub_dict(vm_params, image_name) |
| 175 | # Call image_func for each image |
| 176 | image_func(test, image_params) |
| 177 | # Call vm_func for each vm |
| 178 | vm_func(test, vm_params, env, vm_name) |
| 179 | |
| 180 | |
| 181 | def preprocess(test, params, env): |
| 182 | """ |
| 183 | Preprocess all VMs and images according to the instructions in params. |
| 184 | Also, collect some host information, such as the KVM version. |
| 185 | |
| 186 | @param test: An Autotest test object. |
| 187 | @param params: A dict containing all VM and image parameters. |
| 188 | @param env: The environment (a dict-like object). |
| 189 | """ |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 190 | # Start tcpdump if it isn't already running |
| 191 | if not env.has_key("address_cache"): |
| 192 | env["address_cache"] = {} |
| 193 | if env.has_key("tcpdump") and not env["tcpdump"].is_alive(): |
| 194 | env["tcpdump"].close() |
| 195 | del env["tcpdump"] |
| 196 | if not env.has_key("tcpdump"): |
| 197 | command = "/usr/sbin/tcpdump -npvi any 'dst port 68'" |
| 198 | logging.debug("Starting tcpdump (%s)...", command) |
| 199 | env["tcpdump"] = kvm_subprocess.kvm_tail( |
| 200 | command=command, |
| 201 | output_func=_update_address_cache, |
| 202 | output_params=(env["address_cache"],)) |
| 203 | if kvm_utils.wait_for(lambda: not env["tcpdump"].is_alive(), |
| 204 | 0.1, 0.1, 1.0): |
| 205 | logging.warn("Could not start tcpdump") |
| 206 | logging.warn("Status: %s" % env["tcpdump"].get_status()) |
| 207 | logging.warn("Output:" + kvm_utils.format_str_for_message( |
| 208 | env["tcpdump"].get_output())) |
| 209 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 210 | # Destroy and remove VMs that are no longer needed in the environment |
| 211 | requested_vms = kvm_utils.get_sub_dict_names(params, "vms") |
| 212 | for key in env.keys(): |
| 213 | vm = env[key] |
| 214 | if not kvm_utils.is_vm(vm): |
| 215 | continue |
| 216 | if not vm.name in requested_vms: |
| 217 | logging.debug("VM '%s' found in environment but not required for" |
| 218 | " test; removing it..." % vm.name) |
| 219 | vm.destroy() |
| 220 | del env[key] |
| 221 | |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 222 | # Execute any pre_commands |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 223 | if params.get("pre_command"): |
| 224 | process_command(test, params, env, params.get("pre_command"), |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 225 | int(params.get("pre_command_timeout", "600")), |
| 226 | params.get("pre_command_noncritical") == "yes") |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 227 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 228 | # Preprocess all VMs and images |
| 229 | process(test, params, env, preprocess_image, preprocess_vm) |
| 230 | |
| 231 | # Get the KVM kernel module version and write it as a keyval |
| 232 | logging.debug("Fetching KVM module version...") |
| 233 | if os.path.exists("/dev/kvm"): |
| 234 | kvm_version = os.uname()[2] |
| 235 | try: |
| 236 | file = open("/sys/module/kvm/version", "r") |
| 237 | kvm_version = file.read().strip() |
| 238 | file.close() |
| 239 | except: |
| 240 | pass |
| 241 | else: |
| 242 | kvm_version = "Unknown" |
| 243 | logging.debug("KVM module not loaded") |
| 244 | logging.debug("KVM version: %s" % kvm_version) |
| 245 | test.write_test_keyval({"kvm_version": kvm_version}) |
| 246 | |
| 247 | # Get the KVM userspace version and write it as a keyval |
| 248 | logging.debug("Fetching KVM userspace version...") |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 249 | qemu_path = kvm_utils.get_path(test.bindir, params.get("qemu_binary", |
| 250 | "qemu")) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 251 | version_line = commands.getoutput("%s -help | head -n 1" % qemu_path) |
| 252 | exp = re.compile("[Vv]ersion .*?,") |
| 253 | match = exp.search(version_line) |
| 254 | if match: |
| 255 | kvm_userspace_version = " ".join(match.group().split()[1:]).strip(",") |
| 256 | else: |
| 257 | kvm_userspace_version = "Unknown" |
| 258 | logging.debug("Could not fetch KVM userspace version") |
| 259 | logging.debug("KVM userspace version: %s" % kvm_userspace_version) |
| 260 | test.write_test_keyval({"kvm_userspace_version": kvm_userspace_version}) |
| 261 | |
| 262 | |
| 263 | def postprocess(test, params, env): |
| 264 | """ |
| 265 | Postprocess all VMs and images according to the instructions in params. |
| 266 | |
| 267 | @param test: An Autotest test object. |
| 268 | @param params: Dict containing all VM and image parameters. |
| 269 | @param env: The environment (a dict-like object). |
| 270 | """ |
| 271 | process(test, params, env, postprocess_image, postprocess_vm) |
| 272 | |
lmr | 0ee0a9c | 2009-07-24 19:23:29 +0000 | [diff] [blame] | 273 | # Should we convert PPM files to PNG format? |
| 274 | if params.get("convert_ppm_files_to_png") == "yes": |
| 275 | logging.debug("'convert_ppm_files_to_png' specified; converting PPM" |
| 276 | " files to PNG format...") |
lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 277 | try: |
| 278 | for f in glob.glob(os.path.join(test.debugdir, "*.ppm")): |
lmr | 15c4486 | 2009-09-10 03:23:45 +0000 | [diff] [blame] | 279 | if ppm_utils.image_verify_ppm_file(f): |
| 280 | new_path = f.replace(".ppm", ".png") |
| 281 | image = PIL.Image.open(f) |
| 282 | image.save(new_path, format='PNG') |
lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 283 | except NameError: |
| 284 | pass |
lmr | 0ee0a9c | 2009-07-24 19:23:29 +0000 | [diff] [blame] | 285 | |
| 286 | # Should we keep the PPM files? |
| 287 | if params.get("keep_ppm_files") != "yes": |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 288 | logging.debug("'keep_ppm_files' not specified; removing all PPM files" |
lmr | 0ee0a9c | 2009-07-24 19:23:29 +0000 | [diff] [blame] | 289 | " from debug dir...") |
lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 290 | for f in glob.glob(os.path.join(test.debugdir, '*.ppm')): |
| 291 | os.unlink(f) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 292 | |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 293 | # Execute any post_commands |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 294 | if params.get("post_command"): |
| 295 | process_command(test, params, env, params.get("post_command"), |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 296 | int(params.get("post_command_timeout", "600")), |
| 297 | params.get("post_command_noncritical") == "yes") |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 298 | |
lmr | 588f497 | 2009-10-13 13:43:10 +0000 | [diff] [blame] | 299 | # Kill all unresponsive VMs |
| 300 | if params.get("kill_unresponsive_vms") == "yes": |
| 301 | logging.debug("'kill_unresponsive_vms' specified; killing all VMs " |
| 302 | "that fail to respond to a remote login request...") |
| 303 | for vm in kvm_utils.env_get_all_vms(env): |
| 304 | if vm.is_alive(): |
| 305 | session = vm.remote_login() |
| 306 | if session: |
| 307 | session.close() |
| 308 | else: |
| 309 | vm.destroy(gracefully=False) |
| 310 | |
lmr | a419700 | 2009-08-13 05:00:51 +0000 | [diff] [blame] | 311 | # Kill the tailing threads of all VMs |
| 312 | for vm in kvm_utils.env_get_all_vms(env): |
| 313 | vm.kill_tail_thread() |
| 314 | |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 315 | # Terminate tcpdump if no VMs are alive |
| 316 | living_vms = [vm for vm in kvm_utils.env_get_all_vms(env) if vm.is_alive()] |
| 317 | if not living_vms and env.has_key("tcpdump"): |
| 318 | env["tcpdump"].close() |
| 319 | del env["tcpdump"] |
| 320 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 321 | |
| 322 | def postprocess_on_error(test, params, env): |
| 323 | """ |
| 324 | Perform postprocessing operations required only if the test failed. |
| 325 | |
| 326 | @param test: An Autotest test object. |
| 327 | @param params: A dict containing all VM and image parameters. |
| 328 | @param env: The environment (a dict-like object). |
| 329 | """ |
| 330 | params.update(kvm_utils.get_sub_dict(params, "on_error")) |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 331 | |
| 332 | |
| 333 | def _update_address_cache(address_cache, line): |
| 334 | if re.search("Your.IP", line, re.IGNORECASE): |
| 335 | matches = re.findall(r"\d*\.\d*\.\d*\.\d*", line) |
| 336 | if matches: |
| 337 | address_cache["last_seen"] = matches[0] |
| 338 | if re.search("Client.Ethernet.Address", line, re.IGNORECASE): |
| 339 | matches = re.findall(r"\w*:\w*:\w*:\w*:\w*:\w*", line) |
| 340 | if matches and address_cache.get("last_seen"): |
| 341 | mac_address = matches[0].lower() |
| 342 | logging.debug("(address cache) Adding cache entry: %s ---> %s", |
| 343 | mac_address, address_cache.get("last_seen")) |
| 344 | address_cache[mac_address] = address_cache.get("last_seen") |
| 345 | del address_cache["last_seen"] |