4 # template script for generating Arch Linux container for LXC
8 # lxc: linux Container library
11 # Alexander Vladimirov <alexander.idkfa.vladimirov@gmail.com>
12 # John Lane <lxc@jelmail.com>
14 # This library is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU Lesser General Public
16 # License as published by the Free Software Foundation; either
17 # version 2.1 of the License, or (at your option) any later version.
19 # This library is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # Lesser General Public License for more details.
24 # You should have received a copy of the GNU Lesser General Public
25 # License along with this library; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 # Detect use under userns (unsupported)
30 [ "$arg" = "--" ] && break
31 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
32 echo "This template can't be used for unprivileged containers." 1>&2
33 echo "You may want to try the \"download\" template instead." 1>&2
38 # Make sure the usual locations are in PATH
39 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
43 default_path
="@LXCPATH@"
44 default_locale
="en-US.UTF-8"
45 pacman_config
="/etc/pacman.conf"
46 common_config
="@LXCTEMPLATECONFIG@/common.conf"
47 shared_config
="@LXCTEMPLATECONFIG@/archlinux.common.conf"
49 # by default, install 'base' except the kernel
52 for pkg
in $
(pacman
-Sqg base
); do
53 [ "${pkg_blacklist#*$pkg}" = "$pkg_blacklist" ] && base_packages
+=($pkg)
55 declare -a additional_packages
57 # split comma-separated string into an array
58 # ${1} - string to split
59 # ${2} - separator (default is ",")
60 # ${result} - result value on success
64 read -a result
< <(echo "${1}")
69 [ -f /etc
/arch-release
] && is_arch
=true
71 # Arch-specific preconfiguration for container
73 # on ArchLinux, read defaults from host systemd configuration
74 if [ "${is_arch}" ]; then
75 cp -p /etc
/locale.conf
/etc
/locale.gen
"${rootfs_path}/etc/"
77 echo "LANG=${default_locale}" > "${rootfs_path}/etc/locale.conf"
78 if [ -e "${rootfs_path}/etc/locale.gen" ]; then
79 sed -i 's@^#\(en_US\.UTF-8\)@\1@' "${rootfs_path}/etc/locale.gen"
80 if [ ! "${default_locale}" = "en_US.UTF-8" ]; then
81 echo "${default_locale} ${default_locale##*.}" >> \
82 "${rootfs_path}/etc/locale.gen"
87 # hostname and nameservers
88 echo "${name}" > "${rootfs_path}/etc/hostname"
90 # network configuration
91 cat > "${rootfs_path}/etc/systemd/network/eth0.network" << EOF
99 # chroot and configure system
100 arch-chroot
"${rootfs_path}" /bin
/bash
-s << EOF
103 # set default boot target
104 ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
105 # override getty@.service for container ttys
106 sed -e 's/^ConditionPathExists=/# ConditionPathExists=/' \
107 -e 's/After=dev-%i.device/After=/' \
108 < /lib/systemd/system/getty\@.service \
109 > /etc/systemd/system/getty\@.service
110 # fix systemd-sysctl service
111 sed -e 's/^ConditionPathIsReadWrite=\/proc\/sys\/$/ConditionPathIsReadWrite=\/proc\/sys\/net\//' \
112 -e 's/^ExecStart=\/usr\/lib\/systemd\/systemd-sysctl$/ExecStart=\/usr\/lib\/systemd\/systemd-sysctl --prefix net/' \
113 -i /usr/lib/systemd/system/systemd-sysctl.service
114 # initialize pacman keyring
116 pacman-key --populate archlinux
119 systemctl enable systemd-networkd
120 systemctl enable systemd-resolved
121 ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
123 # enable getty on active ttys
124 local nttys
=$
(cat "${config_path}/config" ${shared_config} ${common_config} | grep "^lxc.tty
" | head -n1 | cut -d= -f2 | tr -d "[:blank
:]")
125 local devttydir=$(cat "${config_path}/config" ${shared_config} ${common_config} |
grep "^lxc.devttydir" |
head -n1 | cut
-d= -f2 |
tr -d "[:blank:]")
127 # bind getty instances to /dev/<devttydir>/tty* if lxc.devttydir is set
128 [ -n "${devttydir}" ] && devtty
="${devttydir}-"
129 if [ ${nttys:-0} -gt 1 ]; then
130 ( cd "${rootfs_path}/etc/systemd/system/getty.target.wants"
131 for i
in $
(seq 1 $nttys); do ln -sf "../getty@.service" "getty@${devtty}tty${i}.service"; done )
133 # update securetty to allow console login if devttydir is set
134 if [ -n "${devttydir}" ]; then
135 for i
in $
(seq 1 ${nttys:-1}); do
136 echo "${devttydir}/tty${i}" >> "${rootfs_path}/etc/securetty"
139 [ -n "${devttydir}" ] && echo "${devttydir}/console" >> "${rootfs_path}/etc/securetty"
140 # Arch default configuration allows only tty1-6 for login
141 [ ${nttys:-0} -gt 6 ] && echo \
142 "You may want to modify container's /etc/securetty \
143 file to allow root logins on tty7 and higher"
147 # write container configuration files
148 copy_configuration
() {
149 mkdir
-p "${config_path}"
150 local config
="${config_path}/config"
151 echo "lxc.utsname = ${name}" >> "${config}"
152 grep -q "^lxc.arch" "${config}" 2>/dev
/null \
153 ||
echo "lxc.arch = ${arch}" >> "${config}"
154 grep -q "^lxc.rootfs" "${config}" 2>/dev
/null \
155 ||
echo "lxc.rootfs = ${rootfs_path}" >> "${config}"
156 [ -e "${shared_config}" ] \
157 && echo "lxc.include = ${shared_config}" >> "${config}"
158 if [ $?
-ne 0 ]; then
159 echo "Failed to configure container"
165 # install packages within container chroot
167 [ "${arch}" != "$(uname -m)" ] && different_arch
=true
169 if [ "${different_arch}" = "true" ]; then
170 container_pacman_config
=$
(mktemp
)
171 container_mirrorlist
=$
(mktemp
)
172 sed -e "s:Architecture =.*:Architecture = ${arch}:g" \
173 -e "s:/etc/pacman.d/mirrorlist:${container_mirrorlist}:g" \
174 "${pacman_config}" > "${container_pacman_config}"
175 sed -e "s:\(x86_64\|\$arch\):${arch}:g" \
176 /etc
/pacman.d
/mirrorlist
> "${container_mirrorlist}"
178 pacman_config
="${container_pacman_config}"
181 if ! pacstrap
-dcGC "${pacman_config}" "${rootfs_path}" \
182 ${base_packages[@]}; then
183 echo "Failed to install container packages"
187 if [ "${different_arch}" = "true" ]; then
188 sed -i -e "s:Architecture =.*:Architecture = ${arch}:g" \
189 "${rootfs_path}"/etc
/pacman.conf
190 cp "${container_mirrorlist}" "${rootfs_path}"/etc
/pacman.d
/mirrorlist
191 rm "${container_pacman_config}" "${container_mirrorlist}"
194 [ -d "${rootfs_path}/lib/modules" ] && ldconfig
-r "${rootfs_path}"
201 ${1} -n|--name=<container_name> [-p|--path=<path>] [-a|--arch=<arch of the container>]
202 [-r|--root_password=<root password>] [-P|--packages=<pkg1,pkg2,...>]
203 [-e|--enable_units=unit1,unit2...] [-d|--disable_units=unit1,unit2...]
204 [-c|--config=<pacman config path>] [-h|--help]
207 -n,--name container name, used to as an identifier for that container from now on
209 -p,--path path to where the container rootfs will be created (${default_path})
210 --rootfs path for actual container rootfs, (${default_path}/rootfs)
211 -P,--packages preinstall additional packages, comma-separated list
212 -e,--enable_units enable systemd services, comma-separated list
213 -d,--disable_units disable systemd services, comma-separated list
214 -c,--config use specified pacman config when installing container packages
215 -a,--arch use specified architecture instead of host's architecture
216 -r,--root_password set container root password
217 -h,--help print this help
222 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
: -- "${@}")
223 if [ ${?} -ne 0 ]; then
224 usage $
(basename ${0})
227 eval set -- "${options}"
232 -h|
--help) usage
${0} && exit 0;;
233 -p|
--path) path
=${2}; shift 2;;
234 -n|
--name) name
=${2}; shift 2;;
235 --rootfs) rootfs_path
=${2}; shift 2;;
236 -P|
--packages) additional_packages
=${2}; shift 2;;
237 -e|
--enable_units) enable_units
=${2}; shift 2;;
238 -d|
--disable_units) disable_units
=${2}; shift 2;;
239 -c|
--config) pacman_config
=${2}; shift 2;;
240 -a|
--arch) arch
=${2}; shift 2;;
241 -r|
--root_password) root_passwd
=${2}; shift 2;;
242 --) shift 1; break ;;
247 if [ -z "${name}" ]; then
248 echo "missing required 'name' parameter"
252 type pacman
>/dev
/null
2>&1
253 if [ ${?} -ne 0 ]; then
254 echo "'pacman' command is missing, refer to wiki.archlinux.org for information about installing pacman"
258 if [ -z "${path}" ]; then
259 path
="${default_path}/${name}"
262 if [ "${EUID}" != "0" ]; then
263 echo "This script should be run as 'root'"
267 if [ -z "$rootfs_path" ]; then
268 rootfs_path
="${path}/rootfs"
270 config_path
="${path}"
273 echo "Interrupted, cleaning up"
274 lxc-destroy
-n "${name}"
275 rm -rf "${path}/${name}"
276 rm -rf "${default_path}/${name}"
280 trap revert SIGHUP SIGINT SIGTERM
283 if [ ${?} -ne 0 ]; then
284 echo "failed to write configuration file"
285 rm -rf "${config_path}"
289 if [ ${#additional_packages[@]} -gt 0 ]; then
290 split_string
${additional_packages}
291 base_packages
+=(${result[@]})
294 mkdir
-p "${rootfs_path}"
296 if [ ${?} -ne 0 ]; then
297 echo "failed to install Arch Linux"
298 rm -rf "${config_path}" "${path}"
303 if [ ${?} -ne 0 ]; then
304 echo "failed to configure Arch Linux for a container"
305 rm -rf "${config_path}" "${path}"
309 if [ ${#enable_units[@]} -gt 0 ]; then
310 split_string
${enable_units}
311 for unit
in ${result[@]}; do
312 [ "${unit##*.}" = "service" ] || unit
="${unit}.service"
313 ln -s "/usr/lib/systemd/system/${unit}" \
314 "${rootfs_path}/etc/systemd/system/multi-user.target.wants/"
318 if [ ${#disable_units[@]} -gt 0 ]; then
319 split_string
${disable_units}
320 for unit
in ${result[@]}; do
321 [ "${unit##*.}" = "service" ] || unit
="${unit}.service"
322 ln -s /dev
/null
"${rootfs_path}/etc/systemd/system/${unit}"
326 if [ -n "${root_passwd}" ]; then
327 echo "root:${root_passwd}" | chroot
"${rootfs_path}" chpasswd
331 Arch Linux container ${name} is successfully created! The configuration is
332 stored in ${config_path}/config. Please refer to https://wiki.archlinux.org for
333 information about configuring Arch Linux.