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/
225 # check the mini debian was not already downloaded
226 mkdir -p "$cache/partial-$SUITE-$arch"
227 if [ $? -ne 0 ]; then
228 echo "Failed to create '$cache/partial-$SUITE-$arch' directory"
232 # download a mini debian into a cache
233 echo "Downloading debian minimal ..."
234 debootstrap --verbose --variant=minbase --arch=$arch \
235 --include $packages \
236 "$SUITE" "$cache/partial-$SUITE-$arch" $MIRROR
237 if [ $? -ne 0 ]; then
238 echo "Failed to download the rootfs, aborting."
242 mv "$1/partial-$SUITE-$arch" "$1/rootfs-$SUITE-$arch"
243 echo "Download complete."
254 # make a local copy of the minidebian
255 echo -n "Copying rootfs to $rootfs..."
256 cp -a "$cache/rootfs-$SUITE-$arch" $rootfs || return 1
262 cache="/var/cache/lxc/debian"
264 mkdir -p /var/lock/subsys/
267 if [ $? -ne 0 ]; then
268 echo "Cache repository is busy."
273 if [ "$arch" == "x86_64" ]; then
277 if [ "$arch" == "i686" ]; then
281 echo "Checking cache download in $cache/rootfs-$SUITE-$arch ... "
282 if [ ! -e "$cache/rootfs-$SUITE-$arch" ]; then
283 download_debian $cache $arch
284 if [ $? -ne 0 ]; then
285 echo "Failed to download 'debian base'"
290 copy_debian $cache $arch $rootfs
291 if [ $? -ne 0 ]; then
292 echo "Failed to copy rootfs"
298 ) 200>/var/lock/subsys/lxc
305 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 }'
315 if [ ! -e "$macdir/$name" ]
317 gen_mac > $macdir/$name
319 macaddr=`cat $macdir/$name`
321 cat <<EOF >> $path/config
322 lxc.network.hwaddr = $macaddr
326 lxc.cgroup.devices.deny = a
328 lxc.cgroup.devices.allow = c 1:3 rwm
329 lxc.cgroup.devices.allow = c 1:5 rwm
331 lxc.cgroup.devices.allow = c 5:1 rwm
332 lxc.cgroup.devices.allow = c 5:0 rwm
333 lxc.cgroup.devices.allow = c 4:0 rwm
334 lxc.cgroup.devices.allow = c 4:1 rwm
336 lxc.cgroup.devices.allow = c 1:9 rwm
337 lxc.cgroup.devices.allow = c 1:8 rwm
338 lxc.cgroup.devices.allow = c 136:* rwm
339 lxc.cgroup.devices.allow = c 5:2 rwm
341 lxc.cgroup.devices.allow = c 254:0 rwm
344 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
345 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
346 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults 0 0
349 if [ $? -ne 0 ]; then
350 echo "Failed to add configuration"
359 cache="/var/cache/lxc/debian"
361 if [ ! -e $cache ]; then
365 # lock, so we won't purge while someone is creating a repository
369 echo "Cache repository is busy."
373 echo -n "Purging the download cache..."
374 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
377 ) 200>/var/lock/subsys/lxc
383 $1 -h|--help -p|--path=<path> --clean
388 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
389 if [ $? -ne 0 ]; then
393 eval set -- "$options"
398 -h|--help) usage $0 && exit 0;;
399 -p|--path) path=$2; shift 2;;
400 -n|--name) name=$2; shift 2;;
401 -c|--clean) clean=$2; shift 2;;
402 --) shift 1; break ;;
407 if [ ! -z "$clean" -a -z "$path" ]; then
413 if [ $? -ne 0 ]; then
414 echo "'debootstrap' command is missing"
418 if [ -z "$path" ]; then
419 echo "'path' parameter is required"
423 if [ "$(id -u)" != "0" ]; then
424 echo "This script should be run as 'root'"
429 oldhostkeydir=/var/lib/lxc/ssh
430 hostkeydir=/var/cache/lxc/ssh
431 macdir=/var/cache/lxc/mac
432 if [ -e $oldhostkeydir ]
434 if [ ! -e $hostkeydir ]
436 mv $oldhostkeydir $hostkeydir
439 if [ ! -e $hostkeydir ]
448 install_debian $rootfs
449 if [ $? -ne 0 ]; then
450 echo "failed to install debian"
454 configure_debian $rootfs $name
455 if [ $? -ne 0 ]; then
456 echo "failed to configure debian for a container"
460 copy_configuration $path $rootfs $name $macdir
461 if [ $? -ne 0 ]; then
462 echo "failed write configuration file"
466 configure_pubkey $path $rootfs
467 if [ $? -ne 0 ]; then
468 echo "failed to configure pubkey"
472 configure_hostkeydir $rootfs $hostkeydir $name
473 if [ $? -ne 0 ]; then
474 echo "failed to configure hostkeydir"
478 if [ ! -z $clean ]; then