]> git.proxmox.com Git - mirror_lxc.git/blame - templates/lxc-busybox.in
Merge pull request #2787 from Blub/2019-01-17/revert-sys-double-bindmount-cleanup
[mirror_lxc.git] / templates / lxc-busybox.in
CommitLineData
b62671d8 1#!/bin/sh
eb960fea 2
b62671d8 3# Client script for LXC container images.
eb960fea 4#
b62671d8
CB
5# Copyright @ Daniel Lezcano <daniel.lezcano@free.fr>
6# Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>
7#
8# This library is free software; you can redistribute it and/or
9# modify it under the terms of the GNU Lesser General Public
10# License as published by the Free Software Foundation; either
11# version 2.1 of the License, or (at your option) any later version.
eb960fea 12
b62671d8
CB
13# This library is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16# Lesser General Public License for more details.
eb960fea 17
b62671d8
CB
18# You should have received a copy of the GNU Lesser General Public
19# License along with this library; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21# USA
eb960fea 22
a542dd3c
BP
23LXC_MAPPED_UID=
24LXC_MAPPED_GID=
8ec981fc 25
1c9bca6b
RK
26BUSYBOX_EXE=
27
207bf0e4
SG
28# Make sure the usual locations are in PATH
29export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
30
b62671d8
CB
31in_userns() {
32 [ -e /proc/self/uid_map ] || { echo no; return; }
33 while read -r line; do
34 fields="$(echo "$line" | awk '{ print $1 " " $2 " " $3 }')"
35 if [ "${fields}" = "0 0 4294967295" ]; then
36 echo no;
37 return;
88e38993 38 fi
b62671d8
CB
39 if echo "${fields}" | grep -q " 0 1$"; then
40 echo userns-root;
41 return;
88e38993 42 fi
b62671d8 43 done < /proc/self/uid_map
88e38993 44
b62671d8
CB
45 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && { echo userns-root; return; }
46 echo yes
88e38993
BP
47}
48
b62671d8
CB
49USERNS="$(in_userns)"
50
eb960fea
DL
51install_busybox()
52{
b62671d8
CB
53 rootfs="${1}"
54 name="${2}"
55 res=0
56 fstree="\
57${rootfs}/selinux \
58${rootfs}/dev \
59${rootfs}/home \
60${rootfs}/root \
61${rootfs}/etc \
62${rootfs}/etc/init.d \
63${rootfs}/bin \
64${rootfs}/usr/bin \
65${rootfs}/sbin \
66${rootfs}/usr/sbin \
67${rootfs}/proc \
68${rootfs}/sys \
69${rootfs}/mnt \
70${rootfs}/tmp \
71${rootfs}/var/log \
c65973ad 72${rootfs}/var/run \
b62671d8
CB
73${rootfs}/usr/share/udhcpc \
74${rootfs}/dev/pts \
75${rootfs}/dev/shm \
76${rootfs}/lib \
77${rootfs}/usr/lib \
78${rootfs}/lib64 \
79${rootfs}/usr/lib64"
80
81 # shellcheck disable=SC2086
82 mkdir -p ${fstree} || return 1
83 # shellcheck disable=SC2086
84 chmod 755 ${fstree} || return 1
85
86 # minimal devices needed for busybox
87 if [ "${USERNS}" = "yes" ]; then
88 for dev in tty console tty0 tty1 ram0 null urandom; do
89 echo "lxc.mount.entry = /dev/${dev} dev/${dev} none bind,optional,create=file 0 0" >> "${path}/config"
90 done
91 else
92 mknod -m 666 "${rootfs}/tty" c 5 0 || res=1
93 mknod -m 666 "${rootfs}/console" c 5 1 || res=1
94 mknod -m 666 "${rootfs}/tty0" c 4 0 || res=1
95 mknod -m 666 "${rootfs}/tty1" c 4 0 || res=1
96 mknod -m 666 "${rootfs}/tty5" c 4 0 || res=1
97 mknod -m 600 "${rootfs}/ram0" b 1 0 || res=1
98 mknod -m 666 "${rootfs}/null" c 1 3 || res=1
99 mknod -m 666 "${rootfs}/zero" c 1 5 || res=1
100 mknod -m 666 "${rootfs}/urandom" c 1 9 || res=1
101 fi
102
103 # root user defined
104 cat <<EOF >> "${rootfs}/etc/passwd"
eb960fea
DL
105root:x:0:0:root:/root:/bin/sh
106EOF
107
b62671d8 108 cat <<EOF >> "${rootfs}/etc/group"
eb960fea
DL
109root:x:0:root
110EOF
111
eb960fea 112 # mount everything
b62671d8 113 cat <<EOF >> "${rootfs}/etc/init.d/rcS"
eb960fea 114#!/bin/sh
b09ecaf3
DL
115/bin/syslogd
116/bin/mount -a
117/bin/udhcpc
eb960fea
DL
118EOF
119
b62671d8
CB
120 # executable
121 chmod 744 "${rootfs}/etc/init.d/rcS" || return 1
eb960fea 122
b62671d8
CB
123 # launch rcS first then make a console available
124 # and propose a shell on the tty, the last one is
125 # not needed
126 cat <<EOF >> "${rootfs}/etc/inittab"
eb960fea 127::sysinit:/etc/init.d/rcS
0016af97
DL
128tty1::respawn:/bin/getty -L tty1 115200 vt100
129console::askfirst:/bin/sh
eb960fea 130EOF
b62671d8
CB
131 # writable and readable for other
132 chmod 644 "${rootfs}/etc/inittab" || return 1
eb960fea 133
1c9bca6b
RK
134 # Look for the pathname of "default.script" from the help of udhcpc
135 DEF_SCRIPT=`${BUSYBOX_EXE} udhcpc -h 2>&1 | grep -- '-s,--script PROG' | cut -d'/' -f2- | cut -d')' -f1`
136 DEF_SCRIPT_DIR=`dirname /${DEF_SCRIPT}`
137 mkdir -p ${rootfs}/${DEF_SCRIPT_DIR}
138 chmod 644 ${rootfs}/${DEF_SCRIPT_DIR} || return 1
139
140 cat <<EOF >> ${rootfs}/${DEF_SCRIPT}
eb960fea 141#!/bin/sh
eb960fea 142case "\$1" in
b62671d8
CB
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
eb960fea 152
b62671d8
CB
153 # check broadcast
154 if [ -n "\$broadcast" ]; then
155 broadcast="broadcast \$broadcast"
156 fi
eb960fea 157
b62671d8
CB
158 # add a new ip address
159 ip addr add \$ip/\$mask \$broadcast dev \$interface
eb960fea 160
b62671d8
CB
161 if [ -n "\$router" ]; then
162 ip route add default via \$router dev \$interface
ed52814c
BP
163 fi
164
b62671d8
CB
165 [ -n "\$domain" ] && echo search \$domain > /etc/resolv.conf
166 for i in \$dns ; do
567f8915
RK
167 grep "nameserver \$i" /etc/resolv.conf > /dev/null 2>&1
168 if [ \$? -ne 0 ]; then
169 echo nameserver \$i >> /etc/resolv.conf
170 fi
4432b512 171 done
b62671d8
CB
172 ;;
173esac
174exit 0
ed52814c
BP
175EOF
176
1c9bca6b 177 chmod 744 ${rootfs}/${DEF_SCRIPT}
ed52814c 178
b62671d8 179 return "${res}"
ed52814c
BP
180}
181
eb960fea
DL
182configure_busybox()
183{
b62671d8
CB
184 rootfs="${1}"
185
b62671d8 186 # copy busybox in the rootfs
4765b926 187 if ! cp "${BUSYBOX_EXE}" "${rootfs}/bin"; then
b62671d8
CB
188 echo "ERROR: Failed to copy busybox binary"
189 return 1
190 fi
191
192 # symlink busybox for the commands it supports
193 # it would be nice to just use "chroot $rootfs busybox --install -s /bin"
194 # but that only works right in a chroot with busybox >= 1.19.0
195 (
196 cd "${rootfs}/bin" || return 1
8b7681f3 197 ./busybox --list | grep -v busybox | xargs -n1 ln -s busybox
b62671d8 198 )
eb960fea 199
b62671d8
CB
200 # relink /sbin/init
201 ln "${rootfs}/bin/busybox" "${rootfs}/sbin/init"
eb960fea 202
b62671d8
CB
203 # /etc/fstab must exist for "mount -a"
204 touch "${rootfs}/etc/fstab"
6ab1ca03 205
b62671d8
CB
206 # passwd exec must be setuid
207 chmod +s "${rootfs}/bin/passwd"
208 touch "${rootfs}/etc/shadow"
19d618b1 209
b62671d8 210 return 0
eb960fea
DL
211}
212
213copy_configuration()
214{
b62671d8
CB
215 path="${1}"
216 rootfs="${2}"
217 name="${3}"
eb960fea 218
b62671d8
CB
219grep -q "^lxc.rootfs.path" "${path}/config" 2>/dev/null || echo "lxc.rootfs.path = ${rootfs}" >> "${path}/config"
220cat <<EOF >> "${path}/config"
55c84efc 221lxc.signal.halt = SIGUSR1
fd998241 222lxc.signal.reboot = SIGTERM
b62671d8 223lxc.uts.name = "${name}"
fe1c5887 224lxc.tty.max = 1
232763d6 225lxc.pty.max = 1
eee3ba81 226lxc.cap.drop = sys_module mac_admin mac_override sys_time
69d66f1e
SG
227
228# When using LXC with apparmor, uncomment the next line to run unconfined:
a1d5fdfd 229#lxc.apparmor.profile = unconfined
f24a52d5
SG
230
231lxc.mount.auto = cgroup:mixed proc:mixed sys:mixed
232lxc.mount.entry = shm /dev/shm tmpfs defaults 0 0
1881820a
SH
233EOF
234
b62671d8
CB
235 libdirs="\
236 lib \
237 usr/lib \
238 lib64 \
239 usr/lib64"
8b7681f3 240
b62671d8
CB
241 for dir in ${libdirs}; do
242 if [ -d "/${dir}" ] && [ -d "${rootfs}/${dir}" ]; then
243 echo "lxc.mount.entry = /${dir} ${dir} none ro,bind 0 0" >> "${path}/config"
244 fi
245 done
246 echo "lxc.mount.entry = /sys/kernel/security sys/kernel/security none ro,bind,optional 0 0" >> "${path}/config"
eb960fea
DL
247}
248
a542dd3c
BP
249remap_userns()
250{
b62671d8 251 path="${1}"
a542dd3c 252
b62671d8
CB
253 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
254 chown "${LXC_MAPPED_UID}" "${path}/config" > /dev/null 2>&1
255 chown -R root "${path}/rootfs" > /dev/null 2>&1
256 fi
a542dd3c 257
b62671d8
CB
258 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
259 chgrp "${LXC_MAPPED_GID}" "${path}/config" > /dev/null 2>&1
260 chgrp -R root "${path}/rootfs" > /dev/null 2>&1
261 fi
a542dd3c
BP
262}
263
b62671d8
CB
264usage() {
265 cat <<EOF
266LXC busybox image builder
267
268Special arguments:
269[ -h | --help ]: Print this help message and exit.
270
271LXC internal arguments (do not pass manually!):
272[ --name <name> ]: The container name
273[ --path <path> ]: The path to the container
274[ --rootfs <rootfs> ]: The path to the container's rootfs
275[ --mapped-uid <map> ]: A uid map (user namespaces)
276[ --mapped-gid <map> ]: A gid map (user namespaces)
eb960fea 277EOF
b62671d8 278 return 0
eb960fea
DL
279}
280
b62671d8
CB
281if ! options=$(getopt -o hp:n: -l help,rootfs:,path:,name:,mapped-uid:,mapped-gid: -- "$@"); then
282 usage
283 exit 1
eb960fea
DL
284fi
285eval set -- "$options"
286
287while true
288do
b62671d8 289 case "$1" in
46a6646a 290 -h|--help) usage && exit 1;;
b62671d8
CB
291 -n|--name) name=$2; shift 2;;
292 -p|--path) path=$2; shift 2;;
293 --rootfs) rootfs=$2; shift 2;;
294 --mapped-uid) LXC_MAPPED_UID=$2; shift 2;;
295 --mapped-gid) LXC_MAPPED_GID=$2; shift 2;;
296 --) shift 1; break ;;
297 *) break ;;
298 esac
eb960fea
DL
299done
300
b62671d8
CB
301# Check that we have all variables we need
302if [ -z "${name}" ] || [ -z "${path}" ] || [ -z "${rootfs}" ]; then
303 echo "ERROR: Please pass the name, path, and rootfs for the container" 1>&2
eb960fea
DL
304 exit 1
305fi
306
1c9bca6b
RK
307# Make sure busybox is present
308BUSYBOX_EXE=`which busybox`
309if [ $? -ne 0 ]; then
310 echo "ERROR: Failed to find busybox binary"
311 exit 1
312fi
313
1881820a
SH
314# detect rootfs
315config="$path/config"
1897e3bc 316if [ -z "$rootfs" ]; then
b62671d8
CB
317 if grep -q '^lxc.rootfs.path' "${config}" 2> /dev/null ; then
318 rootfs=$(awk -F= '/^lxc.rootfs.path =/{ print $2 }' "${config}")
319 else
320 rootfs="${path}/rootfs"
321 fi
1881820a 322fi
eb960fea 323
b62671d8
CB
324if ! install_busybox "${rootfs}" "${name}"; then
325 echo "ERROR: Failed to install rootfs"
326 exit 1
eb960fea
DL
327fi
328
b62671d8
CB
329if ! configure_busybox "${rootfs}"; then
330 echo "ERROR: Failed to configure busybox"
331 exit 1
eb960fea
DL
332fi
333
b62671d8
CB
334if ! copy_configuration "${path}" "${rootfs}" "${name}"; then
335 echo "ERROR: Failed to write config file"
336 exit 1
eb960fea 337fi
a542dd3c 338
b62671d8
CB
339if ! remap_userns "${path}"; then
340 echo "ERROR: Failed to change idmappings"
341 exit 1
ed52814c 342fi