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/
218 # check the mini debian was not already downloaded
219 mkdir -p "$cache/partial-$SUITE-$arch"
220 if [ $? -ne 0 ]; then
221 echo "Failed to create '$cache/partial-$SUITE-$arch' directory"
225 # download a mini debian into a cache
226 echo "Downloading debian minimal ..."
227 debootstrap --verbose --variant=minbase --arch=$arch \
228 --include $packages \
229 "$SUITE" "$cache/partial-$SUITE-$arch" $MIRROR
230 if [ $? -ne 0 ]; then
231 echo "Failed to download the rootfs, aborting."
235 mv "$1/partial-$SUITE-$arch" "$1/rootfs-$SUITE-$arch"
236 echo "Download complete."
247 # make a local copy of the minidebian
248 echo -n "Copying rootfs to $rootfs..."
249 cp -a "$cache/rootfs-$SUITE-$arch" $rootfs || return 1
255 cache="/var/cache/lxc/debian"
257 mkdir -p /var/lock/subsys/
260 if [ $? -ne 0 ]; then
261 echo "Cache repository is busy."
266 if [ "$arch" == "x86_64" ]; then
270 if [ "$arch" == "i686" ]; then
274 echo "Checking cache download in $cache/rootfs-$SUITE-$arch ... "
275 if [ ! -e "$cache/rootfs-$SUITE-$arch" ]; then
276 download_debian $cache $arch
277 if [ $? -ne 0 ]; then
278 echo "Failed to download 'debian base'"
283 copy_debian $cache $arch $rootfs
284 if [ $? -ne 0 ]; then
285 echo "Failed to copy rootfs"
291 ) 200>/var/lock/subsys/lxc
298 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 }'
308 if [ ! -e "$macdir/$name" ]
310 gen_mac > $macdir/$name
312 macaddr=`cat $macdir/$name`
314 cat <<EOF >> $path/config
315 lxc.network.hwaddr = $macaddr
319 lxc.cgroup.devices.deny = a
321 lxc.cgroup.devices.allow = c 1:3 rwm
322 lxc.cgroup.devices.allow = c 1:5 rwm
324 lxc.cgroup.devices.allow = c 5:1 rwm
325 lxc.cgroup.devices.allow = c 5:0 rwm
326 lxc.cgroup.devices.allow = c 4:0 rwm
327 lxc.cgroup.devices.allow = c 4:1 rwm
329 lxc.cgroup.devices.allow = c 1:9 rwm
330 lxc.cgroup.devices.allow = c 1:8 rwm
331 lxc.cgroup.devices.allow = c 136:* rwm
332 lxc.cgroup.devices.allow = c 5:2 rwm
334 lxc.cgroup.devices.allow = c 254:0 rwm
337 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
338 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
339 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults 0 0
342 if [ $? -ne 0 ]; then
343 echo "Failed to add configuration"
352 cache="/var/cache/lxc/debian"
354 if [ ! -e $cache ]; then
358 # lock, so we won't purge while someone is creating a repository
362 echo "Cache repository is busy."
366 echo -n "Purging the download cache..."
367 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
370 ) 200>/var/lock/subsys/lxc
376 $1 -h|--help -p|--path=<path> --clean
381 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
382 if [ $? -ne 0 ]; then
386 eval set -- "$options"
391 -h|--help) usage $0 && exit 0;;
392 -p|--path) path=$2; shift 2;;
393 -n|--name) name=$2; shift 2;;
394 -c|--clean) clean=$2; shift 2;;
395 --) shift 1; break ;;
400 if [ ! -z "$clean" -a -z "$path" ]; then
406 if [ $? -ne 0 ]; then
407 echo "'debootstrap' command is missing"
411 if [ -z "$path" ]; then
412 echo "'path' parameter is required"
416 if [ "$(id -u)" != "0" ]; then
417 echo "This script should be run as 'root'"
422 oldhostkeydir=/var/lib/lxc/ssh
423 hostkeydir=/var/cache/lxc/ssh
424 macdir=/var/cache/lxc/mac
425 if [ -e $oldhostkeydir ]
427 if [ ! -e $hostkeydir ]
429 mv $oldhostkeydir $hostkeydir
432 if [ ! -e $hostkeydir ]
441 install_debian $rootfs
442 if [ $? -ne 0 ]; then
443 echo "failed to install debian"
447 configure_debian $rootfs $name
448 if [ $? -ne 0 ]; then
449 echo "failed to configure debian for a container"
453 copy_configuration $path $rootfs $name $macdir
454 if [ $? -ne 0 ]; then
455 echo "failed write configuration file"
459 configure_pubkey $path $rootfs
460 if [ $? -ne 0 ]; then
461 echo "failed to configure pubkey"
465 configure_hostkeydir $rootfs $hostkeydir $name
466 if [ $? -ne 0 ]; then
467 echo "failed to configure hostkeydir"
471 if [ ! -z $clean ]; then