PartitionIdentifier,
PartitionTable,
SourceFileTransfer,
+ Verb,
die,
install_grub,
nspawn_params_for_blockdev_access,
SomeIO = Union[BinaryIO, TextIO]
PathString = Union[Path, str]
-MKOSI_COMMANDS_CMDLINE = ("build", "shell", "boot", "qemu", "ssh")
-MKOSI_COMMANDS_NEED_BUILD = ("shell", "boot", "qemu", "serve")
-MKOSI_COMMANDS_SUDO = ("build", "clean", "shell", "boot", "qemu", "serve")
-MKOSI_COMMANDS = ("build", "clean", "help", "summary", "genkey", "bump", "serve") + MKOSI_COMMANDS_CMDLINE
+MKOSI_COMMANDS_NEED_BUILD = (Verb.shell, Verb.boot, Verb.qemu, Verb.serve)
+MKOSI_COMMANDS_SUDO = (Verb.build, Verb.clean, Verb.shell, Verb.boot, Verb.qemu, Verb.serve)
+MKOSI_COMMANDS_CMDLINE = (Verb.build, Verb.shell, Verb.boot, Verb.qemu, Verb.ssh)
DRACUT_SYSTEMD_EXTRAS = [
"/usr/bin/systemd-repart",
parser = ArgumentParserMkosi(prog="mkosi", description="Build Bespoke OS Images", add_help=False)
group = parser.add_argument_group("Commands")
- group.add_argument("verb", choices=MKOSI_COMMANDS, default="build", help="Operation to execute")
+ group.add_argument("verb", type=Verb, choices=list(Verb), default=Verb.build, help="Operation to execute")
group.add_argument(
- "cmdline", nargs=argparse.REMAINDER, help="The command line to use for " + str(MKOSI_COMMANDS_CMDLINE)[1:-1]
+ "cmdline", nargs=argparse.REMAINDER, help="The command line to use for " + str([verb.name for verb in MKOSI_COMMANDS_CMDLINE])[1:-1]
)
group.add_argument("-h", "--help", action="help", help="Show this help")
group.add_argument("--version", action="version", version="%(prog)s " + __version__)
# values of positional arguments. Make sure the verb command gets explicitly passed.
# Insert a -- before the positional verb argument otherwise it might be considered as an argument of
# a parameter with nargs='?'. For example mkosi -i summary would be treated as -i=summary.
- for verb in MKOSI_COMMANDS:
+ for verb in Verb:
try:
- v_i = argv.index(verb)
+ v_i = argv.index(verb.name)
except ValueError:
continue
# First run of command line arguments parsing to get the directory of mkosi.default file and the verb argument.
args_pre_parsed, _ = parser.parse_known_args(argv)
- if args_pre_parsed.verb == "help":
+ if args_pre_parsed.verb == Verb.help:
parser.print_help()
sys.exit(0)
def unlink_output(args: MkosiArgs) -> None:
- if not args.force and args.verb != "clean":
+ if not args.force and args.verb != Verb.clean:
return
if not args.skip_final_phase:
# remove the downloaded package cache if the user specified one
# additional "--force".
- if args.verb == "clean":
+ if args.verb == Verb.clean:
remove_build_cache = args.force > 0
remove_package_cache = args.force > 1
else:
args.extra_search_paths = expand_paths(args.extra_search_paths)
if args.cmdline and args.verb not in MKOSI_COMMANDS_CMDLINE:
- die("Additional parameters only accepted for " + str(MKOSI_COMMANDS_CMDLINE)[1:-1] + " invocations.")
+ die("Additional parameters only accepted for " + str([verb.name for verb in MKOSI_COMMANDS_CMDLINE])[1:-1] + " invocations.")
if args.output_format is None:
args.output_format = OutputFormat.gpt_ext4
else:
warn("Ignoring configured output directory as output file is a qualified path.")
- if args.incremental or args.verb == "clean":
+ if args.incremental or args.verb == Verb.clean:
if args.image_id is not None:
# If the image ID is specified, use cache file names that are independent of the image versions, so that
# rebuilding and bumping versions is cheap and reuses previous versions if cached.
"UEFI SecureBoot or signed Verity enabled, but couldn't find certificate. (Consider placing it in mkosi.secure-boot.crt?)"
) # NOQA: E501
- if args.verb in ("shell", "boot"):
- opname = "acquire shell" if args.verb == "shell" else "boot"
+ if args.verb in (Verb.shell, Verb.boot):
+ opname = "acquire shell" if args.verb == Verb.shell else "boot"
if args.output_format in (OutputFormat.tar, OutputFormat.cpio):
die(f"Sorry, can't {opname} with a {args.output_format} archive.")
if should_compress_output(args):
if args.qcow2:
die(f"Sorry, can't {opname} using a qcow2 image.")
- if args.verb == "qemu":
+ if args.verb == Verb.qemu:
if not args.output_format.is_disk():
die("Sorry, can't boot non-disk images with qemu.")
- if needs_build(args) and args.verb == "qemu" and not args.bootable:
+ if needs_build(args) and args.verb == Verb.qemu and not args.bootable:
die("Images built without the --bootable option cannot be booted using qemu")
if needs_build(args) and args.qemu_headless and not args.bootable:
if args.source_file_transfer_final == SourceFileTransfer.mount:
die("Sorry, --source-file-transfer-final=mount is not supported")
- if args.skip_final_phase and args.verb != "build":
+ if args.skip_final_phase and args.verb != Verb.build:
die("--skip-final-phase can only be used when building an image using 'mkosi build'")
if args.ssh_timeout < 0:
def print_summary(args: MkosiArgs) -> None:
# FIXME: normal print
MkosiPrinter.info("COMMANDS:")
- MkosiPrinter.info(" verb: " + args.verb)
+ MkosiPrinter.info(f" verb: {args.verb}")
MkosiPrinter.info(" cmdline: " + " ".join(args.cmdline))
MkosiPrinter.info("\nDISTRIBUTION:")
MkosiPrinter.info(" Distribution: " + args.distribution.name)
def ensure_networkd(args: MkosiArgs) -> bool:
networkd_is_running = run(["systemctl", "is-active", "--quiet", "systemd-networkd"], check=False).returncode == 0
if not networkd_is_running:
- if args.verb != "ssh":
+ if args.verb != Verb.ssh:
# Some programs will use 'mkosi ssh' with pexpect, so don't print warnings that will break
# them.
warn("--network-veth requires systemd-networkd to be running to initialize the host interface "
"of the veth link ('systemctl enable --now systemd-networkd')")
return False
- if args.verb == "qemu" and not has_networkd_vm_vt():
+ if args.verb == Verb.qemu and not has_networkd_vm_vt():
warn(dedent(r"""\
mkosi didn't find 80-vm-vt.network. This is one of systemd's built-in
systemd-networkd config files which configures vt-* interfaces.
if args.nspawn_settings is not None:
cmdline += ["--settings=trusted"]
- if args.verb == "boot":
+ if args.verb == Verb.boot:
cmdline += ["--boot"]
else:
cmdline += nspawn_rlimit_params()
if args.cmdline:
# If the verb is 'shell', args.cmdline contains the command to run.
# Otherwise, the verb is 'boot', and we assume args.cmdline contains nspawn arguments.
- if args.verb == "shell":
+ if args.verb == Verb.shell:
cmdline += ["--"]
cmdline += args.cmdline
def run_command_image(args: MkosiArgs, commands: Sequence[str], timeout: int, check: bool, stdout: _FILE = sys.stdout, stderr: _FILE = sys.stderr) -> CompletedProcess:
- if args.verb == "qemu":
+ if args.verb == Verb.qemu:
return run_ssh(args, commands, check, stdout, stderr, timeout)
else:
cmdline = ["systemd-run", "--quiet", "--wait", "--pipe", "-M", virt_name(args), "/usr/bin/env", *commands]
def needs_build(args: Union[argparse.Namespace, MkosiArgs]) -> bool:
- return args.verb == "build" or (args.verb in MKOSI_COMMANDS_NEED_BUILD and (not args.output.exists() or args.force > 0))
+ return args.verb == Verb.build or (args.verb in MKOSI_COMMANDS_NEED_BUILD and (not args.output.exists() or args.force > 0))
def run_verb(raw: argparse.Namespace) -> None:
prepend_to_environ_path(args.extra_search_paths)
- if args.verb == "genkey":
+ if args.verb == Verb.genkey:
generate_secure_boot_key(args)
- if args.verb == "bump":
+ if args.verb == Verb.bump:
bump_image_version(args)
if args.verb in MKOSI_COMMANDS_SUDO:
check_root()
unlink_output(args)
- if args.verb == "build":
+ if args.verb == Verb.build:
check_output(args)
- if args.verb == "summary":
+ if args.verb == Verb.summary:
print_summary(args)
if needs_build(args):
print_output_size(args)
- if args.verb in ("shell", "boot"):
+ if args.verb in (Verb.shell, Verb.boot):
run_shell(args)
- if args.verb == "qemu":
+ if args.verb == Verb.qemu:
run_qemu(args)
- if args.verb == "ssh":
+ if args.verb == Verb.ssh:
run_ssh(args)
- if args.verb == "serve":
+ if args.verb == Verb.serve:
run_serve(args)
import pytest
import mkosi
+from mkosi.backend import Verb
@contextlib.contextmanager
"tmp_size": None,
"usr_only": False,
"var_size": None,
- "verb": "build",
+ "verb": Verb.build,
"verity": False,
"with_docs": False,
"with_network": False,
def __init__(self):
super().__init__()
for ref_c in self.reference_config.values():
- ref_c["verb"] = "summary"
+ ref_c["verb"] = Verb.summary
self.cli_arguments = ["summary"]
def __init__(self):
super().__init__("a_sub_dir")
for ref_c in self.reference_config.values():
- ref_c["verb"] = "summary"
+ ref_c["verb"] = Verb.summary
class MkosiConfigManyParams(MkosiConfigOne):
def test_verb_none(tmpdir):
with change_cwd(tmpdir.strpath):
args = mkosi.parse_args([])
- assert args["default"].verb == "build"
+ assert args["default"].verb == Verb.build
def test_verb_build(tmpdir):
with change_cwd(tmpdir.strpath):
args = mkosi.parse_args(["build"])
- assert args["default"].verb == "build"
+ assert args["default"].verb == Verb.build
def test_verb_boot_no_cli_args1(tmpdir):
with change_cwd(tmpdir.strpath):
cmdline_ref = ["boot", "--par-for-sub", "--pom", "--for_sub", "1234"]
args = mkosi.parse_args(cmdline_ref)
- assert args["default"].verb == "boot"
+ assert args["default"].verb == Verb.boot
assert args["default"].cmdline == cmdline_ref[1:]
with change_cwd(tmpdir.strpath):
cmdline_ref = ["-pa-package", "boot", "--par-for-sub", "--popenssl", "--for_sub", "1234"]
args = mkosi.parse_args(cmdline_ref)
- assert args["default"].verb == "boot"
+ assert args["default"].verb == Verb.boot
assert "a-package" in args["default"].packages
assert args["default"].cmdline == cmdline_ref[2:]
with change_cwd(tmpdir.strpath):
cmdline_ref = ["-pa-package", "-p", "another-package", "build"]
args = mkosi.parse_args(cmdline_ref)
- assert args["default"].verb == "build"
+ assert args["default"].verb == Verb.build
assert args["default"].packages == ["a-package", "another-package"]
with change_cwd(tmpdir.strpath):
cmdline_ref = ["-pa-package", "-p", "another-package", "summary"]
args = mkosi.parse_args(cmdline_ref)
- assert args["default"].verb == "summary"
+ assert args["default"].verb == Verb.summary
assert args["default"].packages == ["a-package", "another-package"]
with change_cwd(tmpdir.strpath):
cmdline_ref = ["-pa-package", "-p", "another-package", "shell", "python3 -foo -bar;", "ls --inode"]
args = mkosi.parse_args(cmdline_ref)
- assert args["default"].verb == "shell"
+ assert args["default"].verb == Verb.shell
assert args["default"].packages == ["a-package", "another-package"]
assert args["default"].cmdline == cmdline_ref[4:]
with change_cwd(tmpdir.strpath):
cmdline_ref = ["-i", "yes", "summary"]
args = mkosi.parse_args(cmdline_ref)
- assert args["default"].verb == "summary"
+ assert args["default"].verb == Verb.summary
assert args["default"].incremental == True
with change_cwd(tmpdir.strpath):
cmdline_ref = ["-i", "summary"]
args = mkosi.parse_args(cmdline_ref)
- assert args["default"].verb == "summary"
+ assert args["default"].verb == Verb.summary
assert args["default"].incremental == True