def get_address(self, index=0): """ Return the address of a NIC of the guest, in host space. If port redirection is used, return 'localhost' (the NIC has no IP address of its own). Otherwise return the NIC's IP address. @param index: Index of the NIC whose address is requested. """ nics = kvm_utils.get_sub_dict_names(self.params, "nics") nic_name = nics[index] nic_params = kvm_utils.get_sub_dict(self.params, nic_name) if nic_params.get("nic_mode") == "tap": mac, ip = kvm_utils.get_mac_ip_pair_from_dict(nic_params) if not mac: logging.debug("MAC address unavailable") return None if not ip or nic_params.get("always_use_tcpdump") == "yes": # Get the IP address from the cache ip = self.address_cache.get(mac) if not ip: logging.debug("Could not find IP address for MAC address: " "%s" % mac) return None # Make sure the IP address is assigned to this guest nic_dicts = [kvm_utils.get_sub_dict(self.params, nic) for nic in nics] macs = [kvm_utils.get_mac_ip_pair_from_dict(dict)[0] for dict in nic_dicts] if not kvm_utils.verify_ip_address_ownership(ip, macs): logging.debug("Could not verify MAC-IP address mapping: " "%s ---> %s" % (mac, ip)) return None return ip else: return "localhost"
def make_qemu_command(self, name=None, params=None, root_dir=None): """ Generate a qemu command line. All parameters are optional. If a parameter is not supplied, the corresponding value stored in the class attributes is used. @param name: The name of the object @param params: A dict containing VM params @param root_dir: Base directory for relative filenames @note: The params dict should contain: mem -- memory size in MBs cdrom -- ISO filename to use with the qemu -cdrom parameter extra_params -- a string to append to the qemu command shell_port -- port of the remote shell daemon on the guest (SSH, Telnet or the home-made Remote Shell Server) shell_client -- client program to use for connecting to the remote shell daemon on the guest (ssh, telnet or nc) x11_display -- if specified, the DISPLAY environment variable will be be set to this value for the qemu process (useful for SDL rendering) images -- a list of image object names, separated by spaces nics -- a list of NIC object names, separated by spaces For each image in images: drive_format -- string to pass as 'if' parameter for this image (e.g. ide, scsi) image_snapshot -- if yes, pass 'snapshot=on' to qemu for this image image_boot -- if yes, pass 'boot=on' to qemu for this image In addition, all parameters required by get_image_filename. For each NIC in nics: nic_model -- string to pass as 'model' parameter for this NIC (e.g. e1000) """ if name == None: name = self.name if params == None: params = self.params if root_dir == None: root_dir = self.root_dir # Start constructing the qemu command qemu_cmd = "" # Set the X11 display parameter if requested if params.get("x11_display"): qemu_cmd += "DISPLAY=%s " % params.get("x11_display") # Add the qemu binary qemu_cmd += kvm_utils.get_path(root_dir, params.get("qemu_binary", "qemu")) # Add the VM's name qemu_cmd += " -name '%s'" % name # Add the monitor socket parameter qemu_cmd += " -monitor unix:%s,server,nowait" % self.monitor_file_name for image_name in kvm_utils.get_sub_dict_names(params, "images"): image_params = kvm_utils.get_sub_dict(params, image_name) if image_params.get("boot_drive") == "no": continue qemu_cmd += " -drive file=%s" % get_image_filename(image_params, root_dir) if image_params.get("drive_format"): qemu_cmd += ",if=%s" % image_params.get("drive_format") if image_params.get("drive_cache"): qemu_cmd += ",cache=%s" % image_params.get("drive_cache") if image_params.get("drive_serial"): qemu_cmd += ",serial=%s" % image_params.get("drive_serial") if image_params.get("image_snapshot") == "yes": qemu_cmd += ",snapshot=on" if image_params.get("image_boot") == "yes": qemu_cmd += ",boot=on" vlan = 0 for nic_name in kvm_utils.get_sub_dict_names(params, "nics"): nic_params = kvm_utils.get_sub_dict(params, nic_name) # Handle the '-net nic' part qemu_cmd += " -net nic,vlan=%d" % vlan if nic_params.get("nic_model"): qemu_cmd += ",model=%s" % nic_params.get("nic_model") if nic_params.has_key("address_index"): mac, ip = kvm_utils.get_mac_ip_pair_from_dict(nic_params) if mac: qemu_cmd += ",macaddr=%s" % mac # Handle the '-net tap' or '-net user' part mode = nic_params.get("nic_mode", "user") qemu_cmd += " -net %s,vlan=%d" % (mode, vlan) if mode == "tap": if nic_params.get("nic_ifname"): qemu_cmd += ",ifname=%s" % nic_params.get("nic_ifname") script_path = nic_params.get("nic_script") if script_path: script_path = kvm_utils.get_path(root_dir, script_path) qemu_cmd += ",script=%s" % script_path script_path = nic_params.get("nic_downscript") if script_path: script_path = kvm_utils.get_path(root_dir, script_path) qemu_cmd += ",downscript=%s" % script_path # Proceed to next NIC vlan += 1 mem = params.get("mem") if mem: qemu_cmd += " -m %s" % mem iso = params.get("cdrom") if iso: iso = kvm_utils.get_path(root_dir, iso) qemu_cmd += " -cdrom %s" % iso extra_params = params.get("extra_params") if extra_params: qemu_cmd += " %s" % extra_params for redir_name in kvm_utils.get_sub_dict_names(params, "redirs"): redir_params = kvm_utils.get_sub_dict(params, redir_name) guest_port = int(redir_params.get("guest_port")) host_port = self.redirs.get(guest_port) qemu_cmd += " -redir tcp:%s::%s" % (host_port, guest_port) if params.get("display") == "vnc": qemu_cmd += " -vnc :%d" % (self.vnc_port - 5900) elif params.get("display") == "sdl": qemu_cmd += " -sdl" elif params.get("display") == "nographic": qemu_cmd += " -nographic" if params.get("uuid") == "random": qemu_cmd += " -uuid %s" % self.uuid elif params.get("uuid"): qemu_cmd += " -uuid %s" % params.get("uuid") return qemu_cmd