4 # template script for generating centos container for LXC
8 # lxc: linux Container library
11 # Daniel Lezcano <daniel.lezcano@free.fr>
13 # This library is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU Lesser General Public
15 # License as published by the Free Software Foundation; either
16 # version 2.1 of the License, or (at your option) any later version.
18 # This library is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # Lesser General Public License for more details.
23 # You should have received a copy of the GNU Lesser General Public
24 # License along with this library; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
35 # disable selinux in centos
36 mkdir -p $rootfs/selinux
37 echo 0 > $rootfs/selinux/enforce
40 if ! grep -q "^127.0.0.1" $rootfs/etc/hosts
42 cat <<EOF >> $rootfs/etc/hosts
43 # Do not remove the following line, or various programs
44 # that require network functionality will fail.
45 127.0.0.1 localhost.localdomain localhost
46 ::1 localhost6.localdomain6 localhost6
49 if ! grep -q "^::1" $rootfs/etc/hosts
51 cat <<EOF >> $rootfs/etc/hosts
52 ::1 localhost6.localdomain6 localhost6
56 if grep -q ^lxc.network.ipv4 $path/config
58 # configure static network
59 lxc_network_ipv4=`grep ^lxc.network.ipv4 $path/config | cut -d= -f2`
60 address=`echo $lxc_network_ipv4 | cut -d/ -f1`
61 netmask=`netmask -s $lxc_network_ipv4 | cut -d/ -f2`
62 if [ -e $rootfs/etc/sysconfig/network-scripts ]
64 cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
74 cat <<EOF > $rootfs/etc/sysconfig/network
81 cat <<EOF >> $rootfs/etc/hosts
84 echo "Network configured with static ip"
90 # configure the network using the dhcp
91 if [ -e $rootfs/etc/sysconfig/network-scripts ]
93 cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
99 cat <<EOF > $rootfs/etc/sysconfig/network
105 cat <<EOF >> $rootfs/etc/hosts
108 echo "Network configured with dhcp"
112 # When domain is not local, avahi completed by send hostname
113 hostnamebase=`echo $hostname | cut -d. -f1`
114 if [ "$hostnamebase.local" != "$hostname" ]
116 cat <<EOF > $rootfs/etc/dhclient.conf
117 send host-name "$hostnamebase";
122 # provide the lxc service
123 # cat <<EOF > $rootfs/etc/init/lxc.conf
124 ## fake some events needed for correct startup other services
126 #description "Container Upstart"
131 # rm -rf /var/run/*.pid
132 # rm -rf /var/run/network/*
133 # /sbin/initctl emit stopped JOB=udevtrigger --no-wait
134 # /sbin/initctl emit started JOB=udev --no-wait
138 # cat <<EOF > $rootfs/etc/init/console.conf
141 ## This service maintains a console on tty1 from the point the system is
142 ## started until it is shut down again.
144 #start on stopped rc RUNLEVEL=[2345]
145 #stop on runlevel [!2345]
148 #exec /sbin/getty -8 38400 /dev/console
150 #sed -i -e '/getty/d' $rootfs/etc/inittab
151 # Disable tty5 and tty6
152 sed -i -e '/^5:/d' $rootfs/etc/inittab
153 sed -i -e '/^6:/d' $rootfs/etc/inittab
155 cat <<EOF >> $rootfs/etc/inittab
156 0:2345:respawn:/sbin/mingetty console
159 # cat <<EOF > $rootfs/lib/init/fstab
160 ## /lib/init/fstab: lxc system fstab
161 #none /spu spufs gid=spu,optional 0 0
162 #none /tmp none defaults 0 0
163 #none /var/lock tmpfs nodev,noexec,nosuid,showthrough 0 0
164 #none /lib/init/rw tmpfs mode=0755,nosuid,optional 0 0
166 cat <<EOF > $rootfs/etc/fstab
167 # NOT MOUNTING IN LXC
168 #none /dev/pts devpts defaults 0 0
169 #none /proc proc defaults 0 0
170 #none /sys sysfs defaults 0 0
171 #none /dev/shm tmpfs defaults 0 0
174 # reconfigure some services
175 # if [ -z "$LANG" ]; then
176 # chroot $rootfs locale-gen en_US.UTF-8
177 # chroot $rootfs update-locale LANG=en_US.UTF-8
179 # chroot $rootfs locale-gen $LANG
180 # chroot $rootfs update-locale LANG=$LANG
183 # remove pointless services in a container
184 # chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
186 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
187 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
188 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
189 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
190 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
191 mv $rootfs/sbin/hwclock $rootfs/sbin/hwclock.removed_by_lxc
193 echo "Please change root-password !"
194 echo "root:root" | chroot $rootfs chpasswd
196 sed -i -e '/start_udev/d' $rootfs/etc/rc.d/rc.sysinit
199 if [ -f $rootfs/etc/init.d/avahi-daemon ]
202 sed -i -e "s!-D!-D --no-rlimits!" $rootfs/etc/init.d/avahi-daemon
205 mv $rootfs/dev $rootfs/dev.old
207 test -e $rootfs/dev/null || mknod -m 666 $rootfs/dev/null c 1 3
208 test -e $rootfs/dev/zero || mknod -m 666 $rootfs/dev/zero c 1 5
209 test -e $rootfs/dev/random || mknod -m 666 $rootfs/dev/random c 1 8
210 test -e $rootfs/dev/urandom || mknod -m 666 $rootfs/dev/urandom c 1 9
211 test -e $rootfs/dev/pts || mkdir -m 755 $rootfs/dev/pts
212 test -e $rootfs/dev/shm || mkdir -m 1777 $rootfs/dev/shm
213 test -e $rootfs/dev/tty || mknod -m 666 $rootfs/dev/tty c 5 0
214 test -e $rootfs/dev/console || mknod -m 600 $rootfs/dev/console c 5 1
215 test -e $rootfs/dev/tty0 || mknod -m 666 $rootfs/dev/tty0 c 4 0
216 test -e $rootfs/dev/full || mknod -m 666 $rootfs/dev/full c 1 7
217 test -e $rootfs/dev/initctl || mknod -m 600 $rootfs/dev/initctl p
218 test -e $rootfs/dev/ptmx || mknod -m 666 $rootfs/dev/ptmx c 5 2
220 test -e $rootfs/dev/tty1 || mknod -m 666 $rootfs/dev/tty1 c 4 1
221 test -e $rootfs/dev/tty2 || mknod -m 666 $rootfs/dev/tty2 c 4 2
222 test -e $rootfs/dev/tty3 || mknod -m 666 $rootfs/dev/tty3 c 4 3
223 test -e $rootfs/dev/tty4 || mknod -m 666 $rootfs/dev/tty4 c 4 4
224 test -e $rootfs/dev/tty5 || mknod -m 666 $rootfs/dev/tty5 c 4 5
225 test -e $rootfs/dev/tty6 || mknod -m 666 $rootfs/dev/tty6 c 4 6
233 if grep -q ^\#lxc.pubkey $path/config
235 pubkey=`grep ^\#lxc.pubkey $path/config | cut -d= -f2`
237 if [ ! -e $rootfs/root/.ssh ]
239 mkdir $rootfs/root/.ssh
241 if [ ! -z "$pubkey" ]
243 echo "Copying $pubkey -> $rootfs/root/.ssh/authorized_keys"
244 cp $pubkey $rootfs/root/.ssh/authorized_keys
248 configure_hostkeydir(){
256 [ -d $rootfs/etc/ssh ] || mkdir -p $rootfs/etc/ssh
257 if [ -e $hostkeydir ]
259 if [ ! -d $hostkeydir/$name ]
261 mkdir $hostkeydir/$name
262 echo "Creating SSH2 RSA key; this may take some time ..."
263 ssh-keygen -q -f $hostkeydir/$name/ssh_host_rsa_key -N '' -t rsa
264 echo "Creating SSH2 DSA key; this may take some time ..."
265 ssh-keygen -q -f $hostkeydir/$name/ssh_host_dsa_key -N '' -t dsa
267 cp $hostkeydir/$name/ssh_host_* $rootfs/etc/ssh/
277 # check the mini centos was not already downloaded
278 mkdir -p "$cache/partial-$arch"
279 if [ $? -ne 0 ]; then
280 echo "Failed to create '$cache/partial-$arch' directory"
284 # download a mini centos into a cache
285 echo "Downloading centos minimal ..."
286 #febootstrap $DISTRO $cache/partial-$arch
287 rinsearch=`dpkg-architecture -qDEB_BUILD_ARCH`
288 pkglist="openssh-server man zsh bind-utils openssh-clients php wget rsync avahi php-pear php-dom java-1.6.0-openjdk xorg-x11-xauth firefox"
290 cat <<EOF > /var/cache/rinse/$DISTRO-$arch.before_post_install
291 echo "Rinse defines mirror=\$mirror in config"
292 server=\`echo \$mirror | cut -d/ -f3\`
293 echo "Disable mirrorlists"
294 sed -i -e "s/^mirrorlist=/#mirrorlist=/" $cache/partial-$arch/etc/yum.repos.d/CentOS-Base.repo
295 echo "Setup baseurl on server \$server"
296 sed -i -e "s!#baseurl=http://mirror.centos.org!baseurl=http://\$server!" $cache/partial-$arch/etc/yum.repos.d/CentOS-Base.repo
297 rm -f $cache/partial-$arch/etc/mtab
298 ln -s /proc/mounts $cache/partial-$arch/etc/mtab
300 chmod 755 /var/cache/rinse/$DISTRO-$arch.before_post_install
302 cat <<EOF > /var/cache/rinse/$DISTRO-$arch.after_post_install
305 chroot $cache/partial-$arch /bin/mount proc /proc -t proc
307 echo "Install $pkglist"
308 chroot $cache/partial-$arch /usr/bin/yum -y install $pkglist
311 if grep -q ^http_proxy /etc/wgetrc
313 PROXY=`grep ^http_proxy /etc/wgetrc | tail -1 | sed -e 's!.*http://!!' -e 's!/!!'`
316 cat <<EOF >> /var/cache/rinse/$DISTRO-$arch.after_post_install
317 # PROXY CONF FROM WGET
318 echo "Set up proxy $PROXY for pear"
319 chroot $cache/partial-$arch /usr/bin/pear config-set http_proxy $PROXY
323 cat <<EOF >> /var/cache/rinse/$DISTRO-$arch.after_post_install
325 # phpunit install, requires: php-pear php-dom
326 echo "Updating pear.php.net pear channel"
327 chroot $cache/partial-$arch /usr/bin/pear channel-update pear.php.net
328 #optional?# chroot $cache/partial-$arch /usr/bin/pear channel-discover components.ez.no
329 echo "Discovering pear.phpunit.de pear channel"
330 chroot $cache/partial-$arch /usr/bin/pear channel-discover pear.phpunit.de
331 echo "Discovering pear.symfony-project.com pear channel"
332 chroot $cache/partial-$arch /usr/bin/pear channel-discover pear.symfony-project.com
333 echo "Upgrading pear"
334 chroot $cache/partial-$arch /usr/bin/pear upgrade-all
335 chroot $cache/partial-$arch /usr/bin/pear upgrade --force PEAR
336 echo "Installing phpunit/PHPUnit"
337 chroot $cache/partial-$arch /usr/bin/pear install --alldeps phpunit/PHPUnit
340 chroot $cache/partial-$arch /bin/umount /proc
342 chmod 755 /var/cache/rinse/$DISTRO-$arch.after_post_install
344 rinse --config /etc/rinse/rinse.conf \
346 --distribution $DISTRO \
347 --before-post-install /var/cache/rinse/$DISTRO-$arch.before_post_install \
348 --after-post-install /var/cache/rinse/$DISTRO-$arch.after_post_install \
349 --directory $cache/partial-$arch
351 if [ $? -ne 0 ]; then
352 echo "Failed to download the rootfs, aborting."
356 mv "$1/partial-$arch" "$1/rootfs-$arch"
357 echo "Download complete."
368 # make a local copy of the minicentos
369 echo -n "Copying rootfs to $rootfs ..."
370 cp -a $cache/rootfs-$arch $rootfs || return 1
376 cache="/var/cache/lxc/$DISTRO"
378 mkdir -p /var/lock/subsys/
381 if [ $? -ne 0 ]; then
382 echo "Cache repository is busy."
388 echo "Checking cache download in $cache/rootfs-$arch ... "
389 if [ ! -e "$cache/rootfs-$arch" ]; then
390 download_centos $cache $arch
391 if [ $? -ne 0 ]; then
392 echo "Failed to download 'centos base'"
397 echo "Copy $cache/rootfs-$arch to $rootfs ... "
398 copy_centos $cache $arch $rootfs
399 if [ $? -ne 0 ]; then
400 echo "Failed to copy rootfs"
406 ) 200>/var/lock/subsys/lxc
413 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 }'
423 if [ ! -e "$macdir/$name" ]
425 gen_mac > $macdir/$name
427 macaddr=`cat $macdir/$name`
429 cat <<EOF >> $path/config
430 lxc.network.hwaddr = $macaddr
431 # lxc.utsname = $name
436 # lxc.mount = $path/fstab
438 # lxc.console = /dev/console
440 lxc.cgroup.devices.deny = a
442 lxc.cgroup.devices.allow = c 1:3 rwm
443 lxc.cgroup.devices.allow = c 1:5 rwm
445 lxc.cgroup.devices.allow = c 5:1 rwm
446 lxc.cgroup.devices.allow = c 5:0 rwm
447 lxc.cgroup.devices.allow = c 4:0 rwm
448 lxc.cgroup.devices.allow = c 4:1 rwm
450 lxc.cgroup.devices.allow = c 1:9 rwm
451 lxc.cgroup.devices.allow = c 1:8 rwm
452 lxc.cgroup.devices.allow = c 136:* rwm
453 lxc.cgroup.devices.allow = c 5:2 rwm
455 lxc.cgroup.devices.allow = c 254:0 rwm
458 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
459 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
460 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults 0 0
463 # cat <<EOF > $path/fstab
464 #proc $rootfs/proc proc nodev,noexec,nosuid 0 0
465 #devpts $rootfs/dev/pts devpts defaults 0 0
466 ##sysfs $rootfs/sys sysfs defaults 0 0
467 #tmpfs $rootfs/dev/shm tmpfs defaults 0 0
470 if [ $? -ne 0 ]; then
471 echo "Failed to add configuration"
480 cache="/var/cache/lxc/$DISTRO"
482 if [ ! -e $cache ]; then
486 # lock, so we won't purge while someone is creating a repository
490 echo "Cache repository is busy."
494 echo -n "Purging the download cache..."
495 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
498 ) 200>/var/lock/subsys/lxc
504 $1 -h|--help -p|--path=<path> --clean
509 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
510 if [ $? -ne 0 ]; then
514 eval set -- "$options"
519 -h|--help) usage $0 && exit 0;;
520 -p|--path) path=$2; shift 2;;
521 -n|--name) name=$2; shift 2;;
522 -c|--clean) clean=$2; shift 2;;
523 --) shift 1; break ;;
528 if [ ! -z "$clean" -a -z "$path" ]; then
534 if [ $? -ne 0 ]; then
535 echo "'netmask' command is missing"
540 if [ $? -ne 0 ]; then
541 echo "'rinse' command is missing"
545 if [ -z "$path" ]; then
546 echo "'path' parameter is required"
550 if [ "$(id -u)" != "0" ]; then
551 echo "This script should be run as 'root'"
556 oldhostkeydir=/var/lib/lxc/ssh
557 hostkeydir=/var/cache/lxc/ssh
558 macdir=/var/cache/lxc/mac
559 if [ -e $oldhostkeydir ]
561 if [ ! -e $hostkeydir ]
563 mv $oldhostkeydir $hostkeydir
566 if [ ! -e $hostkeydir ]
575 install_centos $rootfs
576 if [ $? -ne 0 ]; then
577 echo "failed to install centos"
581 configure_centos $path $rootfs $name
582 if [ $? -ne 0 ]; then
583 echo "failed to configure centos for a container"
587 copy_configuration $path $rootfs $name $macdir
588 if [ $? -ne 0 ]; then
589 echo "failed write configuration file"
593 configure_pubkey $path $rootfs
594 if [ $? -ne 0 ]; then
595 echo "failed to configure pubkey"
599 configure_hostkeydir $rootfs $hostkeydir $name
600 if [ $? -ne 0 ]; then
601 echo "failed to configure hostkeydir"
605 if [ ! -z $clean ]; then