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
39 if grep -q ^lxc.network.ipv4 $path/config
42 if ! grep -q "^127.0.0.1" $rootfs/etc/hosts
44 cat <<EOF >> $rootfs/etc/hosts
45 # Do not remove the following line, or various programs
46 # that require network functionality will fail.
47 127.0.0.1 localhost.localdomain localhost
48 ::1 localhost6.localdomain6 localhost6
50 if ! grep -q "^::1" $rootfs/etc/hosts
52 cat <<EOF >> $rootfs/etc/hosts
53 ::1 localhost6.localdomain6 localhost6
56 # configure static network
57 lxc_network_ipv4=`grep ^lxc.network.ipv4 $path/config | cut -d= -f2`
58 address=`echo $lxc_network_ipv4 | cut -d/ -f1`
59 netmask=`netmask -s $lxc_network_ipv4 | cut -d/ -f2`
60 if [ -e $rootfs/etc/sysconfig/network-scripts ]
62 cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
72 cat <<EOF > $rootfs/etc/sysconfig/network
79 cat <<EOF >> $rootfs/etc/hosts
82 echo "Network configured with static ip"
88 # configure the network using the dhcp
89 if [ -e $rootfs/etc/sysconfig/network-scripts ]
91 cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
97 cat <<EOF > $rootfs/etc/sysconfig/network
103 cat <<EOF >> $rootfs/etc/hosts
106 echo "Network configured with dhcp"
110 # When domain is not local, avahi completed by send hostname
111 hostnamebase=`echo $hostname | cut -d. -f1`
112 if [ "$hostnamebase.local" != "$hostname" ]
114 cat <<EOF > $rootfs/etc/dhclient.conf
115 send host-name "$hostnamebase";
120 # provide the lxc service
121 # cat <<EOF > $rootfs/etc/init/lxc.conf
122 ## fake some events needed for correct startup other services
124 #description "Container Upstart"
129 # rm -rf /var/run/*.pid
130 # rm -rf /var/run/network/*
131 # /sbin/initctl emit stopped JOB=udevtrigger --no-wait
132 # /sbin/initctl emit started JOB=udev --no-wait
136 # cat <<EOF > $rootfs/etc/init/console.conf
139 ## This service maintains a console on tty1 from the point the system is
140 ## started until it is shut down again.
142 #start on stopped rc RUNLEVEL=[2345]
143 #stop on runlevel [!2345]
146 #exec /sbin/getty -8 38400 /dev/console
148 #sed -i -e '/getty/d' $rootfs/etc/inittab
149 # Disable tty5 and tty6
150 sed -i -e '/^5:/d' $rootfs/etc/inittab
151 sed -i -e '/^6:/d' $rootfs/etc/inittab
153 cat <<EOF >> $rootfs/etc/inittab
154 0:2345:respawn:/sbin/mingetty console
157 # cat <<EOF > $rootfs/lib/init/fstab
158 ## /lib/init/fstab: lxc system fstab
159 #none /spu spufs gid=spu,optional 0 0
160 #none /tmp none defaults 0 0
161 #none /var/lock tmpfs nodev,noexec,nosuid,showthrough 0 0
162 #none /lib/init/rw tmpfs mode=0755,nosuid,optional 0 0
164 cat <<EOF > $rootfs/etc/fstab
165 # NOT MOUNTING IN LXC
166 #none /dev/pts devpts defaults 0 0
167 #none /proc proc defaults 0 0
168 #none /sys sysfs defaults 0 0
169 #none /dev/shm tmpfs defaults 0 0
172 # reconfigure some services
173 # if [ -z "$LANG" ]; then
174 # chroot $rootfs locale-gen en_US.UTF-8
175 # chroot $rootfs update-locale LANG=en_US.UTF-8
177 # chroot $rootfs locale-gen $LANG
178 # chroot $rootfs update-locale LANG=$LANG
181 # remove pointless services in a container
182 # chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
184 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
185 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
186 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
187 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
188 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
189 mv $rootfs/sbin/hwclock $rootfs/sbin/hwclock.removed_by_lxc
191 echo "Please change root-password !"
192 echo "root:root" | chroot $rootfs chpasswd
194 sed -i -e '/start_udev/d' $rootfs/etc/rc.d/rc.sysinit
197 if [ -f $rootfs/etc/init.d/avahi-daemon ]
200 sed -i -e "s!-D!-D --no-rlimits!" $rootfs/etc/init.d/avahi-daemon
204 #rm -f $rootfs/etc/mtab
205 #ln -s /proc/mounts $rootfs/etc/mtab
207 mv $rootfs/dev $rootfs/dev.old
209 test -e $rootfs/dev/null || mknod -m 666 $rootfs/dev/null c 1 3
210 test -e $rootfs/dev/zero || mknod -m 666 $rootfs/dev/zero c 1 5
211 test -e $rootfs/dev/random || mknod -m 666 $rootfs/dev/random c 1 8
212 test -e $rootfs/dev/urandom || mknod -m 666 $rootfs/dev/urandom c 1 9
213 test -e $rootfs/dev/pts || mkdir -m 755 $rootfs/dev/pts
214 test -e $rootfs/dev/shm || mkdir -m 1777 $rootfs/dev/shm
215 test -e $rootfs/dev/tty || mknod -m 666 $rootfs/dev/tty c 5 0
216 test -e $rootfs/dev/console || mknod -m 600 $rootfs/dev/console c 5 1
217 test -e $rootfs/dev/tty0 || mknod -m 666 $rootfs/dev/tty0 c 4 0
218 test -e $rootfs/dev/full || mknod -m 666 $rootfs/dev/full c 1 7
219 test -e $rootfs/dev/initctl || mknod -m 600 $rootfs/dev/initctl p
220 test -e $rootfs/dev/ptmx || mknod -m 666 $rootfs/dev/ptmx c 5 2
222 #test -e $rootfs/dev/pts/0 || mknod $rootfs/dev/pts/0 c 136 0
223 test -e $rootfs/dev/tty1 || mknod -m 666 $rootfs/dev/tty1 c 4 1
224 test -e $rootfs/dev/tty2 || mknod -m 666 $rootfs/dev/tty2 c 4 2
225 test -e $rootfs/dev/tty3 || mknod -m 666 $rootfs/dev/tty3 c 4 3
226 test -e $rootfs/dev/tty4 || mknod -m 666 $rootfs/dev/tty4 c 4 4
227 test -e $rootfs/dev/tty5 || mknod -m 666 $rootfs/dev/tty5 c 4 5
228 test -e $rootfs/dev/tty6 || mknod -m 666 $rootfs/dev/tty6 c 4 6
230 #cp /etc/localtime $rootfs/etc/localtime
231 #cp /etc/timezone $rootfs/etc/timezone
239 if grep -q ^\#lxc.pubkey $path/config
241 pubkey=`grep ^\#lxc.pubkey $path/config | cut -d= -f2`
243 if [ ! -e $rootfs/root/.ssh ]
245 mkdir $rootfs/root/.ssh
247 if [ ! -z "$pubkey" ]
249 echo "Copying $pubkey -> $rootfs/root/.ssh/authorized_keys"
250 cp $pubkey $rootfs/root/.ssh/authorized_keys
254 configure_hostkeydir(){
262 [ -d $rootfs/etc/ssh ] || mkdir -p $rootfs/etc/ssh
263 if [ -e $hostkeydir ]
265 if [ ! -d $hostkeydir/$name ]
267 mkdir $hostkeydir/$name
268 echo "Creating SSH2 RSA key; this may take some time ..."
269 ssh-keygen -q -f $hostkeydir/$name/ssh_host_rsa_key -N '' -t rsa
270 echo "Creating SSH2 DSA key; this may take some time ..."
271 ssh-keygen -q -f $hostkeydir/$name/ssh_host_dsa_key -N '' -t dsa
273 cp $hostkeydir/$name/ssh_host_* $rootfs/etc/ssh/
283 # check the mini centos was not already downloaded
284 mkdir -p "$cache/partial-$arch"
285 if [ $? -ne 0 ]; then
286 echo "Failed to create '$cache/partial-$arch' directory"
290 # download a mini centos into a cache
291 echo "Downloading centos minimal ..."
292 #febootstrap $DISTRO $cache/partial-$arch
293 rinsearch=`dpkg-architecture -qDEB_BUILD_ARCH`
294 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"
296 cat <<EOF > /var/cache/rinse/$DISTRO-$arch.before_post_install
297 echo "Rinse defines mirror=\$mirror in config"
298 server=\`echo \$mirror | cut -d/ -f3\`
299 echo "Disable mirrorlists"
300 sed -i -e "s/^mirrorlist=/#mirrorlist=/" $cache/partial-$arch/etc/yum.repos.d/CentOS-Base.repo
301 echo "Setup baseurl on server \$server"
302 sed -i -e "s!#baseurl=http://mirror.centos.org!baseurl=http://\$server!" $cache/partial-$arch/etc/yum.repos.d/CentOS-Base.repo
303 rm -f $cache/partial-$arch/etc/mtab
304 ln -s /proc/mounts $cache/partial-$arch/etc/mtab
306 chmod 755 /var/cache/rinse/$DISTRO-$arch.before_post_install
308 cat <<EOF > /var/cache/rinse/$DISTRO-$arch.after_post_install
311 chroot $cache/partial-$arch /bin/mount proc /proc -t proc
313 echo "Install $pkglist"
314 chroot $cache/partial-$arch /usr/bin/yum -y install $pkglist
317 if grep -q ^http_proxy /etc/wgetrc
319 PROXY=`grep ^http_proxy /etc/wgetrc | tail -1 | sed -e 's!.*http://!!' -e 's!/!!'`
322 cat <<EOF >> /var/cache/rinse/$DISTRO-$arch.after_post_install
323 # PROXY CONF FROM WGET
324 echo "Set up proxy $PROXY for pear"
325 chroot $cache/partial-$arch /usr/bin/pear config-set http_proxy $PROXY
329 cat <<EOF >> /var/cache/rinse/$DISTRO-$arch.after_post_install
331 # phpunit install, requires: php-pear php-dom
332 echo "Updating pear.php.net pear channel"
333 chroot $cache/partial-$arch /usr/bin/pear channel-update pear.php.net
334 #optional?# chroot $cache/partial-$arch /usr/bin/pear channel-discover components.ez.no
335 echo "Discovering pear.phpunit.de pear channel"
336 chroot $cache/partial-$arch /usr/bin/pear channel-discover pear.phpunit.de
337 echo "Discovering pear.symfony-project.com pear channel"
338 chroot $cache/partial-$arch /usr/bin/pear channel-discover pear.symfony-project.com
339 echo "Upgrading pear"
340 chroot $cache/partial-$arch /usr/bin/pear upgrade-all
341 chroot $cache/partial-$arch /usr/bin/pear upgrade --force PEAR
342 echo "Installing phpunit/PHPUnit"
343 chroot $cache/partial-$arch /usr/bin/pear install --alldeps phpunit/PHPUnit
346 chroot $cache/partial-$arch /bin/umount /proc
348 chmod 755 /var/cache/rinse/$DISTRO-$arch.after_post_install
350 rinse --config /etc/rinse/rinse.conf \
352 --distribution $DISTRO \
353 --before-post-install /var/cache/rinse/$DISTRO-$arch.before_post_install \
354 --after-post-install /var/cache/rinse/$DISTRO-$arch.after_post_install \
355 --directory $cache/partial-$arch
357 if [ $? -ne 0 ]; then
358 echo "Failed to download the rootfs, aborting."
362 mv "$1/partial-$arch" "$1/rootfs-$arch"
363 echo "Download complete."
374 # make a local copy of the minicentos
375 echo -n "Copying rootfs to $rootfs ..."
376 cp -a $cache/rootfs-$arch $rootfs || return 1
382 cache="/var/cache/lxc/centos"
384 mkdir -p /var/lock/subsys/
387 if [ $? -ne 0 ]; then
388 echo "Cache repository is busy."
394 echo "Checking cache download in $cache/rootfs-$arch ... "
395 if [ ! -e "$cache/rootfs-$arch" ]; then
396 download_centos $cache $arch
397 if [ $? -ne 0 ]; then
398 echo "Failed to download 'centos base'"
403 echo "Copy $cache/rootfs-$arch to $rootfs ... "
404 copy_centos $cache $arch $rootfs
405 if [ $? -ne 0 ]; then
406 echo "Failed to copy rootfs"
412 ) 200>/var/lock/subsys/lxc
419 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 }'
429 if [ ! -e "$macdir/$name" ]
431 gen_mac > $macdir/$name
433 macaddr=`cat $macdir/$name`
435 cat <<EOF >> $path/config
436 lxc.network.hwaddr = $macaddr
437 # lxc.utsname = $name
442 # lxc.mount = $path/fstab
444 # lxc.console = /dev/console
446 lxc.cgroup.devices.deny = a
448 lxc.cgroup.devices.allow = c 1:3 rwm
449 lxc.cgroup.devices.allow = c 1:5 rwm
451 lxc.cgroup.devices.allow = c 5:1 rwm
452 lxc.cgroup.devices.allow = c 5:0 rwm
453 lxc.cgroup.devices.allow = c 4:0 rwm
454 lxc.cgroup.devices.allow = c 4:1 rwm
456 lxc.cgroup.devices.allow = c 1:9 rwm
457 lxc.cgroup.devices.allow = c 1:8 rwm
458 lxc.cgroup.devices.allow = c 136:* rwm
459 lxc.cgroup.devices.allow = c 5:2 rwm
461 lxc.cgroup.devices.allow = c 254:0 rwm
464 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
465 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
466 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults 0 0
469 # cat <<EOF > $path/fstab
470 #proc $rootfs/proc proc nodev,noexec,nosuid 0 0
471 #devpts $rootfs/dev/pts devpts defaults 0 0
472 ##sysfs $rootfs/sys sysfs defaults 0 0
473 #tmpfs $rootfs/dev/shm tmpfs defaults 0 0
476 if [ $? -ne 0 ]; then
477 echo "Failed to add configuration"
486 cache="/var/cache/lxc/centos"
488 if [ ! -e $cache ]; then
492 # lock, so we won't purge while someone is creating a repository
496 echo "Cache repository is busy."
500 echo -n "Purging the download cache..."
501 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
504 ) 200>/var/lock/subsys/lxc
510 $1 -h|--help -p|--path=<path> --clean
515 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
516 if [ $? -ne 0 ]; then
520 eval set -- "$options"
525 -h|--help) usage $0 && exit 0;;
526 -p|--path) path=$2; shift 2;;
527 -n|--name) name=$2; shift 2;;
528 -c|--clean) clean=$2; shift 2;;
529 --) shift 1; break ;;
534 if [ ! -z "$clean" -a -z "$path" ]; then
540 if [ $? -ne 0 ]; then
541 echo "'netmask' command is missing"
546 if [ $? -ne 0 ]; then
547 echo "'rinse' command is missing"
551 if [ -z "$path" ]; then
552 echo "'path' parameter is required"
556 if [ "$(id -u)" != "0" ]; then
557 echo "This script should be run as 'root'"
562 oldhostkeydir=/var/lib/lxc/ssh
563 hostkeydir=/var/cache/lxc/ssh
564 macdir=/var/cache/lxc/mac
565 if [ -e $oldhostkeydir ]
567 if [ ! -e $hostkeydir ]
569 mv $oldhostkeydir $hostkeydir
572 if [ ! -e $hostkeydir ]
581 install_centos $rootfs
582 if [ $? -ne 0 ]; then
583 echo "failed to install centos"
587 configure_centos $path $rootfs $name
588 if [ $? -ne 0 ]; then
589 echo "failed to configure centos for a container"
593 copy_configuration $path $rootfs $name $macdir
594 if [ $? -ne 0 ]; then
595 echo "failed write configuration file"
599 configure_pubkey $path $rootfs
600 if [ $? -ne 0 ]; then
601 echo "failed to configure pubkey"
605 configure_hostkeydir $rootfs $hostkeydir $name
606 if [ $? -ne 0 ]; then
607 echo "failed to configure hostkeydir"
611 if [ ! -z $clean ]; then