for config in presets:
try_import(f"mkosi.distributions.{config.distribution}")
+ invoked_as_root = os.getuid() == 0
name = InvokingUser.name()
# Get the user UID/GID either on the host or in the user namespace running the build
with mount_usr(last.tools_tree, umount=False), mount_passwd(name, uid, gid, umount=False):
# After mounting the last tools tree, if we're not going to execute systemd-nspawn, we don't need to
- # be (fake) root anymore, so switch user to the invoking user.
- if args.verb not in (Verb.shell, Verb.boot):
+ # be (fake) root anymore, so switch user to the invoking user. If we're going to invoke qemu and
+ # mkosi was executed as root, we also don't drop privileges as depending on the environment and
+ # options passed, running qemu might need root privileges as well.
+ if not args.verb.needs_root() and (args.verb != Verb.qemu or not invoked_as_root):
os.setresgid(gid, gid, gid)
os.setresuid(uid, uid, uid)