COT.add_disk module

Module for adding disks to VMs.

Functions

add_disk_worker Worker function for actually adding the disk.
confirm_elements Get user confirmation of any risky or unusual operations.
guess_controller_type If a controller type wasn’t specified, try to guess from context.
guess_drive_type_from_extension Guess the disk type (harddisk/cdrom) from the disk file name.
search_for_elements Search for a unique set of objects based on the given criteria.
validate_elements Validate any existing file, disk, controller item, and disk item.
validate_controller_address Check validity of the given address string for the given controller.

Classes

COTAddDisk Add or replace a disk in a virtual machine.
class COTAddDisk(ui)[source]

Bases: COT.submodule.COTSubmodule

Add or replace a disk in a virtual machine.

Inherited attributes: ui, package, output

Attributes: disk_image, drive_type, file_id, controller, subtype, address, diskname, description

__init__(ui)[source]

Instantiate this submodule with the given UI.

Parameters:ui (UI) – User interface instance.
create_subparser()[source]

Create ‘add-disk’ CLI subparser.

ready_to_run()[source]

Check whether the module is ready to run().

Returns:tuple(True, ready_message) or (False, reason_why_not)
run()[source]

Do the actual work of this submodule.

Raises:InvalidInputError – if ready_to_run() reports False
address

Disk device address on controller (1:0, etc.).

Raises:InvalidInputError – see validate_controller_address()
controller

Disk controller type (ide, scsi).

Raises:InvalidInputError – see validate_controller_address()
description = None

Description of the disk.

disk_image

Disk image file to add to the VM.

Raises:InvalidInputError – if the file does not exist.
diskname = None

Name string for the disk.

drive_type = None

Disk drive type (‘harddisk’ or ‘cdrom’).

file_id = None

File identifier to map disk to file.

subtype = None

Controller subtype, such as “virtio”.

add_disk_worker(vm, ui, disk_image, drive_type=None, file_id=None, controller=None, subtype=None, address=None, diskname=None, description=None)[source]

Worker function for actually adding the disk.

All parameters except vm, ui, and disk_image are optional and will be automatically determined by COT if unspecified.

Parameters:
  • vm (VMDescription) – The virtual machine being edited.
  • ui (UI) – User interface in effect.
  • disk_image (DiskRepresentation) – Disk image to add to the VM.
  • drive_type (str) – Disk drive type: 'cdrom' or 'harddisk'. If not specified, will be derived automatically from the disk_image file name extension.
  • file_id (str) – Identifier of the disk file in the VM. If not specified, the VM will automatically derive an appropriate value.
  • controller (str) – Disk controller type: 'ide' or 'scsi'. If not specified, will be derived from the type and the platform of the given vm.
  • subtype (str) – Controller subtype (‘virtio’, ‘lsilogic’, etc.)
  • address (str) – Disk device address on its controller (such as '1:0'). If this matches an existing disk device, that device will be overwritten. If not specified, the first available address not already occupied by an existing device will be selected.
  • diskname (str) – Name for disk device
  • description (str) – Description of disk device
confirm_elements(vm, ui, file_obj, disk_image, disk_obj, disk_item, drive_type, controller, ctrl_item, subtype)[source]

Get user confirmation of any risky or unusual operations.

Parameters:
  • vm (VMDescription) – Virtual machine object
  • ui (UI) – User interface object
  • file_obj (object) – Known file object
  • disk_image (str) – Filename or path for disk file
  • disk_obj (object) – Known disk object
  • disk_item (object) – Known disk device object
  • drive_type (str) – “harddisk” or “cdrom”
  • controller (str) – Controller type (“ide” or “scsi”)
  • ctrl_item (object) – Known controller device object
  • subtype (str) – Controller subtype (such as “virtio”)
guess_controller_type(vm, ctrl_item, drive_type)[source]

If a controller type wasn’t specified, try to guess from context.

Parameters:
  • vm (VMDescription) – Virtual machine object
  • ctrl_item (object) – Any known controller object
  • drive_type (str) – “cdrom” or “harddisk”
Returns:

str – ‘ide’ or ‘scsi’

Raises:

ValueUnsupportedError – if ctrl_item is not an IDE or SCSI controller device.

guess_drive_type_from_extension(disk_file_name)[source]

Guess the disk type (harddisk/cdrom) from the disk file name.

Parameters:disk_file_name (str) – File name or file path.
Returns:str – “cdrom” or “harddisk”
Raises:InvalidInputError – if the disk type cannot be guessed.
search_for_elements(vm, disk_file, file_id, controller, address)[source]

Search for a unique set of objects based on the given criteria.

A disk is defined by up to four different sections in the OVF:

  • File (references the actual disk image file)
  • Disk (references the File, only used for HD not CD-ROM)
  • Item (defines the SCSI/IDE controller)
  • Item (defines the disk drive, links to controller and File or Disk)

For each of these four sections, we need to know whether to add a new one or overwrite an existing one. Depending on the user arguments, we can do this by as many as three different approaches:

  1. Check whether the DISK_IMAGE file name matches an existing File in the OVF (and from there, find the associated Disk and Items)
  2. Check whether the file-id matches an existing File and/or Disk in the OVF (and from there, find the associated Items)
  3. Check whether controller type and/or device address match existing Items in the OVF (and from there, find the associated Disk and/or File)

Where it gets extra fun is if the user has specified more than one of the above arguments - in which case we need to make sure that all relevant approaches agree on what sections we’re talking about...

Parameters:
  • vm (VMDescription) – Virtual machine object
  • disk_file (str) – Disk file name or path
  • file_id (str) – File identifier
  • controller (str) – controller type, “ide” or “scsi”
  • address (str) – device address, such as “1:0”
Raises:

ValueMismatchError – if the criteria select a non-unique set.

Returns:

tuple – (file_object, disk_object, controller_item, disk_item)

validate_controller_address(controller, address)[source]

Check validity of the given address string for the given controller.

Helper method for the controller/address setters.

Parameters:
  • controller (str) – 'ide' or 'scsi'
  • address (str) – A string like ‘0:0’ or ‘2:10’
Raises:

InvalidInputError – if the address/controller combo is invalid.

validate_elements(vm, file_obj, disk_obj, disk_item, ctrl_item, file_id, ctrl_type)[source]

Validate any existing file, disk, controller item, and disk item.

Raises:

ValueMismatchError – if the search criteria select a non-unique set.

Parameters:
  • vm (VMDescription) – Virtual machine object
  • file_obj (object) – Known file object
  • disk_obj (object) – Known disk object
  • disk_item (object) – Known disk device object
  • ctrl_item (object) – Known controller device object
  • file_id (str) – File identifier string
  • ctrl_type (str) – Controller type (“ide” or “scsi”)