Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions virtme/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ def make_parser() -> argparse.ArgumentParser:
help='Show the VM command line')
g.add_argument('--save-initramfs', action='store',
help='Save the generated initramfs to the specified path')
g.add_argument('--initramfs-compression', action='store', default='none',
choices=['none', 'gz', 'lz4', 'xz', 'zstd'],
help='Compression method to use for initramfs')
g.add_argument('--show-boot-console', action='store_true',
help='Show the boot console when running scripts')

Expand Down Expand Up @@ -272,6 +275,14 @@ def sanitize_disk_args(func: str, arg: str) -> Tuple[str, str]:

return name, fn

def initramfs_compress_cmd(method: str) -> List[str]:
if method == 'none': return ['cat']
if method == 'gz': return ['gzip', '-n', '-9', '-f']
if method == 'lz4': return ['lz4', '-l', '-9', '-f']
if method == 'xz': return ['xz', '--check=crc32', '--lzma2=dict=1MiB']
if method == 'zstd': return ['zstd', '-19']
arg_fail("Unsupported initramfs compression method %s" % (method))

# Allowed characters in mount paths. We can extend this over time if needed.
_SAFE_PATH_PATTERN = '[a-zA-Z0-9_+ /.-]+'
_RWDIR_RE = re.compile('^(%s)(?:=(%s))?$' %
Expand Down Expand Up @@ -551,9 +562,15 @@ def do_script(shellcmd: str, use_exec=False, show_boot_console=False) -> None:
else:
initramfsfd,tmpname = tempfile.mkstemp('irfs')
os.unlink(tmpname)
initramfsfile = os.fdopen(initramfsfd, 'r+b')
mkinitramfs.mkinitramfs(initramfsfile, config)
initramfsfile.flush()

compress_cmd = initramfs_compress_cmd(args.initramfs_compression)
compressor = subprocess.Popen(compress_cmd, stdin=subprocess.PIPE, stdout=initramfsfd)
mkinitramfs.mkinitramfs(compressor.stdin, config)
compressor.stdin.close()
if compressor.wait() != 0:
print("writing initramfs failed", file=sys.stderr)
return 1

if args.save_initramfs is not None:
initrdpath = args.save_initramfs
else:
Expand Down