]> git.proxmox.com Git - mirror_lxc.git/blame - templates/lxc-local.in
github: Update for main branch
[mirror_lxc.git] / templates / lxc-local.in
CommitLineData
35444f38
CB
1#!/bin/sh
2
3# Client script for LXC container images.
4#
5# Copyright © 2018 Stéphane Graber <stgraber@ubuntu.com>
6# Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>
7#
8# This library is free software; you can redistribute it and/or
9# modify it under the terms of the GNU Lesser General Public
10# License as published by the Free Software Foundation; either
11# version 2.1 of the License, or (at your option) any later version.
12
13# This library is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16# Lesser General Public License for more details.
17
18# You should have received a copy of the GNU Lesser General Public
19# License along with this library; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21# USA
22
23set -eu
24
3cc0ec84
CB
25LXC_HOOK_DIR=@LXCHOOKDIR@
26LXC_TEMPLATE_CONFIG=@LXCTEMPLATECONFIG@
35444f38
CB
27
28LXC_NAME=
29LXC_PATH=
30LXC_ROOTFS=
31LXC_CONFIG=
32MODE="system"
b80f86f2 33COMPAT_LEVEL=5
35444f38
CB
34
35# Make sure the usual locations are in PATH
655d10ed 36export PATH="$PATH:/usr/sbin:/usr/bin:/sbin:/bin"
35444f38
CB
37
38in_userns() {
39 [ -e /proc/self/uid_map ] || { echo no; return; }
40
41 while read -r line; do
42 fields="$(echo "$line" | awk '{ print $1 " " $2 " " $3 }')"
43 if [ "${fields}" = "0 0 4294967295" ]; then
44 echo no;
45 return;
46 fi
47
48 if echo "${fields}" | grep -q " 0 1$"; then
49 echo userns-root;
50 return;
51 fi
52 done < /proc/self/uid_map
53
16a312e1
LP
54 if [ -e /proc/1/uid_map ]; then
55 if [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ]; then
56 echo userns-root
57 return
58 fi
59 fi
60 echo yes
35444f38
CB
61}
62
63usage() {
64 cat <<EOF
65LXC container image downloader
66
67Special arguments:
68[ -h | --help ]: Print this help message and exit.
35444f38
CB
69[ -m | --metadata ]: Path to the metadata for the image.
70[ -f | --fstree ]: Path to the filesystem tree for the image.
71
72LXC internal arguments (do not pass manually!):
73[ --name <name> ]: The container name
74[ --path <path> ]: The path to the container
75[ --rootfs <rootfs> ]: The path to the container's rootfs
ea8fa04f
CB
76[ --mapped-uid <map> ]: A uid map (user namespaces)
77[ --mapped-gid <map> ]: A gid map (user namespaces)
35444f38
CB
78EOF
79 return 0
80}
81
82if ! options=$(getopt -o hm:f: -l help,metadata:,fstree:,name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@"); then
83 usage
84 exit 1
85fi
86eval set -- "$options"
87
88while :; do
89 case "$1" in
90 -h|--help) usage && exit 1;;
91 --name) LXC_NAME="$2"; shift 2;;
92 --path) LXC_PATH="$2"; shift 2;;
93 --rootfs) LXC_ROOTFS="$2"; shift 2;;
94 -m|--metadata) LXC_CONFIG="$2"; shift 2;;
95 -f|--fstree) LXC_FSTREE="$2"; shift 2;;
ea8fa04f
CB
96 --mapped-uid) LXC_MAPPED_UID="$2"; shift 2;;
97 --mapped-gid) LXC_MAPPED_GID="$2"; shift 2;;
35444f38
CB
98 *) break;;
99 esac
100done
101
102# Check for required binaries
103for bin in tar xz; do
104 if ! command -V "${bin}" >/dev/null 2>&1; then
105 echo "ERROR: Missing required tool: ${bin}" 1>&2
106 exit 1
107 fi
108done
109
110cleanup() {
111 if [ -d "${LOCAL_TEMP}" ]; then
112 rm -Rf "${LOCAL_TEMP}"
113 fi
114}
115
116# Trap all exit signals
117trap cleanup EXIT HUP INT TERM
118
119USERNS="$(in_userns)"
120
121if [ "${USERNS}" != "no" ]; then
122 if [ "${USERNS}" = "yes" ]; then
123 if [ -z "${LXC_MAPPED_UID}" ] || [ "${LXC_MAPPED_UID}" = "-1" ]; then
a488502d 124 echo "ERROR: In a user namespace without a map" 1>&2
35444f38
CB
125 exit 1
126 fi
127 MODE="user"
128 else
129 MODE="user"
130 fi
131fi
132
133relevant_file() {
134 FILE_PATH="${LOCAL_TEMP}/$1"
135
136 if [ -e "${FILE_PATH}-${MODE}" ]; then
137 FILE_PATH="${FILE_PATH}-${MODE}"
138 fi
139
140 if [ -e "${FILE_PATH}.${COMPAT_LEVEL}" ]; then
141 FILE_PATH="${FILE_PATH}.${COMPAT_LEVEL}"
142 fi
143
144 echo "${FILE_PATH}"
145}
146
147
148# Unpack the rootfs
149echo "Unpacking the rootfs"
150
151# Create temporary directory to
152if ! command -V mktemp >/dev/null 2>&1; then
153 LOCAL_TEMP=/tmp/lxc-local.$$
154 mkdir -p "${LOCAL_TEMP}"
155else
156 LOCAL_TEMP=$(mktemp -d)
157fi
158
159# Unpack file that contains meta.tar.xz
160if ! tar Jxf "${LXC_CONFIG}" -C "${LOCAL_TEMP}"; then
161 echo "ERROR: Invalid metadata file" 2>&1
162 exit 1
163fi
164
165EXCLUDES=""
166excludelist=$(relevant_file excludes)
167if [ -f "${excludelist}" ]; then
168 while read -r line; do
169 EXCLUDES="${EXCLUDES} --exclude=${line}"
170 done < "${excludelist}"
171fi
172
173# Do not surround ${EXCLUDES} by quotes. This does not work. The solution could
174# use array but this is not POSIX compliant. The only POSIX compliant solution
175# is to use a function wrapper, but the latter can't be used here as the args
176# are dynamic. We thus need to ignore the warning brought by shellcheck.
177# shellcheck disable=SC2086
178tar --anchored ${EXCLUDES} --numeric-owner -xpJf "${LXC_FSTREE}" -C "${LXC_ROOTFS}"
179
180mkdir -p "${LXC_ROOTFS}/dev/pts/"
181
182# Setup the configuration
183# Setup the configuration
184configfile="$(relevant_file config)"
185if [ ! -e "${configfile}" ]; then
186 echo "ERROR: meta tarball is missing the configuration file" 1>&2
187 exit 1
188fi
189
190## Extract all the network config entries
191sed -i -e "/lxc.net.0/{w ${LXC_PATH}/config-network" -e "d}" "${LXC_PATH}/config"
192
193## Extract any other config entry
194sed -i -e "/lxc./{w ${LXC_PATH}/config-auto" -e "d}" "${LXC_PATH}/config"
195
196## Append the defaults
197{
198 echo ""
199 echo "# Distribution configuration"
200 cat "$configfile"
201} >> "${LXC_PATH}/config"
202
203## Add the container-specific config
204{
205 echo ""
206 echo "# Container specific configuration"
207 if [ -e "${LXC_PATH}/config-auto" ]; then
208 cat "${LXC_PATH}/config-auto"
209 rm "${LXC_PATH}/config-auto"
210 fi
211} >> "${LXC_PATH}/config"
212
213fstab="$(relevant_file fstab)"
214if [ -e "${fstab}" ]; then
215 echo "lxc.mount.fstab = ${LXC_PATH}/fstab" >> "${LXC_PATH}/config"
216fi
217echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_PATH}/config"
218
219## Re-add the previously removed network config
220if [ -e "${LXC_PATH}/config-network" ]; then
221 {
222 echo ""
223 echo "# Network configuration"
224 cat "${LXC_PATH}/config-network"
225 rm "${LXC_PATH}/config-network"
226 } >> "${LXC_PATH}/config"
227fi
228
229TEMPLATE_FILES="${LXC_PATH}/config"
230
231# Setup the fstab
232if [ -e "${fstab}" ]; then
233 cp "${fstab}" "${LXC_PATH}/fstab"
234 TEMPLATE_FILES="${TEMPLATE_FILES};${LXC_PATH}/fstab"
235fi
236
237# Look for extra templates
238if [ -e "$(relevant_file templates)" ]; then
239 while read -r line; do
240 fullpath="${LXC_ROOTFS}/${line}"
241 [ ! -e "${fullpath}" ] && continue
242 TEMPLATE_FILES="${TEMPLATE_FILES};${fullpath}"
243 done < "$(relevant_file templates)"
244fi
245
246
247# Replace variables in all templates
248OLD_IFS=${IFS}
249IFS=";"
250for file in ${TEMPLATE_FILES}; do
251 [ ! -f "${file}" ] && continue
252
253 sed -i "s#LXC_NAME#${LXC_NAME}#g" "${file}"
254 sed -i "s#LXC_PATH#${LXC_PATH}#g" "${file}"
255 sed -i "s#LXC_ROOTFS#${LXC_ROOTFS}#g" "${file}"
256 sed -i "s#LXC_TEMPLATE_CONFIG#${LXC_TEMPLATE_CONFIG}#g" "${file}"
257 sed -i "s#LXC_HOOK_DIR#${LXC_HOOK_DIR}#g" "${file}"
258done
259IFS=${OLD_IFS}
260
261# prevent mingetty from calling vhangup(2) since it fails with userns on CentOS / Oracle
262if [ -f "${LXC_ROOTFS}/etc/init/tty.conf" ]; then
263 sed -i 's|mingetty|mingetty --nohangup|' "${LXC_ROOTFS}/etc/init/tty.conf"
264fi
265
266
267if [ -e "$(relevant_file create-message)" ]; then
268 echo ""
269 echo "---"
270 cat "$(relevant_file create-message)"
271fi
272
273exit 0