lxc-archlinux.in revision f8bed24cee10615813567e65629dd6124f7d7ac3
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# template script for generating Arch Linux container for LXC
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# lxc: linux Container library
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Alexander Vladimirov <alexander.idkfa.vladimirov@gmail.com>
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# John Lane <lxc@jelmail.com>
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# This library is free software; you can redistribute it and/or
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# modify it under the terms of the GNU Lesser General Public
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# License as published by the Free Software Foundation; either
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# version 2.1 of the License, or (at your option) any later version.
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# This library is distributed in the hope that it will be useful,
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# but WITHOUT ANY WARRANTY; without even the implied warranty of
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Lesser General Public License for more details.
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# You should have received a copy of the GNU Lesser General Public
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# License along with this library; if not, write to the Free Software
99e4008cad9e959b683c6f48411fcf15a92be3b5Michel Normand# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
10fba81b9d0221b8e47aa1e0b43236413b7d28dfMichel Normand# Detect use under userns (unsupported)
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano echo "This template can't be used for unprivileged containers." 1>&2
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano echo "You may want to try the \"download\" template instead." 1>&2
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Make sure the usual locations are in PATH
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoshared_config="@LXCTEMPLATECONFIG@/archlinux.common.conf"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# by default, install 'base' except the kernel
b4578c5b380130a41a69b5b49c970157acaf1dbbDwight Engen [ "${pkg_blacklist#*$pkg}" = "$pkg_blacklist" ] && base_packages+=($pkg)
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# split comma-separated string into an array
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# ${1} - string to split
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# ${2} - separator (default is ",")
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# ${result} - result value on success
4d67c1301b9cf6587b0cc2e42f4e61ed6c29097cFerenc Wagner# Arch-specific preconfiguration for container
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano # on ArchLinux, read defaults from host systemd configuration
becc0400fc83516b05a8e76f2cdfbf452cc46c94Michel Normand cp -p /etc/locale.conf /etc/locale.gen "${rootfs_path}/etc/"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano echo "LANG=${default_locale}" > "${rootfs_path}/etc/locale.conf"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano sed -i 's@^#\(en_US\.UTF-8\)@\1@' "${rootfs_path}/etc/locale.gen"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano echo "${default_locale} ${default_locale##*.}" >> \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano cat > "${rootfs_path}/etc/systemd/network/eth0.network" << EOF
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanomkdir /run/lock
0bb4f8cf3b3b984dc150a11cb2e4d83ba4c49970Michel Normand# set default boot target
0bb4f8cf3b3b984dc150a11cb2e4d83ba4c49970Michel Normandln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
0bb4f8cf3b3b984dc150a11cb2e4d83ba4c49970Michel Normand# override getty@.service for container ttys
0bb4f8cf3b3b984dc150a11cb2e4d83ba4c49970Michel Normandsed -e 's/^ConditionPathExists=/# ConditionPathExists=/' \
0bb4f8cf3b3b984dc150a11cb2e4d83ba4c49970Michel Normand -e 's/After=dev-%i.device/After=/' \
0bb4f8cf3b3b984dc150a11cb2e4d83ba4c49970Michel Normand < /lib/systemd/system/getty\@.service \
0bb4f8cf3b3b984dc150a11cb2e4d83ba4c49970Michel Normand > /etc/systemd/system/getty\@.service
0bb4f8cf3b3b984dc150a11cb2e4d83ba4c49970Michel Normand# fix systemd-sysctl service
4d67c1301b9cf6587b0cc2e42f4e61ed6c29097cFerenc Wagnersed -e 's/^ConditionPathIsReadWrite=\/proc\/sys\/$/ConditionPathIsReadWrite=\/proc\/sys\/net\//' \
e2b4064f94f47246e5e2e6359b91b57cab0a0652Serge Hallyn -e 's/^ExecStart=\/usr\/lib\/systemd\/systemd-sysctl$/ExecStart=\/usr\/lib\/systemd\/systemd-sysctl --prefix net/' \
4d67c1301b9cf6587b0cc2e42f4e61ed6c29097cFerenc Wagner -i /usr/lib/systemd/system/systemd-sysctl.service
4d67c1301b9cf6587b0cc2e42f4e61ed6c29097cFerenc Wagner# initialize pacman keyring
0bb4f8cf3b3b984dc150a11cb2e4d83ba4c49970Michel Normandpacman-key --init
0bb4f8cf3b3b984dc150a11cb2e4d83ba4c49970Michel Normandpacman-key --populate archlinux
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn# enable networkd
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallynsystemctl enable systemd-networkd
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallynsystemctl enable systemd-resolved
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallynln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn local nttys=$(cat "${config_path}/config" ${shared_config} ${common_config} | grep "^lxc.tty" | head -n1 | cut -d= -f2 | tr -d "[:blank:]")
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn local devttydir=$(cat "${config_path}/config" ${shared_config} ${common_config} | grep "^lxc.devttydir" | head -n1 | cut -d= -f2 | tr -d "[:blank:]")
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn # bind getty instances to /dev/<devttydir>/tty* if lxc.devttydir is set
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn [ -n "${devttydir}" ] && devtty="${devttydir}-"
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn if [ ${nttys:-0} -gt 1 ]; then
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn ( cd "${rootfs_path}/etc/systemd/system/getty.target.wants"
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn for i in $(seq 1 $nttys); do ln -sf "../getty@.service" "getty@${devtty}tty${i}.service"; done )
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn # update securetty to allow console login if devttydir is set
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn echo "${devttydir}/tty${i}" >> "${rootfs_path}/etc/securetty"
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn [ -n "${devttydir}" ] && echo "${devttydir}/console" >> "${rootfs_path}/etc/securetty"
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn # Arch default configuration allows only tty1-6 for login
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn [ ${nttys:-0} -gt 6 ] && echo \
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn "You may want to modify container's /etc/securetty \
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn file to allow root logins on tty7 and higher"
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn# write container configuration files
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn grep -q "^lxc.rootfs" "${config}" 2>/dev/null \
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn || echo "lxc.rootfs = ${rootfs_path}" >> "${config}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano && echo "lxc.include = ${shared_config}" >> "${config}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if [ $? -ne 0 ]; then
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# install packages within container chroot
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano [ "${arch}" != "$(uname -m)" ] && different_arch=true
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano sed -e "s:Architecture =.*:Architecture = ${arch}:g" \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano -e "s:/etc/pacman.d/mirrorlist:${container_mirrorlist}:g" \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano /etc/pacman.d/mirrorlist > "${container_mirrorlist}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if ! pacstrap -dcGC "${pacman_config}" "${rootfs_path}" \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano sed -i -e "s:Architecture =.*:Architecture = ${arch}:g" \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano cp "${container_mirrorlist}" "${rootfs_path}"/etc/pacman.d/mirrorlist
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano rm "${container_pacman_config}" "${container_mirrorlist}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano [ -d "${rootfs_path}/lib/modules" ] && ldconfig -r "${rootfs_path}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano ${1} -n|--name=<container_name> [-p|--path=<path>] [-a|--arch=<arch of the container>]
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano [-r|--root_password=<root password>] [-P|--packages=<pkg1,pkg2,...>]
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano [-e|--enable_units=unit1,unit2...] [-d|--disable_units=unit1,unit2...]
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano [-c|--config=<pacman config path>] [-h|--help]
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoMandatory args:
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano -n,--name container name, used to as an identifier for that container from now on
options=$(getopt -o hp:P:e:d:n:c:a:r: -l help,rootfs:,path:,packages:,enable_units:,disable_units:,name:,config:,arch:,root_password: -- "${@}")
echo "'pacman' command is missing, refer to wiki.archlinux.org for information about installing pacman"
if [ -z "$rootfs_path" ]; then
"${rootfs_path}/etc/systemd/system/multi-user.target.wants/"
cat << EOF