lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 1 | import sys, os, time, commands, re, logging, signal |
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 | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame^] | 4 | import kvm_vm, kvm_utils, kvm_subprocess |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 5 | |
| 6 | |
| 7 | def preprocess_image(test, params): |
| 8 | """ |
| 9 | Preprocess a single QEMU image according to the instructions in params. |
| 10 | |
| 11 | @param test: Autotest test object. |
| 12 | @param params: A dict containing image preprocessing parameters. |
| 13 | @note: Currently this function just creates an image if requested. |
| 14 | """ |
| 15 | qemu_img_path = os.path.join(test.bindir, "qemu-img") |
| 16 | image_dir = os.path.join(test.bindir, "images") |
| 17 | image_filename = kvm_vm.get_image_filename(params, image_dir) |
| 18 | |
| 19 | create_image = False |
| 20 | |
| 21 | if params.get("force_create_image") == "yes": |
| 22 | logging.debug("'force_create_image' specified; creating image...") |
| 23 | create_image = True |
| 24 | elif params.get("create_image") == "yes" and not \ |
| 25 | os.path.exists(image_filename): |
| 26 | logging.debug("Creating image...") |
| 27 | create_image = True |
| 28 | |
| 29 | if create_image: |
| 30 | if not kvm_vm.create_image(params, qemu_img_path, image_dir): |
| 31 | message = "Could not create image" |
| 32 | logging.error(message) |
| 33 | raise error.TestError(message) |
| 34 | |
| 35 | |
| 36 | def preprocess_vm(test, params, env, name): |
| 37 | """ |
| 38 | Preprocess a single VM object according to the instructions in params. |
| 39 | Start the VM if requested and get a screendump. |
| 40 | |
| 41 | @param test: An Autotest test object. |
| 42 | @param params: A dict containing VM preprocessing parameters. |
| 43 | @param env: The environment (a dict-like object). |
| 44 | @param name: The name of the VM object. |
| 45 | """ |
| 46 | qemu_path = os.path.join(test.bindir, "qemu") |
| 47 | image_dir = os.path.join(test.bindir, "images") |
| 48 | iso_dir = os.path.join(test.bindir, "isos") |
| 49 | |
| 50 | logging.debug("Preprocessing VM '%s'..." % name) |
| 51 | vm = kvm_utils.env_get_vm(env, name) |
| 52 | if vm: |
| 53 | logging.debug("VM object found in environment") |
| 54 | else: |
| 55 | logging.debug("VM object does not exist; creating it") |
| 56 | vm = kvm_vm.VM(name, params, qemu_path, image_dir, iso_dir) |
| 57 | kvm_utils.env_register_vm(env, name, vm) |
| 58 | |
| 59 | start_vm = False |
| 60 | for_migration = False |
| 61 | |
| 62 | if params.get("start_vm_for_migration") == "yes": |
| 63 | logging.debug("'start_vm_for_migration' specified; (re)starting VM with" |
| 64 | " -incoming option...") |
| 65 | start_vm = True |
| 66 | for_migration = True |
| 67 | elif params.get("restart_vm") == "yes": |
| 68 | logging.debug("'restart_vm' specified; (re)starting VM...") |
| 69 | start_vm = True |
| 70 | elif params.get("start_vm") == "yes": |
| 71 | if not vm.is_alive(): |
| 72 | logging.debug("VM is not alive; starting it...") |
| 73 | start_vm = True |
| 74 | elif vm.make_qemu_command() != vm.make_qemu_command(name, params, |
| 75 | qemu_path, |
| 76 | image_dir, |
| 77 | iso_dir): |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame^] | 78 | logging.debug("VM's qemu command differs from requested one; " |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 79 | "restarting it...") |
| 80 | start_vm = True |
| 81 | |
| 82 | if start_vm: |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 83 | if not vm.create(name, params, qemu_path, image_dir, iso_dir, |
| 84 | for_migration): |
| 85 | message = "Could not start VM" |
| 86 | logging.error(message) |
| 87 | raise error.TestError(message) |
| 88 | |
| 89 | scrdump_filename = os.path.join(test.debugdir, "pre_%s.ppm" % name) |
| 90 | vm.send_monitor_cmd("screendump %s" % scrdump_filename) |
| 91 | |
| 92 | |
| 93 | def postprocess_image(test, params): |
| 94 | """ |
| 95 | Postprocess a single QEMU image according to the instructions in params. |
| 96 | Currently this function just removes an image if requested. |
| 97 | |
| 98 | @param test: An Autotest test object. |
| 99 | @param params: A dict containing image postprocessing parameters. |
| 100 | """ |
| 101 | image_dir = os.path.join(test.bindir, "images") |
| 102 | |
| 103 | if params.get("remove_image") == "yes": |
| 104 | kvm_vm.remove_image(params, image_dir) |
| 105 | |
| 106 | |
| 107 | def postprocess_vm(test, params, env, name): |
| 108 | """ |
| 109 | Postprocess a single VM object according to the instructions in params. |
| 110 | Kill the VM if requested and get a screendump. |
| 111 | |
| 112 | @param test: An Autotest test object. |
| 113 | @param params: A dict containing VM postprocessing parameters. |
| 114 | @param env: The environment (a dict-like object). |
| 115 | @param name: The name of the VM object. |
| 116 | """ |
| 117 | logging.debug("Postprocessing VM '%s'..." % name) |
| 118 | vm = kvm_utils.env_get_vm(env, name) |
| 119 | if vm: |
| 120 | logging.debug("VM object found in environment") |
| 121 | else: |
| 122 | logging.debug("VM object does not exist in environment") |
| 123 | return |
| 124 | |
| 125 | scrdump_filename = os.path.join(test.debugdir, "post_%s.ppm" % name) |
| 126 | vm.send_monitor_cmd("screendump %s" % scrdump_filename) |
| 127 | |
| 128 | if params.get("kill_vm") == "yes": |
| 129 | if not kvm_utils.wait_for(vm.is_dead, |
| 130 | float(params.get("kill_vm_timeout", 0)), 0.0, 1.0, |
| 131 | "Waiting for VM to kill itself..."): |
| 132 | logging.debug("'kill_vm' specified; killing VM...") |
| 133 | vm.destroy(gracefully = params.get("kill_vm_gracefully") == "yes") |
| 134 | |
| 135 | |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 136 | def process_command(test, params, env, command, command_timeout, |
| 137 | command_noncritical): |
| 138 | """ |
| 139 | Pre- or post- custom commands to be executed before/after a test is run |
| 140 | |
| 141 | @param test: An Autotest test object. |
| 142 | @param params: A dict containing all VM and image parameters. |
| 143 | @param env: The environment (a dict-like object). |
| 144 | @param command: Script containing the command to be run. |
| 145 | @param commmand_timeout: Timeout for command execution. |
| 146 | @param command_noncritical: if 'yes' test will not fail if command fails. |
| 147 | """ |
| 148 | if command_timeout is None: |
| 149 | command_timeout = "600" |
| 150 | |
| 151 | if command_noncritical is None: |
| 152 | command_noncritical = "no" |
| 153 | |
| 154 | # export environment vars |
| 155 | for k in params.keys(): |
| 156 | logging.info("Adding KVM_TEST_%s to Environment" % (k)) |
| 157 | os.putenv("KVM_TEST_%s" % (k), str(params[k])) |
| 158 | # execute command |
| 159 | logging.info("Executing command '%s'..." % command) |
| 160 | timeout = int(command_timeout) |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame^] | 161 | (status, output) = kvm_subprocess.run_fg("cd %s; %s" % (test.bindir, |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 162 | command), |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame^] | 163 | logging.debug, "(command) ", |
| 164 | timeout=timeout) |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 165 | if status != 0: |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 166 | logging.warn("Custom processing command failed: '%s'..." % command) |
| 167 | if command_noncritical != "yes": |
| 168 | raise error.TestError("Custom processing command failed") |
| 169 | |
| 170 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 171 | def process(test, params, env, image_func, vm_func): |
| 172 | """ |
| 173 | Pre- or post-process VMs and images according to the instructions in params. |
| 174 | Call image_func for each image listed in params and vm_func for each VM. |
| 175 | |
| 176 | @param test: An Autotest test object. |
| 177 | @param params: A dict containing all VM and image parameters. |
| 178 | @param env: The environment (a dict-like object). |
| 179 | @param image_func: A function to call for each image. |
| 180 | @param vm_func: A function to call for each VM. |
| 181 | """ |
| 182 | # Get list of VMs specified for this test |
| 183 | vm_names = kvm_utils.get_sub_dict_names(params, "vms") |
| 184 | for vm_name in vm_names: |
| 185 | vm_params = kvm_utils.get_sub_dict(params, vm_name) |
| 186 | # Get list of images specified for this VM |
| 187 | image_names = kvm_utils.get_sub_dict_names(vm_params, "images") |
| 188 | for image_name in image_names: |
| 189 | image_params = kvm_utils.get_sub_dict(vm_params, image_name) |
| 190 | # Call image_func for each image |
| 191 | image_func(test, image_params) |
| 192 | # Call vm_func for each vm |
| 193 | vm_func(test, vm_params, env, vm_name) |
| 194 | |
| 195 | |
| 196 | def preprocess(test, params, env): |
| 197 | """ |
| 198 | Preprocess all VMs and images according to the instructions in params. |
| 199 | Also, collect some host information, such as the KVM version. |
| 200 | |
| 201 | @param test: An Autotest test object. |
| 202 | @param params: A dict containing all VM and image parameters. |
| 203 | @param env: The environment (a dict-like object). |
| 204 | """ |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 205 | # Destroy and remove VMs that are no longer needed in the environment |
| 206 | requested_vms = kvm_utils.get_sub_dict_names(params, "vms") |
| 207 | for key in env.keys(): |
| 208 | vm = env[key] |
| 209 | if not kvm_utils.is_vm(vm): |
| 210 | continue |
| 211 | if not vm.name in requested_vms: |
| 212 | logging.debug("VM '%s' found in environment but not required for" |
| 213 | " test; removing it..." % vm.name) |
| 214 | vm.destroy() |
| 215 | del env[key] |
| 216 | |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 217 | #execute any pre_commands |
| 218 | if params.get("pre_command"): |
| 219 | process_command(test, params, env, params.get("pre_command"), |
| 220 | params.get("pre_command_timeout"), |
| 221 | params.get("pre_command_noncritical")) |
| 222 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 223 | # Preprocess all VMs and images |
| 224 | process(test, params, env, preprocess_image, preprocess_vm) |
| 225 | |
| 226 | # Get the KVM kernel module version and write it as a keyval |
| 227 | logging.debug("Fetching KVM module version...") |
| 228 | if os.path.exists("/dev/kvm"): |
| 229 | kvm_version = os.uname()[2] |
| 230 | try: |
| 231 | file = open("/sys/module/kvm/version", "r") |
| 232 | kvm_version = file.read().strip() |
| 233 | file.close() |
| 234 | except: |
| 235 | pass |
| 236 | else: |
| 237 | kvm_version = "Unknown" |
| 238 | logging.debug("KVM module not loaded") |
| 239 | logging.debug("KVM version: %s" % kvm_version) |
| 240 | test.write_test_keyval({"kvm_version": kvm_version}) |
| 241 | |
| 242 | # Get the KVM userspace version and write it as a keyval |
| 243 | logging.debug("Fetching KVM userspace version...") |
| 244 | qemu_path = os.path.join(test.bindir, "qemu") |
| 245 | version_line = commands.getoutput("%s -help | head -n 1" % qemu_path) |
| 246 | exp = re.compile("[Vv]ersion .*?,") |
| 247 | match = exp.search(version_line) |
| 248 | if match: |
| 249 | kvm_userspace_version = " ".join(match.group().split()[1:]).strip(",") |
| 250 | else: |
| 251 | kvm_userspace_version = "Unknown" |
| 252 | logging.debug("Could not fetch KVM userspace version") |
| 253 | logging.debug("KVM userspace version: %s" % kvm_userspace_version) |
| 254 | test.write_test_keyval({"kvm_userspace_version": kvm_userspace_version}) |
| 255 | |
| 256 | |
| 257 | def postprocess(test, params, env): |
| 258 | """ |
| 259 | Postprocess all VMs and images according to the instructions in params. |
| 260 | |
| 261 | @param test: An Autotest test object. |
| 262 | @param params: Dict containing all VM and image parameters. |
| 263 | @param env: The environment (a dict-like object). |
| 264 | """ |
| 265 | process(test, params, env, postprocess_image, postprocess_vm) |
| 266 | |
| 267 | # See if we should get rid of all PPM files |
| 268 | if not params.get("keep_ppm_files") == "yes": |
| 269 | # Remove them all |
| 270 | logging.debug("'keep_ppm_files' not specified; removing all PPM files" |
| 271 | " from results dir...") |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame^] | 272 | rm_cmd = "rm -vf %s" % os.path.join(test.debugdir, "*.ppm") |
| 273 | kvm_subprocess.run_fg(rm_cmd, logging.debug, "(rm) ", timeout=5.0) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 274 | |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 275 | #execute any post_commands |
| 276 | if params.get("post_command"): |
| 277 | process_command(test, params, env, params.get("post_command"), |
| 278 | params.get("post_command_timeout"), |
| 279 | params.get("post_command_noncritical")) |
| 280 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 281 | |
| 282 | def postprocess_on_error(test, params, env): |
| 283 | """ |
| 284 | Perform postprocessing operations required only if the test failed. |
| 285 | |
| 286 | @param test: An Autotest test object. |
| 287 | @param params: A dict containing all VM and image parameters. |
| 288 | @param env: The environment (a dict-like object). |
| 289 | """ |
| 290 | params.update(kvm_utils.get_sub_dict(params, "on_error")) |