Skip to content

Commit

Permalink
Merge pull request #163 from arighi/fix-lint-warnings
Browse files Browse the repository at this point in the history
Fix lint warnings
  • Loading branch information
arighi authored Sep 29, 2024
2 parents 5f50149 + c6bbf87 commit 18d08c7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 68 deletions.
2 changes: 1 addition & 1 deletion virtme/architectures.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,10 @@ def config_base():
def qemu_serial_console_args():
return ["-device", "sclpconsole,chardev=console"]


def img_name(self) -> str:
return "image"


ARCHES = {
arch.virtmename: arch
for arch in [
Expand Down
88 changes: 56 additions & 32 deletions virtme/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def get_kernel_version(img_name, path):
# The version detection fails s390x using file or strings tools, so check
# if the file itself contins the version number.
if img_name:
match = re.search(fr"{img_name}-" + "(\S{3,})", path)
match = re.search(fr"{img_name}-\S{{3,}}", path)
if match:
return match.group(1)

Expand Down Expand Up @@ -662,48 +662,59 @@ def start(self, path, verbose=True):
return True


class VirtioFSConfig:
def __init__(self, path: str, mount_tag: str, guest_tools_path=None, memory=None):
self.path = path
self.mount_tag = mount_tag
self.guest_tools_path = guest_tools_path
self.memory = memory


def export_virtiofs(
arch: architectures.Arch,
qemuargs: List[str],
path: str,
mount_tag: str,
guest_tools_path=None,
memory=None,
config: VirtioFSConfig,
verbose=False,
) -> None:
) -> bool:
if not arch.virtiofs_support():
return False

# Try to start virtiofsd deamon
virtio_fs = VirtioFS(guest_tools_path)
ret = virtio_fs.start(path, verbose)
# Try to start virtiofsd daemon
virtio_fs = VirtioFS(config.guest_tools_path)
ret = virtio_fs.start(config.path, verbose)
if not ret:
return False

# Adjust qemu options to use virtiofsd
fsid = "virtfs%d" % len(qemuargs)

vhost_dev_type = arch.vhost_dev_type()

qemuargs.extend(["-chardev", f"socket,id=char{fsid},path={virtio_fs.sock}"])
qemuargs.extend(["-device", f"{vhost_dev_type},chardev=char{fsid},tag={mount_tag}"])
if memory is None:
memory = "128M"
elif memory == 0:
qemuargs.extend(["-device", f"{vhost_dev_type},chardev=char{fsid},tag={config.mount_tag}"])

memory = config.memory if config.memory is not None else "128M"
if memory == 0:
return True

qemuargs.extend(["-object", f"memory-backend-memfd,id=mem,size={memory},share=on"])
qemuargs.extend(["-numa", "node,memdev=mem"])

return True


class VirtFSConfig:
def __init__(self, path: str, mount_tag: str, security_model="none", readonly=True):
self.path = path
self.mount_tag = mount_tag
self.security_model = security_model
self.readonly = readonly


def export_virtfs(
qemu: qemu_helpers.Qemu,
arch: architectures.Arch,
qemuargs: List[str],
path: str,
mount_tag: str,
security_model="none",
readonly=True,
config: VirtFSConfig,
) -> None:
# NB: We can't use -virtfs for this, because it can't handle a mount_tag
# that isn't a valid QEMU identifier.
Expand All @@ -714,9 +725,9 @@ def export_virtfs(
"local,id=%s,path=%s,security_model=%s%s%s"
% (
fsid,
qemu.quote_optarg(path),
security_model,
",readonly=on" if readonly else "",
qemu.quote_optarg(config.path),
config.security_model,
",readonly=on" if config.readonly else "",
",multidevs=remap" if qemu.has_multidevs else "",
),
]
Expand All @@ -725,7 +736,7 @@ def export_virtfs(
[
"-device",
"%s,fsdev=%s,mount_tag=%s"
% (arch.virtio_dev_type("9p"), fsid, qemu.quote_optarg(mount_tag)),
% (arch.virtio_dev_type("9p"), fsid, qemu.quote_optarg(config.mount_tag)),
]
)

Expand Down Expand Up @@ -936,26 +947,32 @@ def do_it() -> int:
virt_arch = architectures.get("microvm")
else:
virt_arch = arch
use_virtiofs = export_virtiofs(
virt_arch,
qemuargs,
args.root,
"ROOTFS",
virtiofs_config = VirtioFSConfig(
path=args.root,
mount_tag="ROOTFS",
guest_tools_path=guest_tools_path,
# virtiofsd requires a NUMA not, if --numa is specified simply use
# the user-defined NUMA node, otherwise create a NUMA node with all
# the memory.
memory=0 if args.numa else args.memory,
)
use_virtiofs = export_virtiofs(
virt_arch,
qemuargs,
virtiofs_config,
verbose=args.verbose,
)
if can_use_microvm(args) and use_virtiofs:
if args.verbose:
sys.stderr.write("virtme: use 'microvm' QEMU architecture\n")
arch = virt_arch
if not use_virtiofs:
export_virtfs(
qemu, arch, qemuargs, args.root, "/dev/root", readonly=(not args.rw)
virtfs_config = VirtFSConfig(
path=args.root,
mount_tag="/dev/root",
readonly=(not args.rw),
)
export_virtfs(qemu, arch, qemuargs, virtfs_config)

# Use the faster virtme-ng-init if we are running on a native architecture.
if (
Expand All @@ -970,7 +987,11 @@ def do_it() -> int:
if args.root == "/":
initcmds = [f"init={guest_tools_path}/{virtme_init_cmd}"]
else:
export_virtfs(qemu, arch, qemuargs, guest_tools_path, "virtme.guesttools")
virtfs_config = VirtFSConfig(
path=guest_tools_path,
mount_tag="virtme.guesttools",
)
export_virtfs(qemu, arch, qemuargs, virtfs_config)
initcmds = [
"init=/bin/sh",
"--",
Expand Down Expand Up @@ -1037,9 +1058,12 @@ def do_it() -> int:
idx = mount_index
mount_index += 1
tag = "virtme.initmount%d" % idx
export_virtfs(
qemu, arch, qemuargs, hostpath, tag, readonly=(dirtype != "rwdir")
virtfs_config = VirtFSConfig(
path=hostpath,
mount_tag=tag,
readonly=(dirtype != "rwdir"),
)
export_virtfs(qemu, arch, qemuargs, virtfs_config)
kernelargs.append("virtme_initmount%d=%s" % (idx, guestpath))

for i, d in enumerate(args.overlay_rwdir):
Expand Down
89 changes: 54 additions & 35 deletions virtme/cpiowriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@
# as a file called LICENSE with SHA-256 hash:
# 8177f97513213526df2cf6184d8ff986c675afb514d4e68a404010521b880643

class FileMetaData:
def __init__(self, **kwargs):
# Define default values for the metadata
defaults = {
'ino': None,
'nlink': None,
'uid': 0,
'gid': 0,
'mtime': 0,
'devmajor': 0,
'devminor': 0,
'rdevmajor': 0,
'rdevminor': 0
}

# Update defaults with any provided keyword arguments
self.meta_data = {**defaults, **kwargs}

def get(self, key):
return self.meta_data.get(key)

def set(self, key, value):
self.meta_data[key] = value


class CpioWriter:
TYPE_DIR = 0o0040000
Expand All @@ -22,23 +46,13 @@ def __write(self, data):
self.__f.write(data)
self.__totalsize += len(data)

def write_object(
self,
name,
body,
mode,
ino=None,
nlink=None,
uid=0,
gid=0,
mtime=0,
devmajor=0,
devminor=0,
rdevmajor=0,
rdevminor=0,
):
if nlink is None:
nlink = 2 if (mode & CpioWriter.TYPE_MASK) == CpioWriter.TYPE_DIR else 1
def write_object(self, name, body, mode, meta_data=None):
# Set default metadata if not provided
meta_data = meta_data or FileMetaData()

# Ensure nlink is set correctly based on mode
if meta_data.get('nlink') is None:
meta_data.set('nlink', 2 if (mode & CpioWriter.TYPE_MASK) == CpioWriter.TYPE_DIR else 1)

if b"\0" in name:
raise ValueError("Filename cannot contain a NUL")
Expand All @@ -51,25 +65,28 @@ def write_object(
filesize = body.seek(0, 2)
body.seek(0)

if ino is None:
ino = self.__next_ino
# Set default ino if not provided
if meta_data.get('ino') is None:
meta_data.set('ino', self.__next_ino)
self.__next_ino += 1

# Prepare fields list using metadata
fields = [
ino,
meta_data.get('ino'),
mode,
uid,
gid,
nlink,
mtime,
meta_data.get('uid'),
meta_data.get('gid'),
meta_data.get('nlink'),
meta_data.get('mtime'),
filesize,
devmajor,
devminor,
rdevmajor,
rdevminor,
meta_data.get('devmajor'),
meta_data.get('devminor'),
meta_data.get('rdevmajor'),
meta_data.get('rdevminor'),
namesize,
0,
]

hdr = ("070701" + "".join("%08X" % f for f in fields)).encode("ascii")

self.__write(hdr)
Expand All @@ -89,14 +106,14 @@ def write_object(
self.__write(((-filesize) % 4) * b"\0")

def write_trailer(self):
self.write_object(name=b"TRAILER!!!", body=b"", mode=0, ino=0, nlink=1)
self.write_object(name=b"TRAILER!!!", body=b"", mode=0, meta_data=FileMetaData(ino=0, nlink=1))
self.__write(((-self.__totalsize) % 512) * b"\0")

def mkdir(self, name, mode):
self.write_object(name=name, mode=CpioWriter.TYPE_DIR | mode, body=b"")
self.write_object(name=name, body=b"", mode=CpioWriter.TYPE_DIR | mode)

def symlink(self, src, dst):
self.write_object(name=dst, mode=CpioWriter.TYPE_SYMLINK | 0o777, body=src)
self.write_object(name=dst, body=src, mode=CpioWriter.TYPE_SYMLINK | 0o777)

def write_file(self, name, body, mode):
self.write_object(name=name, body=body, mode=CpioWriter.TYPE_REG | mode)
Expand All @@ -105,8 +122,10 @@ def mkchardev(self, name, dev, mode):
major, minor = dev
self.write_object(
name=name,
mode=CpioWriter.TYPE_CHRDEV | mode,
rdevmajor=major,
rdevminor=minor,
body=b"",
)
mode=CpioWriter.TYPE_CHRDEV | mode,
meta_data=FileMetaData(
rdevmajor=major,
rdevminor=minor,
)
)

0 comments on commit 18d08c7

Please sign in to comment.