Skip to content

Commit

Permalink
mkosi: add "burn" verb 🔥🔥🔥
Browse files Browse the repository at this point in the history
This simply dd's the resulting image to a block device. (Doesn't
actually dd though, uses systemd-repart --copy-from)

i.e. run "mkosi -f burn /dev/sda"
  • Loading branch information
poettering committed Nov 13, 2023
1 parent 2342a74 commit 397ca3c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
4 changes: 4 additions & 0 deletions mkosi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from mkosi.architecture import Architecture
from mkosi.archive import extract_tar, make_cpio, make_tar
from mkosi.burn import run_burn
from mkosi.config import (
BiosBootloader,
Bootloader,
Expand Down Expand Up @@ -2868,3 +2869,6 @@ def run_verb(args: MkosiArgs, images: Sequence[MkosiConfig]) -> None:

if args.verb == Verb.coredumpctl:
run_coredumpctl(args, last)

if args.verb == Verb.burn:
run_burn(args, last)
40 changes: 40 additions & 0 deletions mkosi/burn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SPDX-License-Identifier: LGPL-2.1+

import os
import sys

from mkosi.config import MkosiArgs, MkosiConfig, OutputFormat
from mkosi.log import complete_step, die
from mkosi.run import run


def run_burn(args: MkosiArgs, config: MkosiConfig) -> None:
if config.output_format not in (OutputFormat.disk, OutputFormat.esp):
die(f"{config.output_format} images cannot be burned to disk")

fname = config.output_dir_or_cwd() / config.output

if len(args.cmdline) != 1:
die("Expected device argument.");

device = args.cmdline[0]

cmd = [
"systemd-repart",
"--no-pager",
"--pretty=no",
"--offline=yes",
"--empty=force",
"--dry-run=no",
f"--copy-from={fname}",
device,
]

with complete_step("Burning 🔥🔥🔥 to medium…", "Burnt. 🔥🔥🔥"):
run(
cmd,
stdin=sys.stdin,
stdout=sys.stdout,
env=os.environ,
log=False,
)
7 changes: 4 additions & 3 deletions mkosi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ class Verb(StrEnum):
documentation = enum.auto()
journalctl = enum.auto()
coredumpctl = enum.auto()
burn = enum.auto()

def supports_cmdline(self) -> bool:
return self in (Verb.build, Verb.shell, Verb.boot, Verb.qemu, Verb.ssh, Verb.journalctl, Verb.coredumpctl)
return self in (Verb.build, Verb.shell, Verb.boot, Verb.qemu, Verb.ssh, Verb.journalctl, Verb.coredumpctl, Verb.burn)

def needs_build(self) -> bool:
return self in (Verb.build, Verb.shell, Verb.boot, Verb.qemu, Verb.serve, Verb.journalctl, Verb.coredumpctl)
return self in (Verb.build, Verb.shell, Verb.boot, Verb.qemu, Verb.serve, Verb.journalctl, Verb.coredumpctl, Verb.burn)

def needs_root(self) -> bool:
return self in (Verb.shell, Verb.boot)
return self in (Verb.shell, Verb.boot, Verb.burn)


class ConfigFeature(StrEnum):
Expand Down
9 changes: 9 additions & 0 deletions mkosi/resources/mkosi.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ mkosi — Build Bespoke OS Images

`mkosi [options…] serve`

`mkosi [options…] burn <device>`

`mkosi [options…] bump`

`mkosi [options…] genkey`
Expand Down Expand Up @@ -125,6 +127,13 @@ The following command line verbs are known:
OS images, for example via `machinectl pull-raw …` and `machinectl
pull-tar …`.

`burn <device>`

: This builds the image if it is not built yet, and then writes it to the
specified block device. The partition contents are written as-is, but the GPT
partition table is corrected to match sector and disk size of the specified
medium.

`bump`

: Bumps the image version from `mkosi.version` and writes the resulting
Expand Down

0 comments on commit 397ca3c

Please sign in to comment.