lxc-archlinux.in revision b335cf8d4b56a49d5bc3fd1229139c2595779891
#
# template script for generating Arch linux container for LXC
#
#
# lxc: linux Container library
# Authors:
# Alexander Vladimirov <idkfa@vlan1.ru>
# John Lane <lxc@jelmail.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# defaults
lxc_network_type="veth"
lxc_network_link="br0"
default_path="@LXCPATH@"
default_timezone="UTC"
# sort of minimal package set
"systemd"
"systemd-sysvcompat"
"filesystem"
"coreutils"
"kmod"
"procps"
"psmisc"
"pacman"
"bash"
"cronie"
"iproute2"
"iputils"
"inetutils"
"dhcpcd"
"dnsutils"
"nano"
"grep"
"less"
"gawk"
"sed"
"tar"
"gzip"
"which"
)
# split comma-separated string into an array
# ${1} - string to split
# ${2} - separator (default is ",")
# ${result} - result value on success
IFS="${2:-,}"
return 0
}
# Arch-specific preconfiguration for container
# read locale and timezone defaults from system rc.conf if running on Arch
if [ "${is_arch}" ]; then
cp -p /etc/vconsole.conf /etc/locale.conf /etc/locale.gen "${rootfs_path}/etc/"
else
0.0 0.0 0.0
0
LOCAL
EOF
fi
fi
fi
127.0.0.1 localhost.localdomain localhost ${name}
::1 localhost.localdomain localhost
EOF
grep nameserver /etc/resolv.conf > "${rootfs_path}/etc/resolv.conf"
mkdir /run/lock
locale-gen
ln -s /usr/share/zoneinfo/${default_timezone} /etc/localtime
# disable services unavailable for container
ln -s /dev/null /etc/systemd/system/systemd-udevd.service
ln -s /dev/null /etc/systemd/system/systemd-udevd-control.socket
ln -s /dev/null /etc/systemd/system/systemd-udevd-kernel.socket
ln -s /dev/null /etc/systemd/system/proc-sys-fs-binfmt_misc.automount
# set default systemd target
ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
EOF
return 0
}
# write container configuration files
mkdir -p "${config_path}"
lxc.utsname=${name}
lxc.autodev=1
lxc.tty=1
lxc.pts=1024
lxc.mount=${config_path}/fstab
lxc.cap.drop=sys_module mac_admin mac_override sys_time
lxc.kmsg=0
lxc.stopsignal=SIGRTMIN+4
#networking
lxc.network.type=${lxc_network_type}
lxc.network.link=${lxc_network_link}
lxc.network.flags=up
lxc.network.name=eth0
lxc.network.mtu=1500
#cgroups
lxc.cgroup.devices.deny = a
lxc.cgroup.devices.allow = c *:* m
lxc.cgroup.devices.allow = b *:* m
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
lxc.cgroup.devices.allow = c 1:7 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:2 rwm
lxc.cgroup.devices.allow = c 136:* rwm
EOF
grep -q "^lxc.rootfs" ${config_path}/config 2>/dev/null || echo "lxc.rootfs = ${rootfs_path}" >> ${config_path}/config
sysfs sys sysfs defaults 0 0
proc proc proc nodev,noexec,nosuid 0 0
EOF
return 0
}
# install packages within container chroot
if [ "${different_arch}" = "true" ]; then
"${pacman_config}" > "${container_pacman_config}"
/etc/pacman.d/mirrorlist > "${container_mirrorlist}"
pacman_config="${container_pacman_config}"
fi
echo "Failed to install container packages"
return 1
fi
if [ "${different_arch}" = "true" ]; then
rm "${container_pacman_config}" "${container_mirrorlist}"
fi
return 0
}
cat <<EOF
usage:
${1} -n|--name=<container_name>
[-P|--packages=<pkg1,pkg2,...>] [-p|--path=<path>] [-t|--network_type=<type>] [-l|--network_link=<link>] [-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}/rootfs. The container config will go under ${default_path} in that case
-P,--packages preinstall additional packages, comma-separated list
-e,--enable_units Enable additional systemd units, comma-separated list
-c,--config use specified pacman config when installing container packages
-a,--arch use specified architecture instead of host's architecture
-t,--network_type set container network interface type (${lxc_network_type})
-l,--network_link set network link device (${lxc_network_link})
-r,--root_passwd set container root password
-h,--help print this help
EOF
return 0
}
options=$(getopt -o hp:P:e:n:c:a:l:t:r: -l help,rootfs:,path:,packages:,enable_units:,name:,config:,arch:,network_type:,network_link:,root_passwd: -- "${@}")
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
echo "network link interface does not exist"
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
if [ -z "$rootfs_path" ]; then
rootfs_path="${path}/rootfs"
fi
config_path="${default_path}/${name}"
echo "Interrupted, cleaning up"
exit 1
}
if [ ${?} -ne 0 ]; then
echo "failed to write configuration file"
exit 1
fi
if [ ${#additional_packages[@]} -gt 0 ]; then
base_packages+=(${result[@]})
fi
mkdir -p "${rootfs_path}"
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
if [ ${#enable_units[@]} -gt 0 ]; then
done
fi
if [ -n "${root_passwd}" ]; then
fi
echo "container config is ${config_path}/config"