The nova.virt.driver Module

Driver base-classes:

(Beginning of) the contract that compute drivers must follow, and shared types that support that contract
class ComputeDriver(virtapi)

Bases: object

Base class for compute drivers.

The interface to this class talks in terms of ‘instances’ (Amazon EC2 and internal Nova terminology), by which we mean ‘running virtual machine’ (XenAPI terminology) or domain (Xen or libvirt terminology).

An instance has an ID, which is the identifier chosen by Nova to represent the instance further up the stack. This is unfortunately also called a ‘name’ elsewhere. As far as this layer is concerned, ‘instance ID’ and ‘instance name’ are synonyms.

Note that the instance ID or name is not human-readable or customer-controlled – it’s an internal ID chosen by Nova. At the nova.virt layer, instances do not have human-readable names at all – such things are only known higher up the stack.

Most virtualization platforms will also have their own identity schemes, to uniquely identify a VM or domain. These IDs must stay internal to the platform-specific layer, and never escape the connection interface. The platform-specific layer is responsible for keeping track of which instance ID maps to which platform-specific ID, and vice versa.

Some methods here take an instance of nova.compute.service.Instance. This is the data structure used by nova.compute to store details regarding an instance, and pass them into this layer. This layer is responsible for translating that generic data structure into terms that are specific to the virtualization platform.

add_to_aggregate(context, aggregate, host, **kwargs)

Add a compute host to an aggregate.

attach_interface(instance, image_meta, network_info)

Attach an interface to the instance.

attach_volume(connection_info, instance, mountpoint)

Attach the disk to the instance at mountpoint using info.

block_stats(instance_name, disk_id)

Return performance counters associated with the given disk_id on the given instance_name. These are returned as [rd_req, rd_bytes, wr_req, wr_bytes, errs], where rd indicates read, wr indicates write, req is the total number of I/O requests made, bytes is the total number of bytes transferred, and errs is the number of requests held up due to a full pipeline.

All counters are long integers.

This method is optional. On some platforms (e.g. XenAPI) performance statistics can be retrieved directly in aggregate form, without Nova having to do the aggregation. On those platforms, this method is unused.

Note that this function takes an instance ID.

capabilities = {'supports_recreate': False, 'has_imagecache': False}
change_instance_metadata(context, instance, diff)

Applies a diff to the instance metadata.

This is an optional driver method which is used to publish changes to the instance’s metadata to the hypervisor. If the hypervisor has no means of publishing the instance metadata to the instance, then this method should not be implemented.

check_can_live_migrate_destination(ctxt, instance_ref, src_compute_info, dst_compute_info, block_migration=False, disk_over_commit=False)

Check if it is possible to execute live migration.

This runs checks on the destination host, and then calls back to the source host to check the results.

Parameters:
  • ctxt – security context
  • instance_ref – nova.db.sqlalchemy.models.Instance
  • src_compute_info – Info about the sending machine
  • dst_compute_info – Info about the receiving machine
  • block_migration – if true, prepare for block migration
  • disk_over_commit – if true, allow disk over commit
check_can_live_migrate_destination_cleanup(ctxt, dest_check_data)

Do required cleanup on dest host after check_can_live_migrate calls

Parameters:
  • ctxt – security context
  • dest_check_data – result of check_can_live_migrate_destination
check_can_live_migrate_source(ctxt, instance_ref, dest_check_data)

Check if it is possible to execute live migration.

This checks if the live migration can succeed, based on the results from check_can_live_migrate_destination.

Parameters:
  • context – security context
  • instance_ref – nova.db.sqlalchemy.models.Instance
  • dest_check_data – result of check_can_live_migrate_destination
confirm_migration(migration, instance, network_info)

Confirms a resize, destroying the source VM.

destroy(instance, network_info, block_device_info=None, destroy_disks=True)

Destroy (shutdown and delete) the specified instance.

If the instance is not found (for example if networking failed), this function should still succeed. It’s probably a good idea to log a warning in that case.

Parameters:
  • instance – Instance object as returned by DB layer.
  • network_infoget_instance_nw_info()
  • block_device_info – Information about block devices that should be detached from the instance.
  • destroy_disks – Indicates if disks should be destroyed
detach_interface(instance, network_info)

Detach an interface from the instance.

detach_volume(connection_info, instance, mountpoint)

Detach the disk attached to the instance.

emit_event(event)

Dispatches an event to the compute manager.

Invokes the event callback registered by the compute manager to dispatch the event. This must only be invoked from a green thread.

ensure_filtering_rules_for_instance(instance_ref, network_info)

Setting up filtering rules and waiting for its completion.

To migrate an instance, filtering rules to hypervisors and firewalls are inevitable on destination host. ( Waiting only for filtering rules to hypervisor, since filtering rules to firewall rules can be set faster).

