summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2024-01-26 11:34:16 +0100
committerHelmut Grohne <helmut@subdivi.de>2024-01-26 11:34:16 +0100
commit8341b7159d18aa12e497bd10c33a53fcd93c02c5 (patch)
tree8320dded955e163039b09eb584b92635d972f8c2 /examples
parent6372178a9e294cce43bdd1f27fb801bd9ebf16e7 (diff)
downloadpython-linuxnamespaces-8341b7159d18aa12e497bd10c33a53fcd93c02c5.tar.gz
add examples/fakeroot.py
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/fakeroot.py33
1 files changed, 33 insertions, 0 deletions
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 <helmut@subdivi.de>
+# 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()