lxc-archlinux.in revision f8bed24cee10615813567e65629dd6124f7d7ac3
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano#!/bin/bash
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano#
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# template script for generating Arch Linux container for LXC
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano#
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano#
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# lxc: linux Container library
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Authors:
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Alexander Vladimirov <alexander.idkfa.vladimirov@gmail.com>
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# John Lane <lxc@jelmail.com>
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
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
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
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
99e4008cad9e959b683c6f48411fcf15a92be3b5Michel Normand
10fba81b9d0221b8e47aa1e0b43236413b7d28dfMichel Normand# Detect use under userns (unsupported)
99e4008cad9e959b683c6f48411fcf15a92be3b5Michel Normandfor arg in "$@"; do
99e4008cad9e959b683c6f48411fcf15a92be3b5Michel Normand [ "$arg" = "--" ] && break
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 exit 1
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano fi
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanodone
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# Make sure the usual locations are in PATH
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoexport PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# defaults
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoarch=$(uname -m)
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanodefault_path="@LXCPATH@"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanodefault_locale="en-US.UTF-8"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanodefault_timezone="UTC"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanopacman_config="/etc/pacman.conf"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanocommon_config="@LXCTEMPLATECONFIG@/common.conf"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoshared_config="@LXCTEMPLATECONFIG@/archlinux.common.conf"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# by default, install 'base' except the kernel
b4578c5b380130a41a69b5b49c970157acaf1dbbDwight Engenpkg_blacklist="linux"
b4578c5b380130a41a69b5b49c970157acaf1dbbDwight Engenbase_packages=()
b4578c5b380130a41a69b5b49c970157acaf1dbbDwight Engenfor pkg in $(pacman -Sqg base); do
b4578c5b380130a41a69b5b49c970157acaf1dbbDwight Engen [ "${pkg_blacklist#*$pkg}" = "$pkg_blacklist" ] && base_packages+=($pkg)
b4578c5b380130a41a69b5b49c970157acaf1dbbDwight Engendone
b4578c5b380130a41a69b5b49c970157acaf1dbbDwight Engendeclare -a additional_packages
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# split comma-separated string into an array
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# ${1} - string to split
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# ${2} - separator (default is ",")
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# ${result} - result value on success
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanosplit_string() {
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano local ifs=${IFS}
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano IFS="${2:-,}"
6a22713f648be8bd21297f57d9b631eb4c537ffeDaniel Lezcano read -a result < <(echo "${1}")
6a22713f648be8bd21297f57d9b631eb4c537ffeDaniel Lezcano IFS=${ifs}
6a22713f648be8bd21297f57d9b631eb4c537ffeDaniel Lezcano return 0
6a22713f648be8bd21297f57d9b631eb4c537ffeDaniel Lezcano}
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
becc0400fc83516b05a8e76f2cdfbf452cc46c94Michel Normand[ -f /etc/arch-release ] && is_arch=true
4d67c1301b9cf6587b0cc2e42f4e61ed6c29097cFerenc Wagner
4d67c1301b9cf6587b0cc2e42f4e61ed6c29097cFerenc Wagner# Arch-specific preconfiguration for container
becc0400fc83516b05a8e76f2cdfbf452cc46c94Michel Normandconfigure_arch() {
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano # on ArchLinux, read defaults from host systemd configuration
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if [ "${is_arch}" ]; then
becc0400fc83516b05a8e76f2cdfbf452cc46c94Michel Normand cp -p /etc/locale.conf /etc/locale.gen "${rootfs_path}/etc/"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano else
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano echo "LANG=${default_locale}" > "${rootfs_path}/etc/locale.conf"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if [ -e "${rootfs_path}/etc/locale.gen" ]; then
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano sed -i 's@^#\(en_US\.UTF-8\)@\1@' "${rootfs_path}/etc/locale.gen"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if [ ! "${default_locale}" = "en_US.UTF-8" ]; then
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano echo "${default_locale} ${default_locale##*.}" >> \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano "${rootfs_path}/etc/locale.gen"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano fi
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano fi
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano fi
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano # hostname and nameservers
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano echo "${name}" > "${rootfs_path}/etc/hostname"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano # network configuration
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano cat > "${rootfs_path}/etc/systemd/network/eth0.network" << EOF
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano[Match]
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoName=eth0
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano[Network]
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoDHCP=ipv4
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoEOF
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano # chroot and configure system
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano arch-chroot "${rootfs_path}" /bin/bash -s << EOF
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanomkdir /run/lock
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanolocale-gen
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
0bb4f8cf3b3b984dc150a11cb2e4d83ba4c49970Michel Normand
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 HallynEOF
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn # enable getty on active ttys
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 local devtty=""
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 fi
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn # update securetty to allow console login if devttydir is set
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn if [ -n "${devttydir}" ]; then
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn for i in $(seq 1 ${nttys:-1}); do
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn echo "${devttydir}/tty${i}" >> "${rootfs_path}/etc/securetty"
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn done
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn fi
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 return 0
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn}
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn# write container configuration files
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyncopy_configuration() {
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn mkdir -p "${config_path}"
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn local config="${config_path}/config"
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn echo "lxc.utsname = ${name}" >> "${config}"
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn grep -q "^lxc.arch" "${config}" 2>/dev/null \
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn || echo "lxc.arch = ${arch}" >> "${config}"
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn grep -q "^lxc.rootfs" "${config}" 2>/dev/null \
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn || echo "lxc.rootfs = ${rootfs_path}" >> "${config}"
1305dd24af039cefc54bc2f21b04b33b62bb26c8Serge Hallyn [ -e "${shared_config}" ] \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano && echo "lxc.include = ${shared_config}" >> "${config}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if [ $? -ne 0 ]; then
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano echo "Failed to configure container"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano return 1
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano fi
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano return 0
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano}
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano# install packages within container chroot
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoinstall_arch() {
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano [ "${arch}" != "$(uname -m)" ] && different_arch=true
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if [ "${different_arch}" = "true" ]; then
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano container_pacman_config=$(mktemp)
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano container_mirrorlist=$(mktemp)
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano sed -e "s:Architecture =.*:Architecture = ${arch}:g" \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano -e "s:/etc/pacman.d/mirrorlist:${container_mirrorlist}:g" \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano "${pacman_config}" > "${container_pacman_config}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano sed -e "s:\(x86_64\|\$arch\):${arch}:g" \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano /etc/pacman.d/mirrorlist > "${container_mirrorlist}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano pacman_config="${container_pacman_config}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano fi
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if ! pacstrap -dcGC "${pacman_config}" "${rootfs_path}" \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano ${base_packages[@]}; then
99e4008cad9e959b683c6f48411fcf15a92be3b5Michel Normand echo "Failed to install container packages"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano return 1
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano fi
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano if [ "${different_arch}" = "true" ]; then
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano sed -i -e "s:Architecture =.*:Architecture = ${arch}:g" \
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano "${rootfs_path}"/etc/pacman.conf
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano cp "${container_mirrorlist}" "${rootfs_path}"/etc/pacman.d/mirrorlist
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano rm "${container_pacman_config}" "${container_mirrorlist}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano fi
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano [ -d "${rootfs_path}/lib/modules" ] && ldconfig -r "${rootfs_path}"
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano return 0
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano}
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanousage() {
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano cat <<EOF
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanousage:
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]
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcanoMandatory args:
d823d5b966f49d975a09a8512d084389d6d7ffc7dlezcano -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 (${default_path})
--rootfs path for actual container rootfs, (${default_path}/rootfs)
-P,--packages preinstall additional packages, comma-separated list
-e,--enable_units enable systemd services, comma-separated list
-d,--disable_units disable systemd services, comma-separated list
-c,--config use specified pacman config when installing container packages
-a,--arch use specified architecture instead of host's architecture
-r,--root_password set container root password
-h,--help print this help
EOF
return 0
}
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: -- "${@}")
if [ ${?} -ne 0 ]; then
usage $(basename ${0})
exit 1
fi
eval set -- "${options}"
while true
do
case "${1}" in
-h|--help) usage ${0} && exit 0;;
-p|--path) path=${2}; shift 2;;
-n|--name) name=${2}; shift 2;;
--rootfs) rootfs_path=${2}; shift 2;;
-P|--packages) additional_packages=${2}; shift 2;;
-e|--enable_units) enable_units=${2}; shift 2;;
-d|--disable_units) disable_units=${2}; shift 2;;
-c|--config) pacman_config=${2}; shift 2;;
-a|--arch) arch=${2}; shift 2;;
-r|--root_password) root_passwd=${2}; shift 2;;
--) shift 1; break ;;
*) break ;;
esac
done
if [ -z "${name}" ]; then
echo "missing required 'name' parameter"
exit 1
fi
type pacman >/dev/null 2>&1
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="${path}"
revert() {
echo "Interrupted, cleaning up"
lxc-destroy -n "${name}"
rm -rf "${path}/${name}"
rm -rf "${default_path}/${name}"
exit 1
}
trap revert SIGHUP SIGINT SIGTERM
copy_configuration
if [ ${?} -ne 0 ]; then
echo "failed to write configuration file"
rm -rf "${config_path}"
exit 1
fi
if [ ${#additional_packages[@]} -gt 0 ]; then
split_string ${additional_packages}
base_packages+=(${result[@]})
fi
mkdir -p "${rootfs_path}"
install_arch
if [ ${?} -ne 0 ]; then
echo "failed to install Arch Linux"
rm -rf "${config_path}" "${path}"
exit 1
fi
configure_arch
if [ ${?} -ne 0 ]; then
echo "failed to configure Arch Linux for a container"
rm -rf "${config_path}" "${path}"
exit 1
fi
if [ ${#enable_units[@]} -gt 0 ]; then
split_string ${enable_units}
for unit in ${result[@]}; do
[ "${unit##*.}" = "service" ] || unit="${unit}.service"
ln -s "/usr/lib/systemd/system/${unit}" \
"${rootfs_path}/etc/systemd/system/multi-user.target.wants/"
done
fi
if [ ${#disable_units[@]} -gt 0 ]; then
split_string ${disable_units}
for unit in ${result[@]}; do
[ "${unit##*.}" = "service" ] || unit="${unit}.service"
ln -s /dev/null "${rootfs_path}/etc/systemd/system/${unit}"
done
fi
if [ -n "${root_passwd}" ]; then
echo "root:${root_passwd}" | chroot "${rootfs_path}" chpasswd
fi
cat << EOF
Arch Linux container ${name} is successfully created! The configuration is
stored in ${config_path}/config. Please refer to https://wiki.archlinux.org for
information about configuring Arch Linux.
EOF