2 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
4 schsh = "/usr/local/bin/schsh"
6 chroots = "/var/lib/schsh"
9 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
10 # DO NOT TOUCH ANYTHING BELOW THIS LINE
12 import os, sys, subprocess, pwd, grp
15 print("Run this a root, please.")
20 chroot = os.path.join(chroots, name)
21 if os.path.exists(chroot):
22 raise Exception(chroot+" already exists, please remove it first")
23 userpw = pwd.getpwnam(name)
25 # schroot configuration
26 with open("/etc/schroot/chroot.d/schsh-"+name, "w") as f:
32 setup.fstab=schsh/{0}.fstab
33 """.format(name, chroot), file=f)
34 with open("/etc/schroot/schsh/"+name+".fstab", "w") as f:
35 # no spaces, schroot does not like them
36 print("# <file system> <mount point> <type> <options> <dump> <pass>", file=f)
38 for folder in ("/lib", "/lib64", "/usr/bin", "/usr/lib", "/usr/lib64", "/usr/share/", "/usr/local/bin"):
39 if os.path.exists(folder):
40 print("{0}\t{0}\tnone\trw,bind\t0\t0".format(folder), file=f)
42 print("/home/{0}/data\t/data\tnone\trw,bind\t0\t0".format(name), file=f)
44 # setup the schroot directory
46 for folder in ["etc", "dev", "data"]:
47 os.mkdir(os.path.join(chroot, folder))
49 # setup /etc/passwd and /etc/group
50 with open(os.path.join(chroot, "etc", "passwd"), "w") as f:
51 print("root:x:0:0:root:/root:/bin/bash", file=f)
52 print("{0}:x:{1}:{2}:,,,:/data:/bin/false".format(name, userpw.pw_uid, userpw.pw_gid), file=f)
53 with open(os.path.join(chroot, "etc", "group"), "w") as f:
54 print("root:x:0:", file=f)
55 usergrp = grp.getgrgid(userpw.pw_gid)
56 print("{0}:x:{1}:".format(usergrp.gr_name, usergrp.gr_gid), file=f)
58 groupgrp = grp.getgrnam(group)
59 assert usergrp.gr_gid != groupgrp.gr_gid
60 print("{0}:x:{1}:{2}".format(groupgrp.gr_name, groupgrp.gr_gid, name), file=f)
63 if userpw.pw_shell != schsh:
64 subprocess.check_output(["usermod", "--shell", schsh, name])
66 subprocess.check_output(["adduser", name, "schsh"])
70 if len(sys.argv) <= 1:
71 print("Usage: %s <usernames>" % sys.argv[0])
73 for name in sys.argv[1:]:
74 print("Setting up",name)