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