]> git.proxmox.com Git - mirror_lxc.git/blame - templates/lxc-local.in
utils: add errno logs for exception case
[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
25LXC_HOOK_DIR="@LXCHOOKDIR@"
26LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
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
36export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
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
54 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && { echo userns-root; return; }
55 echo yes
56}
57
58usage() {
59 cat <<EOF
60LXC container image downloader
61
62Special arguments:
63[ -h | --help ]: Print this help message and exit.
64[ -l | --list ]: List all available images and exit.
65[ -m | --metadata ]: Path to the metadata for the image.
66[ -f | --fstree ]: Path to the filesystem tree for the image.
67
68LXC internal arguments (do not pass manually!):
69[ --name <name> ]: The container name
70[ --path <path> ]: The path to the container
71[ --rootfs <rootfs> ]: The path to the container's rootfs
ea8fa04f
CB
72[ --mapped-uid <map> ]: A uid map (user namespaces)
73[ --mapped-gid <map> ]: A gid map (user namespaces)
35444f38
CB
74EOF
75 return 0
76}
77
78if ! options=$(getopt -o hm:f: -l help,metadata:,fstree:,name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@"); then
79 usage
80 exit 1
81fi
82eval set -- "$options"
83
84while :; do
85 case "$1" in
86 -h|--help) usage && exit 1;;
87 --name) LXC_NAME="$2"; shift 2;;
88 --path) LXC_PATH="$2"; shift 2;;
89 --rootfs) LXC_ROOTFS="$2"; shift 2;;
90 -m|--metadata) LXC_CONFIG="$2"; shift 2;;
91 -f|--fstree) LXC_FSTREE="$2"; shift 2;;
ea8fa04f
CB
92 --mapped-uid) LXC_MAPPED_UID="$2"; shift 2;;
93 --mapped-gid) LXC_MAPPED_GID="$2"; shift 2;;
35444f38
CB
94 *) break;;
95 esac
96done
97
98# Check for required binaries
99for bin in tar xz; do
100 if ! command -V "${bin}" >/dev/null 2>&1; then
101 echo "ERROR: Missing required tool: ${bin}" 1>&2
102 exit 1
103 fi
104done
105
106cleanup() {
107 if [ -d "${LOCAL_TEMP}" ]; then
108 rm -Rf "${LOCAL_TEMP}"
109 fi
110}
111
112# Trap all exit signals
113trap cleanup EXIT HUP INT TERM
114
115USERNS="$(in_userns)"
116
117if [ "${USERNS}" != "no" ]; then
118 if [ "${USERNS}" = "yes" ]; then
119 if [ -z "${LXC_MAPPED_UID}" ] || [ "${LXC_MAPPED_UID}" = "-1" ]; then
a488502d 120 echo "ERROR: In a user namespace without a map" 1>&2
35444f38
CB
121 exit 1
122 fi
123 MODE="user"
124 else
125 MODE="user"
126 fi
127fi
128
129relevant_file() {
130 FILE_PATH="${LOCAL_TEMP}/$1"
131
132 if [ -e "${FILE_PATH}-${MODE}" ]; then
133 FILE_PATH="${FILE_PATH}-${MODE}"
134 fi
135
136 if [ -e "${FILE_PATH}.${COMPAT_LEVEL}" ]; then
137 FILE_PATH="${FILE_PATH}.${COMPAT_LEVEL}"
138 fi
139
140 echo "${FILE_PATH}"
141}
142
143
144# Unpack the rootfs
145echo "Unpacking the rootfs"
146
147# Create temporary directory to
148if ! command -V mktemp >/dev/null 2>&1; then
149 LOCAL_TEMP=/tmp/lxc-local.$$
150 mkdir -p "${LOCAL_TEMP}"
151else
152 LOCAL_TEMP=$(mktemp -d)
153fi
154
155# Unpack file that contains meta.tar.xz
156if ! tar Jxf "${LXC_CONFIG}" -C "${LOCAL_TEMP}"; then
157 echo "ERROR: Invalid metadata file" 2>&1
158 exit 1
159fi
160
161EXCLUDES=""
162excludelist=$(relevant_file excludes)
163if [ -f "${excludelist}" ]; then
164 while read -r line; do
165 EXCLUDES="${EXCLUDES} --exclude=${line}"
166 done < "${excludelist}"
167fi
168
169# Do not surround ${EXCLUDES} by quotes. This does not work. The solution could
170# use array but this is not POSIX compliant. The only POSIX compliant solution
171# is to use a function wrapper, but the latter can't be used here as the args
172# are dynamic. We thus need to ignore the warning brought by shellcheck.
173# shellcheck disable=SC2086
174tar --anchored ${EXCLUDES} --numeric-owner -xpJf "${LXC_FSTREE}" -C "${LXC_ROOTFS}"
175
176mkdir -p "${LXC_ROOTFS}/dev/pts/"
177
178# Setup the configuration
179# Setup the configuration
180configfile="$(relevant_file config)"
181if [ ! -e "${configfile}" ]; then
182 echo "ERROR: meta tarball is missing the configuration file" 1>&2
183 exit 1
184fi
185
186## Extract all the network config entries
187sed -i -e "/lxc.net.0/{w ${LXC_PATH}/config-network" -e "d}" "${LXC_PATH}/config"
188
189## Extract any other config entry
190sed -i -e "/lxc./{w ${LXC_PATH}/config-auto" -e "d}" "${LXC_PATH}/config"
191
192## Append the defaults
193{
194 echo ""
195 echo "# Distribution configuration"
196 cat "$configfile"
197} >> "${LXC_PATH}/config"
198
199## Add the container-specific config
200{
201 echo ""
202 echo "# Container specific configuration"
203 if [ -e "${LXC_PATH}/config-auto" ]; then
204 cat "${LXC_PATH}/config-auto"
205 rm "${LXC_PATH}/config-auto"
206 fi
207} >> "${LXC_PATH}/config"
208
209fstab="$(relevant_file fstab)"
210if [ -e "${fstab}" ]; then
211 echo "lxc.mount.fstab = ${LXC_PATH}/fstab" >> "${LXC_PATH}/config"
212fi
213echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_PATH}/config"
214
215## Re-add the previously removed network config
216if [ -e "${LXC_PATH}/config-network" ]; then
217 {
218 echo ""
219 echo "# Network configuration"
220 cat "${LXC_PATH}/config-network"
221 rm "${LXC_PATH}/config-network"
222 } >> "${LXC_PATH}/config"
223fi
224
225TEMPLATE_FILES="${LXC_PATH}/config"
226
227# Setup the fstab
228if [ -e "${fstab}" ]; then
229 cp "${fstab}" "${LXC_PATH}/fstab"
230 TEMPLATE_FILES="${TEMPLATE_FILES};${LXC_PATH}/fstab"
231fi
232
233# Look for extra templates
234if [ -e "$(relevant_file templates)" ]; then
235 while read -r line; do
236 fullpath="${LXC_ROOTFS}/${line}"
237 [ ! -e "${fullpath}" ] && continue
238 TEMPLATE_FILES="${TEMPLATE_FILES};${fullpath}"
239 done < "$(relevant_file templates)"
240fi
241
242
243# Replace variables in all templates
244OLD_IFS=${IFS}
245IFS=";"
246for file in ${TEMPLATE_FILES}; do
247 [ ! -f "${file}" ] && continue
248
249 sed -i "s#LXC_NAME#${LXC_NAME}#g" "${file}"
250 sed -i "s#LXC_PATH#${LXC_PATH}#g" "${file}"
251 sed -i "s#LXC_ROOTFS#${LXC_ROOTFS}#g" "${file}"
252 sed -i "s#LXC_TEMPLATE_CONFIG#${LXC_TEMPLATE_CONFIG}#g" "${file}"
253 sed -i "s#LXC_HOOK_DIR#${LXC_HOOK_DIR}#g" "${file}"
254done
255IFS=${OLD_IFS}
256
257# prevent mingetty from calling vhangup(2) since it fails with userns on CentOS / Oracle
258if [ -f "${LXC_ROOTFS}/etc/init/tty.conf" ]; then
259 sed -i 's|mingetty|mingetty --nohangup|' "${LXC_ROOTFS}/etc/init/tty.conf"
260fi
261
262
263if [ -e "$(relevant_file create-message)" ]; then
264 echo ""
265 echo "---"
266 cat "$(relevant_file create-message)"
267fi
268
269exit 0