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
152 cat <<EOF >> $rootfs/etc/inittab
153 0:2345:respawn:/sbin/mingetty console
156 # cat <<EOF > $rootfs/lib/init/fstab
157 ## /lib/init/fstab: lxc system fstab
158 #none /spu spufs gid=spu,optional 0 0
159 #none /tmp none defaults 0 0
160 #none /var/lock tmpfs nodev,noexec,nosuid,showthrough 0 0
161 #none /lib/init/rw tmpfs mode=0755,nosuid,optional 0 0
163 cat <<EOF > $rootfs/etc/fstab
164 # NOT MOUNTING IN LXC
165 #none /dev/pts devpts defaults 0 0
166 #none /proc proc defaults 0 0
167 #none /sys sysfs defaults 0 0
168 #none /dev/shm tmpfs defaults 0 0
171 # reconfigure some services
172 # if [ -z "$LANG" ]; then
173 # chroot $rootfs locale-gen en_US.UTF-8
174 # chroot $rootfs update-locale LANG=en_US.UTF-8
176 # chroot $rootfs locale-gen $LANG
177 # chroot $rootfs update-locale LANG=$LANG
180 # remove pointless services in a container
181 # chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
183 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
184 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
185 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
186 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
187 # chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
189 echo "Please change root-password !"
190 echo "root:root" | chroot $rootfs chpasswd
192 sed -i -e '/start_udev/d' $rootfs/etc/rc.d/rc.sysinit
195 if [ -f $rootfs/etc/init.d/avahi-daemon ]
198 sed -i -e "s!-D!-D --no-rlimits!" $rootfs/etc/init.d/avahi-daemon
201 mv $rootfs/dev $rootfs/dev.old
203 test -e $rootfs/dev/null || mknod -m 666 $rootfs/dev/null c 1 3
204 test -e $rootfs/dev/zero || mknod -m 666 $rootfs/dev/zero c 1 5
205 test -e $rootfs/dev/random || mknod -m 666 $rootfs/dev/random c 1 8
206 test -e $rootfs/dev/urandom || mknod -m 666 $rootfs/dev/urandom c 1 9
207 test -e $rootfs/dev/pts || mkdir -m 755 $rootfs/dev/pts
208 test -e $rootfs/dev/shm || mkdir -m 1777 $rootfs/dev/shm
209 test -e $rootfs/dev/tty || mknod -m 666 $rootfs/dev/tty c 5 0
210 test -e $rootfs/dev/console || mknod -m 600 $rootfs/dev/console c 5 1
211 test -e $rootfs/dev/tty0 || mknod -m 666 $rootfs/dev/tty0 c 4 0
212 test -e $rootfs/dev/full || mknod -m 666 $rootfs/dev/full c 1 7
213 test -e $rootfs/dev/initctl || mknod -m 600 $rootfs/dev/initctl p
214 test -e $rootfs/dev/ptmx || mknod -m 666 $rootfs/dev/ptmx c 5 2
216 test -e $rootfs/dev/tty1 || mknod -m 666 $rootfs/dev/tty1 c 4 1
217 test -e $rootfs/dev/tty2 || mknod -m 666 $rootfs/dev/tty2 c 4 2
218 test -e $rootfs/dev/tty3 || mknod -m 666 $rootfs/dev/tty3 c 4 3
219 test -e $rootfs/dev/tty4 || mknod -m 666 $rootfs/dev/tty4 c 4 4
220 test -e $rootfs/dev/tty5 || mknod -m 666 $rootfs/dev/tty5 c 4 5
221 test -e $rootfs/dev/tty6 || mknod -m 666 $rootfs/dev/tty6 c 4 6
229 if grep -q ^\#lxc.pubkey $path/config
231 pubkey=`grep ^\#lxc.pubkey $path/config | cut -d= -f2`
233 if [ ! -e $rootfs/root/.ssh ]
235 mkdir $rootfs/root/.ssh
237 if [ ! -z "$pubkey" ]
239 echo "Copying $pubkey -> $rootfs/root/.ssh/authorized_keys"
240 cp $pubkey $rootfs/root/.ssh/authorized_keys
244 configure_hostkeydir(){
252 [ -d $rootfs/etc/ssh ] || mkdir -p $rootfs/etc/ssh
253 if [ -e $hostkeydir ]
255 if [ ! -d $hostkeydir/$name ]
257 mkdir $hostkeydir/$name
258 echo "Creating SSH2 RSA key; this may take some time ..."
259 ssh-keygen -q -f $hostkeydir/$name/ssh_host_rsa_key -N '' -t rsa
260 echo "Creating SSH2 DSA key; this may take some time ..."
261 ssh-keygen -q -f $hostkeydir/$name/ssh_host_dsa_key -N '' -t dsa
263 cp $hostkeydir/$name/ssh_host_* $rootfs/etc/ssh/
273 # check the mini centos was not already downloaded
274 mkdir -p "$cache/partial-$arch"
275 if [ $? -ne 0 ]; then
276 echo "Failed to create '$cache/partial-$arch' directory"
280 # download a mini centos into a cache
281 echo "Downloading centos minimal ..."
282 #febootstrap $DISTRO $cache/partial-$arch
283 rinsearch=`dpkg-architecture -qDEB_BUILD_ARCH`
284 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"
286 cat <<EOF > /var/cache/rinse/$DISTRO-$arch.before_post_install
287 echo "Rinse defines mirror=\$mirror in config"
288 server=\`echo \$mirror | cut -d/ -f3\`
289 echo "Disable mirrorlists"
290 sed -i -e "s/^mirrorlist=/#mirrorlist=/" $cache/partial-$arch/etc/yum.repos.d/CentOS-Base.repo
291 echo "Setup baseurl on server \$server"
292 sed -i -e "s!#baseurl=http://mirror.centos.org!baseurl=http://\$server!" $cache/partial-$arch/etc/yum.repos.d/CentOS-Base.repo
293 rm -f $cache/partial-$arch/etc/mtab
294 ln -s /proc/mounts $cache/partial-$arch/etc/mtab
296 chmod 755 /var/cache/rinse/$DISTRO-$arch.before_post_install
298 cat <<EOF > /var/cache/rinse/$DISTRO-$arch.after_post_install
301 chroot $cache/partial-$arch /bin/mount proc /proc -t proc
303 echo "Install $pkglist"
304 chroot $cache/partial-$arch /usr/bin/yum -y install $pkglist
307 if grep -q ^http_proxy /etc/wgetrc
309 PROXY=`grep ^http_proxy /etc/wgetrc | tail -1 | sed -e 's!.*http://!!' -e 's!/!!'`
312 cat <<EOF >> /var/cache/rinse/$DISTRO-$arch.after_post_install
313 # PROXY CONF FROM WGET
314 echo "Set up proxy $PROXY for pear"
315 chroot $cache/partial-$arch /usr/bin/pear config-set http_proxy $PROXY
319 cat <<EOF >> /var/cache/rinse/$DISTRO-$arch.after_post_install
321 # phpunit install, requires: php-pear php-dom
322 echo "Updating pear.php.net pear channel"
323 chroot $cache/partial-$arch /usr/bin/pear channel-update pear.php.net
324 #optional?# chroot $cache/partial-$arch /usr/bin/pear channel-discover components.ez.no
325 echo "Discovering pear.phpunit.de pear channel"
326 chroot $cache/partial-$arch /usr/bin/pear channel-discover pear.phpunit.de
327 echo "Discovering pear.symfony-project.com pear channel"
328 chroot $cache/partial-$arch /usr/bin/pear channel-discover pear.symfony-project.com
329 echo "Upgrading pear"
330 chroot $cache/partial-$arch /usr/bin/pear upgrade-all
331 chroot $cache/partial-$arch /usr/bin/pear upgrade --force PEAR
332 echo "Installing phpunit/PHPUnit"
333 chroot $cache/partial-$arch /usr/bin/pear install --alldeps phpunit/PHPUnit
336 chroot $cache/partial-$arch /bin/umount /proc
338 chmod 755 /var/cache/rinse/$DISTRO-$arch.after_post_install
340 rinse --config /etc/rinse/rinse.conf \
342 --distribution $DISTRO \
343 --before-post-install /var/cache/rinse/$DISTRO-$arch.before_post_install \
344 --after-post-install /var/cache/rinse/$DISTRO-$arch.after_post_install \
345 --directory $cache/partial-$arch
347 if [ $? -ne 0 ]; then
348 echo "Failed to download the rootfs, aborting."
352 mv "$1/partial-$arch" "$1/rootfs-$arch"
353 echo "Download complete."
364 # make a local copy of the minicentos
365 echo -n "Copying rootfs to $rootfs ..."
366 cp -a $cache/rootfs-$arch $rootfs || return 1
372 cache="/var/cache/lxc/$DISTRO"
374 mkdir -p /var/lock/subsys/
377 if [ $? -ne 0 ]; then
378 echo "Cache repository is busy."
384 echo "Checking cache download in $cache/rootfs-$arch ... "
385 if [ ! -e "$cache/rootfs-$arch" ]; then
386 download_centos $cache $arch
387 if [ $? -ne 0 ]; then
388 echo "Failed to download 'centos base'"
393 echo "Copy $cache/rootfs-$arch to $rootfs ... "
394 copy_centos $cache $arch $rootfs
395 if [ $? -ne 0 ]; then
396 echo "Failed to copy rootfs"
402 ) 200>/var/lock/subsys/lxc
409 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 }'
419 if [ ! -e "$macdir/$name" ]
421 gen_mac > $macdir/$name
423 macaddr=`cat $macdir/$name`
425 cat <<EOF >> $path/config
426 lxc.network.hwaddr = $macaddr
427 # lxc.utsname = $name
432 # lxc.mount = $path/fstab
434 # lxc.console = /dev/console
436 lxc.cgroup.devices.deny = a
438 lxc.cgroup.devices.allow = c 1:3 rwm
439 lxc.cgroup.devices.allow = c 1:5 rwm
441 lxc.cgroup.devices.allow = c 5:1 rwm
442 lxc.cgroup.devices.allow = c 5:0 rwm
443 lxc.cgroup.devices.allow = c 4:0 rwm
444 lxc.cgroup.devices.allow = c 4:1 rwm
446 lxc.cgroup.devices.allow = c 1:9 rwm
447 lxc.cgroup.devices.allow = c 1:8 rwm
448 lxc.cgroup.devices.allow = c 136:* rwm
449 lxc.cgroup.devices.allow = c 5:2 rwm
451 lxc.cgroup.devices.allow = c 254:0 rwm
454 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
455 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
456 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults 0 0
459 # cat <<EOF > $path/fstab
460 #proc $rootfs/proc proc nodev,noexec,nosuid 0 0
461 #devpts $rootfs/dev/pts devpts defaults 0 0
462 ##sysfs $rootfs/sys sysfs defaults 0 0
463 #tmpfs $rootfs/dev/shm tmpfs defaults 0 0
466 if [ $? -ne 0 ]; then
467 echo "Failed to add configuration"
476 cache="/var/cache/lxc/$DISTRO"
478 if [ ! -e $cache ]; then
482 # lock, so we won't purge while someone is creating a repository
486 echo "Cache repository is busy."
490 echo -n "Purging the download cache..."
491 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
494 ) 200>/var/lock/subsys/lxc
500 $1 -h|--help -p|--path=<path> --clean
505 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
506 if [ $? -ne 0 ]; then
510 eval set -- "$options"
515 -h|--help) usage $0 && exit 0;;
516 -p|--path) path=$2; shift 2;;
517 -n|--name) name=$2; shift 2;;
518 -c|--clean) clean=$2; shift 2;;
519 --) shift 1; break ;;
524 if [ ! -z "$clean" -a -z "$path" ]; then
530 if [ $? -ne 0 ]; then
531 echo "'netmask' command is missing"
536 if [ $? -ne 0 ]; then
537 echo "'rinse' command is missing"
541 if [ -z "$path" ]; then
542 echo "'path' parameter is required"
546 if [ "$(id -u)" != "0" ]; then
547 echo "This script should be run as 'root'"
552 oldhostkeydir=/var/lib/lxc/ssh
553 hostkeydir=/var/cache/lxc/ssh
554 macdir=/var/cache/lxc/mac
555 if [ -e $oldhostkeydir ]
557 if [ ! -e $hostkeydir ]
559 mv $oldhostkeydir $hostkeydir
562 if [ ! -e $hostkeydir ]
571 install_centos $rootfs
572 if [ $? -ne 0 ]; then
573 echo "failed to install centos"
577 configure_centos $path $rootfs $name
578 if [ $? -ne 0 ]; then
579 echo "failed to configure centos for a container"
583 copy_configuration $path $rootfs $name $macdir
584 if [ $? -ne 0 ]; then
585 echo "failed write configuration file"
589 configure_pubkey $path $rootfs
590 if [ $? -ne 0 ]; then
591 echo "failed to configure pubkey"
595 configure_hostkeydir $rootfs $hostkeydir $name
596 if [ $? -ne 0 ]; then
597 echo "failed to configure hostkeydir"
601 if [ ! -z $clean ]; then