diff options
| author | Daniel P. Berrange <berrange@redhat.com> | 2012-07-20 12:25:11 +0100 |
|---|---|---|
| committer | Daniel P. Berrange <berrange@redhat.com> | 2012-07-20 13:47:02 +0100 |
| commit | 9283f68ea31ffe97efd075c791211a13a8bae6a9 (patch) | |
| tree | 9b2700160d60c48ba45a9449f9ced6f3e50c9ccb /nova | |
| parent | 2766ee2b6ed68f1784aad1b41e81baa75cc305df (diff) | |
| download | nova-9283f68ea31ffe97efd075c791211a13a8bae6a9.tar.gz nova-9283f68ea31ffe97efd075c791211a13a8bae6a9.tar.xz nova-9283f68ea31ffe97efd075c791211a13a8bae6a9.zip | |
Move libvirt disk config setup out of main get_guest_config method
The get_guest_config method in the libvirt driver is getting
quite large and complicated, particularly wrt disk config.
Moving the disk config setup out into a separate method,
get_guest_storage_method, eases understanding of the code
Change-Id: Ibbfbb88cafa45c98792e6732da726ab4afe4ca00
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Diffstat (limited to 'nova')
| -rw-r--r-- | nova/virt/libvirt/driver.py | 223 |
1 files changed, 119 insertions, 104 deletions
diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index d279c2ef3..26c19af15 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -1543,111 +1543,22 @@ class LibvirtDriver(driver.ComputeDriver): return cpu - def get_guest_config(self, instance, network_info, image_meta, rescue=None, - block_device_info=None): - """Get config data for parameters. + def get_guest_storage_config(self, instance, image_meta, + rescue, block_device_info, + inst_type, + root_device_name, root_device): + devices = [] - :param rescue: optional dictionary that should contain the key - 'ramdisk_id' if a ramdisk is needed for the rescue image and - 'kernel_id' if a kernel is needed for the rescue image. - """ block_device_mapping = driver.block_device_info_get_mapping( block_device_info) - devs = [] - - # FIXME(vish): stick this in db - inst_type_id = instance['instance_type_id'] - inst_type = instance_types.get_instance_type(inst_type_id) - - guest = config.LibvirtConfigGuest() - guest.virt_type = FLAGS.libvirt_type - guest.name = instance['name'] - guest.uuid = instance['uuid'] - guest.memory = inst_type['memory_mb'] * 1024 - guest.vcpus = inst_type['vcpus'] - - guest.cpu = self.get_guest_cpu_config() - - root_device_name = driver.block_device_info_get_root(block_device_info) - if root_device_name: - root_device = block_device.strip_dev(root_device_name) - else: - # NOTE(yamahata): - # for nova.api.ec2.cloud.CloudController.get_metadata() - root_device = self.default_root_device - db.instance_update( - nova_context.get_admin_context(), instance['uuid'], - {'root_device_name': '/dev/' + self.default_root_device}) - - if FLAGS.libvirt_type == "lxc": - guest.os_type = "exe" - guest.os_init_path = "/sbin/init" - guest.os_cmdline = "console=ttyS0" - elif FLAGS.libvirt_type == "uml": - guest.os_type = "uml" - guest.os_kernel = "/usr/bin/linux" - guest.os_root = root_device_name or "/dev/ubda" - else: - if FLAGS.libvirt_type == "xen": - guest.os_type = "linux" - guest.os_root = root_device_name or "/dev/xvda" - else: - guest.os_type = "hvm" - - if rescue: - if rescue.get('kernel_id'): - guest.os_kernel = os.path.join(FLAGS.instances_path, - instance['name'], - "kernel.rescue") - if rescue.get('ramdisk_id'): - guest.os_initrd = os.path.join(FLAGS.instances_path, - instance['name'], - "ramdisk.rescue") - elif instance['kernel_id']: - guest.os_kernel = os.path.join(FLAGS.instances_path, - instance['name'], - "kernel") - if FLAGS.libvirt_type == "xen": - guest.os_cmdline = "ro" - else: - guest.os_cmdline = "root=%s console=ttyS0" % ( - root_device_name or "/dev/vda",) - if instance['ramdisk_id']: - guest.os_initrd = os.path.join(FLAGS.instances_path, - instance['name'], - "ramdisk") - else: - guest.os_boot_dev = "hd" - - if FLAGS.libvirt_type != "lxc" and FLAGS.libvirt_type != "uml": - guest.acpi = True - - clk = config.LibvirtConfigGuestClock() - clk.offset = "utc" - guest.set_clock(clk) - - if FLAGS.libvirt_type == "kvm": - # TODO(berrange) One day this should be per-guest - # OS type configurable - tmpit = config.LibvirtConfigGuestTimer() - tmpit.name = "pit" - tmpit.tickpolicy = "delay" - - tmrtc = config.LibvirtConfigGuestTimer() - tmrtc.name = "rtc" - tmrtc.tickpolicy = "catchup" - - clk.add_timer(tmpit) - clk.add_timer(tmrtc) - if FLAGS.libvirt_type == "lxc": fs = config.LibvirtConfigGuestFilesys() fs.source_type = "mount" fs.source_dir = os.path.join(FLAGS.instances_path, instance['name'], "rootfs") - guest.add_device(fs) + devices.append(fs) else: if image_meta and image_meta.get('disk_format') == 'iso': root_device_type = 'cdrom' @@ -1675,11 +1586,11 @@ class LibvirtDriver(driver.ComputeDriver): diskrescue = disk_info('disk.rescue', self.default_root_device, device_type=root_device_type) - guest.add_device(diskrescue) + devices.append(diskrescue) diskos = disk_info('disk', self.default_second_device) - guest.add_device(diskos) + devices.append(diskos) else: ebs_root = self._volume_in_mapping(self.default_root_device, block_device_info) @@ -1693,7 +1604,7 @@ class LibvirtDriver(driver.ComputeDriver): root_device, bus, root_device_type) - guest.add_device(diskos) + devices.append(diskos) ephemeral_device = None if not (self._volume_in_mapping(self.default_second_device, @@ -1706,7 +1617,7 @@ class LibvirtDriver(driver.ComputeDriver): if ephemeral_device is not None: disklocal = disk_info('disk.local', ephemeral_device) - guest.add_device(disklocal) + devices.append(disklocal) if ephemeral_device is not None: swap_device = self.default_third_device @@ -1722,19 +1633,19 @@ class LibvirtDriver(driver.ComputeDriver): diskeph = disk_info(_get_eph_disk(eph), block_device.strip_dev( eph['device_name'])) - guest.add_device(diskeph) + devices.append(diskeph) swap = driver.block_device_info_get_swap(block_device_info) if driver.swap_is_usable(swap): diskswap = disk_info('disk.swap', block_device.strip_dev( swap['device_name'])) - guest.add_device(diskswap) + devices.append(diskswap) elif (inst_type['swap'] > 0 and not self._volume_in_mapping(swap_device, block_device_info)): diskswap = disk_info('disk.swap', swap_device) - guest.add_device(diskswap) + devices.append(diskswap) db.instance_update( nova_context.get_admin_context(), instance['uuid'], {'default_swap_device': '/dev/' + swap_device}) @@ -1745,7 +1656,7 @@ class LibvirtDriver(driver.ComputeDriver): cfg = self.volume_driver_method('connect_volume', connection_info, mount_device) - guest.add_device(cfg) + devices.append(cfg) if self._has_config_drive(instance): diskconfig = config.LibvirtConfigGuestDisk() @@ -1757,7 +1668,111 @@ class LibvirtDriver(driver.ComputeDriver): "disk.config") diskconfig.target_dev = self.default_last_device diskconfig.target_bus = default_disk_bus - guest.add_device(diskconfig) + devices.append(diskconfig) + + return devices + + def get_guest_config(self, instance, network_info, image_meta, rescue=None, + block_device_info=None): + """Get config data for parameters. + + :param rescue: optional dictionary that should contain the key + 'ramdisk_id' if a ramdisk is needed for the rescue image and + 'kernel_id' if a kernel is needed for the rescue image. + """ + # FIXME(vish): stick this in db + inst_type_id = instance['instance_type_id'] + inst_type = instance_types.get_instance_type(inst_type_id) + + guest = config.LibvirtConfigGuest() + guest.virt_type = FLAGS.libvirt_type + guest.name = instance['name'] + guest.uuid = instance['uuid'] + guest.memory = inst_type['memory_mb'] * 1024 + guest.vcpus = inst_type['vcpus'] + + guest.cpu = self.get_guest_cpu_config() + + root_device_name = driver.block_device_info_get_root(block_device_info) + if root_device_name: + root_device = block_device.strip_dev(root_device_name) + else: + # NOTE(yamahata): + # for nova.api.ec2.cloud.CloudController.get_metadata() + root_device = self.default_root_device + db.instance_update( + nova_context.get_admin_context(), instance['uuid'], + {'root_device_name': '/dev/' + self.default_root_device}) + + if FLAGS.libvirt_type == "lxc": + guest.os_type = "exe" + guest.os_init_path = "/sbin/init" + guest.os_cmdline = "console=ttyS0" + elif FLAGS.libvirt_type == "uml": + guest.os_type = "uml" + guest.os_kernel = "/usr/bin/linux" + guest.os_root = root_device_name or "/dev/ubda" + else: + if FLAGS.libvirt_type == "xen": + guest.os_type = "linux" + guest.os_root = root_device_name or "/dev/xvda" + else: + guest.os_type = "hvm" + + if rescue: + if rescue.get('kernel_id'): + guest.os_kernel = os.path.join(FLAGS.instances_path, + instance['name'], + "kernel.rescue") + if rescue.get('ramdisk_id'): + guest.os_initrd = os.path.join(FLAGS.instances_path, + instance['name'], + "ramdisk.rescue") + elif instance['kernel_id']: + guest.os_kernel = os.path.join(FLAGS.instances_path, + instance['name'], + "kernel") + if FLAGS.libvirt_type == "xen": + guest.os_cmdline = "ro" + else: + guest.os_cmdline = "root=%s console=ttyS0" % ( + root_device_name or "/dev/vda",) + if instance['ramdisk_id']: + guest.os_initrd = os.path.join(FLAGS.instances_path, + instance['name'], + "ramdisk") + else: + guest.os_boot_dev = "hd" + + if FLAGS.libvirt_type != "lxc" and FLAGS.libvirt_type != "uml": + guest.acpi = True + + clk = config.LibvirtConfigGuestClock() + clk.offset = "utc" + guest.set_clock(clk) + + if FLAGS.libvirt_type == "kvm": + # TODO(berrange) One day this should be per-guest + # OS type configurable + tmpit = config.LibvirtConfigGuestTimer() + tmpit.name = "pit" + tmpit.tickpolicy = "delay" + + tmrtc = config.LibvirtConfigGuestTimer() + tmrtc.name = "rtc" + tmrtc.tickpolicy = "catchup" + + clk.add_timer(tmpit) + clk.add_timer(tmrtc) + + for cfg in self.get_guest_storage_config(instance, + image_meta, + rescue, + block_device_info, + inst_type, + root_device_name, + root_device): + guest.add_device(cfg) for (network, mapping) in network_info: cfg = self.vif_driver.plug(instance, (network, mapping)) |