Concretely, the below method must be called. - setup_basic_filtering (for nova-basic, etc.) - prepare_instance_filter(for nova-instance-instance-xxx, etc.)

to_xml may have to be called since it defines PROJNET, PROJMASK. but libvirt migrates those value through migrateToURI(), so , no need to be called.

Don’t use thread for this method since migration should not be started when setting-up filtering rules operations are not completed.

Params instance_ref:
 nova.db.sqlalchemy.models.Instance object
filter_defer_apply_off()

Turn off deferral of IPTables rules and apply the rules now.

filter_defer_apply_on()

Defer application of IPTables rules.

finish_migration(context, migration, instance, disk_info, network_info, image_meta, resize_instance, block_device_info=None)

Completes a resize, turning on the migrated instance

Parameters:
  • network_infoget_instance_nw_info()
  • image_meta – image object returned by nova.image.glance that defines the image from which this instance was created
finish_revert_migration(instance, network_info, block_device_info=None)

Finish reverting a resize, powering back on the instance.

get_all_bw_counters(instances)

Return bandwidth usage counters for each interface on each running VM

get_all_volume_usage(context, compute_host_bdms)

Return usage info for volumes attached to vms on a given host

get_available_nodes()

Returns nodenames of all nodes managed by the compute service.

This method is for multi compute-nodes support. If a driver supports multi compute-nodes, this method returns a list of nodenames managed by the service. Otherwise, this method should return [hypervisor_hostname].

get_available_resource(nodename)

Retrieve resource information.

This method is called when nova-compute launches, and as part of a periodic task

Parameters:nodename – node which the caller want to get resources from a driver that manages only one node can safely ignore this
Returns:Dictionary describing resources
get_console_output(instance)
get_console_pool_info(console_type)
get_diagnostics(instance)

Return data about VM diagnostics.

get_host_ip_addr()

Retrieves the IP address of the dom0

get_host_stats(refresh=False)

Return currently known host stats.

get_host_uptime(host)

Returns the result of calling “uptime” on the target host.

get_info(instance)

Get the current status of an instance, by name (not ID!)

Returns a dict containing:

State :the running state, one of the power_state codes
Max_mem :(int) the maximum memory in KBytes allowed
Mem :(int) the memory in KBytes used by the domain
Num_cpu :(int) the number of virtual CPUs for the domain
Cpu_time :(int) the CPU time used in nanoseconds
get_num_instances()

Return the total number of virtual machines.

Return the number of virtual machines that the hypervisor knows about.

Note

This implementation works for all drivers, but it is not particularly efficient. Maintainers of the virt drivers are encouraged to override this method with something more efficient.

get_per_instance_usage()

Get information about instance resource usage.

Returns:dict of nova uuid => dict of usage info
get_spice_console(instance)
get_vnc_console(instance)
get_volume_connector(instance)

Get connector information for the instance for attaching to volumes.

Connector information is a dictionary representing the ip of the machine that will be making the connection, the name of the iscsi initiator and the hostname of the machine as follows:

{
    'ip': ip,
    'initiator': initiator,
    'host': hostname
}
host_maintenance_mode(host, mode)

Start/Stop host maintenance window. On start, it triggers guest VMs evacuation.

host_power_action(host, action)

Reboots, shuts down or powers up the host.

init_host(host)

Initialize anything that is necessary for the driver to function, including catching up with currently running VM’s on the given host.

inject_file(instance, b64_path, b64_contents)

Writes a file on the specified instance.

The first parameter is an instance of nova.compute.service.Instance, and so the instance is being specified as instance.name. The second parameter is the base64-encoded path to which the file is to be written on the instance; the third is the contents of the file, also base64-encoded.

inject_network_info(instance, nw_info)

inject network info for specified instance.

instance_exists(instance_id)

Checks existence of an instance on the host.

Parameters:instance_id – The ID / name of the instance to lookup

Returns True if an instance with the supplied ID exists on the host, False otherwise.

Note

This implementation works for all drivers, but it is not particularly efficient. Maintainers of the virt drivers are encouraged to override this method with something more efficient.

instance_on_disk(instance)

Checks access of instance files on the host.

Parameters:instance – instance to lookup

Returns True if files of an instance with the supplied ID accessible on the host, False otherwise.

Note

Used in rebuild for HA implementation and required for validation of access to instance shared disk files

interface_stats(instance_name, iface_id)

Return performance counters associated with the given iface_id on the given instance_id. These are returned as [rx_bytes, rx_packets, rx_errs, rx_drop, tx_bytes, tx_packets, tx_errs, tx_drop], where rx indicates receive, tx indicates transmit, bytes and packets indicate the total number of bytes or packets transferred, and errs and dropped is the total number of packets failed / dropped.

All counters are long integers.

