3 # Client script for LXC container images.
5 # Copyright @ Daniel Lezcano <daniel.lezcano@free.fr>
6 # Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>
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.
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.
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
26 # Make sure the usual locations are in PATH
27 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
30 [ -e /proc
/self
/uid_map
] ||
{ echo no
; return; }
31 while read -r line
; do
32 fields
="$(echo "$line" | awk '{ print $1 " " $2 " " $3 }')"
33 if [ "${fields}" = "0 0 4294967295" ]; then
37 if echo "${fields}" |
grep -q " 0 1$"; then
41 done < /proc
/self
/uid_map
43 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && { echo userns-root
; return; }
60 ${rootfs}/etc/init.d \
70 ${rootfs}/usr/share/udhcpc \
78 # shellcheck disable=SC2086
79 mkdir
-p ${fstree} ||
return 1
80 # shellcheck disable=SC2086
81 chmod 755 ${fstree} ||
return 1
83 # minimal devices needed for busybox
84 if [ "${USERNS}" = "yes" ]; then
85 for dev
in tty console tty0 tty1 ram0 null urandom
; do
86 echo "lxc.mount.entry = /dev/${dev} dev/${dev} none bind,optional,create=file 0 0" >> "${path}/config"
89 mknod
-m 666 "${rootfs}/tty" c
5 0 || res
=1
90 mknod
-m 666 "${rootfs}/console" c
5 1 || res
=1
91 mknod
-m 666 "${rootfs}/tty0" c
4 0 || res
=1
92 mknod
-m 666 "${rootfs}/tty1" c
4 0 || res
=1
93 mknod
-m 666 "${rootfs}/tty5" c
4 0 || res
=1
94 mknod
-m 600 "${rootfs}/ram0" b
1 0 || res
=1
95 mknod
-m 666 "${rootfs}/null" c
1 3 || res
=1
96 mknod
-m 666 "${rootfs}/zero" c
1 5 || res
=1
97 mknod
-m 666 "${rootfs}/urandom" c
1 9 || res
=1
101 cat <<EOF >> "${rootfs}/etc/passwd"
102 root:x:0:0:root:/root:/bin/sh
105 cat <<EOF >> "${rootfs}/etc/group"
110 cat <<EOF >> "${rootfs}/etc/init.d/rcS"
118 chmod 744 "${rootfs}/etc/init.d/rcS" ||
return 1
120 # launch rcS first then make a console available
121 # and propose a shell on the tty, the last one is
123 cat <<EOF >> "${rootfs}/etc/inittab"
124 ::sysinit:/etc/init.d/rcS
125 tty1::respawn:/bin/getty -L tty1 115200 vt100
126 console::askfirst:/bin/sh
128 # writable and readable for other
129 chmod 644 "${rootfs}/etc/inittab" ||
return 1
131 cat <<EOF >> "${rootfs}/usr/share/udhcpc/default.script"
135 ip addr flush dev \$interface
139 # flush all the routes
140 if [ -n "\$router" ]; then
141 ip route del default 2> /dev/null
145 if [ -n "\$broadcast" ]; then
146 broadcast="broadcast \$broadcast"
149 # add a new ip address
150 ip addr add \$ip/\$mask \$broadcast dev \$interface
152 if [ -n "\$router" ]; then
153 ip route add default via \$router dev \$interface
156 [ -n "\$domain" ] && echo search \$domain > /etc/resolv.conf
158 echo nameserver \$i >> /etc/resolv.conf
165 chmod 744 "${rootfs}/usr/share/udhcpc/default.script"
174 if ! which busybox
> /dev
/null
2>&1; then
175 echo "ERROR: Failed to find busybox binary"
179 # copy busybox in the rootfs
180 if ! cp "$(which busybox)" "${rootfs}/bin"; then
181 echo "ERROR: Failed to copy busybox binary"
185 # symlink busybox for the commands it supports
186 # it would be nice to just use "chroot $rootfs busybox --install -s /bin"
187 # but that only works right in a chroot with busybox >= 1.19.0
189 cd "${rootfs}/bin" ||
return 1
190 .
/busybox
--help |
grep 'Currently defined functions:' -A300 | \
191 grep -v 'Currently defined functions:' |
tr , '\n' | \
192 xargs -n1 ln -s busybox
196 ln "${rootfs}/bin/busybox" "${rootfs}/sbin/init"
198 # /etc/fstab must exist for "mount -a"
199 touch "${rootfs}/etc/fstab"
201 # passwd exec must be setuid
202 chmod +s
"${rootfs}/bin/passwd"
203 touch "${rootfs}/etc/shadow"
214 grep -q "^lxc.rootfs.path" "${path}/config" 2>/dev/null || echo "lxc.rootfs.path = ${rootfs}" >> "${path}/config"
215 cat <<EOF >> "${path}/config"
216 lxc.signal.halt = SIGUSR1
217 lxc.signal.reboot = SIGTERM
218 lxc.uts.name = "${name}"
221 lxc.cap.drop = sys_module mac_admin mac_override sys_time
223 # When using LXC with apparmor, uncomment the next line to run unconfined:
224 #lxc.apparmor.profile = unconfined
226 lxc.mount.auto = cgroup:mixed proc:mixed sys:mixed
227 lxc.mount.entry = shm /dev/shm tmpfs defaults 0 0
236 for dir
in ${libdirs}; do
237 if [ -d "/${dir}" ] && [ -d "${rootfs}/${dir}" ]; then
238 echo "lxc.mount.entry = /${dir} ${dir} none ro,bind 0 0" >> "${path}/config"
241 echo "lxc.mount.entry = /sys/kernel/security sys/kernel/security none ro,bind,optional 0 0" >> "${path}/config"
248 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
249 chown
"${LXC_MAPPED_UID}" "${path}/config" > /dev
/null
2>&1
250 chown
-R root
"${path}/rootfs" > /dev
/null
2>&1
253 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
254 chgrp
"${LXC_MAPPED_GID}" "${path}/config" > /dev
/null
2>&1
255 chgrp
-R root
"${path}/rootfs" > /dev
/null
2>&1
261 LXC busybox image builder
264 [ -h | --help ]: Print this help message and exit.
266 LXC internal arguments (do not pass manually!):
267 [ --name <name> ]: The container name
268 [ --path <path> ]: The path to the container
269 [ --rootfs <rootfs> ]: The path to the container's rootfs
270 [ --mapped-uid <map> ]: A uid map (user namespaces)
271 [ --mapped-gid <map> ]: A gid map (user namespaces)
276 if ! options
=$
(getopt
-o hp
:n
: -l help,rootfs
:,path
:,name
:,mapped-uid
:,mapped-gid
: -- "$@"); then
280 eval set -- "$options"
285 -h|
--help) usage
exit 1;;
286 -n|
--name) name
=$2; shift 2;;
287 -p|
--path) path
=$2; shift 2;;
288 --rootfs) rootfs
=$2; shift 2;;
289 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
290 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
291 --) shift 1; break ;;
296 # Check that we have all variables we need
297 if [ -z "${name}" ] || [ -z "${path}" ] || [ -z "${rootfs}" ]; then
298 echo "ERROR: Please pass the name, path, and rootfs for the container" 1>&2
303 config
="$path/config"
304 if [ -z "$rootfs" ]; then
305 if grep -q '^lxc.rootfs.path' "${config}" 2> /dev
/null
; then
306 rootfs
=$
(awk -F= '/^lxc.rootfs.path =/{ print $2 }' "${config}")
308 rootfs
="${path}/rootfs"
312 if ! install_busybox
"${rootfs}" "${name}"; then
313 echo "ERROR: Failed to install rootfs"
317 if ! configure_busybox
"${rootfs}"; then
318 echo "ERROR: Failed to configure busybox"
322 if ! copy_configuration
"${path}" "${rootfs}" "${name}"; then
323 echo "ERROR: Failed to write config file"
327 if ! remap_userns
"${path}"; then
328 echo "ERROR: Failed to change idmappings"