Use libguestfs for virtual setup

more_events
Roberto Hueso Gómez 2020-04-17 15:38:32 +02:00
parent 84e02462c0
commit dfb69e9dd5
1 changed files with 12 additions and 11 deletions

View File

@ -192,17 +192,18 @@ class OgVirtualOperations:
if int(part['format']) == 0: if int(part['format']) == 0:
continue continue
drive_path = f'disk{disk}_part{part["partition"]}.qcow2' drive_path = f'{self.OG_PARTITIONS_PATH}/disk{disk}_part{part["partition"]}.qcow2'
sequence = [f'qemu-img create -f qcow2 {drive_path} {part["size"]}K', g = guestfs.GuestFS(python_return_dict=True)
f'qemu-nbd -c /dev/nbd0 {drive_path}', g.disk_create(drive_path, "qcow2", int(part['size']) * 1024)
f'parted /dev/nbd0 mklabel gpt', g.add_drive_opts(drive_path, format="qcow2", readonly=0)
f'parted -a opt /dev/nbd0 mkpart primary {part["filesystem"]} 2048s 100%', g.launch()
f'mkfs.{part["filesystem"].lower()} /dev/nbd0p1', devices = g.list_devices()
f'qemu-nbd -d /dev/nbd0'] assert(len(devices) == 1)
for cmd in sequence: g.part_disk(devices[0], "gpt")
process = subprocess.run([cmd], shell=True) partitions = g.list_partitions()
if process.returncode != 0: assert(len(partitions) == 1)
raise RuntimeError g.mkfs(part["filesystem"].lower(), partitions[0])
g.close()
with open(path, 'r+') as f: with open(path, 'r+') as f:
data = json.loads(f.read()) data = json.loads(f.read())