- initramfs.img, rootfs.img for AArch64 Linux guest - mkdisk.py for disk image creation - initramfs and rootfs directories
80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Generate a virtio-blk disk image for UniversalisOS Linux guest.
|
|
|
|
Creates a raw disk image with the UOSVBLK1 header format that
|
|
virtio-blk can detect and serve to the guest.
|
|
|
|
Usage:
|
|
python3 mkdisk.py <input_file> <output.img> [--size=SIZE]
|
|
|
|
The output image format:
|
|
Offset 0x000: "UOSVBLK1" magic (8 bytes)
|
|
Offset 0x008: data size in bytes (8 bytes, little-endian)
|
|
Offset 0x010: raw sector data (512-byte aligned)
|
|
"""
|
|
import sys
|
|
import os
|
|
import struct
|
|
|
|
MAGIC = b"UOSVBLK1"
|
|
SECTOR_SIZE = 512
|
|
|
|
def create_disk_image(input_path, output_path, target_size=None):
|
|
"""Create a virtio-blk disk image from an input file."""
|
|
|
|
# Read input file
|
|
with open(input_path, 'rb') as f:
|
|
data = f.read()
|
|
|
|
# Pad to sector boundary
|
|
if len(data) % SECTOR_SIZE != 0:
|
|
data += b'\x00' * (SECTOR_SIZE - (len(data) % SECTOR_SIZE))
|
|
|
|
# Pad to target size if specified
|
|
if target_size and target_size > len(data):
|
|
data += b'\x00' * (target_size - len(data))
|
|
|
|
# Create image with header
|
|
header = MAGIC + struct.pack('<Q', len(data)) # magic + size (LE 64-bit)
|
|
|
|
# Pad header to sector boundary
|
|
if len(header) % SECTOR_SIZE != 0:
|
|
header += b'\x00' * (SECTOR_SIZE - len(header))
|
|
|
|
image = header + data
|
|
|
|
# Write output
|
|
with open(output_path, 'wb') as f:
|
|
f.write(image)
|
|
|
|
print(f"Disk image created: {output_path}")
|
|
print(f" Input: {input_path} ({len(data)} bytes)")
|
|
print(f" Header: {len(header)} bytes")
|
|
print(f" Total: {len(image)} bytes ({len(image) // 1024} KB)")
|
|
print(f" Sectors: {len(image) // SECTOR_SIZE}")
|
|
|
|
def main():
|
|
if len(sys.argv) < 3:
|
|
print(f"Usage: {sys.argv[0]} <input> <output.img> [--size=SIZE]")
|
|
print(f" input: input file (initramfs, rootfs, etc.)")
|
|
print(f" output: output disk image")
|
|
print(f" --size: target size in bytes (optional, pads with zeros)")
|
|
sys.exit(1)
|
|
|
|
input_path = sys.argv[1]
|
|
output_path = sys.argv[2]
|
|
target_size = None
|
|
|
|
for arg in sys.argv[3:]:
|
|
if arg.startswith('--size='):
|
|
target_size = int(arg.split('=')[1])
|
|
|
|
if not os.path.exists(input_path):
|
|
print(f"Error: input file not found: {input_path}")
|
|
sys.exit(1)
|
|
|
|
create_disk_image(input_path, output_path, target_size)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|