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
41 # configure static network
42 lxc_network_ipv4=`grep ^lxc.network.ipv4 $path/config | cut -d= -f2`
43 address=`echo $lxc_network_ipv4 | cut -d/ -f1`
44 netmask=`netmask -s $lxc_network_ipv4 | cut -d/ -f2`
45 if [ -e $rootfs/etc/sysconfig/network-scripts ]
47 cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
57 cat <<EOF > $rootfs/etc/sysconfig/network
64 cat <<EOF >> $rootfs/etc/hosts
67 echo "Network configured with static ip"
73 # configure the network using the dhcp
74 if [ -e $rootfs/etc/sysconfig/network-scripts ]
76 cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
82 cat <<EOF > $rootfs/etc/sysconfig/network
88 cat <<EOF >> $rootfs/etc/hosts
91 echo "Network configured with dhcp"
95 # When domain is not local, avahi completed by send hostname
96 hostnamebase=`echo $hostname | cut -d. -f1`
97 if [ "$hostnamebase.local" != "$hostname" ]
99 cat <<EOF > $rootfs/etc/dhclient.conf
100 send host-name "$hostnamebase";
105 # provide the lxc service
106 # cat <<EOF > $rootfs/etc/init/lxc.conf
107 ## fake some events needed for correct startup other services
109 #description "Container Upstart"
114 # rm -rf /var/run/*.pid
115 # rm -rf /var/run/network/*
116 # /sbin/initctl emit stopped JOB=udevtrigger --no-wait
117 # /sbin/initctl emit started JOB=udev --no-wait
121 # cat <<EOF > $rootfs/etc/init/console.conf
124 ## This service maintains a console on tty1 from the point the system is
125 ## started until it is shut down again.
127 #start on stopped rc RUNLEVEL=[2345]
128 #stop on runlevel [!2345]
131 #exec /sbin/getty -8 38400 /dev/console
133 #sed -i -e '/getty/d' $rootfs/etc/inittab
134 # Disable tty5 and tty6
135 sed -i -e '/^5:/d' $rootfs/etc/inittab
136 sed -i -e '/^6:/d' $rootfs/etc/inittab
138 cat <<EOF >> $rootfs/etc/inittab
139 0:2345:respawn:/sbin/mingetty console
142 # cat <<EOF > $rootfs/lib/init/fstab
143 ## /lib/init/fstab: lxc system fstab
144 #none /spu spufs gid=spu,optional 0 0
145 #none /tmp none defaults 0 0
146 #none /var/lock tmpfs nodev,noexec,nosuid,showthrough 0 0
147 #none /lib/init/rw tmpfs mode=0755,nosuid,optional 0 0
149 cat <<EOF > $rootfs/etc/fstab
150 # NOT MOUNTING IN LXC
151 #none /dev/pts devpts defaults 0 0
152 #none /proc proc defaults 0 0
153 #none /sys sysfs defaults 0 0
154 #none /dev/shm tmpfs defaults 0 0
157 # reconfigure some services
158 # if [ -z "$LANG" ]; then
159 # chroot $rootfs locale-gen en_US.UTF-8
160 # chroot $rootfs update-locale LANG=en_US.UTF-8
162 # chroot $rootfs locale-gen $LANG
163 # chroot $rootfs update-locale LANG=$LANG
166 # remove pointless services in a container
167 # chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
169 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
170 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
171 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
172 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
173 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
174 mv $rootfs/sbin/hwclock $rootfs/sbin/hwclock.removed_by_lxc
176 echo "Please change root-password !"
177 echo "root:root" | chroot $rootfs chpasswd
179 sed -i -e '/start_udev/d' $rootfs/etc/rc.d/rc.sysinit
182 if [ -f $rootfs/etc/init.d/avahi-daemon ]
185 sed -i -e "s!-D!-D --no-rlimits!" $rootfs/etc/init.d/avahi-daemon
189 #rm -f $rootfs/etc/mtab
190 #ln -s /proc/mounts $rootfs/etc/mtab
192 mv $rootfs/dev $rootfs/dev.old
194 test -e $rootfs/dev/null || mknod -m 666 $rootfs/dev/null c 1 3
195 test -e $rootfs/dev/zero || mknod -m 666 $rootfs/dev/zero c 1 5
196 test -e $rootfs/dev/random || mknod -m 666 $rootfs/dev/random c 1 8
197 test -e $rootfs/dev/urandom || mknod -m 666 $rootfs/dev/urandom c 1 9
198 test -e $rootfs/dev/pts || mkdir -m 755 $rootfs/dev/pts
199 test -e $rootfs/dev/shm || mkdir -m 1777 $rootfs/dev/shm
200 test -e $rootfs/dev/tty || mknod -m 666 $rootfs/dev/tty c 5 0
201 test -e $rootfs/dev/console || mknod -m 600 $rootfs/dev/console c 5 1
202 test -e $rootfs/dev/tty0 || mknod -m 666 $rootfs/dev/tty0 c 4 0
203 test -e $rootfs/dev/full || mknod -m 666 $rootfs/dev/full c 1 7
204 test -e $rootfs/dev/initctl || mknod -m 600 $rootfs/dev/initctl p
205 test -e $rootfs/dev/ptmx || mknod -m 666 $rootfs/dev/ptmx c 5 2
207 #test -e $rootfs/dev/pts/0 || mknod $rootfs/dev/pts/0 c 136 0
208 test -e $rootfs/dev/tty1 || mknod -m 666 $rootfs/dev/tty1 c 4 1
209 test -e $rootfs/dev/tty2 || mknod -m 666 $rootfs/dev/tty2 c 4 2
210 test -e $rootfs/dev/tty3 || mknod -m 666 $rootfs/dev/tty3 c 4 3
211 test -e $rootfs/dev/tty4 || mknod -m 666 $rootfs/dev/tty4 c 4 4
212 test -e $rootfs/dev/tty5 || mknod -m 666 $rootfs/dev/tty5 c 4 5
213 test -e $rootfs/dev/tty6 || mknod -m 666 $rootfs/dev/tty6 c 4 6
215 #cp /etc/localtime $rootfs/etc/localtime
216 #cp /etc/timezone $rootfs/etc/timezone
224 if grep -q ^\#lxc.pubkey $path/config
226 pubkey=`grep ^\#lxc.pubkey $path/config | cut -d= -f2`
228 if [ ! -e $rootfs/root/.ssh ]
230 mkdir $rootfs/root/.ssh
232 if [ ! -z "$pubkey" ]
234 echo "Copying $pubkey -> $rootfs/root/.ssh/authorized_keys"
235 cp $pubkey $rootfs/root/.ssh/authorized_keys
239 configure_hostkeydir(){
247 [ -d $rootfs/etc/ssh ] || mkdir -p $rootfs/etc/ssh
248 if [ -e $hostkeydir ]
250 if [ ! -d $hostkeydir/$name ]
252 mkdir $hostkeydir/$name
253 echo "Creating SSH2 RSA key; this may take some time ..."
254 ssh-keygen -q -f $hostkeydir/$name/ssh_host_rsa_key -N '' -t rsa
255 echo "Creating SSH2 DSA key; this may take some time ..."
256 ssh-keygen -q -f $hostkeydir/$name/ssh_host_dsa_key -N '' -t dsa
258 cp $hostkeydir/$name/ssh_host_* $rootfs/etc/ssh/
268 # check the mini centos was not already downloaded
269 mkdir -p "$cache/partial-$arch"
270 if [ $? -ne 0 ]; then
271 echo "Failed to create '$cache/partial-$arch' directory"
275 # download a mini centos into a cache
276 echo "Downloading centos minimal ..."
277 #febootstrap $DISTRO $cache/partial-$arch
278 rinsearch=`dpkg-architecture -qDEB_BUILD_ARCH`
279 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"
281 cat <<EOF > /var/cache/rinse/$DISTRO-$arch.before_post_install
282 echo "Rinse defines mirror=\$mirror in config"
283 server=\`echo \$mirror | cut -d/ -f3\`
284 echo "Disable mirrorlists"
285 sed -i -e "s/^mirrorlist=/#mirrorlist=/" $cache/partial-$arch/etc/yum.repos.d/CentOS-Base.repo
286 echo "Setup baseurl on server \$server"
287 sed -i -e "s!#baseurl=http://mirror.centos.org!baseurl=http://\$server!" $cache/partial-$arch/etc/yum.repos.d/CentOS-Base.repo
288 rm -f $cache/partial-$arch/etc/mtab
289 ln -s /proc/mounts $cache/partial-$arch/etc/mtab
291 chmod 755 /var/cache/rinse/$DISTRO-$arch.before_post_install
293 cat <<EOF > /var/cache/rinse/$DISTRO-$arch.after_post_install
296 chroot $cache/partial-$arch /bin/mount proc /proc -t proc
298 echo "Install $pkglist"
299 chroot $cache/partial-$arch /usr/bin/yum -y install $pkglist
302 if grep -q ^http_proxy /etc/wgetrc
304 PROXY=`grep ^http_proxy /etc/wgetrc | tail -1 | sed -e 's!.*http://!!' -e 's!/!!'`
307 cat <<EOF >> /var/cache/rinse/$DISTRO-$arch.after_post_install
308 # PROXY CONF FROM WGET
309 echo "Set up proxy $PROXY for pear"
310 chroot $cache/partial-$arch /usr/bin/pear config-set http_proxy $PROXY
314 cat <<EOF >> /var/cache/rinse/$DISTRO-$arch.after_post_install
316 # phpunit install, requires: php-pear php-dom
317 echo "Updating pear.php.net pear channel"
318 chroot $cache/partial-$arch /usr/bin/pear channel-update pear.php.net
319 #optional?# chroot $cache/partial-$arch /usr/bin/pear channel-discover components.ez.no
320 echo "Discovering pear.phpunit.de pear channel"
321 chroot $cache/partial-$arch /usr/bin/pear channel-discover pear.phpunit.de
322 echo "Discovering pear.symfony-project.com pear channel"
323 chroot $cache/partial-$arch /usr/bin/pear channel-discover pear.symfony-project.com
324 echo "Upgrading pear"
325 chroot $cache/partial-$arch /usr/bin/pear upgrade-all
326 chroot $cache/partial-$arch /usr/bin/pear upgrade --force PEAR
327 echo "Installing phpunit/PHPUnit"
328 chroot $cache/partial-$arch /usr/bin/pear install --alldeps phpunit/PHPUnit
331 chroot $cache/partial-$arch /bin/umount /proc
333 chmod 755 /var/cache/rinse/$DISTRO-$arch.after_post_install
335 rinse --config /etc/rinse/rinse.conf \
337 --distribution $DISTRO \
338 --before-post-install /var/cache/rinse/$DISTRO-$arch.before_post_install \
339 --after-post-install /var/cache/rinse/$DISTRO-$arch.after_post_install \
340 --directory $cache/partial-$arch
342 if [ $? -ne 0 ]; then
343 echo "Failed to download the rootfs, aborting."
347 mv "$1/partial-$arch" "$1/rootfs-$arch"
348 echo "Download complete."
359 # make a local copy of the minicentos
360 echo -n "Copying rootfs to $rootfs ..."
361 cp -a $cache/rootfs-$arch $rootfs || return 1
367 cache="/var/cache/lxc/centos"
369 mkdir -p /var/lock/subsys/
372 if [ $? -ne 0 ]; then
373 echo "Cache repository is busy."
379 echo "Checking cache download in $cache/rootfs-$arch ... "
380 if [ ! -e "$cache/rootfs-$arch" ]; then
381 download_centos $cache $arch
382 if [ $? -ne 0 ]; then
383 echo "Failed to download 'centos base'"
388 echo "Copy $cache/rootfs-$arch to $rootfs ... "
389 copy_centos $cache $arch $rootfs
390 if [ $? -ne 0 ]; then
391 echo "Failed to copy rootfs"
397 ) 200>/var/lock/subsys/lxc
404 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 }'
414 if [ ! -e "$macdir/$name" ]
416 gen_mac > $macdir/$name
418 macaddr=`cat $macdir/$name`
420 cat <<EOF >> $path/config
421 lxc.network.hwaddr = $macaddr
422 # lxc.utsname = $name
427 # lxc.mount = $path/fstab
429 # lxc.console = /dev/console
431 lxc.cgroup.devices.deny = a
433 lxc.cgroup.devices.allow = c 1:3 rwm
434 lxc.cgroup.devices.allow = c 1:5 rwm
436 lxc.cgroup.devices.allow = c 5:1 rwm
437 lxc.cgroup.devices.allow = c 5:0 rwm
438 lxc.cgroup.devices.allow = c 4:0 rwm
439 lxc.cgroup.devices.allow = c 4:1 rwm
441 lxc.cgroup.devices.allow = c 1:9 rwm
442 lxc.cgroup.devices.allow = c 1:8 rwm
443 lxc.cgroup.devices.allow = c 136:* rwm
444 lxc.cgroup.devices.allow = c 5:2 rwm
446 lxc.cgroup.devices.allow = c 254:0 rwm
449 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
450 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
451 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults 0 0
454 # cat <<EOF > $path/fstab
455 #proc $rootfs/proc proc nodev,noexec,nosuid 0 0
456 #devpts $rootfs/dev/pts devpts defaults 0 0
457 ##sysfs $rootfs/sys sysfs defaults 0 0
458 #tmpfs $rootfs/dev/shm tmpfs defaults 0 0
461 if [ $? -ne 0 ]; then
462 echo "Failed to add configuration"
471 cache="/var/cache/lxc/centos"
473 if [ ! -e $cache ]; then
477 # lock, so we won't purge while someone is creating a repository
481 echo "Cache repository is busy."
485 echo -n "Purging the download cache..."
486 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
489 ) 200>/var/lock/subsys/lxc
495 $1 -h|--help -p|--path=<path> --clean
500 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
501 if [ $? -ne 0 ]; then
505 eval set -- "$options"
510 -h|--help) usage $0 && exit 0;;
511 -p|--path) path=$2; shift 2;;
512 -n|--name) name=$2; shift 2;;
513 -c|--clean) clean=$2; shift 2;;
514 --) shift 1; break ;;
519 if [ ! -z "$clean" -a -z "$path" ]; then
525 if [ $? -ne 0 ]; then
526 echo "'netmask' command is missing"
531 if [ $? -ne 0 ]; then
532 echo "'rinse' command is missing"
536 if [ -z "$path" ]; then
537 echo "'path' parameter is required"
541 if [ "$(id -u)" != "0" ]; then
542 echo "This script should be run as 'root'"
547 oldhostkeydir=/var/lib/lxc/ssh
548 hostkeydir=/var/cache/lxc/ssh
549 macdir=/var/cache/lxc/mac
550 if [ -e $oldhostkeydir ]
552 if [ ! -e $hostkeydir ]
554 mv $oldhostkeydir $hostkeydir
557 if [ ! -e $hostkeydir ]
566 install_centos $rootfs
567 if [ $? -ne 0 ]; then
568 echo "failed to install centos"
572 configure_centos $path $rootfs $name
573 if [ $? -ne 0 ]; then
574 echo "failed to configure centos for a container"
578 copy_configuration $path $rootfs $name $macdir
579 if [ $? -ne 0 ]; then
580 echo "failed write configuration file"
584 configure_pubkey $path $rootfs
585 if [ $? -ne 0 ]; then
586 echo "failed to configure pubkey"
590 configure_hostkeydir $rootfs $hostkeydir $name
591 if [ $? -ne 0 ]; then
592 echo "failed to configure hostkeydir"
596 if [ ! -z $clean ]; then