blob: f60cfe8fad31d92f34a82809a156878899d4fc70 [file] [log] [blame]
lmr6f669ce2009-05-31 19:02:42 +00001import sys, os, time, commands, re, logging
2from autotest_lib.client.bin import test
3from autotest_lib.client.common_lib import error
4import kvm_vm, kvm_utils
5
6
7def 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
36def 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):
78 logging.debug("VM's qemu command differs from requested one;"
79 "restarting it...")
80 start_vm = True
81
82 if start_vm:
lmr6f669ce2009-05-31 19:02:42 +000083 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
93def 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
107def 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
136def process(test, params, env, image_func, vm_func):
137 """
138 Pre- or post-process VMs and images according to the instructions in params.
139 Call image_func for each image listed in params and vm_func for each VM.
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 image_func: A function to call for each image.
145 @param vm_func: A function to call for each VM.
146 """
147 # Get list of VMs specified for this test
148 vm_names = kvm_utils.get_sub_dict_names(params, "vms")
149 for vm_name in vm_names:
150 vm_params = kvm_utils.get_sub_dict(params, vm_name)
151 # Get list of images specified for this VM
152 image_names = kvm_utils.get_sub_dict_names(vm_params, "images")
153 for image_name in image_names:
154 image_params = kvm_utils.get_sub_dict(vm_params, image_name)
155 # Call image_func for each image
156 image_func(test, image_params)
157 # Call vm_func for each vm
158 vm_func(test, vm_params, env, vm_name)
159
160
161def preprocess(test, params, env):
162 """
163 Preprocess all VMs and images according to the instructions in params.
164 Also, collect some host information, such as the KVM version.
165
166 @param test: An Autotest test object.
167 @param params: A dict containing all VM and image parameters.
168 @param env: The environment (a dict-like object).
169 """
170 # Verify the identities of all living VMs
171 for vm in env.values():
172 if not kvm_utils.is_vm(vm):
173 continue
174 if vm.is_dead():
175 continue
176 if not vm.verify_process_identity():
177 logging.debug("VM '%s' seems to have been replaced by another"
178 " process" % vm.name)
179 vm.pid = None
180
181 # Destroy and remove VMs that are no longer needed in the environment
182 requested_vms = kvm_utils.get_sub_dict_names(params, "vms")
183 for key in env.keys():
184 vm = env[key]
185 if not kvm_utils.is_vm(vm):
186 continue
187 if not vm.name in requested_vms:
188 logging.debug("VM '%s' found in environment but not required for"
189 " test; removing it..." % vm.name)
190 vm.destroy()
191 del env[key]
192
193 # Preprocess all VMs and images
194 process(test, params, env, preprocess_image, preprocess_vm)
195
196 # Get the KVM kernel module version and write it as a keyval
197 logging.debug("Fetching KVM module version...")
198 if os.path.exists("/dev/kvm"):
199 kvm_version = os.uname()[2]
200 try:
201 file = open("/sys/module/kvm/version", "r")
202 kvm_version = file.read().strip()
203 file.close()
204 except:
205 pass
206 else:
207 kvm_version = "Unknown"
208 logging.debug("KVM module not loaded")
209 logging.debug("KVM version: %s" % kvm_version)
210 test.write_test_keyval({"kvm_version": kvm_version})
211
212 # Get the KVM userspace version and write it as a keyval
213 logging.debug("Fetching KVM userspace version...")
214 qemu_path = os.path.join(test.bindir, "qemu")
215 version_line = commands.getoutput("%s -help | head -n 1" % qemu_path)
216 exp = re.compile("[Vv]ersion .*?,")
217 match = exp.search(version_line)
218 if match:
219 kvm_userspace_version = " ".join(match.group().split()[1:]).strip(",")
220 else:
221 kvm_userspace_version = "Unknown"
222 logging.debug("Could not fetch KVM userspace version")
223 logging.debug("KVM userspace version: %s" % kvm_userspace_version)
224 test.write_test_keyval({"kvm_userspace_version": kvm_userspace_version})
225
226
227def postprocess(test, params, env):
228 """
229 Postprocess all VMs and images according to the instructions in params.
230
231 @param test: An Autotest test object.
232 @param params: Dict containing all VM and image parameters.
233 @param env: The environment (a dict-like object).
234 """
235 process(test, params, env, postprocess_image, postprocess_vm)
236
237 # See if we should get rid of all PPM files
238 if not params.get("keep_ppm_files") == "yes":
239 # Remove them all
240 logging.debug("'keep_ppm_files' not specified; removing all PPM files"
241 " from results dir...")
242 kvm_utils.run_bg("rm -vf %s" % os.path.join(test.debugdir, "*.ppm"),
243 None, logging.debug, "(rm) ", timeout=5.0)
244
245
246def postprocess_on_error(test, params, env):
247 """
248 Perform postprocessing operations required only if the test failed.
249
250 @param test: An Autotest test object.
251 @param params: A dict containing all VM and image parameters.
252 @param env: The environment (a dict-like object).
253 """
254 params.update(kvm_utils.get_sub_dict(params, "on_error"))