from mkosi.types import PathString
from mkosi.user import INVOKING_USER
from mkosi.util import (
+ current_home_dir,
flatten,
flock,
flock_or_die,
uidmap = "rootidmap" if tree.source.stat().st_uid != 0 else "noidmap"
cmdline += ["--bind", f"{tree.source}:{target}:norbind,{uidmap}"]
+ if config.runtime_home and (path := current_home_dir()):
+ uidmap = "rootidmap" if path.stat().st_uid != 0 else "noidmap"
+ cmdline += ["--bind", f"{path}:/root:norbind,{uidmap}"]
+
if config.runtime_scratch == ConfigFeature.enabled or (
config.runtime_scratch == ConfigFeature.auto and
config.output_format == OutputFormat.disk
runtime_scratch: ConfigFeature
runtime_network: Network
runtime_build_sources: bool
+ runtime_home: bool
unit_properties: list[str]
ssh_key: Optional[Path]
ssh_certificate: Optional[Path]
parse=config_parse_boolean,
help="Mount build sources and build directory in /work when booting the image",
),
+ ConfigSetting(
+ dest="runtime_home",
+ metavar="BOOL",
+ section="Host",
+ parse=config_parse_boolean,
+ help="Mount current home directory to /root when booting the image",
+ ),
ConfigSetting(
dest="unit_properties",
long="--unit-property",
Runtime Scratch: {config.runtime_scratch}
Runtime Network: {config.runtime_network}
Runtime Build Sources: {config.runtime_build_sources}
+ Runtime Home or Working Directory: {yes_no(config.runtime_home)}
Unit Properties: {line_join_list(config.unit_properties)}
SSH Signing Key: {none_to_none(config.ssh_key)}
SSH Certificate: {none_to_none(config.ssh_certificate)}
from mkosi.tree import copy_tree, rmtree
from mkosi.types import PathString
from mkosi.user import INVOKING_USER, become_root_in_subuid_range, become_root_in_subuid_range_cmd
-from mkosi.util import StrEnum, flock, flock_or_die, groupby, round_up, try_or
+from mkosi.util import StrEnum, current_home_dir, flock, flock_or_die, groupby, round_up, try_or
from mkosi.versioncomp import GenericVersion
QEMU_KVM_DEVICE_VERSION = GenericVersion("9.0")
# A shared memory backend might increase ram usage so only add one if actually necessary for virtiofsd.
shm = []
- if config.runtime_trees or config.runtime_build_sources or config.output_format == OutputFormat.directory:
+ if (
+ config.runtime_trees or
+ config.runtime_build_sources or
+ config.runtime_home or
+ config.output_format == OutputFormat.directory
+ ):
shm = ["-object", f"memory-backend-memfd,id=mem,size={config.qemu_mem // 1024**2}M,share=on"]
machine = f"type={config.architecture.default_qemu_machine()}"
tag=tree.target.name if tree.target else tree.source.name,
)
+ if config.runtime_home and (p := current_home_dir()):
+ sock = stack.enter_context(start_virtiofsd(config, p))
+ add_virtiofs_mount(
+ sock,
+ Path("/root"),
+ cmdline,
+ credentials,
+ tag="user-home",
+ )
+
if want_scratch(config) or config.output_format in (OutputFormat.disk, OutputFormat.esp):
cmdline += ["-device", "virtio-scsi-pci,id=mkosi"]
they were mounted to when running the build script when using `mkosi
boot` or `mkosi qemu`.
+`RuntimeHome=`, `--runtime-home=`
+: Mount the current home directory from which mkosi is running to
+ `/root` when using `mkosi boot` or `mkosi qemu`.
+
`UnitProperties=`, `--unit-property=`
: Configure systemd unit properties to add to the systemd scopes
allocated when using `mkosi boot` or `mkosi qemu`. These are passed
from mkosi.log import ARG_DEBUG, ARG_DEBUG_SHELL, die
from mkosi.sandbox import joinpath, umask
from mkosi.types import _FILE, CompletedProcess, PathString, Popen
-from mkosi.util import flatten, one_zero
+from mkosi.util import current_home_dir, flatten, one_zero
SD_LISTEN_FDS_START = 3
if Path(d).exists():
cmdline += ["--bind", d, d]
+ path = current_home_dir()
+ if not path and Path.cwd() not in (Path("/"), Path("/home")):
+ path = Path.cwd()
+
# Either add the home directory we're running from or the current working directory if we're not running from
# inside a home directory.
- if Path.cwd() == Path("/"):
- d = ""
- if Path.cwd().is_relative_to("/root"):
- d = "/root"
- elif Path.cwd() == Path("/home"):
- d = "/home"
- elif Path.cwd().is_relative_to("/home"):
- # `Path.parents` only supports slices and negative indexing from Python 3.10 onwards.
- # TODO: Remove list() when we depend on Python 3.10 or newer.
- d = os.fspath(list(Path.cwd().parents)[-2])
- else:
- d = os.fspath(Path.cwd())
-
- if d and not any(Path(d).is_relative_to(dir) for dir in (*dirs, "/usr", "/nix", "/tmp")):
- cmdline += ["--bind", d, d]
+ if path and not any(path.is_relative_to(dir) for dir in (*dirs, "/usr", "/nix", "/tmp")):
+ cmdline += ["--bind", path, path]
else:
cmdline += ["--dir", "/var/tmp", "--unshare-ipc"]
if network and Path("/etc/resolv.conf").exists():
cmdline += ["--ro-bind", "/etc/resolv.conf", "/etc/resolv.conf"]
- path = "/usr/bin:/usr/sbin" if tools != Path("/") else os.environ["PATH"]
-
- cmdline += ["--setenv", "PATH", f"/scripts:{path}"]
+ cmdline += ["--setenv", "PATH", f"/scripts:{'/usr/bin:/usr/sbin' if tools != Path('/') else os.environ['PATH']}"]
if scripts:
cmdline += ["--ro-bind", scripts, "/scripts"]
grouped[k].append(i)
return [(key, group) for key, group in grouped.items()]
+
+
+def current_home_dir() -> Optional[Path]:
+ if Path.cwd() in (Path("/"), Path("/home")):
+ return None
+
+ if Path.cwd().is_relative_to("/root"):
+ return Path("/root")
+
+ if Path.cwd().is_relative_to("/home"):
+ # `Path.parents` only supports slices and negative indexing from Python 3.10 onwards.
+ # TODO: Remove list() when we depend on Python 3.10 or newer.
+ return list(Path.cwd().parents)[-3]
+
+ return None
)
from mkosi.run import run
from mkosi.types import PathString
+from mkosi.util import current_home_dir
def run_vmspawn(args: Args, config: Config) -> None:
target = Path("/root/src") / (tree.target or "")
cmdline += ["--bind", f"{tree.source}:{target}"]
+ if config.runtime_home and (p := current_home_dir()):
+ cmdline += ["--bind", f"{p}:/root"]
+
if kernel:
cmdline += ["--linux", kernel]
],
"RootShell": "/bin/tcsh",
"RuntimeBuildSources": true,
+ "RuntimeHome": true,
"RuntimeNetwork": "interface",
"RuntimeScratch": "enabled",
"RuntimeSize": 8589934592,
root_password=("test1234", False),
root_shell="/bin/tcsh",
runtime_build_sources=True,
+ runtime_home=True,
runtime_network=Network.interface,
runtime_scratch=ConfigFeature.enabled,
runtime_size=8589934592,