From 8341b7159d18aa12e497bd10c33a53fcd93c02c5 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Fri, 26 Jan 2024 11:34:16 +0100 Subject: add examples/fakeroot.py --- examples/fakeroot.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 examples/fakeroot.py diff --git a/examples/fakeroot.py b/examples/fakeroot.py new file mode 100755 index 0000000..47db330 --- /dev/null +++ b/examples/fakeroot.py @@ -0,0 +1,33 @@ +#!/usr/bin/python3 +# Copyright 2024 Helmut Grohne +# SPDX-License-Identifier: GPL-3 + +"""Vaguely emulate fakeroot using a user namespace that maps the current user +and group to root and the rest of the low range to a subuid range without +performing any chroot. This is similar to unshare --map-root-user --map-auto. +""" + +import os +import sys + +if __file__.split("/")[-2:-1] == ["examples"]: + sys.path.insert(0, "/".join(__file__.split("/")[:-2])) + +import linuxnamespaces + + +def main() -> None: + uidmap = [ + linuxnamespaces.IDMapping(0, os.getuid(), 1), + linuxnamespaces.IDAllocation.loadsubid("uid").allocatemap(65535, 1), + ] + gidmap = [ + linuxnamespaces.IDMapping(0, os.getgid(), 1), + linuxnamespaces.IDAllocation.loadsubid("gid").allocatemap(65535, 1), + ] + linuxnamespaces.unshare_user_idmap(uidmap, gidmap) + os.execlp(os.environ["SHELL"], os.environ["SHELL"]) + + +if __name__ == "__main__": + main() -- cgit v1.2.3