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:-wheezy}
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/
226 # check the mini debian was not already downloaded
227 mkdir -p "$cache/partial-$SUITE-$arch"
228 if [ $? -ne 0 ]; then
229 echo "Failed to create '$cache/partial-$SUITE-$arch' directory"
233 # download a mini debian into a cache
234 echo "Downloading debian minimal ..."
235 debootstrap --verbose --variant=minbase --arch=$arch \
236 --include $packages \
237 "$SUITE" "$cache/partial-$SUITE-$arch" $MIRROR
238 if [ $? -ne 0 ]; then
239 echo "Failed to download the rootfs, aborting."
243 mv "$1/partial-$SUITE-$arch" "$1/rootfs-$SUITE-$arch"
244 echo "Download complete."
255 # make a local copy of the minidebian
256 echo -n "Copying rootfs to $rootfs..."
257 cp -a "$cache/rootfs-$SUITE-$arch" $rootfs || return 1
263 cache="/var/cache/lxc/debian"
265 mkdir -p /var/lock/subsys/
268 if [ $? -ne 0 ]; then
269 echo "Cache repository is busy."
274 if [ "$arch" == "x86_64" ]; then
278 if [ "$arch" == "i686" ]; then
282 echo "Checking cache download in $cache/rootfs-$SUITE-$arch ... "
283 if [ ! -e "$cache/rootfs-$SUITE-$arch" ]; then
284 download_debian $cache $arch
285 if [ $? -ne 0 ]; then
286 echo "Failed to download 'debian base'"
291 copy_debian $cache $arch $rootfs
292 if [ $? -ne 0 ]; then
293 echo "Failed to copy rootfs"
299 ) 200>/var/lock/subsys/lxc
306 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 }'
316 if [ ! -e "$macdir/$name" ]
318 gen_mac > $macdir/$name
320 macaddr=`cat $macdir/$name`
322 cat <<EOF >> $path/config
323 lxc.network.hwaddr = $macaddr
327 lxc.cgroup.devices.deny = a
329 lxc.cgroup.devices.allow = c 1:3 rwm
330 lxc.cgroup.devices.allow = c 1:5 rwm
332 lxc.cgroup.devices.allow = c 5:1 rwm
333 lxc.cgroup.devices.allow = c 5:0 rwm
334 lxc.cgroup.devices.allow = c 4:0 rwm
335 lxc.cgroup.devices.allow = c 4:1 rwm
337 lxc.cgroup.devices.allow = c 1:9 rwm
338 lxc.cgroup.devices.allow = c 1:8 rwm
339 lxc.cgroup.devices.allow = c 136:* rwm
340 lxc.cgroup.devices.allow = c 5:2 rwm
342 lxc.cgroup.devices.allow = c 254:0 rwm
345 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
346 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
347 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults 0 0
350 if [ $? -ne 0 ]; then
351 echo "Failed to add configuration"
360 cache="/var/cache/lxc/debian"
362 if [ ! -e $cache ]; then
366 # lock, so we won't purge while someone is creating a repository
370 echo "Cache repository is busy."
374 echo -n "Purging the download cache..."
375 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
378 ) 200>/var/lock/subsys/lxc
384 $1 -h|--help -p|--path=<path> --clean
389 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
390 if [ $? -ne 0 ]; then
394 eval set -- "$options"
399 -h|--help) usage $0 && exit 0;;
400 -p|--path) path=$2; shift 2;;
401 -n|--name) name=$2; shift 2;;
402 -c|--clean) clean=$2; shift 2;;
403 --) shift 1; break ;;
408 if [ ! -z "$clean" -a -z "$path" ]; then
414 if [ $? -ne 0 ]; then
415 echo "'debootstrap' command is missing"
419 if [ -z "$path" ]; then
420 echo "'path' parameter is required"
424 if [ "$(id -u)" != "0" ]; then
425 echo "This script should be run as 'root'"
430 oldhostkeydir=/var/lib/lxc/ssh
431 hostkeydir=/var/cache/lxc/ssh
432 macdir=/var/cache/lxc/mac
433 if [ -e $oldhostkeydir ]
435 if [ ! -e $hostkeydir ]
437 mv $oldhostkeydir $hostkeydir
440 if [ ! -e $hostkeydir ]
449 install_debian $rootfs
450 if [ $? -ne 0 ]; then
451 echo "failed to install debian"
455 configure_debian $rootfs $name
456 if [ $? -ne 0 ]; then
457 echo "failed to configure debian for a container"
461 copy_configuration $path $rootfs $name $macdir
462 if [ $? -ne 0 ]; then
463 echo "failed write configuration file"
467 configure_pubkey $path $rootfs
468 if [ $? -ne 0 ]; then
469 echo "failed to configure pubkey"
473 configure_hostkeydir $rootfs $hostkeydir $name
474 if [ $? -ne 0 ]; then
475 echo "failed to configure hostkeydir"
479 if [ ! -z $clean ]; then