#!/bin/bash # # Create an LXC RHEL-based container # Inspired by: http://wiki.1tux.org/wiki/Centos6/Installation/Minimal_installation_using_yum # http://wiki.1tux.org/wiki/Lxc/Installation/Guest/Centos/6 # https://github.com/docker/docker/blob/master/contrib/mkimage-yum.sh # get_release_package () { local url=$1 local relase_package=$2 curl $url | grep $relase_package | sed -n 1p | sed 's/.*\( \|>\)\([^ <>]\+\.rpm\).*/\2/' } create_repofiles () { local os_type=$1 local os_version=$2 local os_arch=$3 case $os_type in centos) local os_url="ftp://ftp.tlk-l.net/pub/mirrors/centos.org/$os_version/os/$os_arch/Packages/" local release_package="release" ;; scientific) local os_url="http://mirror.imbg.org.ua/pub/linux/scientific/$os_version/$os_arch/os/Packages/" local release_package="sl-release" ;; *) echo "$os_type type is not supported." exit 1 ;; esac local release_rpm=$( get_release_package $os_url $release_package ) cd $ROOTFSDIR curl $os_url/$release_rpm > $release_rpm rpm2cpio $release_rpm | cpio -idm sed -i "s/\$releasever/$os_version/g" ./etc/yum.repos.d/* rm -f $release_rpm [ "$os_arch" != "$( uname -i)" ] && setarch $os_arch } yum_install_footfs () { yum --nogpgcheck --assumeyes --installroot=$ROOTFSDIR --setopt=tsflags=nodocs \ --setopt=group_package_types=mandatory groupinstall Core yum --nogpgcheck --assumeyes --installroot=$ROOTFSDIR install \ openssh-server vim-enhanced wget man mc yum --nogpgcheck --assumeyes --installroot=$ROOTFSDIR clean all } make_device_nodes () { local target=$ROOTFSDIR rm -rf "$target"/dev mkdir -m 755 "$target"/dev mkdir -m 1777 "$target"/dev/shm mkdir -m 755 "$target"/dev/pts mknod -m 600 "$target"/dev/console c 5 1 mknod -m 600 "$target"/dev/initctl p mknod -m 666 "$target"/dev/full c 1 7 mknod -m 666 "$target"/dev/null c 1 3 mknod -m 666 "$target"/dev/ptmx c 5 2 mknod -m 666 "$target"/dev/random c 1 8 mknod -m 666 "$target"/dev/tty c 5 0 mknod -m 666 "$target"/dev/tty0 c 4 0 mknod -m 666 "$target"/dev/tty1 c 4 1 mknod -m 666 "$target"/dev/urandom c 1 9 mknod -m 666 "$target"/dev/zero c 1 5 } make_files () { local target=$ROOTFSDIR cp -a "$target"/etc/skel/. "$target"/root/. cat > /etc/hosts << END 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 END cat > "$target"/etc/sysconfig/network << END NETWORKING=yes END cat > "$target"/etc/fstab << END /dev/root / rootfs defaults 0 0 none /dev/shm tmpfs nosuid,nodev 0 0 END } create_upstart_script () { local target=$ROOTFSDIR cat > "$target"/etc/init/lxc-sysinit.conf << END start on startup env container pre-start script if [ "x$container" != "xlxc" -a "x$container" != "xlibvirt" ]; then stop; fi initctl start tty TTY=console rm -f /var/lock/subsys/* rm -f /var/run/*.pid telinit 3 exit 0; end script END } visual_tuning () { local target=$ROOTFSDIR local host_name=$( hostname -s | tr '[a-z]' '[A-Z]' ) cat >> "$target"/root/.bashrc << END # Colorize BASH prompt for root PS1="\[\033[1;31m\]${host_name}\[\033[0;31m\]:\[\033[0;36m\]LXC\[\033[0;31m\]:\[\033[0;33m\]\h \[\033[1;31m\][\u \W]#\[\033[0;39m\] " END } # parse command line options while getopts "d:t:v:a:" opt; do case $opt in d) export ROOTFSDIR="$OPTARG" ;; t) os_type="$OPTARG" ;; v) os_version="$OPTARG" ;; a) os_arch="$OPTARG" ;; esac done # defaults [ -z "$ROOTFSDIR" ] && exit 1 [ -z "$os_arch" ] && os_arch="x86_64" [ -z "$os_type" ] && os_type="centos" [ -z "$os_version" ] && os_version="6.6" #TODO: function to get latest version mkdir -p $ROOTFSDIR create_repofiles "$os_type" "$os_version" "$os_arch" yum_install_footfs make_device_nodes make_files visual_tuning if [ "$os_type" = "centos" -o "$os_type" = "scientific" ]; then [ "${os_version:0:1}" = "6" ] && create_upstart_script fi