4 # lxc: linux Container library
7 # Daniel Lezcano <daniel.lezcano@free.fr>
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.
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.
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
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 SUITE=${SUITE:-jessie}
25 MIRROR=${MIRROR:-"http://cdn.debian.net/debian/"}
32 # Squeeze only has /dev/tty and /dev/tty0 by default,
33 # therefore creating missing device nodes for tty1-4.
34 test -e $rootfs/dev/tty1 || mknod $rootfs/dev/tty1 c 4 1
35 test -e $rootfs/dev/tty2 || mknod $rootfs/dev/tty2 c 4 2
36 test -e $rootfs/dev/tty3 || mknod $rootfs/dev/tty3 c 4 3
37 test -e $rootfs/dev/tty4 || mknod $rootfs/dev/tty4 c 4 4
39 # configure the inittab
40 cat <<EOF > $rootfs/etc/inittab
42 si::sysinit:/etc/init.d/rcS
43 l0:0:wait:/etc/init.d/rc 0
44 l1:1:wait:/etc/init.d/rc 1
45 l2:2:wait:/etc/init.d/rc 2
46 l3:3:wait:/etc/init.d/rc 3
47 l4:4:wait:/etc/init.d/rc 4
48 l5:5:wait:/etc/init.d/rc 5
49 l6:6:wait:/etc/init.d/rc 6
50 # Normally not reached, but fallthrough in case of emergency.
51 z6:6:respawn:/sbin/sulogin
52 1:2345:respawn:/sbin/getty 38400 console
53 c1:12345:respawn:/sbin/getty 38400 tty1 linux
54 c2:12345:respawn:/sbin/getty 38400 tty2 linux
55 c3:12345:respawn:/sbin/getty 38400 tty3 linux
56 c4:12345:respawn:/sbin/getty 38400 tty4 linux
59 # disable selinux in debian
60 mkdir -p $rootfs/selinux
61 echo 0 > $rootfs/selinux/enforce
63 if grep -q ^lxc.network.ipv4 $path/config
65 # configure static network
66 lxc_network_ipv4=`grep ^lxc.network.ipv4 $path/config | cut -d= -f2`
67 address=`echo $lxc_network_ipv4 | cut -d/ -f1`
68 netmask=`netmask -s $lxc_network_ipv4 | cut -d/ -f2`
69 gateway=`grep ^\#lxc.network.ipv4.gw $path/config | cut -d= -f2`
70 if [ -e $rootfs/etc/network/interfaces ]
72 cat <<EOF > $rootfs/etc/network/interfaces
74 iface lo inet loopback
77 iface eth0 inet static
81 if [ ! -z "$gateway" ]
83 cat <<EOF >> $rootfs/etc/network/interfaces
87 cat <<EOF >> $rootfs/etc/hosts
90 echo "Network configured with static ip"
95 # configure the network using the dhcp
96 if [ -e $rootfs/etc/network/interfaces ]
98 cat <<EOF > $rootfs/etc/network/interfaces
100 iface lo inet loopback
105 cat <<EOF >> $rootfs/etc/hosts
108 echo "Network configured with dhcp"
113 # When domain is not local, avahi completed by send hostname
114 hostnamebase=`echo $hostname | cut -d. -f1`
115 if [ "$hostnamebase.local" != "$hostname" ]
117 cat <<EOF >> $rootfs/etc/dhcp/dhclient.conf
118 send host-name "$hostnamebase";
123 cat <<EOF > $rootfs/etc/hostname
127 # reconfigure some services
128 if [ -z "$LANG" ]; then
129 chroot $rootfs locale-gen en_US.UTF-8
130 chroot $rootfs update-locale LANG=en_US.UTF-8
132 chroot $rootfs locale-gen $LANG
133 chroot $rootfs update-locale LANG=$LANG
136 # remove pointless services in a container
137 chroot $rootfs /usr/sbin/update-rc.d -f checkroot remove
138 chroot $rootfs /usr/sbin/update-rc.d -f umountfs remove
139 chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh remove
140 chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh remove
143 if [ -f $rootfs/etc/init.d/avahi-daemon ]
146 sed -i -e "s!-D!-D --no-rlimits!" $rootfs/etc/init.d/avahi-daemon
149 echo "root:root" | chroot $rootfs chpasswd
150 echo "Root password is 'root', please change !"
158 if grep -q ^\#lxc.pubkey $path/config
160 pubkey=`grep ^\#lxc.pubkey $path/config | cut -d= -f2`
162 if [ ! -e $rootfs/root/.ssh ]
164 mkdir $rootfs/root/.ssh
166 if [ ! -z "$pubkey" ]
168 echo "Copying $pubkey -> $rootfs/root/.ssh/authorized_keys"
169 cp $pubkey $rootfs/root/.ssh/authorized_keys
173 configure_hostkeydir(){
181 [ -d $rootfs/etc/ssh ] || mkdir -p $rootfs/etc/ssh
182 if [ -e $hostkeydir ]
184 if [ ! -d $hostkeydir/$name ]
186 mkdir $hostkeydir/$name
187 echo "Creating SSH2 RSA key; this may take some time ..."
188 ssh-keygen -q -f $hostkeydir/$name/ssh_host_rsa_key -N '' -t rsa
189 echo "Creating SSH2 DSA key; this may take some time ..."
190 ssh-keygen -q -f $hostkeydir/$name/ssh_host_dsa_key -N '' -t dsa
192 cp $hostkeydir/$name/ssh_host_* $rootfs/etc/ssh/
229 # check the mini debian was not already downloaded
230 mkdir -p "$cache/partial-$SUITE-$arch"
231 if [ $? -ne 0 ]; then
232 echo "Failed to create '$cache/partial-$SUITE-$arch' directory"
236 # download a mini debian into a cache
237 echo "Downloading debian minimal ..."
238 debootstrap --verbose --variant=minbase --arch=$arch \
239 --include $packages \
240 "$SUITE" "$cache/partial-$SUITE-$arch" $MIRROR
241 if [ $? -ne 0 ]; then
242 echo "Failed to download the rootfs, aborting."
246 mv "$1/partial-$SUITE-$arch" "$1/rootfs-$SUITE-$arch"
247 echo "Download complete."
258 # make a local copy of the minidebian
259 echo -n "Copying rootfs to $rootfs..."
260 [ -d $rootfs ] && rmdir $rootfs
261 cp --reflink=auto -a "$cache/rootfs-$SUITE-$arch" $rootfs || return 1
267 cache="/var/cache/lxc/debian-8"
269 mkdir -p /var/lock/subsys/
272 if [ $? -ne 0 ]; then
273 echo "Cache repository is busy."
278 if [ "$arch" == "x86_64" ]; then
282 if [ "$arch" == "i686" ]; then
286 echo "Checking cache download in $cache/rootfs-$SUITE-$arch ... "
287 if [ ! -e "$cache/rootfs-$SUITE-$arch" ]; then
288 download_debian $cache $arch
289 if [ $? -ne 0 ]; then
290 echo "Failed to download 'debian base'"
295 copy_debian $cache $arch $rootfs
296 if [ $? -ne 0 ]; then
297 echo "Failed to copy rootfs"
303 ) 200>/var/lock/subsys/lxc
310 dd if=/dev/urandom bs=1 count=3 2>/dev/null | od -tx1 | head -1 | cut -d' ' -f2- | awk '{ print "00:16:3e:"$1":"$2":"$3 }'
320 if [ ! -e "$macdir/$name" ]
322 gen_mac > $macdir/$name
324 macaddr=`cat $macdir/$name`
326 cat <<EOF >> $path/config
327 lxc.network.hwaddr = $macaddr
331 lxc.cgroup.devices.deny = a
333 lxc.cgroup.devices.allow = c 1:3 rwm
334 lxc.cgroup.devices.allow = c 1:5 rwm
336 lxc.cgroup.devices.allow = c 5:1 rwm
337 lxc.cgroup.devices.allow = c 5:0 rwm
338 lxc.cgroup.devices.allow = c 4:0 rwm
339 lxc.cgroup.devices.allow = c 4:1 rwm
341 lxc.cgroup.devices.allow = c 1:9 rwm
342 lxc.cgroup.devices.allow = c 1:8 rwm
343 lxc.cgroup.devices.allow = c 136:* rwm
344 lxc.cgroup.devices.allow = c 5:2 rwm
346 lxc.cgroup.devices.allow = c 254:0 rwm
349 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
350 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
351 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults 0 0
354 if [ $? -ne 0 ]; then
355 echo "Failed to add configuration"
364 cache="/var/cache/lxc/debian-8"
366 if [ ! -e $cache ]; then
370 # lock, so we won't purge while someone is creating a repository
374 echo "Cache repository is busy."
378 echo -n "Purging the download cache..."
379 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
382 ) 200>/var/lock/subsys/lxc
388 $1 -h|--help -p|--path=<path> --clean
393 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
394 if [ $? -ne 0 ]; then
398 eval set -- "$options"
403 -h|--help) usage $0 && exit 0;;
404 -p|--path) path=$2; shift 2;;
405 -n|--name) name=$2; shift 2;;
406 -c|--clean) clean=$2; shift 2;;
407 --) shift 1; break ;;
412 if [ ! -z "$clean" -a -z "$path" ]; then
418 if [ $? -ne 0 ]; then
419 echo "'debootstrap' command is missing"
423 if [ -z "$path" ]; then
424 echo "'path' parameter is required"
428 if [ "$(id -u)" != "0" ]; then
429 echo "This script should be run as 'root'"
434 oldhostkeydir=/var/lib/lxc/ssh
435 hostkeydir=/var/cache/lxc/ssh
436 macdir=/var/cache/lxc/mac
437 if [ -e $oldhostkeydir ]
439 if [ ! -e $hostkeydir ]
441 mv $oldhostkeydir $hostkeydir
444 if [ ! -e $hostkeydir ]
453 install_debian $rootfs
454 if [ $? -ne 0 ]; then
455 echo "failed to install debian"
459 configure_debian $rootfs $name
460 if [ $? -ne 0 ]; then
461 echo "failed to configure debian for a container"
465 copy_configuration $path $rootfs $name $macdir
466 if [ $? -ne 0 ]; then
467 echo "failed write configuration file"
471 configure_pubkey $path $rootfs
472 if [ $? -ne 0 ]; then
473 echo "failed to configure pubkey"
477 configure_hostkeydir $rootfs $hostkeydir $name
478 if [ $? -ne 0 ]; then
479 echo "failed to configure hostkeydir"
483 if [ ! -z $clean ]; then