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