COT.commands.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.commands.command.ReadWriteCommand
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 command with the given UI.
Parameters: ui (UI) – User interface instance.
-
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 command.
Raises: InvalidInputError
– ifready_to_run()
reportsFalse
-
address
¶ Disk device address on controller (
1 -- 0
, etc.).Raises: InvalidInputError
– seevalidate_controller_address()
-
controller
¶ Disk controller type (
ide
,scsi
).Raises: InvalidInputError
– seevalidate_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
, anddisk_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
(platform, ctrl_item, drive_type)[source]¶ If a controller type wasn’t specified, try to guess from context.
Parameters: Returns: str – ‘ide’ or ‘scsi’
Raises: ValueUnsupportedError
– ifctrl_item
is not None but is also not an IDE or SCSI controller device.Examples
>>> from COT.platforms import Platform >>> guess_controller_type(Platform(), None, 'harddisk') 'ide'
-
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.Examples
>>> guess_drive_type_from_extension('/foo/bar.vmdk') 'harddisk' >>> guess_drive_type_from_extension('baz.vmdk.iso') 'cdrom' >>> try: ... guess_drive_type_from_extension('/etc/os-release') ... raise AssertionError("no exception raised") ... except InvalidInputError as e: ... print(e) Unable to guess disk drive type for file '/etc/os-release' from its extension ''. Known extensions are ['.img', '.iso', '.qcow2', '.raw', '.vmdk'] Please specify '--type harddisk' or '--type cdrom'.
-
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:
- Check whether the DISK_IMAGE file name matches an existing File in the OVF (and from there, find the associated Disk and Items)
- Check whether the file-id matches an existing File and/or Disk in the OVF (and from there, find the associated Items)
- 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: Raises: InvalidInputError
– if the address/controller combo is invalid.Examples
>>> validate_controller_address("ide", "0:0") >>> try: ... validate_controller_address("ide", "1:3") ... except InvalidInputError as e: ... print(e) IDE disk address must be between 0:0 and 1:1 >>> validate_controller_address("scsi", "1:3") >>> try: ... validate_controller_address("scsi", "4:0") ... except InvalidInputError as e: ... print(e) SCSI disk address must be between 0:0 and 3:15
-
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”)