This method is optional. On some platforms (e.g. XenAPI) performance statistics can be retrieved directly in aggregate form, without Nova having to do the aggregation. On those platforms, this method is unused.

Note that this function takes an instance ID.

legacy_nwinfo()

True if the driver requires the legacy network_info format.

list_instance_uuids()

Return the UUIDS of all the instances known to the virtualization layer, as a list.

list_instances()

Return the names of all the instances known to the virtualization layer, as a list.

live_migration(ctxt, instance_ref, dest, post_method, recover_method, block_migration=False, migrate_data=None)

Live migration of an instance to another host.

Params ctxt:security context
Params instance_ref:
 nova.db.sqlalchemy.models.Instance object instance object that is migrated.
Params dest:destination host
Params post_method:
 post operation method. expected nova.compute.manager.post_live_migration.
Params recover_method:
 recovery method when any exception occurs. expected nova.compute.manager.recover_live_migration.
Params block_migration:
 if true, migrate VM disk.
Params migrate_data:
 implementation specific params.
macs_for_instance(instance)

What MAC addresses must this instance have?

Some hypervisors (such as bare metal) cannot do freeform virtualisation of MAC addresses. This method allows drivers to return a set of MAC addresses that the instance is to have. allocate_for_instance will take this into consideration when provisioning networking for the instance.

Mapping of MAC addresses to actual networks (or permitting them to be freeform) is up to the network implementation layer. For instance, with openflow switches, fixed MAC addresses can still be virtualised onto any L2 domain, with arbitrary VLANs etc, but regular switches require pre-configured MAC->network mappings that will match the actual configuration.

Most hypervisors can use the default implementation which returns None. Hypervisors with MAC limits should return a set of MAC addresses, which will be supplied to the allocate_for_instance call by the compute manager, and it is up to that call to ensure that all assigned network details are compatible with the set of MAC addresses.

This is called during spawn_instance by the compute manager.

Returns:None, or a set of MAC ids (e.g. set([‘12:34:56:78:90:ab’])). None means ‘no constraints’, a set means ‘these and only these MAC addresses’.
manage_image_cache(context, all_instances)

Manage the driver’s local image cache.

Some drivers chose to cache images for instances on disk. This method is an opportunity to do management of that cache which isn’t directly related to other calls into the driver. The prime example is to clean the cache and remove images which are no longer of interest.

migrate_disk_and_power_off(context, instance, dest, instance_type, network_info, block_device_info=None)

Transfers the disk of a running instance in multiple phases, turning off the instance before the end.

pause(instance)

Pause the specified instance.

plug_vifs(instance, network_info)

Plug VIFs into networks.

poll_rebooting_instances(timeout, instances)

Poll for rebooting instances

Parameters:
  • timeout – the currently configured timeout for considering rebooting instances to be stuck
  • instances – instances that have been in rebooting state longer than the configured timeout
post_live_migration_at_destination(ctxt, instance_ref, network_info, block_migration=False, block_device_info=None)

Post operation of live migration at destination host.

Parameters:
  • ctxt – security context
  • instance_ref – instance object that is migrated
  • network_info – instance network information
  • block_migration – if true, post operation of block_migration.
power_off(instance)

Power off the specified instance.

power_on(context, instance, network_info, block_device_info=None)

Power on the specified instance.

pre_block_migration(ctxt, instance_ref, disk_info)

Prepare a block device for migration

Parameters:
  • ctxt – security context
  • instance_ref – instance object that will have its disk migrated
  • disk_info – information about disk to be migrated (as returned from get_instance_disk_info())
pre_live_migration(ctxt, instance_ref, block_device_info, network_info, migrate_data=None)

Prepare an instance for live migration

Parameters:
  • ctxt – security context
  • instance_ref – instance object that will be migrated
  • block_device_info – instance block device information
  • network_info – instance network information
  • migrate_data – implementation specific data dict.
reboot(context, instance, network_info, reboot_type, block_device_info=None, bad_volumes_callback=None)

Reboot the specified instance.

After this is called successfully, the instance’s state goes back to power_state.RUNNING. The virtualization platform should ensure that the reboot action has completed successfully even in cases in which the underlying domain/vm is paused or halted/stopped.

Parameters:
  • instance – Instance object as returned by DB layer.
  • network_infoget_instance_nw_info()
  • reboot_type – Either a HARD or SOFT reboot
  • block_device_info – Info pertaining to attached volumes
  • bad_volumes_callback – Function to handle any bad volumes encountered
refresh_provider_fw_rules()

This triggers a firewall update based on database changes.

When this is called, rules have either been added or removed from the datastore. You can retrieve rules with nova.db.provider_fw_rule_get_all().

Provider rules take precedence over security group rules. If an IP would be allowed by a security group ingress rule, but blocked by a provider rule, then packets from the IP are dropped. This includes intra-project traffic in the case of the allow_project_net_traffic flag for the libvirt-derived classes.

