Debian Codegraph @

templates/lxc-archlinux.in

raw ◀ ▶
1 Debian patch touch this file
1 #!/bin/bash
2
3 #
4 # template script for generating Arch Linux container for LXC
5 #
6
7 #
8 # lxc: linux Container library
9
10 # Authors:
11 # Alexander Vladimirov <[email protected]>
12 # John Lane <[email protected]>
13
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.
18
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.
23
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
27
28 # Detect use under userns (unsupported)
29 for arg in "$@"; do
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
34 exit 1
35 fi
36 done
37
38 # Make sure the usual locations are in PATH
39 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
40
41 # defaults
42 arch=$(uname -m)
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"
48
49 # by default, install 'base' except the kernel
50 pkg_blacklist="linux"
51 base_packages=("systemd-sysvcompat")
52 for pkg in $(pacman -Sqg base); do
53 [ "${pkg_blacklist#*$pkg}" = "$pkg_blacklist" ] && base_packages+=($pkg)
54 done
55 declare -a additional_packages
56
57 # split comma-separated string into an array
58 # ${1} - string to split
59 # ${2} - separator (default is ",")
60 # ${result} - result value on success
61 split_string() {
62 local ifs=${IFS}
63 IFS="${2:-,}"
64 read -a result < <(echo "${1}")
65 IFS=${ifs}
66 return 0
67 }
68
69 [ -f /etc/arch-release ] && is_arch=true
70
71 # Arch-specific preconfiguration for container
72 configure_arch() {
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/"
76 else
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"
83 fi
84 fi
85 fi
86
87 # hostname and nameservers
88 echo "${name}" > "${rootfs_path}/etc/hostname"
89
90 # network configuration
91 cat > "${rootfs_path}/etc/systemd/network/eth0.network" << EOF
92 [Match]
93 Name=eth0
94
95 [Network]
96 DHCP=ipv4
97 EOF
98
99 # chroot and configure system
100 arch-chroot "${rootfs_path}" /bin/bash -s << EOF
101 mkdir /run/lock
102 locale-gen
103 # set default boot target
104 ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
105 # override [email protected] 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
115 pacman-key --init
116 pacman-key --populate archlinux
117
118 # enable networkd
119 systemctl enable systemd-networkd
120 systemctl enable systemd-resolved
121 ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
122 EOF
123 # enable getty on active ttys
124 local nttys=$(cat "${config_path}/config" ${shared_config} ${common_config} | grep "^lxc.tty.max" | head -n1 | cut -d= -f2 | tr -d "[:blank:]")
125 local devttydir=$(cat "${config_path}/config" ${shared_config} ${common_config} | grep "^lxc.tty.dir" | head -n1 | cut -d= -f2 | tr -d "[:blank:]")
126 local devtty=""
127 # bind getty instances to /dev/<devttydir>/tty* if lxc.tty.dir 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 "../[email protected]" "getty@${devtty}tty${i}.service"; done )
132 fi
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"
137 done
138 fi
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"
144 return 0
145 }
146
147 # write container configuration files
148 copy_configuration() {
149 mkdir -p "${config_path}"
150 local config="${config_path}/config"
151 echo "lxc.uts.name = ${name}" >> "${config}"
152 grep -q "^lxc.arch" "${config}" 2>/dev/null \
153 || echo "lxc.arch = ${arch}" >> "${config}"
154 grep -q "^lxc.rootfs.path" "${config}" 2>/dev/null \
155 || echo "lxc.rootfs.path = ${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"
160 return 1
161 fi
162 return 0
163 }
164
165 # install packages within container chroot
166 install_arch() {
167 [ "${arch}" != "$(uname -m)" ] && different_arch=true
168
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}"
177
178 pacman_config="${container_pacman_config}"
179 fi
180
181 if ! pacstrap -dcGC "${pacman_config}" "${rootfs_path}" \
182 ${base_packages[@]}; then
183 echo "Failed to install container packages"
184 return 1
185 fi
186
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}"
192 fi
193
194 [ -d "${rootfs_path}/lib/modules" ] && ldconfig -r "${rootfs_path}"
195 return 0
196 }
197
198 usage() {
199 cat <<EOF
200 usage:
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]
205
206 Mandatory args:
207 -n,--name container name, used to as an identifier for that container from now on
208 Optional args:
•209 -p,--path path the directory where the container directory will be created (${default_path})
210 --rootfs path for actual container rootfs, (${default_path}/{container_name}/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
218 EOF
219 return 0
220 }
221
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})
225 exit 1
226 fi
227 eval set -- "${options}"
228
229 while true
230 do
231 case "${1}" in
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 ;;
243 *) break ;;
244 esac
245 done
246
247 if [ -z "${name}" ]; then
248 echo "missing required 'name' parameter"
249 exit 1
250 fi
251
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"
255 exit 1
256 fi
257
258 if [ -z "${path}" ]; then
•259 path="${default_path}"
260 fi
261
•262 path="${path}/${name}"
263
264 if [ "${EUID}" != "0" ]; then
265 echo "This script should be run as 'root'"
266 exit 1
267 fi
268
269 if [ -z "$rootfs_path" ]; then
270 rootfs_path="${path}/rootfs"
271 fi
272 config_path="${path}"
273
274 revert() {
275 echo "Interrupted, cleaning up"
276 lxc-destroy -n "${name}"
277 rm -rf "${path}/${name}"
278 rm -rf "${default_path}/${name}"
279 exit 1
280 }
281
282 trap revert SIGHUP SIGINT SIGTERM
283
284 copy_configuration
285 if [ ${?} -ne 0 ]; then
286 echo "failed to write configuration file"
287 rm -rf "${config_path}"
288 exit 1
289 fi
290
291 if [ ${#additional_packages[@]} -gt 0 ]; then
292 split_string ${additional_packages}
293 base_packages+=(${result[@]})
294 fi
295
296 mkdir -p "${rootfs_path}"
297 install_arch
298 if [ ${?} -ne 0 ]; then
299 echo "failed to install Arch Linux"
300 rm -rf "${config_path}" "${path}"
301 exit 1
302 fi
303
304 configure_arch
305 if [ ${?} -ne 0 ]; then
306 echo "failed to configure Arch Linux for a container"
307 rm -rf "${config_path}" "${path}"
308 exit 1
309 fi
310
311 if [ ${#enable_units[@]} -gt 0 ]; then
312 split_string ${enable_units}
313 for unit in ${result[@]}; do
314 [ "${unit##*.}" = "service" ] || unit="${unit}.service"
315 ln -s "/usr/lib/systemd/system/${unit}" \
316 "${rootfs_path}/etc/systemd/system/multi-user.target.wants/"
317 done
318 fi
319
320 if [ ${#disable_units[@]} -gt 0 ]; then
321 split_string ${disable_units}
322 for unit in ${result[@]}; do
323 [ "${unit##*.}" = "service" ] || unit="${unit}.service"
324 ln -s /dev/null "${rootfs_path}/etc/systemd/system/${unit}"
325 done
326 fi
327
328 if [ -n "${root_passwd}" ]; then
329 echo "root:${root_passwd}" | chroot "${rootfs_path}" chpasswd
330 fi
331
332 cat << EOF
333 Arch Linux container ${name} is successfully created! The configuration is
334 stored in ${config_path}/config. Please refer to https://wiki.archlinux.org for
335 information about configuring Arch Linux.
336 EOF