lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 2 | """ |
| 3 | Utility classes and functions to handle Virtual Machine creation using qemu. |
| 4 | |
| 5 | @copyright: 2008-2009 Red Hat Inc. |
| 6 | """ |
| 7 | |
lmr | b635b86 | 2009-09-10 14:53:21 +0000 | [diff] [blame] | 8 | import time, socket, os, logging, fcntl, re, commands |
| 9 | import kvm_utils, kvm_subprocess |
| 10 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 11 | |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 12 | def get_image_filename(params, root_dir): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 13 | """ |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 14 | Generate an image path from params and root_dir. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 15 | |
| 16 | @param params: Dictionary containing the test parameters. |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 17 | @param root_dir: Base directory for relative filenames. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 18 | |
| 19 | @note: params should contain: |
| 20 | image_name -- the name of the image file, without extension |
| 21 | image_format -- the format of the image (qcow2, raw etc) |
| 22 | """ |
| 23 | image_name = params.get("image_name", "image") |
| 24 | image_format = params.get("image_format", "qcow2") |
| 25 | image_filename = "%s.%s" % (image_name, image_format) |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 26 | image_filename = kvm_utils.get_path(root_dir, image_filename) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 27 | return image_filename |
| 28 | |
| 29 | |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 30 | def create_image(params, root_dir): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 31 | """ |
| 32 | Create an image using qemu_image. |
| 33 | |
| 34 | @param params: Dictionary containing the test parameters. |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 35 | @param root_dir: Base directory for relative filenames. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 36 | |
| 37 | @note: params should contain: |
| 38 | image_name -- the name of the image file, without extension |
| 39 | image_format -- the format of the image (qcow2, raw etc) |
| 40 | image_size -- the requested size of the image (a string |
| 41 | qemu-img can understand, such as '10G') |
| 42 | """ |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 43 | qemu_img_cmd = kvm_utils.get_path(root_dir, params.get("qemu_img_binary", |
| 44 | "qemu-img")) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 45 | qemu_img_cmd += " create" |
| 46 | |
| 47 | format = params.get("image_format", "qcow2") |
| 48 | qemu_img_cmd += " -f %s" % format |
| 49 | |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 50 | image_filename = get_image_filename(params, root_dir) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 51 | qemu_img_cmd += " %s" % image_filename |
| 52 | |
| 53 | size = params.get("image_size", "10G") |
| 54 | qemu_img_cmd += " %s" % size |
| 55 | |
| 56 | logging.debug("Running qemu-img command:\n%s" % qemu_img_cmd) |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 57 | (status, output) = kvm_subprocess.run_fg(qemu_img_cmd, logging.debug, |
lmr | 1e36c52 | 2009-10-05 19:15:03 +0000 | [diff] [blame] | 58 | "(qemu-img) ", timeout=120) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 59 | |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 60 | if status is None: |
| 61 | logging.error("Timeout elapsed while waiting for qemu-img command " |
| 62 | "to complete:\n%s" % qemu_img_cmd) |
| 63 | return None |
| 64 | elif status != 0: |
| 65 | logging.error("Could not create image; " |
| 66 | "qemu-img command failed:\n%s" % qemu_img_cmd) |
| 67 | logging.error("Status: %s" % status) |
| 68 | logging.error("Output:" + kvm_utils.format_str_for_message(output)) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 69 | return None |
| 70 | if not os.path.exists(image_filename): |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 71 | logging.error("Image could not be created for some reason; " |
| 72 | "qemu-img command:\n%s" % qemu_img_cmd) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 73 | return None |
| 74 | |
| 75 | logging.info("Image created in %s" % image_filename) |
| 76 | return image_filename |
| 77 | |
| 78 | |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 79 | def remove_image(params, root_dir): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 80 | """ |
| 81 | Remove an image file. |
| 82 | |
| 83 | @param params: A dict |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 84 | @param root_dir: Base directory for relative filenames. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 85 | |
| 86 | @note: params should contain: |
| 87 | image_name -- the name of the image file, without extension |
| 88 | image_format -- the format of the image (qcow2, raw etc) |
| 89 | """ |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 90 | image_filename = get_image_filename(params, root_dir) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 91 | logging.debug("Removing image file %s..." % image_filename) |
| 92 | if os.path.exists(image_filename): |
| 93 | os.unlink(image_filename) |
| 94 | else: |
| 95 | logging.debug("Image file %s not found") |
| 96 | |
| 97 | |
| 98 | class VM: |
| 99 | """ |
| 100 | This class handles all basic VM operations. |
| 101 | """ |
| 102 | |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 103 | def __init__(self, name, params, root_dir, address_cache): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 104 | """ |
| 105 | Initialize the object and set a few attributes. |
| 106 | |
| 107 | @param name: The name of the object |
| 108 | @param params: A dict containing VM params |
| 109 | (see method make_qemu_command for a full description) |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 110 | @param root_dir: Base directory for relative filenames |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 111 | @param address_cache: A dict that maps MAC addresses to IP addresses |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 112 | """ |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 113 | self.process = None |
lmr | 953ffba | 2009-07-27 13:20:10 +0000 | [diff] [blame] | 114 | self.redirs = {} |
| 115 | self.vnc_port = 5900 |
lmr | a253322 | 2009-07-20 12:43:46 +0000 | [diff] [blame] | 116 | self.uuid = None |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 117 | |
| 118 | self.name = name |
| 119 | self.params = params |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 120 | self.root_dir = root_dir |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 121 | self.address_cache = address_cache |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 122 | |
lmr | 8b134f9 | 2009-06-08 14:47:31 +0000 | [diff] [blame] | 123 | # Find available monitor filename |
| 124 | while True: |
| 125 | # The monitor filename should be unique |
lmr | d16a67d | 2009-06-10 19:52:59 +0000 | [diff] [blame] | 126 | self.instance = (time.strftime("%Y%m%d-%H%M%S-") + |
| 127 | kvm_utils.generate_random_string(4)) |
lmr | 8b134f9 | 2009-06-08 14:47:31 +0000 | [diff] [blame] | 128 | self.monitor_file_name = os.path.join("/tmp", |
| 129 | "monitor-" + self.instance) |
| 130 | if not os.path.exists(self.monitor_file_name): |
| 131 | break |
| 132 | |
| 133 | |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 134 | def clone(self, name=None, params=None, root_dir=None, address_cache=None): |
lmr | 2c24117 | 2009-06-08 15:11:29 +0000 | [diff] [blame] | 135 | """ |
| 136 | Return a clone of the VM object with optionally modified parameters. |
| 137 | The clone is initially not alive and needs to be started using create(). |
| 138 | Any parameters not passed to this function are copied from the source |
| 139 | VM. |
| 140 | |
| 141 | @param name: Optional new VM name |
| 142 | @param params: Optional new VM creation parameters |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 143 | @param root_dir: Optional new base directory for relative filenames |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 144 | @param address_cache: A dict that maps MAC addresses to IP addresses |
lmr | 2c24117 | 2009-06-08 15:11:29 +0000 | [diff] [blame] | 145 | """ |
| 146 | if name == None: |
| 147 | name = self.name |
| 148 | if params == None: |
| 149 | params = self.params.copy() |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 150 | if root_dir == None: |
| 151 | root_dir = self.root_dir |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 152 | if address_cache == None: |
| 153 | address_cache = self.address_cache |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 154 | return VM(name, params, root_dir, address_cache) |
lmr | 2c24117 | 2009-06-08 15:11:29 +0000 | [diff] [blame] | 155 | |
| 156 | |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 157 | def make_qemu_command(self, name=None, params=None, root_dir=None): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 158 | """ |
| 159 | Generate a qemu command line. All parameters are optional. If a |
| 160 | parameter is not supplied, the corresponding value stored in the |
| 161 | class attributes is used. |
| 162 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 163 | @param name: The name of the object |
| 164 | @param params: A dict containing VM params |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 165 | @param root_dir: Base directory for relative filenames |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 166 | |
| 167 | @note: The params dict should contain: |
| 168 | mem -- memory size in MBs |
| 169 | cdrom -- ISO filename to use with the qemu -cdrom parameter |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 170 | extra_params -- a string to append to the qemu command |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 171 | shell_port -- port of the remote shell daemon on the guest |
| 172 | (SSH, Telnet or the home-made Remote Shell Server) |
| 173 | shell_client -- client program to use for connecting to the |
| 174 | remote shell daemon on the guest (ssh, telnet or nc) |
lmr | eeff0eb | 2009-06-10 19:19:15 +0000 | [diff] [blame] | 175 | x11_display -- if specified, the DISPLAY environment variable |
| 176 | will be be set to this value for the qemu process (useful for |
| 177 | SDL rendering) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 178 | images -- a list of image object names, separated by spaces |
| 179 | nics -- a list of NIC object names, separated by spaces |
| 180 | |
| 181 | For each image in images: |
| 182 | drive_format -- string to pass as 'if' parameter for this |
| 183 | image (e.g. ide, scsi) |
| 184 | image_snapshot -- if yes, pass 'snapshot=on' to qemu for |
| 185 | this image |
| 186 | image_boot -- if yes, pass 'boot=on' to qemu for this image |
| 187 | In addition, all parameters required by get_image_filename. |
| 188 | |
| 189 | For each NIC in nics: |
| 190 | nic_model -- string to pass as 'model' parameter for this |
| 191 | NIC (e.g. e1000) |
| 192 | """ |
| 193 | if name == None: |
| 194 | name = self.name |
| 195 | if params == None: |
| 196 | params = self.params |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 197 | if root_dir == None: |
| 198 | root_dir = self.root_dir |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 199 | |
lmr | eeff0eb | 2009-06-10 19:19:15 +0000 | [diff] [blame] | 200 | # Start constructing the qemu command |
| 201 | qemu_cmd = "" |
| 202 | # Set the X11 display parameter if requested |
| 203 | if params.get("x11_display"): |
| 204 | qemu_cmd += "DISPLAY=%s " % params.get("x11_display") |
| 205 | # Add the qemu binary |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 206 | qemu_cmd += kvm_utils.get_path(root_dir, params.get("qemu_binary", |
| 207 | "qemu")) |
lmr | eeff0eb | 2009-06-10 19:19:15 +0000 | [diff] [blame] | 208 | # Add the VM's name |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 209 | qemu_cmd += " -name '%s'" % name |
lmr | eeff0eb | 2009-06-10 19:19:15 +0000 | [diff] [blame] | 210 | # Add the monitor socket parameter |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 211 | qemu_cmd += " -monitor unix:%s,server,nowait" % self.monitor_file_name |
| 212 | |
| 213 | for image_name in kvm_utils.get_sub_dict_names(params, "images"): |
| 214 | image_params = kvm_utils.get_sub_dict(params, image_name) |
lmr | 9d75ee3 | 2009-09-29 13:14:13 +0000 | [diff] [blame] | 215 | if image_params.get("boot_drive") == "no": |
| 216 | continue |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 217 | qemu_cmd += " -drive file=%s" % get_image_filename(image_params, |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 218 | root_dir) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 219 | if image_params.get("drive_format"): |
| 220 | qemu_cmd += ",if=%s" % image_params.get("drive_format") |
lmr | 0d4d274 | 2009-06-18 06:15:17 +0000 | [diff] [blame] | 221 | if image_params.get("drive_cache"): |
| 222 | qemu_cmd += ",cache=%s" % image_params.get("drive_cache") |
| 223 | if image_params.get("drive_serial"): |
| 224 | qemu_cmd += ",serial=%s" % image_params.get("drive_serial") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 225 | if image_params.get("image_snapshot") == "yes": |
| 226 | qemu_cmd += ",snapshot=on" |
| 227 | if image_params.get("image_boot") == "yes": |
| 228 | qemu_cmd += ",boot=on" |
| 229 | |
| 230 | vlan = 0 |
| 231 | for nic_name in kvm_utils.get_sub_dict_names(params, "nics"): |
| 232 | nic_params = kvm_utils.get_sub_dict(params, nic_name) |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 233 | # Handle the '-net nic' part |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 234 | qemu_cmd += " -net nic,vlan=%d" % vlan |
| 235 | if nic_params.get("nic_model"): |
| 236 | qemu_cmd += ",model=%s" % nic_params.get("nic_model") |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 237 | if nic_params.has_key("address_index"): |
| 238 | mac, ip = kvm_utils.get_mac_ip_pair_from_dict(nic_params) |
lmr | 7124700 | 2009-09-10 03:13:34 +0000 | [diff] [blame] | 239 | if mac: |
| 240 | qemu_cmd += ",macaddr=%s" % mac |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 241 | # Handle the '-net tap' or '-net user' part |
| 242 | mode = nic_params.get("nic_mode", "user") |
| 243 | qemu_cmd += " -net %s,vlan=%d" % (mode, vlan) |
| 244 | if mode == "tap": |
| 245 | if nic_params.get("nic_ifname"): |
| 246 | qemu_cmd += ",ifname=%s" % nic_params.get("nic_ifname") |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 247 | script_path = nic_params.get("nic_script") |
| 248 | if script_path: |
| 249 | script_path = kvm_utils.get_path(root_dir, script_path) |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 250 | qemu_cmd += ",script=%s" % script_path |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 251 | script_path = nic_params.get("nic_downscript") |
| 252 | if script_path: |
| 253 | script_path = kvm_utils.get_path(root_dir, script_path) |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 254 | qemu_cmd += ",downscript=%s" % script_path |
lmr | 620b8bc | 2009-10-23 12:12:53 +0000 | [diff] [blame] | 255 | else: |
| 256 | qemu_cmd += ",downscript=no" |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 257 | # Proceed to next NIC |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 258 | vlan += 1 |
| 259 | |
| 260 | mem = params.get("mem") |
| 261 | if mem: |
| 262 | qemu_cmd += " -m %s" % mem |
| 263 | |
lmr | c43bf37 | 2009-11-10 13:19:00 +0000 | [diff] [blame] | 264 | smp = params.get("smp") |
| 265 | if smp: |
| 266 | qemu_cmd += " -smp %s" % smp |
| 267 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 268 | iso = params.get("cdrom") |
| 269 | if iso: |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 270 | iso = kvm_utils.get_path(root_dir, iso) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 271 | qemu_cmd += " -cdrom %s" % iso |
| 272 | |
lmr | b0a9b76 | 2009-10-09 20:43:30 +0000 | [diff] [blame] | 273 | # We may want to add {floppy_otps} parameter for -fda |
| 274 | # {fat:floppy:}/path/. However vvfat is not usually recommended |
| 275 | floppy = params.get("floppy") |
| 276 | if floppy: |
lmr | f69f6b1 | 2009-11-10 16:33:44 +0000 | [diff] [blame^] | 277 | floppy = kvm_utils.get_path(root_dir, floppy) |
lmr | b0a9b76 | 2009-10-09 20:43:30 +0000 | [diff] [blame] | 278 | qemu_cmd += " -fda %s" % floppy |
| 279 | |
| 280 | tftp = params.get("tftp") |
| 281 | if tftp: |
lmr | f69f6b1 | 2009-11-10 16:33:44 +0000 | [diff] [blame^] | 282 | tftp = kvm_utils.get_path(root_dir, tftp) |
lmr | b0a9b76 | 2009-10-09 20:43:30 +0000 | [diff] [blame] | 283 | qemu_cmd += " -tftp %s" % tftp |
| 284 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 285 | extra_params = params.get("extra_params") |
| 286 | if extra_params: |
| 287 | qemu_cmd += " %s" % extra_params |
| 288 | |
| 289 | for redir_name in kvm_utils.get_sub_dict_names(params, "redirs"): |
| 290 | redir_params = kvm_utils.get_sub_dict(params, redir_name) |
| 291 | guest_port = int(redir_params.get("guest_port")) |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 292 | host_port = self.redirs.get(guest_port) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 293 | qemu_cmd += " -redir tcp:%s::%s" % (host_port, guest_port) |
| 294 | |
| 295 | if params.get("display") == "vnc": |
| 296 | qemu_cmd += " -vnc :%d" % (self.vnc_port - 5900) |
| 297 | elif params.get("display") == "sdl": |
| 298 | qemu_cmd += " -sdl" |
| 299 | elif params.get("display") == "nographic": |
| 300 | qemu_cmd += " -nographic" |
| 301 | |
lmr | a253322 | 2009-07-20 12:43:46 +0000 | [diff] [blame] | 302 | if params.get("uuid") == "random": |
| 303 | qemu_cmd += " -uuid %s" % self.uuid |
| 304 | elif params.get("uuid"): |
| 305 | qemu_cmd += " -uuid %s" % params.get("uuid") |
| 306 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 307 | return qemu_cmd |
| 308 | |
| 309 | |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 310 | def create(self, name=None, params=None, root_dir=None, |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 311 | for_migration=False, timeout=5.0): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 312 | """ |
| 313 | Start the VM by running a qemu command. |
| 314 | All parameters are optional. The following applies to all parameters |
| 315 | but for_migration: If a parameter is not supplied, the corresponding |
| 316 | value stored in the class attributes is used, and if it is supplied, |
| 317 | it is stored for later use. |
| 318 | |
| 319 | @param name: The name of the object |
| 320 | @param params: A dict containing VM params |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 321 | @param root_dir: Base directory for relative filenames |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 322 | @param for_migration: If True, start the VM with the -incoming |
| 323 | option |
| 324 | """ |
lmr | 135b5e6 | 2009-06-10 19:22:31 +0000 | [diff] [blame] | 325 | self.destroy() |
| 326 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 327 | if name != None: |
| 328 | self.name = name |
| 329 | if params != None: |
| 330 | self.params = params |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 331 | if root_dir != None: |
| 332 | self.root_dir = root_dir |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 333 | name = self.name |
| 334 | params = self.params |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 335 | root_dir = self.root_dir |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 336 | |
| 337 | # Verify the md5sum of the ISO image |
| 338 | iso = params.get("cdrom") |
| 339 | if iso: |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 340 | iso = kvm_utils.get_path(root_dir, iso) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 341 | if not os.path.exists(iso): |
| 342 | logging.error("ISO file not found: %s" % iso) |
| 343 | return False |
| 344 | compare = False |
| 345 | if params.get("md5sum_1m"): |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 346 | logging.debug("Comparing expected MD5 sum with MD5 sum of " |
| 347 | "first MB of ISO file...") |
lmr | 03ba22e | 2009-10-23 12:07:44 +0000 | [diff] [blame] | 348 | actual_hash = kvm_utils.hash_file(iso, 1048576, method="md5") |
| 349 | expected_hash = params.get("md5sum_1m") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 350 | compare = True |
| 351 | elif params.get("md5sum"): |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 352 | logging.debug("Comparing expected MD5 sum with MD5 sum of ISO " |
| 353 | "file...") |
lmr | 03ba22e | 2009-10-23 12:07:44 +0000 | [diff] [blame] | 354 | actual_hash = kvm_utils.md5sum_file(iso, method="md5") |
| 355 | expected_hash = params.get("md5sum") |
| 356 | compare = True |
| 357 | elif params.get("sha1sum"): |
| 358 | logging.debug("Comparing expected SHA1 sum with SHA1 sum of " |
| 359 | "ISO file...") |
| 360 | actual_hash = kvm_utils.md5sum_file(iso, method="sha1") |
| 361 | expected_hash = params.get("md5sum") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 362 | compare = True |
| 363 | if compare: |
lmr | 03ba22e | 2009-10-23 12:07:44 +0000 | [diff] [blame] | 364 | if actual_hash == expected_hash: |
| 365 | logging.debug("Hashes match") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 366 | else: |
lmr | 03ba22e | 2009-10-23 12:07:44 +0000 | [diff] [blame] | 367 | logging.error("Actual hash differs from expected one") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 368 | return False |
| 369 | |
lmr | dc2ac6a | 2009-06-10 19:15:49 +0000 | [diff] [blame] | 370 | # Make sure the following code is not executed by more than one thread |
| 371 | # at the same time |
| 372 | lockfile = open("/tmp/kvm-autotest-vm-create.lock", "w+") |
| 373 | fcntl.lockf(lockfile, fcntl.LOCK_EX) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 374 | |
lmr | dc2ac6a | 2009-06-10 19:15:49 +0000 | [diff] [blame] | 375 | try: |
| 376 | # Handle port redirections |
| 377 | redir_names = kvm_utils.get_sub_dict_names(params, "redirs") |
| 378 | host_ports = kvm_utils.find_free_ports(5000, 6000, len(redir_names)) |
| 379 | self.redirs = {} |
| 380 | for i in range(len(redir_names)): |
| 381 | redir_params = kvm_utils.get_sub_dict(params, redir_names[i]) |
| 382 | guest_port = int(redir_params.get("guest_port")) |
| 383 | self.redirs[guest_port] = host_ports[i] |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 384 | |
lmr | dc2ac6a | 2009-06-10 19:15:49 +0000 | [diff] [blame] | 385 | # Find available VNC port, if needed |
| 386 | if params.get("display") == "vnc": |
| 387 | self.vnc_port = kvm_utils.find_free_port(5900, 6000) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 388 | |
lmr | a253322 | 2009-07-20 12:43:46 +0000 | [diff] [blame] | 389 | # Find random UUID if specified 'uuid = random' in config file |
| 390 | if params.get("uuid") == "random": |
| 391 | f = open("/proc/sys/kernel/random/uuid") |
| 392 | self.uuid = f.read().strip() |
| 393 | f.close() |
| 394 | |
lmr | dc2ac6a | 2009-06-10 19:15:49 +0000 | [diff] [blame] | 395 | # Make qemu command |
| 396 | qemu_command = self.make_qemu_command() |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 397 | |
lmr | dc2ac6a | 2009-06-10 19:15:49 +0000 | [diff] [blame] | 398 | # Is this VM supposed to accept incoming migrations? |
| 399 | if for_migration: |
| 400 | # Find available migration port |
| 401 | self.migration_port = kvm_utils.find_free_port(5200, 6000) |
| 402 | # Add -incoming option to the qemu command |
| 403 | qemu_command += " -incoming tcp:0:%d" % self.migration_port |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 404 | |
lmr | dc2ac6a | 2009-06-10 19:15:49 +0000 | [diff] [blame] | 405 | logging.debug("Running qemu command:\n%s", qemu_command) |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 406 | self.process = kvm_subprocess.run_bg(qemu_command, None, |
| 407 | logging.debug, "(qemu) ") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 408 | |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 409 | if not self.process.is_alive(): |
| 410 | logging.error("VM could not be created; " |
| 411 | "qemu command failed:\n%s" % qemu_command) |
| 412 | logging.error("Status: %s" % self.process.get_status()) |
| 413 | logging.error("Output:" + kvm_utils.format_str_for_message( |
| 414 | self.process.get_output())) |
lmr | dc2ac6a | 2009-06-10 19:15:49 +0000 | [diff] [blame] | 415 | self.destroy() |
| 416 | return False |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 417 | |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 418 | if not kvm_utils.wait_for(self.is_alive, timeout, 0, 1): |
| 419 | logging.error("VM is not alive for some reason; " |
| 420 | "qemu command:\n%s" % qemu_command) |
| 421 | self.destroy() |
| 422 | return False |
| 423 | |
lmr | fe6515e | 2009-07-29 13:01:17 +0000 | [diff] [blame] | 424 | # Get the output so far, to see if we have any problems with |
| 425 | # hugepage setup. |
| 426 | output = self.process.get_output() |
| 427 | |
| 428 | if "alloc_mem_area" in output: |
| 429 | logging.error("Could not allocate hugepage memory; " |
| 430 | "qemu command:\n%s" % qemu_command) |
| 431 | logging.error("Output:" + kvm_utils.format_str_for_message( |
| 432 | self.process.get_output())) |
| 433 | return False |
| 434 | |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 435 | logging.debug("VM appears to be alive with PID %d", |
| 436 | self.process.get_pid()) |
lmr | dc2ac6a | 2009-06-10 19:15:49 +0000 | [diff] [blame] | 437 | return True |
| 438 | |
| 439 | finally: |
| 440 | fcntl.lockf(lockfile, fcntl.LOCK_UN) |
| 441 | lockfile.close() |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 442 | |
| 443 | |
| 444 | def send_monitor_cmd(self, command, block=True, timeout=20.0): |
| 445 | """ |
| 446 | Send command to the QEMU monitor. |
| 447 | |
| 448 | Connect to the VM's monitor socket and wait for the (qemu) prompt. |
| 449 | If block is True, read output from the socket until the (qemu) prompt |
| 450 | is found again, or until timeout expires. |
| 451 | |
| 452 | Return a tuple containing an integer indicating success or failure, |
| 453 | and the data read so far. The integer is 0 on success and 1 on failure. |
| 454 | A failure is any of the following cases: connection to the socket |
| 455 | failed, or the first (qemu) prompt could not be found, or block is |
| 456 | True and the second prompt could not be found. |
| 457 | |
| 458 | @param command: Command that will be sent to the monitor |
| 459 | @param block: Whether the output from the socket will be read until |
| 460 | the timeout expires |
| 461 | @param timeout: Timeout (seconds) before giving up on reading from |
| 462 | socket |
| 463 | """ |
| 464 | def read_up_to_qemu_prompt(s, timeout): |
| 465 | """ |
| 466 | Read data from socket s until the (qemu) prompt is found. |
| 467 | |
| 468 | If the prompt is found before timeout expires, return a tuple |
| 469 | containing True and the data read. Otherwise return a tuple |
| 470 | containing False and the data read so far. |
| 471 | |
| 472 | @param s: Socket object |
| 473 | @param timeout: Time (seconds) before giving up trying to get the |
| 474 | qemu prompt. |
| 475 | """ |
| 476 | o = "" |
| 477 | end_time = time.time() + timeout |
| 478 | while time.time() < end_time: |
| 479 | try: |
lmr | bcd2083 | 2009-10-15 11:56:56 +0000 | [diff] [blame] | 480 | o += s.recv(1024) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 481 | if o.splitlines()[-1].split()[-1] == "(qemu)": |
| 482 | return (True, o) |
| 483 | except: |
| 484 | time.sleep(0.01) |
| 485 | return (False, o) |
| 486 | |
| 487 | # Connect to monitor |
| 488 | logging.debug("Sending monitor command: %s" % command) |
| 489 | try: |
| 490 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 491 | s.setblocking(False) |
| 492 | s.connect(self.monitor_file_name) |
| 493 | except: |
| 494 | logging.debug("Could not connect to monitor socket") |
| 495 | return (1, "") |
lmr | bcd2083 | 2009-10-15 11:56:56 +0000 | [diff] [blame] | 496 | |
| 497 | # Send the command and get the resulting output |
| 498 | try: |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 499 | status, data = read_up_to_qemu_prompt(s, timeout) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 500 | if not status: |
lmr | bcd2083 | 2009-10-15 11:56:56 +0000 | [diff] [blame] | 501 | logging.debug("Could not find (qemu) prompt; output so far:" + |
| 502 | kvm_utils.format_str_for_message(data)) |
| 503 | return (1, "") |
| 504 | # Send command |
| 505 | s.sendall(command + "\n") |
| 506 | # Receive command output |
| 507 | data = "" |
| 508 | if block: |
| 509 | status, data = read_up_to_qemu_prompt(s, timeout) |
| 510 | data = "\n".join(data.splitlines()[1:]) |
| 511 | if not status: |
| 512 | logging.debug("Could not find (qemu) prompt after command; " |
| 513 | "output so far:" + |
| 514 | kvm_utils.format_str_for_message(data)) |
| 515 | return (1, data) |
| 516 | return (0, data) |
| 517 | |
| 518 | # Clean up before exiting |
| 519 | finally: |
| 520 | s.shutdown(socket.SHUT_RDWR) |
| 521 | s.close() |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 522 | |
| 523 | |
| 524 | def destroy(self, gracefully=True): |
| 525 | """ |
| 526 | Destroy the VM. |
| 527 | |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 528 | If gracefully is True, first attempt to shutdown the VM with a shell |
| 529 | command. Then, attempt to destroy the VM via the monitor with a 'quit' |
| 530 | command. If that fails, send SIGKILL to the qemu process. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 531 | |
| 532 | @param gracefully: Whether an attempt will be made to end the VM |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 533 | using a shell command before trying to end the qemu process |
| 534 | with a 'quit' or a kill signal. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 535 | """ |
lmr | f320b04 | 2009-09-15 05:48:06 +0000 | [diff] [blame] | 536 | try: |
| 537 | # Is it already dead? |
| 538 | if self.is_dead(): |
| 539 | logging.debug("VM is already down") |
| 540 | return |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 541 | |
lmr | f320b04 | 2009-09-15 05:48:06 +0000 | [diff] [blame] | 542 | logging.debug("Destroying VM with PID %d..." % |
| 543 | self.process.get_pid()) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 544 | |
lmr | f320b04 | 2009-09-15 05:48:06 +0000 | [diff] [blame] | 545 | if gracefully and self.params.get("shutdown_command"): |
| 546 | # Try to destroy with shell command |
| 547 | logging.debug("Trying to shutdown VM with shell command...") |
| 548 | session = self.remote_login() |
| 549 | if session: |
| 550 | try: |
| 551 | # Send the shutdown command |
| 552 | session.sendline(self.params.get("shutdown_command")) |
| 553 | logging.debug("Shutdown command sent; waiting for VM " |
| 554 | "to go down...") |
| 555 | if kvm_utils.wait_for(self.is_dead, 60, 1, 1): |
| 556 | logging.debug("VM is down") |
| 557 | return |
| 558 | finally: |
| 559 | session.close() |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 560 | |
lmr | f320b04 | 2009-09-15 05:48:06 +0000 | [diff] [blame] | 561 | # Try to destroy with a monitor command |
| 562 | logging.debug("Trying to kill VM with monitor command...") |
| 563 | status, output = self.send_monitor_cmd("quit", block=False) |
| 564 | # Was the command sent successfully? |
| 565 | if status == 0: |
| 566 | # Wait for the VM to be really dead |
| 567 | if kvm_utils.wait_for(self.is_dead, 5, 0.5, 0.5): |
| 568 | logging.debug("VM is down") |
| 569 | return |
| 570 | |
| 571 | # If the VM isn't dead yet... |
| 572 | logging.debug("Cannot quit normally; sending a kill to close the " |
| 573 | "deal...") |
| 574 | kvm_utils.kill_process_tree(self.process.get_pid(), 9) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 575 | # Wait for the VM to be really dead |
| 576 | if kvm_utils.wait_for(self.is_dead, 5, 0.5, 0.5): |
| 577 | logging.debug("VM is down") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 578 | return |
| 579 | |
lmr | f320b04 | 2009-09-15 05:48:06 +0000 | [diff] [blame] | 580 | logging.error("Process %s is a zombie!" % self.process.get_pid()) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 581 | |
lmr | f320b04 | 2009-09-15 05:48:06 +0000 | [diff] [blame] | 582 | finally: |
| 583 | if self.process: |
| 584 | self.process.close() |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 585 | |
| 586 | |
| 587 | def is_alive(self): |
| 588 | """ |
| 589 | Return True if the VM's monitor is responsive. |
| 590 | """ |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 591 | # Check if the process is running |
| 592 | if self.is_dead(): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 593 | return False |
| 594 | # Try sending a monitor command |
| 595 | (status, output) = self.send_monitor_cmd("help") |
| 596 | if status: |
| 597 | return False |
| 598 | return True |
| 599 | |
| 600 | |
| 601 | def is_dead(self): |
| 602 | """ |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 603 | Return True if the qemu process is dead. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 604 | """ |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 605 | return not self.process or not self.process.is_alive() |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 606 | |
| 607 | |
lmr | a419700 | 2009-08-13 05:00:51 +0000 | [diff] [blame] | 608 | def kill_tail_thread(self): |
| 609 | """ |
| 610 | Stop the tailing thread which reports the output of qemu. |
| 611 | """ |
| 612 | if self.process: |
| 613 | self.process.kill_tail_thread() |
| 614 | |
| 615 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 616 | def get_params(self): |
| 617 | """ |
| 618 | Return the VM's params dict. Most modified params take effect only |
| 619 | upon VM.create(). |
| 620 | """ |
| 621 | return self.params |
| 622 | |
| 623 | |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 624 | def get_address(self, index=0): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 625 | """ |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 626 | Return the address of a NIC of the guest, in host space. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 627 | |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 628 | If port redirection is used, return 'localhost' (the NIC has no IP |
| 629 | address of its own). Otherwise return the NIC's IP address. |
| 630 | |
| 631 | @param index: Index of the NIC whose address is requested. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 632 | """ |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 633 | nics = kvm_utils.get_sub_dict_names(self.params, "nics") |
| 634 | nic_name = nics[index] |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 635 | nic_params = kvm_utils.get_sub_dict(self.params, nic_name) |
| 636 | if nic_params.get("nic_mode") == "tap": |
| 637 | mac, ip = kvm_utils.get_mac_ip_pair_from_dict(nic_params) |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 638 | if not mac: |
| 639 | logging.debug("MAC address unavailable") |
| 640 | return None |
| 641 | if not ip or nic_params.get("always_use_tcpdump") == "yes": |
| 642 | # Get the IP address from the cache |
| 643 | ip = self.address_cache.get(mac) |
| 644 | if not ip: |
| 645 | logging.debug("Could not find IP address for MAC address: " |
| 646 | "%s" % mac) |
| 647 | return None |
| 648 | # Make sure the IP address is assigned to this guest |
| 649 | nic_dicts = [kvm_utils.get_sub_dict(self.params, nic) |
| 650 | for nic in nics] |
| 651 | macs = [kvm_utils.get_mac_ip_pair_from_dict(dict)[0] |
| 652 | for dict in nic_dicts] |
| 653 | if not kvm_utils.verify_ip_address_ownership(ip, macs): |
| 654 | logging.debug("Could not verify MAC-IP address mapping: " |
| 655 | "%s ---> %s" % (mac, ip)) |
| 656 | return None |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 657 | return ip |
| 658 | else: |
| 659 | return "localhost" |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 660 | |
| 661 | |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 662 | def get_port(self, port, nic_index=0): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 663 | """ |
| 664 | Return the port in host space corresponding to port in guest space. |
| 665 | |
| 666 | @param port: Port number in host space. |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 667 | @param nic_index: Index of the NIC. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 668 | @return: If port redirection is used, return the host port redirected |
| 669 | to guest port port. Otherwise return port. |
| 670 | """ |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 671 | nic_name = kvm_utils.get_sub_dict_names(self.params, "nics")[nic_index] |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 672 | nic_params = kvm_utils.get_sub_dict(self.params, nic_name) |
| 673 | if nic_params.get("nic_mode") == "tap": |
| 674 | return port |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 675 | else: |
lmr | f469634 | 2009-08-13 04:06:33 +0000 | [diff] [blame] | 676 | if not self.redirs.has_key(port): |
| 677 | logging.warn("Warning: guest port %s requested but not " |
| 678 | "redirected" % port) |
| 679 | return self.redirs.get(port) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 680 | |
| 681 | |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 682 | def get_pid(self): |
| 683 | """ |
| 684 | Return the VM's PID. |
| 685 | """ |
| 686 | return self.process.get_pid() |
| 687 | |
| 688 | |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 689 | def remote_login(self, nic_index=0, timeout=10): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 690 | """ |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 691 | Log into the guest via SSH/Telnet/Netcat. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 692 | If timeout expires while waiting for output from the guest (e.g. a |
| 693 | password prompt or a shell prompt) -- fail. |
| 694 | |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 695 | @param nic_index: The index of the NIC to connect to. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 696 | @param timeout: Time (seconds) before giving up logging into the |
| 697 | guest. |
| 698 | @return: kvm_spawn object on success and None on failure. |
| 699 | """ |
| 700 | username = self.params.get("username", "") |
| 701 | password = self.params.get("password", "") |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 702 | prompt = self.params.get("shell_prompt", "[\#\$]") |
lmr | 59f9e2d | 2009-10-05 18:47:16 +0000 | [diff] [blame] | 703 | linesep = eval("'%s'" % self.params.get("shell_linesep", r"\n")) |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 704 | client = self.params.get("shell_client") |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 705 | address = self.get_address(nic_index) |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 706 | port = self.get_port(int(self.params.get("shell_port"))) |
| 707 | |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 708 | if not address or not port: |
| 709 | logging.debug("IP address or port unavailable") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 710 | return None |
| 711 | |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 712 | if client == "ssh": |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 713 | session = kvm_utils.ssh(address, port, username, password, |
lmr | 59f9e2d | 2009-10-05 18:47:16 +0000 | [diff] [blame] | 714 | prompt, linesep, timeout) |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 715 | elif client == "telnet": |
| 716 | session = kvm_utils.telnet(address, port, username, password, |
lmr | 59f9e2d | 2009-10-05 18:47:16 +0000 | [diff] [blame] | 717 | prompt, linesep, timeout) |
lmr | 9f6ebf1 | 2009-08-17 20:44:10 +0000 | [diff] [blame] | 718 | elif client == "nc": |
| 719 | session = kvm_utils.netcat(address, port, username, password, |
lmr | 59f9e2d | 2009-10-05 18:47:16 +0000 | [diff] [blame] | 720 | prompt, linesep, timeout) |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 721 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 722 | if session: |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 723 | session.set_status_test_command(self.params.get("status_test_" |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 724 | "command", "")) |
| 725 | return session |
| 726 | |
| 727 | |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 728 | def copy_files_to(self, local_path, remote_path, nic_index=0, timeout=300): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 729 | """ |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 730 | Transfer files to the guest. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 731 | |
| 732 | @param local_path: Host path |
| 733 | @param remote_path: Guest path |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 734 | @param nic_index: The index of the NIC to connect to. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 735 | @param timeout: Time (seconds) before giving up on doing the remote |
| 736 | copy. |
| 737 | """ |
| 738 | username = self.params.get("username", "") |
| 739 | password = self.params.get("password", "") |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 740 | client = self.params.get("file_transfer_client") |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 741 | address = self.get_address(nic_index) |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 742 | port = self.get_port(int(self.params.get("file_transfer_port"))) |
| 743 | |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 744 | if not address or not port: |
| 745 | logging.debug("IP address or port unavailable") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 746 | return None |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 747 | |
| 748 | if client == "scp": |
| 749 | return kvm_utils.scp_to_remote(address, port, username, password, |
| 750 | local_path, remote_path, timeout) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 751 | |
| 752 | |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 753 | def copy_files_from(self, remote_path, local_path, nic_index=0, timeout=300): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 754 | """ |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 755 | Transfer files from the guest. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 756 | |
| 757 | @param local_path: Guest path |
| 758 | @param remote_path: Host path |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 759 | @param nic_index: The index of the NIC to connect to. |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 760 | @param timeout: Time (seconds) before giving up on doing the remote |
| 761 | copy. |
| 762 | """ |
| 763 | username = self.params.get("username", "") |
| 764 | password = self.params.get("password", "") |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 765 | client = self.params.get("file_transfer_client") |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 766 | address = self.get_address(nic_index) |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 767 | port = self.get_port(int(self.params.get("file_transfer_port"))) |
| 768 | |
lmr | ee90dd9 | 2009-08-13 04:13:39 +0000 | [diff] [blame] | 769 | if not address or not port: |
| 770 | logging.debug("IP address or port unavailable") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 771 | return None |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 772 | |
lmr | 912c74b | 2009-08-17 20:43:27 +0000 | [diff] [blame] | 773 | if client == "scp": |
| 774 | return kvm_utils.scp_from_remote(address, port, username, password, |
| 775 | remote_path, local_path, timeout) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 776 | |
| 777 | |
| 778 | def send_key(self, keystr): |
| 779 | """ |
| 780 | Send a key event to the VM. |
| 781 | |
| 782 | @param: keystr: A key event string (e.g. "ctrl-alt-delete") |
| 783 | """ |
| 784 | # For compatibility with versions of QEMU that do not recognize all |
| 785 | # key names: replace keyname with the hex value from the dict, which |
| 786 | # QEMU will definitely accept |
| 787 | dict = { "comma": "0x33", |
| 788 | "dot": "0x34", |
| 789 | "slash": "0x35" } |
| 790 | for key in dict.keys(): |
| 791 | keystr = keystr.replace(key, dict[key]) |
| 792 | self.send_monitor_cmd("sendkey %s 1" % keystr) |
| 793 | time.sleep(0.2) |
| 794 | |
| 795 | |
| 796 | def send_string(self, str): |
| 797 | """ |
| 798 | Send a string to the VM. |
| 799 | |
| 800 | @param str: String, that must consist of alphanumeric characters only. |
| 801 | Capital letters are allowed. |
| 802 | """ |
| 803 | for char in str: |
| 804 | if char.isupper(): |
| 805 | self.send_key("shift-%s" % char.lower()) |
| 806 | else: |
| 807 | self.send_key(char) |
lmr | a253322 | 2009-07-20 12:43:46 +0000 | [diff] [blame] | 808 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 809 | |
lmr | a253322 | 2009-07-20 12:43:46 +0000 | [diff] [blame] | 810 | def get_uuid(self): |
| 811 | """ |
| 812 | Catch UUID of the VM. |
| 813 | |
| 814 | @return: None,if not specified in config file |
| 815 | """ |
| 816 | if self.params.get("uuid") == "random": |
| 817 | return self.uuid |
| 818 | else: |
| 819 | return self.params.get("uuid", None) |