Commit a985b417 authored by Iustin Pop's avatar Iustin Pop
Browse files

Simplify a little the hypervisor routines

Instead of “instance.hvparams”, we use a shorter “hvp” name to make readability
better.

Reviewed-by: imsnah
parent f9d6542d
......@@ -235,14 +235,15 @@ class KVMHypervisor(hv_base.BaseHypervisor):
if not instance.hvparams[constants.HV_ACPI]:
kvm_cmd.extend(['-no-acpi'])
boot_disk = (instance.hvparams[constants.HV_BOOT_ORDER] == "disk")
boot_cdrom = (instance.hvparams[constants.HV_BOOT_ORDER] == "cdrom")
boot_network = (instance.hvparams[constants.HV_BOOT_ORDER] == "network")
hvp = instance.hvparams
boot_disk = hvp[constants.HV_BOOT_ORDER] == "disk"
boot_cdrom = hvp[constants.HV_BOOT_ORDER] == "cdrom"
boot_network = hvp[constants.HV_BOOT_ORDER] == "network"
if boot_network:
kvm_cmd.extend(['-boot', 'n'])
disk_type = instance.hvparams[constants.HV_DISK_TYPE]
disk_type = hvp[constants.HV_DISK_TYPE]
if disk_type == constants.HT_DISK_PARAVIRTUAL:
if_val = ',if=virtio'
else:
......@@ -263,7 +264,7 @@ class KVMHypervisor(hv_base.BaseHypervisor):
drive_val = 'file=%s,format=raw%s%s' % (dev_path, if_val, boot_val)
kvm_cmd.extend(['-drive', drive_val])
iso_image = instance.hvparams[constants.HV_CDROM_IMAGE_PATH]
iso_image = hvp[constants.HV_CDROM_IMAGE_PATH]
if iso_image:
options = ',format=raw,media=cdrom'
if boot_cdrom:
......@@ -274,10 +275,10 @@ class KVMHypervisor(hv_base.BaseHypervisor):
drive_val = 'file=%s%s' % (iso_image, options)
kvm_cmd.extend(['-drive', drive_val])
kernel_path = instance.hvparams[constants.HV_KERNEL_PATH]
kernel_path = hvp[constants.HV_KERNEL_PATH]
if kernel_path:
kvm_cmd.extend(['-kernel', kernel_path])
initrd_path = instance.hvparams[constants.HV_INITRD_PATH]
initrd_path = hvp[constants.HV_INITRD_PATH]
if initrd_path:
kvm_cmd.extend(['-initrd', initrd_path])
root_append = 'root=%s ro' % instance.hvparams[constants.HV_ROOT_PATH]
......@@ -286,13 +287,13 @@ class KVMHypervisor(hv_base.BaseHypervisor):
else:
kvm_cmd.extend(['-append', root_append])
mouse_type = instance.hvparams[constants.HV_USB_MOUSE]
mouse_type = hvp[constants.HV_USB_MOUSE]
if mouse_type:
kvm_cmd.extend(['-usb'])
kvm_cmd.extend(['-usbdevice', mouse_type])
# FIXME: handle vnc password
vnc_bind_address = instance.hvparams[constants.HV_VNC_BIND_ADDRESS]
vnc_bind_address = hvp[constants.HV_VNC_BIND_ADDRESS]
if vnc_bind_address:
if utils.IsValidIP(vnc_bind_address):
if instance.network_port > constants.VNC_BASE_PORT:
......@@ -311,14 +312,14 @@ class KVMHypervisor(hv_base.BaseHypervisor):
# Only allow tls and other option when not binding to a file, for now.
# kvm/qemu gets confused otherwise about the filename to use.
vnc_append = ''
if instance.hvparams[constants.HV_VNC_TLS]:
if hvp[constants.HV_VNC_TLS]:
vnc_append = '%s,tls' % vnc_append
if instance.hvparams[constants.HV_VNC_X509_VERIFY]:
if hvp[constants.HV_VNC_X509_VERIFY]:
vnc_append = '%s,x509verify=%s' % (vnc_append,
instance.hvparams[constants.HV_VNC_X509])
elif instance.hvparams[constants.HV_VNC_X509]:
hvp[constants.HV_VNC_X509])
elif hvp[constants.HV_VNC_X509]:
vnc_append = '%s,x509=%s' % (vnc_append,
instance.hvparams[constants.HV_VNC_X509])
hvp[constants.HV_VNC_X509])
vnc_arg = '%s%s' % (vnc_arg, vnc_append)
else:
......@@ -331,8 +332,9 @@ class KVMHypervisor(hv_base.BaseHypervisor):
monitor_dev = 'unix:%s,server,nowait' % \
self._InstanceMonitor(instance.name)
kvm_cmd.extend(['-monitor', monitor_dev])
if instance.hvparams[constants.HV_SERIAL_CONSOLE]:
serial_dev = 'unix:%s,server,nowait' % self._InstanceSerial(instance.name)
if hvp[constants.HV_SERIAL_CONSOLE]:
serial_dev = ('unix:%s,server,nowait' %
self._InstanceSerial(instance.name))
kvm_cmd.extend(['-serial', serial_dev])
else:
kvm_cmd.extend(['-serial', 'none'])
......@@ -340,7 +342,7 @@ class KVMHypervisor(hv_base.BaseHypervisor):
# Save the current instance nics, but defer their expansion as parameters,
# as we'll need to generate executable temp files for them.
kvm_nics = instance.nics
hvparams = instance.hvparams
hvparams = hvp
return (kvm_cmd, kvm_nics, hvparams)
......
......@@ -439,15 +439,16 @@ class XenPvmHypervisor(XenHypervisor):
"""Write the Xen config file for the instance.
"""
hvp = instance.hvparams
config = StringIO()
config.write("# this is autogenerated by Ganeti, please do not edit\n#\n")
# kernel handling
kpath = instance.hvparams[constants.HV_KERNEL_PATH]
kpath = hvp[constants.HV_KERNEL_PATH]
config.write("kernel = '%s'\n" % kpath)
# initrd handling
initrd_path = instance.hvparams[constants.HV_INITRD_PATH]
initrd_path = hvp[constants.HV_INITRD_PATH]
if initrd_path:
config.write("ramdisk = '%s'\n" % initrd_path)
......@@ -479,8 +480,7 @@ class XenPvmHypervisor(XenHypervisor):
# just in case it exists
utils.RemoveFile("/etc/xen/auto/%s" % instance.name)
try:
utils.WriteFile("/etc/xen/%s" % instance.name,
data=config.getvalue())
utils.WriteFile("/etc/xen/%s" % instance.name, data=config.getvalue())
except EnvironmentError, err:
raise errors.HypervisorError("Cannot write Xen instance confile"
" file /etc/xen/%s: %s" %
......@@ -567,6 +567,8 @@ class XenHvmHypervisor(XenHypervisor):
"""Create a Xen 3.1 HVM config file.
"""
hvp = instance.hvparams
config = StringIO()
config.write("# this is autogenerated by Ganeti, please do not edit\n#\n")
config.write("kernel = '/usr/lib/xen/boot/hvmloader'\n")
......@@ -588,16 +590,15 @@ class XenHvmHypervisor(XenHypervisor):
config.write("device_model = '/usr/lib64/xen/bin/qemu-dm'\n")
else:
config.write("device_model = '/usr/lib/xen/bin/qemu-dm'\n")
config.write("boot = '%s'\n" % instance.hvparams[constants.HV_BOOT_ORDER])
config.write("boot = '%s'\n" % hvp[constants.HV_BOOT_ORDER])
config.write("sdl = 0\n")
config.write("usb = 1\n")
config.write("usbdevice = 'tablet'\n")
config.write("vnc = 1\n")
if instance.hvparams[constants.HV_VNC_BIND_ADDRESS] is None:
if hvp[constants.HV_VNC_BIND_ADDRESS] is None:
config.write("vnclisten = '%s'\n" % constants.VNC_DEFAULT_BIND_ADDRESS)
else:
config.write("vnclisten = '%s'\n" %
instance.hvparams["vnc_bind_address"])
config.write("vnclisten = '%s'\n" % hvp["vnc_bind_address"])
if instance.network_port > constants.VNC_BASE_PORT:
display = instance.network_port - constants.VNC_BASE_PORT
......@@ -619,7 +620,7 @@ class XenHvmHypervisor(XenHypervisor):
config.write("localtime = 1\n")
vif_data = []
nic_type = instance.hvparams[constants.HV_NIC_TYPE]
nic_type = hvp[constants.HV_NIC_TYPE]
if nic_type is None:
# ensure old instances don't change
nic_type_str = ", type=ioemu"
......@@ -637,13 +638,13 @@ class XenHvmHypervisor(XenHypervisor):
config.write("vif = [%s]\n" % ",".join(vif_data))
disk_data = cls._GetConfigFileDiskData(instance.disk_template,
block_devices)
disk_type = instance.hvparams[constants.HV_DISK_TYPE]
disk_type = hvp[constants.HV_DISK_TYPE]
if disk_type in (None, constants.HT_DISK_IOEMU):
replacement = ",ioemu:hd"
else:
replacement = ",hd"
disk_data = [line.replace(",sd", replacement) for line in disk_data]
iso_path = instance.hvparams[constants.HV_CDROM_IMAGE_PATH]
iso_path = hvp[constants.HV_CDROM_IMAGE_PATH]
if iso_path:
iso = "'file:%s,hdc:cdrom,r'" % iso_path
disk_data.append(iso)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment