lxc-oracle.in revision 14d9c0f09d1a55d124ef210a4b4e205c9fe7596c
#
# Template script for generating Oracle Enterprise Linux container for LXC
# based on lxc-fedora, lxc-ubuntu
#
# Copyright � 2011 Wim Coekaerts <wim.coekaerts@oracle.com>
# Copyright � 2012 Dwight Engen <dwight.engen@oracle.com>
#
# Modified for Oracle Linux 5
# Wim Coekaerts <wim.coekaerts@oracle.com>
#
# Modified for Oracle Linux 6, combined OL5,6 into one template script
# Dwight Engen <dwight.engen@oracle.com>
#
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# use virbr0 that is setup by default by libvirtd
die()
{
echo "failed: $1"
exit 1
}
{
return 0
fi
return 1
}
# fix up the container_rootfs
{
echo "Configuring container for Oracle Linux $container_release_major"
# this doesnt actually disable it if it's enabled in the host, since
# libselinux::is_selinux_enabled() in the guest will check
# /proc/filesystems and see selinuxfs, thus reporting that it is on
# (ie. check the output of sestatus in the guest)
echo 0 > $container_rootfs/selinux/enforce
if [ -e $container_rootfs/etc/selinux/config ]; then
else
fi
sed -i 's|session[ ]*required[ ]*pam_selinux.so[ ]*close|#session required pam_selinux.so close|' $container_rootfs/etc/pam.d/login
sed -i 's|session[ ]*required[ ]*pam_selinux.so[ ]*open|#session required pam_selinux.so open|' $container_rootfs/etc/pam.d/login
sed -i 's|session[ ]*required[ ]*pam_loginuid.so|#session required pam_loginuid.so|' $container_rootfs/etc/pam.d/login
# configure the network to use dhcp. we set DHCP_HOSTNAME so the guest
# will report its name and be resolv'able by the hosts dnsmasq
cat <<EOF > $container_rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
HOSTNAME=$name
DHCP_HOSTNAME=$name
NM_CONTROLLED=no
TYPE=Ethernet
EOF
# set the hostname
cat <<EOF > $container_rootfs/etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=$name
EOF
# set minimal hosts
# disable ipv6
cat <<EOF > $container_rootfs/etc/fstab
proc /proc proc nodev,noexec,nosuid 0 0
sysfs /sys sysfs defaults 0 0
EOF
# remove module stuff for iptables it just shows errors that are not
# relevant in a container
sed -i 's|IPTABLES_MODULES=".*|IPTABLES_MODULES=""|' $container_rootfs/etc/sysconfig/iptables-config
sed -i 's|IPTABLES_MODULES_UNLOAD=".*|IPTABLES_MODULES_UNLOAD="no"|' $container_rootfs/etc/sysconfig/iptables-config
fi
# disable readahead in the container
fi
# disable udev in the container
fi
# prevent rc.sysinit from attempting to loadkeys
fi
# dont try to sync the hwclock at shutdown
# dont start lvm
sed -i 's|action $"Setting up Logical Volume Management:"|#action $"Setting up Logical Volume Management:"|' $container_rootfs/etc/rc.sysinit
sed -i 's|action $"Setting up Logical Volume Management:"|/bin/true #action $"Setting up Logical Volume Management:"|' $container_rootfs/etc/rc.d/rc.sysinit
# fix assumptions that plymouth is available
sed -i 's|\[ "$PROMPT" != no \] && plymouth|[ "$PROMPT" != no ] \&\& [ -n "$PLYMOUTH" ] \&\& plymouth|' $container_rootfs/etc/rc.sysinit
sed -i 's|\[ "$PROMPT" != no \] && plymouth|[ "$PROMPT" != no ] \&\& [ -n "$PLYMOUTH" ] \&\& plymouth|' $container_rootfs/etc/rc.d/rc.sysinit
# since lxc.devttydir is specified in the config.
# allow root login on console, tty[1-4], and pts/0 for libvirt
sed -i 's|&& $1 !~ /^\\/dev\\/ram/|\&\& $2 !~ /^\\/dev\\/lxc/ \&\& $1 !~ /^\\/dev\\/ram/|' $container_rootfs/etc/init.d/halt
fi
cat <<EOF > $container_rootfs/etc/init/console.conf
# console - getty
#
# This service maintains a getty on the console from the point the system is
# started until it is shut down again.
start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]
respawn
exec /sbin/mingetty /dev/console
EOF
fi
# there might be other services that are useless but the below set is a good start
# some of these might not exist in the image, so we silence chkconfig complaining
# about the service file not being found
do
done
do
done
# or libvirt itself to be a symlink to the right pty.
# take care to not nuke /dev in case $container_rootfs isn't set
dev_path="$container_rootfs/dev"
fi
# pty's will get crossed up with the hosts (https://lkml.org/lkml/2012/1/23/512)
# add oracle user, set root password
echo -e "Added container user:\033[1moracle\033[0m password:\033[1moracle\033[0m"
echo -e "Added container user:\033[1mroot\033[0m password:\033[1mroot\033[0m"
}
# create the container's lxc config file
{
echo "Create configuration file $cfg_dir/config"
# generate a hwaddr for the container with a high mac address
cat <<EOF >> $cfg_dir/config || die "unable to create $cfg_dir/config"
# Container configuration for Oracle Linux $release_major.$release_minor
lxc.arch = $arch
lxc.utsname = $name
lxc.devttydir = lxc
lxc.tty = 4
lxc.pts = 1024
lxc.rootfs = $container_rootfs
lxc.mount = $cfg_dir/fstab
# Networking
EOF
# see if the network settings were already specified
fi
cat <<EOF >> $cfg_dir/config || die "unable to create $cfg_dir/config"
lxc.network.name = eth0
lxc.network.mtu = 1500
lxc.network.hwaddr = $hwaddr
# Control Group devices: all denied except those whitelisted
lxc.cgroup.devices.deny = a
lxc.cgroup.devices.allow = c 1:3 rwm # /dev/null
lxc.cgroup.devices.allow = c 1:5 rwm # /dev/zero
lxc.cgroup.devices.allow = c 1:7 rwm # /dev/full
lxc.cgroup.devices.allow = c 5:0 rwm # /dev/tty
lxc.cgroup.devices.allow = c 1:8 rwm # /dev/random
lxc.cgroup.devices.allow = c 1:9 rwm # /dev/urandom
lxc.cgroup.devices.allow = c 136:* rwm # /dev/tty[1-4] ptys and lxc console
lxc.cgroup.devices.allow = c 5:2 rwm # /dev/ptmx pty master
lxc.cgroup.devices.allow = c 254:0 rwm # /dev/rtc0
EOF
cat <<EOF > $cfg_dir/fstab || die "unable to create $cfg_dir/fstab"
proc $container_rootfs/proc proc nodev,noexec,nosuid 0 0
devpts $container_rootfs/dev/pts devpts defaults 0 0
sysfs $container_rootfs/sys sysfs defaults 0 0
EOF
}
{
if is_btrfs_subvolume $template_rootfs; then
# lxc-create already made $container_rootfs a btrfs subvolume, but
# in this case we want to snapshot the original subvolume so we we
# have to delete the one that lxc-create made
btrfs subvolume snapshot $template_rootfs $container_rootfs || die "btrfs clone template"
else
cp -ax $template_rootfs $container_rootfs || die "copy template"
fi
}
{
cmds="rpm wget yum"
db_dump_cmd="db5.1_dump"
db_load_cmd="db4.3_load"
fi
db_dump_cmd="db_dump"
db_load_cmd="db43_load"
fi
cmds="$cmds $db_dump_cmd $db_load_cmd file"
fi
if [ $? -ne 0 ]; then
fi
done
(
flock -x 200
if [ $? -ne 0 ]; then
die "The template is busy."
fi
echo "Downloading release $release_major.$release_minor for $basearch"
# get yum repo file
else
die "Unsupported release $release_major"
fi
if [ $? -ne 0 ]; then
die "Failed to download repo file $public_yum_url/$repofile"
fi
# yum will take $basearch from host, so force the arch we want
# replace url if they specified one
sed -i "s|baseurl=http://public-yum.oracle.com/repo|baseurl=$repourl/repo|" $container_rootfs/etc/yum.repos.d/$repofile
sed -i "s|gpgkey=http://public-yum.oracle.com|gpgkey=$repourl|" $container_rootfs/etc/yum.repos.d/$repofile
fi
# disable all repos, then enable the repo for the version we are installing.
else
fi
else
fi
# create rpm db, download and yum install minimal packages
yum_cmd="yum --installroot $container_rootfs --disablerepo=* --enablerepo=$repo -y --nogpgcheck"
min_pkgs="yum initscripts passwd rsyslog vim-minimal openssh-server dhclient chkconfig rootfiles policycoreutils oraclelinux-release"
if [ $? -ne 0 ]; then
die "Failed to download and install the rootfs, aborting."
fi
# rsyslog and pam depend on coreutils for some common commands in
# their POSTIN scriptlets, but coreutils wasn't installed yet. now
# that coreutils is installed, reinstall the packages so their POSTIN
# runs right. similarly, libutempter depends on libselinux.so.1 when
rpm --root $container_rootfs --nodeps -e rsyslog pam libutempter
if [ $? -ne 0 ]; then
die "Unable to reinstall packages"
fi
fi
# these distributions put the rpm database in a place the guest is
# not expecting it, so move it
fi
# if the native rpm created the db with Hash version 9, we need to
# downgrade it to Hash version 8 for use with OL5.x
echo "Fixing (downgrading) rpm database from version $db_version"
done
fi
# the host rpm may not be the same as the guest, rebuild the db with
# the guest rpm version
echo "Rebuilding rpm database"
) 200>/var/lock/subsys/lxc-oracle-$name
}
usage()
{
cat <<EOF
-a|--arch=<arch> architecture (ie. i386, x86_64)
-R|--release=<release> release to download for the new container
-u|--url=<url> replace yum repo url (ie. local yum mirror)
-t|--templatefs=<path> copy/clone rootfs at path instead of downloading
-h|--help
Release is of the format "major.minor", for example "5.8", "6.3", or "6.latest"
EOF
return 0
}
if [ $? -ne 0 ]; then
exit 1
fi
eval set -- "$options"
while true
do
case "$1" in
--) shift 1; break ;;
*) break ;;
esac
done
# make sure mandatory args are given and valid
echo "This script should be run as 'root'"
exit 1
fi
if [ -z $name ]; then
echo "Container name must be given"
exit 1
fi
if [ -z $cfg_dir ]; then
echo "Configuration directory must be given, check lxc-create"
exit 1
fi
if [ "$arch" = "i686" ]; then
basearch="i386"
fi
echo "Bad architecture given, check lxc-create"
exit 1
fi
container_rootfs="$cfg_dir/rootfs"
if [ -n "$template_rootfs" ]; then
fi
if [ -z "$release_version" ]; then
echo "No release specified with -R, defaulting to 6.3"
release_version="6.3"
fi
else
if [ -f /etc/fedora-release ]; then
host_distribution="Fedora"
elif [ -f /etc/oracle-release ]; then
host_distribution="OracleServer"
else
echo "Unable to determine host distribution, ensure lsb_release is installed"
exit 1
fi
fi
echo "Host is $host_distribution $host_release_version"
if [ -n "$template_rootfs" ]; then
else
fi
echo "Container : $container_rootfs"
echo "Config : $cfg_dir/config"
echo "Network : eth0 ($lxc_network_type) on $lxc_network_link"