Skip to content

Commit d704a4b

Browse files
committed
Allow booting directory images owned by root if running as root
Alternative to #4214. While we still don't support booting images built as non-root uids without the foreign UID range, we can make an exception for images built and booted as root.
1 parent 56d5ed4 commit d704a4b

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

mkosi/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5263,10 +5263,16 @@ def run_verb(args: Args, tools: Optional[Config], images: Sequence[Config], *, r
52635263
return
52645264

52655265
if (output := last.output_dir_or_cwd() / last.output).is_dir() and not is_foreign_uid_tree(output):
5266-
die(
5267-
"Can only operate on foreign UID range owned directory images",
5268-
hint="Add ForeignUIDRange=yes to [Build] and rebuild the image to use the foreign UID range",
5269-
)
5266+
if output.stat().st_uid != 0:
5267+
die(
5268+
"Can only operate on foreign UID range or root owned directory images",
5269+
hint="Add ForeignUIDRange=yes to [Build] and rebuild the image to use the foreign UID range",
5270+
)
5271+
5272+
if os.getuid() != 0:
5273+
die(
5274+
"Must be root to operate on root owned directory images",
5275+
)
52705276

52715277
run_vm = {
52725278
Vmm.qemu: run_qemu,

mkosi/qemu.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from mkosi.log import ARG_DEBUG, die
5252
from mkosi.partition import finalize_root, find_partitions
5353
from mkosi.run import AsyncioThread, find_binary, run, spawn, workdir
54-
from mkosi.tree import copy_tree, maybe_make_nocow, rmtree
54+
from mkosi.tree import copy_tree, is_foreign_uid_tree, maybe_make_nocow, rmtree
5555
from mkosi.user import INVOKING_USER
5656
from mkosi.util import (
5757
PathString,
@@ -395,11 +395,13 @@ def start_virtiofsd(
395395
group=st.st_gid if st else None,
396396
sandbox=config.sandbox(
397397
options=[
398-
"--bind" if uidmap else "--bind-foreign", directory, workdir(directory),
398+
"--bind" if uidmap or not is_foreign_uid_tree(directory) else "--bind-foreign",
399+
directory,
400+
workdir(directory),
399401
"--become-root",
400402
],
401403
),
402-
) as proc: # fmt: skip
404+
) as proc:
403405
yield path
404406
proc.terminate()
405407

0 commit comments

Comments
 (0)