lxc-busybox.in revision 7a409fd5167ecdcbf33a64f1cf9202dc051f0dcf
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek#!/bin/bash
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek#
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek# lxc: linux Container library
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
d6d50c17e94dc0d3000345e8a933311c14bbb828Jakub Hrozek# Authors:
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek# Daniel Lezcano <daniel.lezcano@free.fr>
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek# This library is free software; you can redistribute it and/or
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek# modify it under the terms of the GNU Lesser General Public
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek# License as published by the Free Software Foundation; either
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek# version 2.1 of the License, or (at your option) any later version.
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek# This library is distributed in the hope that it will be useful,
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek# but WITHOUT ANY WARRANTY; without even the implied warranty of
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek# Lesser General Public License for more details.
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek# You should have received a copy of the GNU Lesser General Public
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek# License along with this library; if not, write to the Free Software
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozekinstall_busybox()
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek{
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek rootfs=$1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek name=$2
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek res=0
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek tree="\
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/selinux \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/dev \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/home \
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek$rootfs/root \
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek$rootfs/etc \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/etc/init.d \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/bin \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/usr/bin \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/sbin \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/usr/sbin \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/proc \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/sys \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/mnt \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/tmp \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/var/log \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/usr/share/udhcpc \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/dev/pts \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/dev/shm \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/lib \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/usr/lib \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/lib64 \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$rootfs/usr/lib64"
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek mkdir -p $tree || return 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek chmod 755 $tree || return 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek pushd $rootfs/dev > /dev/null || return 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # minimal devices needed for busybox
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek mknod tty c 5 0 || res=1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek mknod console c 5 1 || res=1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek chmod 666 tty console || res=1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek mknod tty0 c 4 0 || res=1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek mknod tty1 c 4 0 || res=1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek mknod tty5 c 4 0 || res=1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek chmod 666 tty0 || res=1
fbeb1aba9e11e7aab8adac943276ca040f0c5311Jakub Hrozek mknod ram0 b 1 0 || res=1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek chmod 600 ram0 || res=1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek mknod null c 1 3 || res=1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek chmod 666 null || res=1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek mknod urandom c 1 9 || res=1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek chmod 666 urandom || res=1
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek popd > /dev/null
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # root user defined
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek cat <<EOF >> $rootfs/etc/passwd
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekroot:x:0:0:root:/root:/bin/sh
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub HrozekEOF
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek cat <<EOF >> $rootfs/etc/group
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekroot:x:0:root
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub HrozekEOF
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # mount everything
fbeb1aba9e11e7aab8adac943276ca040f0c5311Jakub Hrozek cat <<EOF >> $rootfs/etc/init.d/rcS
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek#!/bin/sh
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek/bin/syslogd
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek/bin/mount -a
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek/bin/udhcpc
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub HrozekEOF
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # executable
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek chmod 744 $rootfs/etc/init.d/rcS || return 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # mount points
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek cat <<EOF >> $rootfs/etc/fstab
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekshm /dev/shm tmpfs defaults 0 0
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub HrozekEOF
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # writable and readable for other
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek chmod 644 $rootfs/etc/fstab || return 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # launch rcS first then make a console available
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # and propose a shell on the tty, the last one is
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # not needed
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek cat <<EOF >> $rootfs/etc/inittab
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek::sysinit:/etc/init.d/rcS
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozektty1::respawn:/bin/getty -L tty1 115200 vt100
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekconsole::askfirst:/bin/sh
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub HrozekEOF
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # writable and readable for other
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek chmod 644 $rootfs/etc/inittab || return 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek cat <<EOF >> $rootfs/usr/share/udhcpc/default.script
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek#!/bin/sh
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekcase "\$1" in
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek deconfig)
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ip addr flush dev \$interface
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ;;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek renew|bound)
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # flush all the routes
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if [ -n "\$router" ]; then
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ip route del default 2> /dev/null
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek fi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # check broadcast
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if [ -n "\$broadcast" ]; then
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek broadcast="broadcast \$broadcast"
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek fi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # add a new ip address
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ip addr add \$ip/\$mask \$broadcast dev \$interface
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if [ -n "\$router" ]; then
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ip route add default via \$router dev \$interface
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek fi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek [ -n "\$domain" ] && echo search \$domain > /etc/resolv.conf
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek for i in \$dns ; do
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek echo nameserver \$i >> /etc/resolv.conf
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek done
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ;;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekesac
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekexit 0
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub HrozekEOF
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek chmod 744 $rootfs/usr/share/udhcpc/default.script
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return $res
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek}
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekconfigure_busybox()
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek{
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek rootfs=$1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek which busybox >/dev/null 2>&1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if [ $? -ne 0 ]; then
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek echo "busybox executable is not accessible"
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek fi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek file $(which busybox) | grep -q "statically linked"
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if [ $? -ne 0 ]; then
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek echo "warning : busybox is not statically linked."
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek echo "warning : The template script may not correctly"
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek echo "warning : setup the container environment."
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek fi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # copy busybox in the rootfs
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek cp $(which busybox) $rootfs/bin
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if [ $? -ne 0 ]; then
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek echo "failed to copy busybox in the rootfs"
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek fi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # symlink busybox for the commands it supports
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # it would be nice to just use "chroot $rootfs busybox --install -s /bin"
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # but that only works right in a chroot with busybox >= 1.19.0
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek pushd $rootfs/bin > /dev/null || return 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ./busybox --help | grep 'Currently defined functions:' -A300 | \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek grep -v 'Currently defined functions:' | tr , '\n' | \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek xargs -n1 ln -s busybox
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek popd > /dev/null
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # relink /sbin/init
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ln $rootfs/bin/busybox $rootfs/sbin/init
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # passwd exec must be setuid
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek chmod +s $rootfs/bin/passwd
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek touch $rootfs/etc/shadow
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek # setting passwd for root
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek CHPASSWD_FILE=$rootfs/root/chpasswd.sh
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek cat <<EOF >$CHPASSWD_FILE
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekecho "setting root password to \"root\""
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekmount -n --bind /lib $rootfs/lib
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozekif [ \$? -ne 0 ]; then
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek echo "Failed bind-mounting /lib at $rootfs/lib"
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek exit 1
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekfi
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekchroot $rootfs chpasswd <<EOFF 2>/dev/null
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekroot:root
a23014d69b56cbdf48ad05229c334648b5309d8fJakub HrozekEOFF
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekif [ \$? -ne 0 ]; then
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek echo "Failed to change root password"
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek exit 1
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekfi
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekumount $rootfs/lib
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub HrozekEOF
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek lxc-unshare -s MOUNT -- /bin/sh < $CHPASSWD_FILE
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek rm $CHPASSWD_FILE
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek # add ssh functionality if dropbear package available on host
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek which dropbear >/dev/null 2>&1
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if [ $? -eq 0 ]; then
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek # copy dropbear binary
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek cp $(which dropbear) $rootfs/usr/sbin
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if [ $? -ne 0 ]; then
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek echo "Failed to copy dropbear in the rootfs"
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek return 1
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek fi
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek # make symlinks to various ssh utilities
fbeb1aba9e11e7aab8adac943276ca040f0c5311Jakub Hrozek utils="\
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek $rootfs/usr/bin/dbclient \
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek $rootfs/usr/bin/scp \
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek $rootfs/usr/bin/ssh \
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek $rootfs/usr/sbin/dropbearkey \
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek $rootfs/usr/sbin/dropbearconvert \
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek "
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek echo $utils | xargs -n1 ln -s /usr/sbin/dropbear
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek # add necessary config files
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek mkdir $rootfs/etc/dropbear
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek dropbearkey -t rsa -f $rootfs/etc/dropbear/dropbear_rsa_host_key &> /dev/null
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek dropbearkey -t dss -f $rootfs/etc/dropbear/dropbear_dss_host_key &> /dev/null
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek echo "'dropbear' ssh utility installed"
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek fi
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek return 0
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek}
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozekcopy_configuration()
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek{
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek path=$1
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek rootfs=$2
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek name=$3
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekgrep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozekcat <<EOF >> $path/config
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozeklxc.utsname = $name
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozeklxc.tty = 1
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozeklxc.pts = 1
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozeklxc.cap.drop = sys_module mac_admin mac_override sys_time
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek# When using LXC with apparmor, uncomment the next line to run unconfined:
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek#lxc.aa_profile = unconfined
a23014d69b56cbdf48ad05229c334648b5309d8fJakub HrozekEOF
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek libdirs="\
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek lib \
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek usr/lib \
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek lib64 \
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek usr/lib64"
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek for dir in $libdirs; do
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if [ -d "/$dir" ] && [ -d "$rootfs/$dir" ]; then
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek echo "lxc.mount.entry = /$dir $dir none ro,bind 0 0" >> $path/config
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek fi
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek done
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek echo "lxc.mount.entry = /sys/kernel/security sys/kernel/security none ro,bind 0 0" >>$path/config
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek echo "lxc.mount.auto = proc:mixed sys" >>$path/config
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek}
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekusage()
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek{
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek cat <<EOF
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek$1 -h|--help -p|--path=<path>
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub HrozekEOF
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return 0
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek}
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekoptions=$(getopt -o hp:n: -l help,rootfs:,path:,name: -- "$@")
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekif [ $? -ne 0 ]; then
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek usage $(basename $0)
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek exit 1
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozekfi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekeval set -- "$options"
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekwhile true
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekdo
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek case "$1" in
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek -h|--help) usage $0 && exit 0;;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek -p|--path) path=$2; shift 2;;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek --rootfs) rootfs=$2; shift 2;;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek -n|--name) name=$2; shift 2;;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek --) shift 1; break ;;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek *) break ;;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek esac
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekdone
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekif [ "$(id -u)" != "0" ]; then
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek echo "This script should be run as 'root'"
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek exit 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekfi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekif [ -z "$path" ]; then
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek echo "'path' parameter is required"
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek exit 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekfi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek# detect rootfs
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekconfig="$path/config"
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekif [ -z "$rootfs" ]; then
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek else
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek rootfs=$path/rootfs
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek fi
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekfi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekinstall_busybox $rootfs $name
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekif [ $? -ne 0 ]; then
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek echo "failed to install busybox's rootfs"
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek exit 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekfi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekconfigure_busybox $rootfs
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozekif [ $? -ne 0 ]; then
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek echo "failed to configure busybox template"
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek exit 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekfi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekcopy_configuration $path $rootfs $name
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekif [ $? -ne 0 ]; then
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek echo "failed to write configuration file"
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek exit 1
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekfi
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek