]>
Commit | Line | Data |
---|---|---|
eb960fea DL |
1 | #!/bin/bash |
2 | ||
3 | # | |
4 | # lxc: linux Container library | |
5 | ||
6 | # Authors: | |
7 | # Daniel Lezcano <daniel.lezcano@free.fr> | |
8 | ||
9 | # This library is free software; you can redistribute it and/or | |
10 | # modify it under the terms of the GNU Lesser General Public | |
11 | # License as published by the Free Software Foundation; either | |
12 | # version 2.1 of the License, or (at your option) any later version. | |
13 | ||
14 | # This library is distributed in the hope that it will be useful, | |
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | # Lesser General Public License for more details. | |
18 | ||
19 | # You should have received a copy of the GNU Lesser General Public | |
20 | # License along with this library; if not, write to the Free Software | |
250b1eec | 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
eb960fea | 22 | |
8ec981fc | 23 | # Detect use under userns (unsupported) |
c63c04fc | 24 | for arg in "$@"; do |
96283b54 SG |
25 | [ "$arg" = "--" ] && break |
26 | if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then | |
8ec981fc SG |
27 | echo "This template can't be used for unprivileged containers." 1>&2 |
28 | echo "You may want to try the \"download\" template instead." 1>&2 | |
29 | exit 1 | |
30 | fi | |
31 | done | |
32 | ||
207bf0e4 SG |
33 | # Make sure the usual locations are in PATH |
34 | export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin | |
35 | ||
9e214906 SH |
36 | am_in_userns() { |
37 | [ -e /proc/self/uid_map ] || { echo no; return; } | |
38 | [ "$(wc -l /proc/self/uid_map | awk '{ print $1 }')" -eq 1 ] || { echo yes; return; } | |
39 | line=$(awk '{ print $1 " " $2 " " $3 }' /proc/self/uid_map) | |
40 | [ "$line" = "0 0 4294967295" ] && { echo no; return; } | |
41 | echo yes | |
42 | } | |
43 | ||
44 | in_userns=0 | |
45 | [ $(am_in_userns) = "yes" ] && in_userns=1 | |
46 | ||
eb960fea DL |
47 | install_busybox() |
48 | { | |
49 | rootfs=$1 | |
50 | name=$2 | |
51 | res=0 | |
52 | tree="\ | |
32b37181 | 53 | $rootfs/selinux \ |
eb960fea DL |
54 | $rootfs/dev \ |
55 | $rootfs/home \ | |
56 | $rootfs/root \ | |
57 | $rootfs/etc \ | |
58 | $rootfs/etc/init.d \ | |
59 | $rootfs/bin \ | |
c94e60d1 | 60 | $rootfs/usr/bin \ |
eb960fea | 61 | $rootfs/sbin \ |
c94e60d1 | 62 | $rootfs/usr/sbin \ |
eb960fea | 63 | $rootfs/proc \ |
fefddf9f | 64 | $rootfs/sys \ |
eb960fea DL |
65 | $rootfs/mnt \ |
66 | $rootfs/tmp \ | |
67 | $rootfs/var/log \ | |
68 | $rootfs/usr/share/udhcpc \ | |
10e657e5 | 69 | $rootfs/dev/pts \ |
bf6cc736 DL |
70 | $rootfs/dev/shm \ |
71 | $rootfs/lib \ | |
72 | $rootfs/usr/lib \ | |
73 | $rootfs/lib64 \ | |
74 | $rootfs/usr/lib64" | |
eb960fea DL |
75 | |
76 | mkdir -p $tree || return 1 | |
77 | chmod 755 $tree || return 1 | |
78 | ||
79 | pushd $rootfs/dev > /dev/null || return 1 | |
80 | ||
81 | # minimal devices needed for busybox | |
9e214906 SH |
82 | if [ $in_userns -eq 1 ]; then |
83 | for dev in tty console tty0 tty1 tty5 ram0 null urandom; do | |
84 | touch $rootfs/dev/$dev | |
85 | echo "/dev/$dev dev/$dev none bind 0 0" >> $path/fstab | |
86 | done | |
87 | else | |
fbbc1539 DE |
88 | mknod -m 666 tty c 5 0 || res=1 |
89 | mknod -m 666 console c 5 1 || res=1 | |
90 | mknod -m 666 tty0 c 4 0 || res=1 | |
91 | mknod -m 666 tty1 c 4 0 || res=1 | |
92 | mknod -m 666 tty5 c 4 0 || res=1 | |
93 | mknod -m 600 ram0 b 1 0 || res=1 | |
94 | mknod -m 666 null c 1 3 || res=1 | |
95 | mknod -m 666 zero c 1 5 || res=1 | |
96 | mknod -m 666 urandom c 1 9 || res=1 | |
9e214906 | 97 | fi |
eb960fea DL |
98 | |
99 | popd > /dev/null | |
100 | ||
101 | # root user defined | |
102 | cat <<EOF >> $rootfs/etc/passwd | |
103 | root:x:0:0:root:/root:/bin/sh | |
104 | EOF | |
105 | ||
106 | cat <<EOF >> $rootfs/etc/group | |
107 | root:x:0:root | |
108 | EOF | |
109 | ||
eb960fea DL |
110 | # mount everything |
111 | cat <<EOF >> $rootfs/etc/init.d/rcS | |
112 | #!/bin/sh | |
b09ecaf3 DL |
113 | /bin/syslogd |
114 | /bin/mount -a | |
115 | /bin/udhcpc | |
eb960fea DL |
116 | EOF |
117 | ||
118 | # executable | |
119 | chmod 744 $rootfs/etc/init.d/rcS || return 1 | |
120 | ||
121 | # mount points | |
122 | cat <<EOF >> $rootfs/etc/fstab | |
eb960fea DL |
123 | shm /dev/shm tmpfs defaults 0 0 |
124 | EOF | |
125 | ||
126 | # writable and readable for other | |
127 | chmod 644 $rootfs/etc/fstab || return 1 | |
128 | ||
129 | # launch rcS first then make a console available | |
130 | # and propose a shell on the tty, the last one is | |
131 | # not needed | |
132 | cat <<EOF >> $rootfs/etc/inittab | |
133 | ::sysinit:/etc/init.d/rcS | |
0016af97 DL |
134 | tty1::respawn:/bin/getty -L tty1 115200 vt100 |
135 | console::askfirst:/bin/sh | |
eb960fea DL |
136 | EOF |
137 | # writable and readable for other | |
138 | chmod 644 $rootfs/etc/inittab || return 1 | |
139 | ||
140 | cat <<EOF >> $rootfs/usr/share/udhcpc/default.script | |
141 | #!/bin/sh | |
eb960fea | 142 | case "\$1" in |
14d9c0f0 SG |
143 | deconfig) |
144 | ip addr flush dev \$interface | |
145 | ;; | |
146 | ||
147 | renew|bound) | |
148 | # flush all the routes | |
149 | if [ -n "\$router" ]; then | |
150 | ip route del default 2> /dev/null | |
151 | fi | |
152 | ||
153 | # check broadcast | |
154 | if [ -n "\$broadcast" ]; then | |
155 | broadcast="broadcast \$broadcast" | |
156 | fi | |
157 | ||
158 | # add a new ip address | |
159 | ip addr add \$ip/\$mask \$broadcast dev \$interface | |
160 | ||
161 | if [ -n "\$router" ]; then | |
162 | ip route add default via \$router dev \$interface | |
163 | fi | |
164 | ||
165 | [ -n "\$domain" ] && echo search \$domain > /etc/resolv.conf | |
166 | for i in \$dns ; do | |
167 | echo nameserver \$i >> /etc/resolv.conf | |
168 | done | |
169 | ;; | |
eb960fea DL |
170 | esac |
171 | exit 0 | |
172 | EOF | |
173 | ||
174 | chmod 744 $rootfs/usr/share/udhcpc/default.script | |
175 | ||
176 | return $res | |
177 | } | |
178 | ||
179 | configure_busybox() | |
180 | { | |
181 | rootfs=$1 | |
182 | ||
169bf5e0 | 183 | which busybox >/dev/null 2>&1 |
7674618c | 184 | |
eb960fea | 185 | if [ $? -ne 0 ]; then |
14d9c0f0 SG |
186 | echo "busybox executable is not accessible" |
187 | return 1 | |
eb960fea DL |
188 | fi |
189 | ||
50dbb820 | 190 | file -L $(which busybox) | grep -q "statically linked" |
32b37181 | 191 | if [ $? -ne 0 ]; then |
14d9c0f0 SG |
192 | echo "warning : busybox is not statically linked." |
193 | echo "warning : The template script may not correctly" | |
194 | echo "warning : setup the container environment." | |
32b37181 DL |
195 | fi |
196 | ||
eb960fea DL |
197 | # copy busybox in the rootfs |
198 | cp $(which busybox) $rootfs/bin | |
199 | if [ $? -ne 0 ]; then | |
14d9c0f0 SG |
200 | echo "failed to copy busybox in the rootfs" |
201 | return 1 | |
eb960fea DL |
202 | fi |
203 | ||
6902a6c6 DE |
204 | # symlink busybox for the commands it supports |
205 | # it would be nice to just use "chroot $rootfs busybox --install -s /bin" | |
206 | # but that only works right in a chroot with busybox >= 1.19.0 | |
207 | pushd $rootfs/bin > /dev/null || return 1 | |
208 | ./busybox --help | grep 'Currently defined functions:' -A300 | \ | |
209 | grep -v 'Currently defined functions:' | tr , '\n' | \ | |
210 | xargs -n1 ln -s busybox | |
211 | popd > /dev/null | |
eb960fea DL |
212 | |
213 | # relink /sbin/init | |
214 | ln $rootfs/bin/busybox $rootfs/sbin/init | |
215 | ||
216 | # passwd exec must be setuid | |
217 | chmod +s $rootfs/bin/passwd | |
32b37181 | 218 | touch $rootfs/etc/shadow |
19d618b1 | 219 | |
ce4c4ca4 BP |
220 | # setting passwd for root |
221 | CHPASSWD_FILE=$rootfs/root/chpasswd.sh | |
222 | ||
223 | cat <<EOF >$CHPASSWD_FILE | |
224 | echo "setting root password to \"root\"" | |
225 | ||
7a409fd5 | 226 | mount -n --bind /lib $rootfs/lib |
ce4c4ca4 BP |
227 | if [ \$? -ne 0 ]; then |
228 | echo "Failed bind-mounting /lib at $rootfs/lib" | |
229 | exit 1 | |
230 | fi | |
231 | ||
232 | chroot $rootfs chpasswd <<EOFF 2>/dev/null | |
233 | root:root | |
234 | EOFF | |
235 | ||
236 | ||
237 | if [ \$? -ne 0 ]; then | |
238 | echo "Failed to change root password" | |
239 | exit 1 | |
240 | fi | |
241 | ||
242 | umount $rootfs/lib | |
243 | ||
244 | EOF | |
245 | ||
246 | lxc-unshare -s MOUNT -- /bin/sh < $CHPASSWD_FILE | |
247 | rm $CHPASSWD_FILE | |
c94e60d1 PBB |
248 | |
249 | # add ssh functionality if dropbear package available on host | |
169bf5e0 | 250 | which dropbear >/dev/null 2>&1 |
c94e60d1 PBB |
251 | if [ $? -eq 0 ]; then |
252 | # copy dropbear binary | |
253 | cp $(which dropbear) $rootfs/usr/sbin | |
254 | if [ $? -ne 0 ]; then | |
255 | echo "Failed to copy dropbear in the rootfs" | |
256 | return 1 | |
257 | fi | |
258 | ||
259 | # make symlinks to various ssh utilities | |
260 | utils="\ | |
261 | $rootfs/usr/bin/dbclient \ | |
262 | $rootfs/usr/bin/scp \ | |
263 | $rootfs/usr/bin/ssh \ | |
264 | $rootfs/usr/sbin/dropbearkey \ | |
265 | $rootfs/usr/sbin/dropbearconvert \ | |
266 | " | |
267 | echo $utils | xargs -n1 ln -s /usr/sbin/dropbear | |
268 | ||
269 | # add necessary config files | |
270 | mkdir $rootfs/etc/dropbear | |
17abf278 ER |
271 | dropbearkey -t rsa -f $rootfs/etc/dropbear/dropbear_rsa_host_key > /dev/null 2>&1 |
272 | dropbearkey -t dss -f $rootfs/etc/dropbear/dropbear_dss_host_key > /dev/null 2>&1 | |
c94e60d1 PBB |
273 | |
274 | echo "'dropbear' ssh utility installed" | |
275 | fi | |
276 | ||
eb960fea DL |
277 | return 0 |
278 | } | |
279 | ||
280 | copy_configuration() | |
281 | { | |
282 | path=$1 | |
283 | rootfs=$2 | |
284 | name=$3 | |
285 | ||
1881820a | 286 | grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config |
eb960fea | 287 | cat <<EOF >> $path/config |
f0f1d8c0 | 288 | lxc.haltsignal = SIGUSR1 |
eb960fea DL |
289 | lxc.utsname = $name |
290 | lxc.tty = 1 | |
32b37181 | 291 | lxc.pts = 1 |
eee3ba81 | 292 | lxc.cap.drop = sys_module mac_admin mac_override sys_time |
69d66f1e SG |
293 | |
294 | # When using LXC with apparmor, uncomment the next line to run unconfined: | |
295 | #lxc.aa_profile = unconfined | |
1881820a SH |
296 | EOF |
297 | ||
6bc424b5 | 298 | libdirs="\ |
5d01f616 SG |
299 | lib \ |
300 | usr/lib \ | |
301 | lib64 \ | |
302 | usr/lib64" | |
6bc424b5 SY |
303 | |
304 | for dir in $libdirs; do | |
5d01f616 | 305 | if [ -d "/$dir" ] && [ -d "$rootfs/$dir" ]; then |
eba7df9e | 306 | echo "lxc.mount.entry = /$dir $dir none ro,bind 0 0" >> $path/config |
6bc424b5 SY |
307 | fi |
308 | done | |
7f1dea04 | 309 | echo "lxc.mount.entry = /sys/kernel/security sys/kernel/security none ro,bind,optional 0 0" >>$path/config |
fefddf9f | 310 | echo "lxc.mount.auto = proc:mixed sys" >>$path/config |
eb960fea DL |
311 | } |
312 | ||
313 | usage() | |
314 | { | |
315 | cat <<EOF | |
316 | $1 -h|--help -p|--path=<path> | |
317 | EOF | |
318 | return 0 | |
319 | } | |
320 | ||
1897e3bc | 321 | options=$(getopt -o hp:n: -l help,rootfs:,path:,name: -- "$@") |
eb960fea | 322 | if [ $? -ne 0 ]; then |
14d9c0f0 SG |
323 | usage $(basename $0) |
324 | exit 1 | |
eb960fea DL |
325 | fi |
326 | eval set -- "$options" | |
327 | ||
328 | while true | |
329 | do | |
330 | case "$1" in | |
331 | -h|--help) usage $0 && exit 0;; | |
332 | -p|--path) path=$2; shift 2;; | |
1897e3bc | 333 | --rootfs) rootfs=$2; shift 2;; |
14d9c0f0 | 334 | -n|--name) name=$2; shift 2;; |
eb960fea DL |
335 | --) shift 1; break ;; |
336 | *) break ;; | |
337 | esac | |
338 | done | |
339 | ||
340 | if [ "$(id -u)" != "0" ]; then | |
341 | echo "This script should be run as 'root'" | |
342 | exit 1 | |
343 | fi | |
344 | ||
345 | if [ -z "$path" ]; then | |
346 | echo "'path' parameter is required" | |
347 | exit 1 | |
348 | fi | |
349 | ||
1881820a SH |
350 | # detect rootfs |
351 | config="$path/config" | |
1897e3bc SH |
352 | if [ -z "$rootfs" ]; then |
353 | if grep -q '^lxc.rootfs' $config 2>/dev/null ; then | |
853d58fd | 354 | rootfs=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $config) |
1897e3bc SH |
355 | else |
356 | rootfs=$path/rootfs | |
357 | fi | |
1881820a | 358 | fi |
eb960fea DL |
359 | |
360 | install_busybox $rootfs $name | |
361 | if [ $? -ne 0 ]; then | |
362 | echo "failed to install busybox's rootfs" | |
363 | exit 1 | |
364 | fi | |
365 | ||
366 | configure_busybox $rootfs | |
367 | if [ $? -ne 0 ]; then | |
368 | echo "failed to configure busybox template" | |
369 | exit 1 | |
370 | fi | |
371 | ||
372 | copy_configuration $path $rootfs $name | |
373 | if [ $? -ne 0 ]; then | |
374 | echo "failed to write configuration file" | |
375 | exit 1 | |
376 | fi |