From 134167cd4911ebf8550e870d24a43d98a271b998 Mon Sep 17 00:00:00 2001 From: Oliver Kurth Date: Wed, 24 Apr 2024 00:17:08 +0000 Subject: [PATCH] ova-compose: add raw_image option for disks --- ova-compose/ova-compose.py | 21 ++++++++++++++++----- pytest/configs/raw-image.yaml | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 pytest/configs/raw-image.yaml diff --git a/ova-compose/ova-compose.py b/ova-compose/ova-compose.py index 6053734..61c8a39 100755 --- a/ova-compose/ova-compose.py +++ b/ova-compose/ova-compose.py @@ -572,16 +572,13 @@ class OVFDisk(object): 'byte * 2^40' : 2 ** 40, } - - def __init__(self, path, units=None, disk_id=None, file_id=None): + def __init__(self, path, units=None, disk_id=None, file_id=None, raw_image=None): if disk_id is None: self.id = f"vmdisk{OVFDisk.next_id}" OVFDisk.next_id += 1 else: self.id = disk_id - self.file = OVFFile(path, file_id=file_id) - # units can be unspecified (default: byte), # one of KB, MB, ... # or byte * 2^10, ... with exponents 10-40 @@ -595,6 +592,16 @@ def __init__(self, path, units=None, disk_id=None, file_id=None): assert False, "invalid units used" self.units = units + if raw_image is not None: + if os.path.exists(raw_image): + # check if the vmdk exists, and if it does if it's newer than the raw image + # if not, create vmdk from raw image + if not os.path.exists(path) or os.path.getctime(raw_image) > os.path.getctime(path): + subprocess.check_call(['vmdk-convert', raw_image, path]) + else: + print(f"warning: raw image file {raw_image} does not exist, using {path}") + + self.file = OVFFile(path) disk_info = OVF._disk_info(path) self.capacity = int(disk_info['capacity'] / self.allocation_factors[self.units]) self.used = disk_info['used'] @@ -941,9 +948,13 @@ def from_dict(cls, config): file_id=hw.get('file_id', None)) files.append(file) hw['image'] = file - elif 'disk_image' in hw: + elif 'disk_image' in hw or 'raw_image' in hw: + if 'disk_image' not in hw: + # if vmdk file is unset, use the raw image name and replace the extension + hw['disk_image'] = os.path.splitext(hw['raw_image'])[0] + ".vmdk" disk = OVFDisk(hw['disk_image'], units=hw.get('units', None), + raw_image=hw.get('raw_image', None), disk_id=hw.get('disk_id', None), file_id=hw.get('file_id', None)) disks.append(disk) diff --git a/pytest/configs/raw-image.yaml b/pytest/configs/raw-image.yaml new file mode 100644 index 0000000..b275cec --- /dev/null +++ b/pytest/configs/raw-image.yaml @@ -0,0 +1,34 @@ +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 + raw_image: dummy.img + usb1: + type: usb_controller + ethernet1: + type: ethernet + subtype: VmxNet3 + network: vm_network + videocard1: + type: video_card + vmci1: + type: vmci