lxc-archlinux.in revision 75350ec8c77acad383eea8e36b2dc3faeea34460
#
# template script for generating Arch linux container for LXC
#
#
# lxc: linux Container library
# Authors:
# Alexander Vladimirov <idkfa@vlan1.ru>
# 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
# defaults
lxc_network_type="veth"
lxc_network_link="br0"
default_rc_timezone="UTC"
# sort of minimal package set
"filesystem"
"initscripts"
"coreutils"
"module-init-tools"
"procps"
"psmisc"
"pacman"
"bash"
"syslog-ng"
"cronie"
"iproute2"
"iputils"
"inetutils"
"dhcpcd"
"dnsutils"
"nano"
"grep"
"less"
"gawk"
"sed"
"tar"
"wget"
"gzip"
"which"
)
# find and extract parameter value from given config file
# ${1} - file to read parameter from
# ${2} - parameter name
# ${result} - result value on success
function read_parameter_value {
[ -f ${1} ] && [ "${2}" ] || return 1
local pattern="^[[:space:]]*${2}[[:space:]]*=[[:space:]]*"
return 0
}
# split comma-separated string into an array
# ${1} - string to split
# ${2} - separator (default is ",")
# ${result} - result value on success
function split_string {
IFS="${2:-,}"
return 0
}
# Arch-specific preconfiguration for container
function configure_arch {
# read locale and timezone defaults from system rc.conf if running on Arch
if [ "${is_arch}" ]; then
read_parameter_value "/etc/rc.conf" "LOCALE"
rc_locale=${result:-${default_rc_locale}}
read_parameter_value "/etc/rc.conf" "TIMEZONE"
rc_timezone=${result:-${default_rc_timezone}}
else
fi
echo "Setting up rc.conf"
# /etc/rc.conf - Main Configuration for Arch Linux
LOCALE="${rc_locale}"
DAEMON_LOCALE="no"
HARDWARECLOCK="local"
TIMEZONE="${rc_timezone}"
KEYMAP=us
CONSOLEFONT=
CONSOLEMAP=
USECOLOR="yes"
MODULES=()
HOSTNAME="${name}"
interface=eth0
address=
netmask=
broadcast=
gateway=
DAEMONS=(syslog-ng crond network)
EOF
fi
fi
echo "Setting up rc.sysinit"
#!/bin/bash
. /etc/rc.conf
. /etc/rc.d/functions
echo "starting Arch Linux"
rm -f \$(find /var/run -name '*pid')
rm -f /run/daemons/*
rm -f /var/lock/subsys/*
rm -f /etc/mtab
touch /etc/mtab
run_hook sysinit_end
EOF
echo "Setting up rc.shutdown"
#!/bin/bash
. /etc/rc.conf
. /etc/rc.d/functions
stty onlcr
run_hook shutdown_start
[[ -x /etc/rc.local.shutdown ]] && /etc/rc.local.shutdown
stop_all_daemons
run_hook shutdown_prekillall
kill_all
run_hook shutdown_postkillall
[[ \${TIMEZONE} ]] && cp --remove-destination "/usr/share/zoneinfo/\${TIMEZONE}" /etc/localtime
halt -w
umount -a -r -t nodevtmpfs,notmpfs,nosysfs,noproc,nodevpts -O no_netdev
run_hook shutdown_postumount
run_hook shutdown_poweroff
if [[ \${RUNLEVEL} = 0 ]]; then
poweroff -d -f -i
else
reboot -d -f -i
fi
# vim: set ts=2 sw=2 noet:
EOF
chmod 755 "${rootfs_path}/etc/rc.shutdown.lxc" "${rootfs_path}/etc/rc.sysinit.lxc"
echo "Setting up inittab"
id:3:initdefault:
rc::sysinit:/etc/rc.sysinit.lxc
rs:S1:wait:/etc/rc.single
rm:2345:wait:/etc/rc.multi
rh:06:wait:/etc/rc.shutdown.lxc
su:S:wait:/sbin/sulogin -p
c1:2345:respawn:/sbin/agetty -8 38400 tty1 linux
EOF
echo "Setting up hosts"
127.0.0.1 localhost.localdomain localhost ${name}
::1 localhost.localdomain localhost
EOF
echo "Setting up nameserver"
grep nameserver /etc/resolv.conf > "${rootfs_path}/etc/resolv.conf"
echo "Setting up device nodes"
return 0
}
# write container configuration files
function copy_configuration {
mkdir -p "${config_path}"
grep -q "^lxc.rootfs" "${config_path}/config" 2>/dev/null || echo "lxc.rootfs=${rootfs_path}" >> "${config_path}/config"
lxc.utsname = ${name}
lxc.tty = 4
lxc.pts = 1024
lxc.mount = ${config_path}/fstab
# When using LXC with apparmor, uncomment the next line to run unconfined:
#lxc.aa_profile = unconfined
#networking
lxc.network.type = ${lxc_network_type}
lxc.network.flags = up
lxc.network.link = ${lxc_network_link}
lxc.network.name = eth0
lxc.network.mtu = 1500
#cgroups
lxc.cgroup.devices.deny = a
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
# /dev/pts
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm
EOF
none ${rootfs_path}/dev/pts devpts defaults 0 0
none ${rootfs_path}/proc proc nodev,noexec,nosuid 0 0
none ${rootfs_path}/sys sysfs defaults 0 0
none ${rootfs_path}/dev/shm tmpfs defaults 0 0
EOF
if [ ${?} -ne 0 ]; then
echo "Failed to configure container"
return 1
fi
return 0
}
# lock chroot and mount subdirectories before installing container
function mount_chroot {
echo "mounting chroot"
umask 0022
if [ -n "${host_mirror_path}" ]; then
fi
}
function umount_chroot {
if [ -z "${umount_done}" ]; then
echo "unmounting chroot"
umount "${rootfs_path}/proc"
umount "${rootfs_path}/sys"
umount "${rootfs_path}/dev"
umount "${rootfs_path}/${cache_dir}"
fi
}
# install packages within container chroot
function install_arch {
cat <<EOF > "${pacman_config}"
[options]
HoldPkg = pacman glibc
SyncFirst = pacman
Architecture = auto
#IgnorePkg = udev
[core]
Include = /etc/pacman.d/mirrorlist
Server = ${host_mirror}
[extra]
Include = /etc/pacman.d/mirrorlist
Server = ${host_mirror}
[community]
Include = /etc/pacman.d/mirrorlist
Server = ${host_mirror}
EOF
mkdir -p "${rootfs_path}/etc"
fi
cache_dir=$( (grep -m 1 '^CacheDir' "${pacman_config}" || echo 'CacheDir = /var/cache/pacman/pkg') | sed 's/CacheDir\s*=\s*//')
params="--root ${rootfs_path} --config=${pacman_config} --noconfirm"
echo "Failed to preinstall udev package record"
return 1
fi
if ! pacman -S ${params} ${base_packages[@]}; then
echo "Failed to install container packages"
return 1
fi
mv "${pacman_config}" "${rootfs_path}/etc/pacman.conf"
return 0
}
usage()
{
cat <<EOF
usage:
${1} -n|--name=<container_name>
[-P|--packages=<pkg1,pkg2,...>] [-p|--path=<path>] [-h|--help]
Mandatory args:
-n,--name container name, used to as an identifier for that container from now on
Optional args:
-p,--path path to where the container rootfs will be created, defaults to $default_path. The container config will go under $default_path in that case
-P,--packages preinstall additional packages, comma-separated list
-h,--help print this help
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
if [ -z "${name}" ]; then
echo "missing required 'name' parameter"
exit 1
fi
if [ ${?} -ne 0 ]; then
echo "'pacman' command is missing, refer to wiki.archlinux.org for information about installing pacman"
exit 1
fi
if [ -z "${path}" ]; then
path="${default_path}/${name}"
fi
if [ "${EUID}" != "0" ]; then
echo "This script should be run as 'root'"
exit 1
fi
rootfs_path="${path}/rootfs"
# check for 'lxc.rootfs' passed in through default config by lxc-create
fi
config_path="${default_path}/${name}"
revert()
{
echo "Interrupted, so cleaning up"
# maybe was interrupted before copy config
exit 1
}
if [ ${?} -ne 0 ]; then
echo "failed write configuration file"
exit 1
fi
if [ ${#additional_packages[@]} -gt 0 ]; then
base_packages+=(${result[@]})
fi
if [ ${?} -ne 0 ]; then
echo "failed to install Arch linux"
exit 1
fi
if [ ${?} -ne 0 ]; then
echo "failed to configure Arch linux for a container"
exit 1
fi
echo "container rootfs and config created"