blob: 359f81d655fd5e33fed7549a5be2ca29645d3473 [file] [log] [blame]
lmre9f528e2009-08-12 15:18:10 +00001import sys, os, time, commands, re, logging, signal, glob
lmr6f669ce2009-05-31 19:02:42 +00002from autotest_lib.client.bin import test
3from autotest_lib.client.common_lib import error
lmra4967622009-07-23 01:36:32 +00004import kvm_vm, kvm_utils, kvm_subprocess
lmre9f528e2009-08-12 15:18:10 +00005try:
6 import PIL.Image
7except 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.')
lmr6f669ce2009-05-31 19:02:42 +000012
13
14def 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 """
22 qemu_img_path = os.path.join(test.bindir, "qemu-img")
23 image_dir = os.path.join(test.bindir, "images")
24 image_filename = kvm_vm.get_image_filename(params, image_dir)
25
26 create_image = False
27
28 if params.get("force_create_image") == "yes":
29 logging.debug("'force_create_image' specified; creating image...")
30 create_image = True
31 elif params.get("create_image") == "yes" and not \
32 os.path.exists(image_filename):
33 logging.debug("Creating image...")
34 create_image = True
35
36 if create_image:
37 if not kvm_vm.create_image(params, qemu_img_path, image_dir):
38 message = "Could not create image"
39 logging.error(message)
40 raise error.TestError(message)
41
42
43def preprocess_vm(test, params, env, name):
44 """
45 Preprocess a single VM object according to the instructions in params.
46 Start the VM if requested and get a screendump.
47
48 @param test: An Autotest test object.
49 @param params: A dict containing VM preprocessing parameters.
50 @param env: The environment (a dict-like object).
51 @param name: The name of the VM object.
52 """
53 qemu_path = os.path.join(test.bindir, "qemu")
54 image_dir = os.path.join(test.bindir, "images")
55 iso_dir = os.path.join(test.bindir, "isos")
lmrf4696342009-08-13 04:06:33 +000056 script_dir = os.path.join(test.bindir, "scripts")
lmr6f669ce2009-05-31 19:02:42 +000057
58 logging.debug("Preprocessing VM '%s'..." % name)
59 vm = kvm_utils.env_get_vm(env, name)
60 if vm:
61 logging.debug("VM object found in environment")
62 else:
63 logging.debug("VM object does not exist; creating it")
lmrf4696342009-08-13 04:06:33 +000064 vm = kvm_vm.VM(name, params, qemu_path, image_dir, iso_dir, script_dir)
lmr6f669ce2009-05-31 19:02:42 +000065 kvm_utils.env_register_vm(env, name, vm)
66
67 start_vm = False
68 for_migration = False
69
70 if params.get("start_vm_for_migration") == "yes":
71 logging.debug("'start_vm_for_migration' specified; (re)starting VM with"
72 " -incoming option...")
73 start_vm = True
74 for_migration = True
75 elif params.get("restart_vm") == "yes":
76 logging.debug("'restart_vm' specified; (re)starting VM...")
77 start_vm = True
78 elif params.get("start_vm") == "yes":
79 if not vm.is_alive():
80 logging.debug("VM is not alive; starting it...")
81 start_vm = True
82 elif vm.make_qemu_command() != vm.make_qemu_command(name, params,
83 qemu_path,
84 image_dir,
lmrf4696342009-08-13 04:06:33 +000085 iso_dir,
86 script_dir):
lmra4967622009-07-23 01:36:32 +000087 logging.debug("VM's qemu command differs from requested one; "
lmr6f669ce2009-05-31 19:02:42 +000088 "restarting it...")
89 start_vm = True
90
91 if start_vm:
lmr6f669ce2009-05-31 19:02:42 +000092 if not vm.create(name, params, qemu_path, image_dir, iso_dir,
lmrf4696342009-08-13 04:06:33 +000093 script_dir, for_migration):
lmr6f669ce2009-05-31 19:02:42 +000094 message = "Could not start VM"
95 logging.error(message)
96 raise error.TestError(message)
97
98 scrdump_filename = os.path.join(test.debugdir, "pre_%s.ppm" % name)
99 vm.send_monitor_cmd("screendump %s" % scrdump_filename)
100
101
102def postprocess_image(test, params):
103 """
104 Postprocess a single QEMU image according to the instructions in params.
105 Currently this function just removes an image if requested.
106
107 @param test: An Autotest test object.
108 @param params: A dict containing image postprocessing parameters.
109 """
110 image_dir = os.path.join(test.bindir, "images")
111
112 if params.get("remove_image") == "yes":
113 kvm_vm.remove_image(params, image_dir)
114
115
116def postprocess_vm(test, params, env, name):
117 """
118 Postprocess a single VM object according to the instructions in params.
119 Kill the VM if requested and get a screendump.
120
121 @param test: An Autotest test object.
122 @param params: A dict containing VM postprocessing parameters.
123 @param env: The environment (a dict-like object).
124 @param name: The name of the VM object.
125 """
126 logging.debug("Postprocessing VM '%s'..." % name)
127 vm = kvm_utils.env_get_vm(env, name)
128 if vm:
129 logging.debug("VM object found in environment")
130 else:
131 logging.debug("VM object does not exist in environment")
132 return
133
134 scrdump_filename = os.path.join(test.debugdir, "post_%s.ppm" % name)
135 vm.send_monitor_cmd("screendump %s" % scrdump_filename)
136
137 if params.get("kill_vm") == "yes":
138 if not kvm_utils.wait_for(vm.is_dead,
139 float(params.get("kill_vm_timeout", 0)), 0.0, 1.0,
140 "Waiting for VM to kill itself..."):
141 logging.debug("'kill_vm' specified; killing VM...")
142 vm.destroy(gracefully = params.get("kill_vm_gracefully") == "yes")
143
144
lmr86d1ea52009-06-15 20:34:39 +0000145def process_command(test, params, env, command, command_timeout,
146 command_noncritical):
147 """
148 Pre- or post- custom commands to be executed before/after a test is run
149
150 @param test: An Autotest test object.
151 @param params: A dict containing all VM and image parameters.
152 @param env: The environment (a dict-like object).
lmr0bc6efc2009-07-27 13:27:42 +0000153 @param command: Command to be run.
154 @param command_timeout: Timeout for command execution.
155 @param command_noncritical: If True test will not fail if command fails.
lmr86d1ea52009-06-15 20:34:39 +0000156 """
lmr0bc6efc2009-07-27 13:27:42 +0000157 # Export environment vars
lmr86d1ea52009-06-15 20:34:39 +0000158 for k in params.keys():
lmr0bc6efc2009-07-27 13:27:42 +0000159 os.putenv("KVM_TEST_%s" % k, str(params[k]))
160 # Execute command
lmr86d1ea52009-06-15 20:34:39 +0000161 logging.info("Executing command '%s'..." % command)
lmra4967622009-07-23 01:36:32 +0000162 (status, output) = kvm_subprocess.run_fg("cd %s; %s" % (test.bindir,
lmr86d1ea52009-06-15 20:34:39 +0000163 command),
lmra4967622009-07-23 01:36:32 +0000164 logging.debug, "(command) ",
lmr0bc6efc2009-07-27 13:27:42 +0000165 timeout=command_timeout)
lmr86d1ea52009-06-15 20:34:39 +0000166 if status != 0:
lmr0bc6efc2009-07-27 13:27:42 +0000167 logging.warn("Custom processing command failed: '%s'" % command)
168 if not command_noncritical:
lmr86d1ea52009-06-15 20:34:39 +0000169 raise error.TestError("Custom processing command failed")
170
171
lmr6f669ce2009-05-31 19:02:42 +0000172def process(test, params, env, image_func, vm_func):
173 """
174 Pre- or post-process VMs and images according to the instructions in params.
175 Call image_func for each image listed in params and vm_func for each VM.
176
177 @param test: An Autotest test object.
178 @param params: A dict containing all VM and image parameters.
179 @param env: The environment (a dict-like object).
180 @param image_func: A function to call for each image.
181 @param vm_func: A function to call for each VM.
182 """
183 # Get list of VMs specified for this test
184 vm_names = kvm_utils.get_sub_dict_names(params, "vms")
185 for vm_name in vm_names:
186 vm_params = kvm_utils.get_sub_dict(params, vm_name)
187 # Get list of images specified for this VM
188 image_names = kvm_utils.get_sub_dict_names(vm_params, "images")
189 for image_name in image_names:
190 image_params = kvm_utils.get_sub_dict(vm_params, image_name)
191 # Call image_func for each image
192 image_func(test, image_params)
193 # Call vm_func for each vm
194 vm_func(test, vm_params, env, vm_name)
195
196
197def preprocess(test, params, env):
198 """
199 Preprocess all VMs and images according to the instructions in params.
200 Also, collect some host information, such as the KVM version.
201
202 @param test: An Autotest test object.
203 @param params: A dict containing all VM and image parameters.
204 @param env: The environment (a dict-like object).
205 """
lmr6f669ce2009-05-31 19:02:42 +0000206 # Destroy and remove VMs that are no longer needed in the environment
207 requested_vms = kvm_utils.get_sub_dict_names(params, "vms")
208 for key in env.keys():
209 vm = env[key]
210 if not kvm_utils.is_vm(vm):
211 continue
212 if not vm.name in requested_vms:
213 logging.debug("VM '%s' found in environment but not required for"
214 " test; removing it..." % vm.name)
215 vm.destroy()
216 del env[key]
217
lmr0bc6efc2009-07-27 13:27:42 +0000218 # Execute any pre_commands
lmr86d1ea52009-06-15 20:34:39 +0000219 if params.get("pre_command"):
220 process_command(test, params, env, params.get("pre_command"),
lmr0bc6efc2009-07-27 13:27:42 +0000221 int(params.get("pre_command_timeout", "600")),
222 params.get("pre_command_noncritical") == "yes")
lmr86d1ea52009-06-15 20:34:39 +0000223
lmr6f669ce2009-05-31 19:02:42 +0000224 # Preprocess all VMs and images
225 process(test, params, env, preprocess_image, preprocess_vm)
226
227 # Get the KVM kernel module version and write it as a keyval
228 logging.debug("Fetching KVM module version...")
229 if os.path.exists("/dev/kvm"):
230 kvm_version = os.uname()[2]
231 try:
232 file = open("/sys/module/kvm/version", "r")
233 kvm_version = file.read().strip()
234 file.close()
235 except:
236 pass
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...")
245 qemu_path = os.path.join(test.bindir, "qemu")
246 version_line = commands.getoutput("%s -help | head -n 1" % qemu_path)
247 exp = re.compile("[Vv]ersion .*?,")
248 match = exp.search(version_line)
249 if match:
250 kvm_userspace_version = " ".join(match.group().split()[1:]).strip(",")
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
257
258def postprocess(test, params, env):
259 """
260 Postprocess all VMs and images according to the instructions in params.
261
262 @param test: An Autotest test object.
263 @param params: Dict containing all VM and image parameters.
264 @param env: The environment (a dict-like object).
265 """
266 process(test, params, env, postprocess_image, postprocess_vm)
267
lmr0ee0a9c2009-07-24 19:23:29 +0000268 # Should we convert PPM files to PNG format?
269 if params.get("convert_ppm_files_to_png") == "yes":
270 logging.debug("'convert_ppm_files_to_png' specified; converting PPM"
271 " files to PNG format...")
lmre9f528e2009-08-12 15:18:10 +0000272 try:
273 for f in glob.glob(os.path.join(test.debugdir, "*.ppm")):
274 image = PIL.Image.open(f)
275 image.save(history_scrdump_filename, format = 'PNG')
276 except NameError:
277 pass
lmr0ee0a9c2009-07-24 19:23:29 +0000278
279 # Should we keep the PPM files?
280 if params.get("keep_ppm_files") != "yes":
lmr6f669ce2009-05-31 19:02:42 +0000281 logging.debug("'keep_ppm_files' not specified; removing all PPM files"
lmr0ee0a9c2009-07-24 19:23:29 +0000282 " from debug dir...")
lmre9f528e2009-08-12 15:18:10 +0000283 for f in glob.glob(os.path.join(test.debugdir, '*.ppm')):
284 os.unlink(f)
lmr6f669ce2009-05-31 19:02:42 +0000285
lmr0bc6efc2009-07-27 13:27:42 +0000286 # Execute any post_commands
lmr86d1ea52009-06-15 20:34:39 +0000287 if params.get("post_command"):
288 process_command(test, params, env, params.get("post_command"),
lmr0bc6efc2009-07-27 13:27:42 +0000289 int(params.get("post_command_timeout", "600")),
290 params.get("post_command_noncritical") == "yes")
lmr86d1ea52009-06-15 20:34:39 +0000291
lmr6f669ce2009-05-31 19:02:42 +0000292
293def postprocess_on_error(test, params, env):
294 """
295 Perform postprocessing operations required only if the test failed.
296
297 @param test: An Autotest test object.
298 @param params: A dict containing all VM and image parameters.
299 @param env: The environment (a dict-like object).
300 """
301 params.update(kvm_utils.get_sub_dict(params, "on_error"))