refresh_security_group_members(security_group_id)

This method is called when a security group is added to an instance.

This message is sent to the virtualization drivers on hosts that are running an instance that belongs to a security group that has a rule that references the security group identified by security_group_id. It is the responsibility of this method to make sure any rules that authorize traffic flow with members of the security group are updated and any new members can communicate, and any removed members cannot.

Scenario:
  • we are running on host ‘H0’ and we have an instance ‘i-0’.
  • instance ‘i-0’ is a member of security group ‘speaks-b’
  • group ‘speaks-b’ has an ingress rule that authorizes group ‘b’
  • another host ‘H1’ runs an instance ‘i-1’
  • instance ‘i-1’ is a member of security group ‘b’

When ‘i-1’ launches or terminates we will receive the message to update members of group ‘b’, at which time we will make any changes needed to the rules for instance ‘i-0’ to allow or deny traffic coming from ‘i-1’, depending on if it is being added or removed from the group.

In this scenario, ‘i-1’ could just as easily have been running on our host ‘H0’ and this method would still have been called. The point was that this method isn’t called on the host where instances of that group are running (as is the case with refresh_security_group_rules()) but is called where references are made to authorizing those instances.

An error should be raised if the operation cannot complete.

refresh_security_group_rules(security_group_id)

This method is called after a change to security groups.

All security groups and their associated rules live in the datastore, and calling this method should apply the updated rules to instances running the specified security group.

An error should be raised if the operation cannot complete.

register_event_listener(callback)

Register a callback to receive events.

Register a callback to receive asynchronous event notifications from hypervisors. The callback will be invoked with a single parameter, which will be an instance of the nova.virt.event.Event class.

remove_from_aggregate(context, aggregate, host, **kwargs)

Remove a compute host from an aggregate.

rescue(context, instance, network_info, image_meta, rescue_password)

Rescue the specified instance.

reset_network(instance)

reset networking for specified instance.

restore(instance)

Restore the specified instance.

resume(instance, network_info, block_device_info=None)

resume the specified instance.

resume_state_on_host_boot(context, instance, network_info, block_device_info=None)

resume guest state when a host is booted.

set_admin_password(context, instance_id, new_pass=None)

Set the root password on the specified instance.

The first parameter is an instance of nova.compute.service.Instance, and so the instance is being specified as instance.name. The second parameter is the value of the new password.

set_host_enabled(host, enabled)

Sets the specified host’s ability to accept new instances.

snapshot(context, instance, image_id, update_task_state)

Snapshots the specified instance.

Parameters:
  • context – security context
  • instance – Instance object as returned by DB layer.
  • image_id – Reference to a pre-created image that will hold the snapshot.
soft_delete(instance)

Soft delete the specified instance.

spawn(context, instance, image_meta, injected_files, admin_password, network_info=None, block_device_info=None)

Create a new instance/VM/domain on the virtualization platform.

Once this successfully completes, the instance should be running (power_state.RUNNING).

If this fails, any partial instance should be completely cleaned up, and the virtualization platform should be in the state that it was before this call began.

Parameters:
  • context – security context
  • instance – Instance object as returned by DB layer. This function should use the data there to guide the creation of the new instance.
  • image_meta – image object returned by nova.image.glance that defines the image from which to boot this instance
  • injected_files – User files to inject into instance.
  • admin_password – Administrator password to set in instance.
  • network_infoget_instance_nw_info()
  • block_device_info – Information about block devices to be attached to the instance.
suspend(instance)

suspend the specified instance.

undo_aggregate_operation(context, op, aggregate, host, set_error=True)

Undo for Resource Pools.

unfilter_instance(instance, network_info)

Stop filtering instance.

unpause(instance)

Unpause paused VM instance.

unplug_vifs(instance, network_info)

Unplug VIFs from networks.

unrescue(instance, network_info)

Unrescue the specified instance.

block_device_info_get_ephemerals(block_device_info)
block_device_info_get_mapping(block_device_info)
block_device_info_get_root(block_device_info)
block_device_info_get_swap(block_device_info)
compute_driver_matches(match)
driver_dict_from_config(named_driver_config, *args, **kwargs)
load_compute_driver(virtapi, compute_driver=None)

Load a compute driver module.

Load the compute driver module specified by the compute_driver configuration option or, if supplied, the driver name supplied as an argument.

Compute drivers constructors take a VirtAPI object as their first object and this must be supplied.

Parameters:
  • virtapi – a VirtAPI instance
  • compute_driver – a compute driver name to override the config opt
Returns:

a ComputeDriver instance

swap_is_usable(swap)

Previous topic

The nova.virt.disk.vfs.localfs Module

Next topic

The nova.virt.event Module

This Page