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 touch $rootfs/etc/hosts
41 if ! grep -q "^127.0.0.1" $rootfs/etc/hosts
43 cat <<EOF >> $rootfs/etc/hosts
44 # Do not remove the following line, or various programs
45 # that require network functionality will fail.
46 127.0.0.1 localhost.localdomain localhost
47 ::1 localhost6.localdomain6 localhost6
50 if ! grep -q "^::1" $rootfs/etc/hosts
52 cat <<EOF >> $rootfs/etc/hosts
53 ::1 localhost6.localdomain6 localhost6
57 if grep -q ^lxc.network.ipv4 $path/config
59 # configure static network
60 lxc_network_ipv4=`grep ^lxc.network.ipv4 $path/config | cut -d= -f2`
61 address=`echo $lxc_network_ipv4 | cut -d/ -f1`
62 netmask=`netmask -s $lxc_network_ipv4 | cut -d/ -f2`
63 if [ -e $rootfs/etc/sysconfig/network-scripts ]
65 cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
75 cat <<EOF > $rootfs/etc/sysconfig/network
82 cat <<EOF >> $rootfs/etc/hosts
85 echo "Network configured with static ip"
91 # configure the network using the dhcp
92 if [ -e $rootfs/etc/sysconfig/network-scripts ]
94 cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
100 cat <<EOF > $rootfs/etc/sysconfig/network
106 cat <<EOF >> $rootfs/etc/hosts
109 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/dhclient.conf
118 send host-name "$hostnamebase";
123 # provide the lxc service
124 # cat <<EOF > $rootfs/etc/init/lxc.conf
125 ## fake some events needed for correct startup other services
127 #description "Container Upstart"
132 # rm -rf /var/run/*.pid
133 # rm -rf /var/run/network/*
134 # /sbin/initctl emit stopped JOB=udevtrigger --no-wait
135 # /sbin/initctl emit started JOB=udev --no-wait
139 # cat <<EOF > $rootfs/etc/init/console.conf
142 ## This service maintains a console on tty1 from the point the system is
143 ## started until it is shut down again.
145 #start on stopped rc RUNLEVEL=[2345]
146 #stop on runlevel [!2345]
149 #exec /sbin/getty -8 38400 /dev/console
151 #sed -i -e '/getty/d' $rootfs/etc/inittab
152 # Disable tty5 and tty6
153 sed -i -e '/^5:/d' $rootfs/etc/inittab
154 sed -i -e '/^6:/d' $rootfs/etc/inittab
156 cat <<EOF >> $rootfs/etc/inittab
157 0:2345:respawn:/sbin/mingetty console
160 # cat <<EOF > $rootfs/lib/init/fstab
161 ## /lib/init/fstab: lxc system fstab
162 #none /spu spufs gid=spu,optional 0 0
163 #none /tmp none defaults 0 0
164 #none /var/lock tmpfs nodev,noexec,nosuid,showthrough 0 0
165 #none /lib/init/rw tmpfs mode=0755,nosuid,optional 0 0
167 cat <<EOF > $rootfs/etc/fstab
168 # NOT MOUNTING IN LXC
169 #none /dev/pts devpts defaults 0 0
170 #none /proc proc defaults 0 0
171 #none /sys sysfs defaults 0 0
172 #none /dev/shm tmpfs defaults 0 0
175 # reconfigure some services
176 if [ -z "$LANG" ]; then
177 #chroot $rootfs locale-gen en_US.UTF-8
178 #chroot $rootfs update-locale LANG=en_US.UTF-8
179 cat > $rootfs/etc/sysconfig/i18n <<-FIN
181 SYSFONT="latarcyrheb-sun16"
184 #chroot $rootfs locale-gen $LANG
185 #chroot $rootfs update-locale LANG=$LANG
186 cat > $rootfs/etc/sysconfig/i18n <<-FIN
188 SYSFONT="latarcyrheb-sun16"
192 # remove pointless services in a container
193 # chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
195 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
196 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
197 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
198 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
199 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
200 mv $rootfs/sbin/hwclock $rootfs/sbin/hwclock.removed_by_lxc
202 echo "Please change root-password !"
203 echo "root:root" | chroot $rootfs chpasswd
205 sed -i -e '/start_udev/d' $rootfs/etc/rc.d/rc.sysinit
208 if [ -f $rootfs/etc/init.d/avahi-daemon ]
211 sed -i -e "s!-D!-D --no-rlimits!" $rootfs/etc/init.d/avahi-daemon
214 mv $rootfs/dev $rootfs/dev.old
216 test -e $rootfs/dev/null || mknod -m 666 $rootfs/dev/null c 1 3
217 test -e $rootfs/dev/zero || mknod -m 666 $rootfs/dev/zero c 1 5
218 test -e $rootfs/dev/random || mknod -m 666 $rootfs/dev/random c 1 8
219 test -e $rootfs/dev/urandom || mknod -m 666 $rootfs/dev/urandom c 1 9
220 test -e $rootfs/dev/pts || mkdir -m 755 $rootfs/dev/pts
221 test -e $rootfs/dev/shm || mkdir -m 1777 $rootfs/dev/shm
222 test -e $rootfs/dev/tty || mknod -m 666 $rootfs/dev/tty c 5 0
223 test -e $rootfs/dev/console || mknod -m 600 $rootfs/dev/console c 5 1
224 test -e $rootfs/dev/tty0 || mknod -m 666 $rootfs/dev/tty0 c 4 0
225 test -e $rootfs/dev/full || mknod -m 666 $rootfs/dev/full c 1 7
226 test -e $rootfs/dev/initctl || mknod -m 600 $rootfs/dev/initctl p
227 test -e $rootfs/dev/ptmx || mknod -m 666 $rootfs/dev/ptmx c 5 2
229 test -e $rootfs/dev/tty1 || mknod -m 666 $rootfs/dev/tty1 c 4 1
230 test -e $rootfs/dev/tty2 || mknod -m 666 $rootfs/dev/tty2 c 4 2
231 test -e $rootfs/dev/tty3 || mknod -m 666 $rootfs/dev/tty3 c 4 3
232 test -e $rootfs/dev/tty4 || mknod -m 666 $rootfs/dev/tty4 c 4 4
233 test -e $rootfs/dev/tty5 || mknod -m 666 $rootfs/dev/tty5 c 4 5
234 test -e $rootfs/dev/tty6 || mknod -m 666 $rootfs/dev/tty6 c 4 6
242 if grep -q ^\#lxc.pubkey $path/config
244 pubkey=`grep ^\#lxc.pubkey $path/config | cut -d= -f2`
246 if [ ! -e $rootfs/root/.ssh ]
248 mkdir $rootfs/root/.ssh
250 if [ ! -z "$pubkey" ]
252 echo "Copying $pubkey -> $rootfs/root/.ssh/authorized_keys"
253 cp $pubkey $rootfs/root/.ssh/authorized_keys
257 configure_hostkeydir(){
265 [ -d $rootfs/etc/ssh ] || mkdir -p $rootfs/etc/ssh
266 if [ -e $hostkeydir ]
268 if [ ! -d $hostkeydir/$name ]
270 mkdir $hostkeydir/$name
271 echo "Creating SSH2 RSA key; this may take some time ..."
272 ssh-keygen -q -f $hostkeydir/$name/ssh_host_rsa_key -N '' -t rsa
273 echo "Creating SSH2 DSA key; this may take some time ..."
274 ssh-keygen -q -f $hostkeydir/$name/ssh_host_dsa_key -N '' -t dsa
276 cp $hostkeydir/$name/ssh_host_* $rootfs/etc/ssh/
286 # check the mini centos was not already downloaded
287 mkdir -p "$cache/partial-$arch"
288 if [ $? -ne 0 ]; then
289 echo "Failed to create '$cache/partial-$arch' directory"
293 # download a mini centos into a cache
294 echo "Downloading centos minimal ..."
295 #febootstrap $DISTRO $cache/partial-$arch
296 rinsearch=`dpkg-architecture -qDEB_BUILD_ARCH`
297 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 make rpm-build createrepo"
299 cat <<EOF > /var/cache/rinse/$DISTRO-$arch.before_post_install
300 echo "Rinse defines mirror=\$mirror in config"
301 server=\`echo \$mirror | cut -d/ -f3\`
302 echo "Disable mirrorlists"
303 sed -i -e "s/^mirrorlist=/#mirrorlist=/" $cache/partial-$arch/etc/yum.repos.d/CentOS-Base.repo
304 echo "Setup baseurl on server \$server"
305 sed -i -e "s!#baseurl=http://mirror.centos.org!baseurl=http://\$server!" $cache/partial-$arch/etc/yum.repos.d/CentOS-Base.repo
306 rm -f $cache/partial-$arch/etc/mtab
307 ln -s /proc/mounts $cache/partial-$arch/etc/mtab
309 chmod 755 /var/cache/rinse/$DISTRO-$arch.before_post_install
311 cat <<EOF > /var/cache/rinse/$DISTRO-$arch.after_post_install
314 chroot $cache/partial-$arch /bin/mount proc /proc -t proc
316 echo "Install $pkglist"
317 chroot $cache/partial-$arch /usr/bin/yum -y install $pkglist
320 if grep -q ^http_proxy /etc/wgetrc
322 PROXY=`grep ^http_proxy /etc/wgetrc | tail -1 | sed -e 's!.*http://!!' -e 's!/!!'`
325 cat <<EOF >> /var/cache/rinse/$DISTRO-$arch.after_post_install
326 # PROXY CONF FROM WGET
327 echo "Set up proxy $PROXY for pear"
328 chroot $cache/partial-$arch /usr/bin/pear config-set http_proxy $PROXY
332 cat <<EOF >> /var/cache/rinse/$DISTRO-$arch.after_post_install
334 # phpunit install, requires: php-pear php-dom
335 echo "Updating pear.php.net pear channel"
336 chroot $cache/partial-$arch /usr/bin/pear channel-update pear.php.net
337 #optional?# chroot $cache/partial-$arch /usr/bin/pear channel-discover components.ez.no
338 echo "Discovering pear.phpunit.de pear channel"
339 chroot $cache/partial-$arch /usr/bin/pear channel-discover pear.phpunit.de
340 echo "Discovering pear.symfony-project.com pear channel"
341 chroot $cache/partial-$arch /usr/bin/pear channel-discover pear.symfony-project.com
342 echo "Upgrading pear"
343 chroot $cache/partial-$arch /usr/bin/pear upgrade-all
344 chroot $cache/partial-$arch /usr/bin/pear upgrade --force PEAR
345 echo "Installing phpunit/PHPUnit"
346 chroot $cache/partial-$arch /usr/bin/pear install --alldeps phpunit/PHPUnit
349 chroot $cache/partial-$arch /bin/umount /proc
351 chmod 755 /var/cache/rinse/$DISTRO-$arch.after_post_install
353 rinse --config /etc/rinse/rinse.conf \
355 --distribution $DISTRO \
356 --before-post-install /var/cache/rinse/$DISTRO-$arch.before_post_install \
357 --after-post-install /var/cache/rinse/$DISTRO-$arch.after_post_install \
358 --directory $cache/partial-$arch
360 if [ $? -ne 0 ]; then
361 echo "Failed to download the rootfs, aborting."
365 mv "$1/partial-$arch" "$1/rootfs-$arch"
366 echo "Download complete."
377 # make a local copy of the minicentos
378 echo -n "Copying rootfs to $rootfs ..."
379 cp -a $cache/rootfs-$arch $rootfs || return 1
385 cache="/var/cache/lxc/$DISTRO"
387 mkdir -p /var/lock/subsys/
390 if [ $? -ne 0 ]; then
391 echo "Cache repository is busy."
397 echo "Checking cache download in $cache/rootfs-$arch ... "
398 if [ ! -e "$cache/rootfs-$arch" ]; then
399 download_centos $cache $arch
400 if [ $? -ne 0 ]; then
401 echo "Failed to download 'centos base'"
406 echo "Copy $cache/rootfs-$arch to $rootfs ... "
407 copy_centos $cache $arch $rootfs
408 if [ $? -ne 0 ]; then
409 echo "Failed to copy rootfs"
415 ) 200>/var/lock/subsys/lxc
422 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 }'
432 if [ ! -e "$macdir/$name" ]
434 gen_mac > $macdir/$name
436 macaddr=`cat $macdir/$name`
438 cat <<EOF >> $path/config
439 lxc.network.hwaddr = $macaddr
440 # lxc.utsname = $name
445 # lxc.mount = $path/fstab
447 # lxc.console = /dev/console
449 lxc.cgroup.devices.deny = a
451 lxc.cgroup.devices.allow = c 1:3 rwm
452 lxc.cgroup.devices.allow = c 1:5 rwm
454 lxc.cgroup.devices.allow = c 1:7 rwm
456 lxc.cgroup.devices.allow = c 5:1 rwm
457 lxc.cgroup.devices.allow = c 5:0 rwm
458 lxc.cgroup.devices.allow = c 4:0 rwm
459 lxc.cgroup.devices.allow = c 4:1 rwm
461 lxc.cgroup.devices.allow = c 1:9 rwm
462 lxc.cgroup.devices.allow = c 1:8 rwm
463 lxc.cgroup.devices.allow = c 136:* rwm
464 lxc.cgroup.devices.allow = c 5:2 rwm
466 lxc.cgroup.devices.allow = c 254:0 rwm
469 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
470 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
471 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults 0 0
474 # cat <<EOF > $path/fstab
475 #proc $rootfs/proc proc nodev,noexec,nosuid 0 0
476 #devpts $rootfs/dev/pts devpts defaults 0 0
477 ##sysfs $rootfs/sys sysfs defaults 0 0
478 #tmpfs $rootfs/dev/shm tmpfs defaults 0 0
481 if [ $? -ne 0 ]; then
482 echo "Failed to add configuration"
491 cache="/var/cache/lxc/$DISTRO"
493 if [ ! -e $cache ]; then
497 # lock, so we won't purge while someone is creating a repository
501 echo "Cache repository is busy."
505 echo -n "Purging the download cache..."
506 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
509 ) 200>/var/lock/subsys/lxc
515 $1 -h|--help -p|--path=<path> --clean
520 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
521 if [ $? -ne 0 ]; then
525 eval set -- "$options"
530 -h|--help) usage $0 && exit 0;;
531 -p|--path) path=$2; shift 2;;
532 -n|--name) name=$2; shift 2;;
533 -c|--clean) clean=$2; shift 2;;
534 --) shift 1; break ;;
539 if [ ! -z "$clean" -a -z "$path" ]; then
545 if [ $? -ne 0 ]; then
546 echo "'netmask' command is missing"
551 if [ $? -ne 0 ]; then
552 echo "'rinse' command is missing"
556 if [ -z "$path" ]; then
557 echo "'path' parameter is required"
561 if [ "$(id -u)" != "0" ]; then
562 echo "This script should be run as 'root'"
567 oldhostkeydir=/var/lib/lxc/ssh
568 hostkeydir=/var/cache/lxc/ssh
569 macdir=/var/cache/lxc/mac
570 if [ -e $oldhostkeydir ]
572 if [ ! -e $hostkeydir ]
574 mv $oldhostkeydir $hostkeydir
577 if [ ! -e $hostkeydir ]
586 install_centos $rootfs
587 if [ $? -ne 0 ]; then
588 echo "failed to install centos"
592 configure_centos $path $rootfs $name
593 if [ $? -ne 0 ]; then
594 echo "failed to configure centos for a container"
598 copy_configuration $path $rootfs $name $macdir
599 if [ $? -ne 0 ]; then
600 echo "failed write configuration file"
604 configure_pubkey $path $rootfs
605 if [ $? -ne 0 ]; then
606 echo "failed to configure pubkey"
610 configure_hostkeydir $rootfs $hostkeydir $name
611 if [ $? -ne 0 ]; then
612 echo "failed to configure hostkeydir"
616 if [ ! -z $clean ]; then