forked from globalcitizen/lxc-gentoo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lxc-gentoo-userns
executable file
·82 lines (65 loc) · 1.92 KB
/
lxc-gentoo-userns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
# AGPLv3 for now
if [[ $# -ne 2 ]]; then
printf "usage: %s rootfs id\n" "$0"
exit 1
fi
set -u
shopt -s globstar
rootfs="$1"
conffile="$rootfs.conf"
newconffile="$conffile.new"
id="$2"
if [[ $id -lt 65536 ]]; then
(( id = id * 65536 ))
fi
if [[ ! -d "$rootfs" ]]; then
printf "Error: rootfs does not exist\n"
exit 1
fi
# rootfs part
printf "Cleaning devnodes:\n"
# devnodes are bind mounted from host:
for device in "$rootfs/dev/"**; do
if [[ -b $device || -c $device ]]; then
str="$(stat --printf="%F %t %T" "$device")"
printf " device: %s: %s\n" "$str" "$device"
rm "$device"
touch "$device"
fi
done
printf "Running uidmapshift...\n"
uidmapshift -b "$rootfs" 0 "$id" 65536
printf "root:%s:65536\n" "$id" >> /etc/subuid
printf "root:%s:65536\n" "$id" >> /etc/subgid
printf "Processing config file\n"
mapfile -t < "$conffile"
cat <<EOF >> "$newconffile"
lxc.id_map = u 0 $id 65536
lxc.id_map = g 0 $id 65536
lxc.devttydir =
EOF
for line in "${MAPFILE[@]}"; do
if [[ $line == lxc.cap.drop* ]]; then
printf "lxc.cap.drop = sys_module mac_admin mac_override sys_time\n" >> "$newconffile"
elif [[ $line == lxc.cgroup.devices.deny* ]]; then
: # don't persist it
elif [[ $line == lxc.cgroup.devices.allow* ]]; then
: # same
else
printf "%s\n" "$line" >> "$newconffile"
fi
done
cat <<EOF >> "$newconffile"
# userns device mounts
lxc.mount.entry = /dev/console dev/console none bind,create=file 0 0
lxc.mount.entry = /dev/full dev/full none bind,create=file 0 0
lxc.mount.entry = /dev/null dev/null none bind,create=file 0 0
lxc.mount.entry = /dev/random dev/random none bind,create=file 0 0
lxc.mount.entry = /dev/tty dev/tty none bind,create=file 0 0
lxc.mount.entry = /dev/urandom dev/urandom none bind,create=file 0 0
lxc.mount.entry = /dev/zero dev/zero none bind,create=file 0 0
EOF
mv "$conffile" "$conffile.old" && mv "$newconffile" "$conffile"
chown root:root "$conffile"
chown root:root "$conffile.old"