ansible_gentooimgr/gentooimgr/qemu.py

102 lines
3.1 KiB
Python
Raw Permalink Normal View History

2023-12-21 18:53:25 +00:00
"""Qemu commands to run and handle the image"""
import os
import sys
import argparse
from subprocess import Popen, PIPE
2023-12-26 00:06:43 +00:00
from gentooimgr import LOG
2023-12-21 18:53:25 +00:00
import gentooimgr.config
import gentooimgr.common
def create_image(args, config: dict, overwrite: bool = False) -> str:
"""Creates an image (.img) file using qemu that will be used to create the cloud image
:Parameters:
- config: dictionary/json configuration containing required information
- overwrite: if True, run_image() will call this and re-create.
:Returns:
Full path to image file produced by qemu
"""
image = gentooimgr.common.get_image_name(args, config)
name, ext = os.path.splitext(image)
if os.path.exists(image) and not overwrite:
return os.path.abspath(image)
cmd = ['qemu-img', 'create', '-f', ext[1:], image, str(config.get("imgsize", "12G"))]
proc = Popen(cmd, stderr=PIPE, stdout=PIPE)
stdout, stderr = proc.communicate()
return os.path.abspath(image)
def run_image(
args: argparse.Namespace,
config: dict,
mounts=[]):
"""Handle mounts and run the live cd image
- mount_isos: list of iso paths to mount in qemu as disks.
"""
iso = config.get("iso")
2023-12-26 00:06:43 +00:00
prefix = args.temporary_dir
LOG.info(f"iso from config {iso}")
2023-12-21 18:53:25 +00:00
if iso is None:
iso = gentooimgr.common.find_iso(
os.path.join(
os.path.abspath(os.path.dirname(__file__)),
".."
)
)
2023-12-26 00:06:43 +00:00
LOG.info(f"iso from cwd {iso}")
if not iso:
prefix = config.get('temporary_dir')
iso = gentooimgr.common.find_iso(prefix)
LOG.info(f"iso from {prefix} {iso}")
assert iso, f"iso not found {iso}"
2023-12-21 18:53:25 +00:00
if isinstance(iso, list):
2023-12-21 19:42:13 +00:00
assert len(iso), f"iso list is empty {iso}"
2023-12-21 18:53:25 +00:00
iso = iso[0]
image = gentooimgr.common.get_image_name(args, config)
qmounts = []
mounts.extend(args.mounts)
for i in mounts:
qmounts.append("-drive")
qmounts.append(f"file={i},media=cdrom")
2023-12-26 00:06:43 +00:00
assert image, f"image is empty {image}"
if not os.path.exists(image):
if os.path.exists(os.path.join(prefix, image)):
image = os.path.join(prefix, image)
assert os.path.exists(image), f"image not found {image}"
2023-12-21 18:53:25 +00:00
threads = args.threads
cmd = [
"qemu-system-x86_64",
"-enable-kvm",
"-boot", "d",
"-m", str(config.get("memory", 2048)),
"-smp", str(threads),
"-drive", f"file={image},if=virtio,index=0",
"-cdrom", iso,
"-net", "nic,model=virtio",
"-vga", "virtio",
"-cpu", "kvm64",
"-chardev", "file,id=charserial0,path=gentoo.log",
"-device", "isa-serial,chardev=charserial0,id=serial0",
"-chardev", "pty,id=charserial1",
"-device", "isa-serial,chardev=charserial1,id=serial1"
]
2023-12-26 00:06:43 +00:00
# "-net", "user",
# -net user: network backend 'user' is not compiled into this binary"
2023-12-21 18:53:25 +00:00
cmd += qmounts
2023-12-26 00:06:43 +00:00
LOG.info(cmd)
2023-12-21 18:53:25 +00:00
proc = Popen(cmd, stderr=PIPE, stdout=PIPE)
stdout, stderr = proc.communicate()
if stderr:
2023-12-26 00:06:43 +00:00
LOG.error(str(stderr))
2023-12-21 18:53:25 +00:00