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:
27 print >>f, """[schsh-{0}]
32 setup.fstab=schsh/{0}.fstab
33 """.format(name, chroot)
34 with open("/etc/schroot/schsh/"+name+".fstab", "w") as f:
35 print >>f, """# <file system> <mount point> <type> <options> <dump> <pass>
36 /bin \t/bin \tnone \trw,bind \t0 \t0
37 /lib \t/lib \tnone \trw,bind \t0 \t0
38 /usr/bin \t/usr/bin \tnone \trw,bind \t0 \t0
39 /usr/lib \t/usr/lib \tnone \trw,bind \t0 \t0
40 /home/{0}/data\t/data \tnone \trw,bind \t0 \t0
41 """.replace(' ', '').format(name) # need to remove spaces so schroot does not complain
43 # setup the schroot directory
45 for folder in ["etc", "dev", "bin", "usr", "data"]:
46 os.mkdir(os.path.join(chroot, folder))
48 # setup /etc/passwd and /etc/group
49 with open(os.path.join(chroot, "etc", "passwd"), "w") as f:
50 print >>f, "root:x:0:0:root:/root:/bin/bash"
51 print >>f, "{0}:x:{1}:{2}:,,,:/data:/bin/false".format(name, userpw.pw_uid, userpw.pw_gid)
52 with open(os.path.join(chroot, "etc", "group"), "w") as f:
53 print >>f, "root:x:0:"
54 usergrp = grp.getgrgid(userpw.pw_gid)
55 print >>f, "{0}:x:{1}:".format(usergrp.gr_name, usergrp.gr_gid)
57 groupgrp = grp.getgrnam(group)
58 assert usergrp.gr_gid != groupgrp.gr_gid
59 print >>f, "{0}:x:{1}:{2}".format(groupgrp.gr_name, groupgrp.gr_gid, name)
62 if userpw.pw_shell != schsh:
63 subprocess.check_output(["usermod", "--shell", schsh, name])
65 subprocess.check_output(["adduser", name, "schsh"])
69 if len(sys.argv) <= 1:
70 print "Usage: %s <usernames>" % sys.argv[0]
72 for name in sys.argv[1:]:
73 print "Setting up",name