Skip to content

Commit

Permalink
Merge pull request #57 from vmware/topic/okurth/convert-raw-disk-images
Browse files Browse the repository at this point in the history
ova-compose: add raw_image option for disks
  • Loading branch information
oliverkurth committed Apr 29, 2024
2 parents c4e8c5d + 134167c commit a33bbe9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
21 changes: 16 additions & 5 deletions ova-compose/ova-compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions pytest/configs/raw-image.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a33bbe9

Please sign in to comment.