Skip to content

Commit

Permalink
Merge pull request #49 from vmware/topic/okurth/disk-allocation-units
Browse files Browse the repository at this point in the history
add 'units' option for disk images
  • Loading branch information
oliverkurth authored Mar 7, 2024
2 parents b5a8a24 + 62e2f31 commit 68acac8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
47 changes: 35 additions & 12 deletions ova-compose/ova-compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,14 +554,42 @@ def xml_item(self):
class OVFDisk(object):
next_id = 0

allocation_units_map = {
'byte' : "byte",
'KB' : "byte * 2^10",
'MB' : "byte * 2^20",
'GB' : "byte * 2^30",
'TB' : "byte * 2^40",
}

def __init__(self, path):
allocation_factors = {
'byte' : 1,
'byte * 2^10' : 2 ** 10,
'byte * 2^20' : 2 ** 20,
'byte * 2^30' : 2 ** 30,
'byte * 2^40' : 2 ** 40,
}

def __init__(self, path, units=None):
self.id = f"vmdisk{OVFDisk.next_id}"
OVFDisk.next_id += 1
self.file = OVFFile(path)

# units can be unspecified (default: byte),
# one of KB, MB, ...
# or byte * 2^10, ... with exponents 10-40
if units is None:
units = "byte"
elif units in self.allocation_units_map:
units = self.allocation_units_map[units]
elif units in self.allocation_factors:
pass
else:
assert False, "invalid units used"
self.units = units

disk_info = OVF._disk_info(path)
self.capacity = disk_info['capacity']
self.capacity = int(disk_info['capacity'] / self.allocation_factors[self.units])
self.used = disk_info['used']


Expand All @@ -572,7 +600,7 @@ def host_resource(self):
def xml_item(self):
return ET.Element('{%s}Disk' % NS_OVF, {
'{%s}capacity' % NS_OVF: str(self.capacity),
'{%s}capacityAllocationUnits' % NS_OVF: 'byte',
'{%s}capacityAllocationUnits' % NS_OVF: self.units,
'{%s}diskId' % NS_OVF: self.id,
'{%s}fileRef' % NS_OVF: self.file.id,
'{%s}populatedSize' % NS_OVF: str(self.used),
Expand All @@ -581,18 +609,13 @@ def xml_item(self):


class OVFEmptyDisk(OVFDisk):

def __init__(self, capacity, units="MB"):
self.id = f"vmdisk{OVFDisk.next_id}"
OVFDisk.next_id += 1
self.capacity = capacity
if units == "KB":
units = "byte * 2^10"
elif units == "MB":
units = "byte * 2^20"
elif units == "GB":
units = "byte * 2^30"
elif units == "TB":
units = "byte * 2^30"
if units in self.allocation_units_map:
units = self.allocation_units_map[units]
self.units = units


Expand Down Expand Up @@ -908,7 +931,7 @@ def from_dict(cls, config):
files.append(file)
hw['image'] = file
elif 'disk_image' in hw:
disk = OVFDisk(hw['disk_image'])
disk = OVFDisk(hw['disk_image'], units=hw.get('units', None))
disks.append(disk)
files.append(disk.file)
hw['disk'] = disk
Expand Down
35 changes: 35 additions & 0 deletions pytest/configs/allocation_units.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
system:
name: minimal
type: vmx-14 vmx-20
os_vmw: vmw.vmwarePhoton64Guest

networks:
vm_network:
name: "None"
description: "The None network"

hardware:
cpus: 2
memory:
type: memory
size: 4096
sata1:
type: sata_controller
cdrom1:
type: cd_drive
parent: sata1
rootdisk:
type: hard_disk
parent: sata1
units: KB
disk_image: dummy.vmdk
usb1:
type: usb_controller
ethernet1:
type: ethernet
subtype: VmxNet3
network: vm_network
videocard1:
type: video_card
vmci1:
type: vmci

0 comments on commit 68acac8

Please sign in to comment.