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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 SUITE=${SUITE:-squeeze}
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 if [ -e $rootfs/etc/network/interfaces ]
71 cat <<EOF > $rootfs/etc/network/interfaces
73 iface lo inet loopback
76 iface eth0 inet static
81 cat <<EOF >> $rootfs/etc/hosts
84 echo "Network configured with static ip"
89 # configure the network using the dhcp
90 if [ -e $rootfs/etc/network/interfaces ]
92 cat <<EOF > $rootfs/etc/network/interfaces
94 iface lo inet loopback
99 cat <<EOF >> $rootfs/etc/hosts
102 echo "Network configured with dhcp"
107 # When domain is not local, avahi completed by send hostname
108 hostnamebase=`echo $hostname | cut -d. -f1`
109 if [ "$hostnamebase.local" != "$hostname" ]
111 cat <<EOF >> $rootfs/etc/dhcp/dhclient.conf
112 send host-name "$hostnamebase";
117 cat <<EOF > $rootfs/etc/hostname
121 # reconfigure some services
122 if [ -z "$LANG" ]; then
123 chroot $rootfs locale-gen en_US.UTF-8
124 chroot $rootfs update-locale LANG=en_US.UTF-8
126 chroot $rootfs locale-gen $LANG
127 chroot $rootfs update-locale LANG=$LANG
130 # remove pointless services in a container
131 chroot $rootfs /usr/sbin/update-rc.d -f checkroot remove
132 chroot $rootfs /usr/sbin/update-rc.d -f umountfs remove
133 chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh remove
134 chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh remove
137 if [ -f $rootfs/etc/init.d/avahi-daemon ]
140 sed -i -e "s!-D!-D --no-rlimits!" $rootfs/etc/init.d/avahi-daemon
143 echo "root:root" | chroot $rootfs chpasswd
144 echo "Root password is 'root', please change !"
152 if grep -q ^\#lxc.pubkey $path/config
154 pubkey=`grep ^\#lxc.pubkey $path/config | cut -d= -f2`
156 if [ ! -e $rootfs/root/.ssh ]
158 mkdir $rootfs/root/.ssh
160 if [ ! -z "$pubkey" ]
162 echo "Copying $pubkey -> $rootfs/root/.ssh/authorized_keys"
163 cp $pubkey $rootfs/root/.ssh/authorized_keys
167 configure_hostkeydir(){
175 [ -d $rootfs/etc/ssh ] || mkdir -p $rootfs/etc/ssh
176 if [ -e $hostkeydir ]
178 if [ ! -d $hostkeydir/$name ]
180 mkdir $hostkeydir/$name
181 echo "Creating SSH2 RSA key; this may take some time ..."
182 ssh-keygen -q -f $hostkeydir/$name/ssh_host_rsa_key -N '' -t rsa
183 echo "Creating SSH2 DSA key; this may take some time ..."
184 ssh-keygen -q -f $hostkeydir/$name/ssh_host_dsa_key -N '' -t dsa
186 cp $hostkeydir/$name/ssh_host_* $rootfs/etc/ssh/
219 # check the mini debian was not already downloaded
220 mkdir -p "$cache/partial-$SUITE-$arch"
221 if [ $? -ne 0 ]; then
222 echo "Failed to create '$cache/partial-$SUITE-$arch' directory"
226 # download a mini debian into a cache
227 echo "Downloading debian minimal ..."
228 debootstrap --verbose --variant=minbase --arch=$arch \
229 --include $packages \
230 "$SUITE" "$cache/partial-$SUITE-$arch" $MIRROR
231 if [ $? -ne 0 ]; then
232 echo "Failed to download the rootfs, aborting."
236 mv "$1/partial-$SUITE-$arch" "$1/rootfs-$SUITE-$arch"
237 echo "Download complete."
248 # make a local copy of the minidebian
249 echo -n "Copying rootfs to $rootfs..."
250 cp -a "$cache/rootfs-$SUITE-$arch" $rootfs || return 1
256 cache="/var/cache/lxc/debian"
258 mkdir -p /var/lock/subsys/
261 if [ $? -ne 0 ]; then
262 echo "Cache repository is busy."
267 if [ "$arch" == "x86_64" ]; then
271 if [ "$arch" == "i686" ]; then
275 echo "Checking cache download in $cache/rootfs-$SUITE-$arch ... "
276 if [ ! -e "$cache/rootfs-$SUITE-$arch" ]; then
277 download_debian $cache $arch
278 if [ $? -ne 0 ]; then
279 echo "Failed to download 'debian base'"
284 copy_debian $cache $arch $rootfs
285 if [ $? -ne 0 ]; then
286 echo "Failed to copy rootfs"
292 ) 200>/var/lock/subsys/lxc
299 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 }'
309 if [ ! -e "$macdir/$name" ]
311 gen_mac > $macdir/$name
313 macaddr=`cat $macdir/$name`
315 cat <<EOF >> $path/config
316 lxc.network.hwaddr = $macaddr
320 lxc.cgroup.devices.deny = a
322 lxc.cgroup.devices.allow = c 1:3 rwm
323 lxc.cgroup.devices.allow = c 1:5 rwm
325 lxc.cgroup.devices.allow = c 5:1 rwm
326 lxc.cgroup.devices.allow = c 5:0 rwm
327 lxc.cgroup.devices.allow = c 4:0 rwm
328 lxc.cgroup.devices.allow = c 4:1 rwm
330 lxc.cgroup.devices.allow = c 1:9 rwm
331 lxc.cgroup.devices.allow = c 1:8 rwm
332 lxc.cgroup.devices.allow = c 136:* rwm
333 lxc.cgroup.devices.allow = c 5:2 rwm
335 lxc.cgroup.devices.allow = c 254:0 rwm
338 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
339 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
340 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults 0 0
343 if [ $? -ne 0 ]; then
344 echo "Failed to add configuration"
353 cache="/var/cache/lxc/debian"
355 if [ ! -e $cache ]; then
359 # lock, so we won't purge while someone is creating a repository
363 echo "Cache repository is busy."
367 echo -n "Purging the download cache..."
368 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
371 ) 200>/var/lock/subsys/lxc
377 $1 -h|--help -p|--path=<path> --clean
382 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
383 if [ $? -ne 0 ]; then
387 eval set -- "$options"
392 -h|--help) usage $0 && exit 0;;
393 -p|--path) path=$2; shift 2;;
394 -n|--name) name=$2; shift 2;;
395 -c|--clean) clean=$2; shift 2;;
396 --) shift 1; break ;;
401 if [ ! -z "$clean" -a -z "$path" ]; then
407 if [ $? -ne 0 ]; then
408 echo "'debootstrap' command is missing"
412 if [ -z "$path" ]; then
413 echo "'path' parameter is required"
417 if [ "$(id -u)" != "0" ]; then
418 echo "This script should be run as 'root'"
423 oldhostkeydir=/var/lib/lxc/ssh
424 hostkeydir=/var/cache/lxc/ssh
425 macdir=/var/cache/lxc/mac
426 if [ -e $oldhostkeydir ]
428 if [ ! -e $hostkeydir ]
430 mv $oldhostkeydir $hostkeydir
433 if [ ! -e $hostkeydir ]
442 install_debian $rootfs
443 if [ $? -ne 0 ]; then
444 echo "failed to install debian"
448 configure_debian $rootfs $name
449 if [ $? -ne 0 ]; then
450 echo "failed to configure debian for a container"
454 copy_configuration $path $rootfs $name $macdir
455 if [ $? -ne 0 ]; then
456 echo "failed write configuration file"
460 configure_pubkey $path $rootfs
461 if [ $? -ne 0 ]; then
462 echo "failed to configure pubkey"
466 configure_hostkeydir $rootfs $hostkeydir $name
467 if [ $? -ne 0 ]; then
468 echo "failed to configure hostkeydir"
472 if [ ! -z $clean ]; then