]>
Commit | Line | Data |
---|---|---|
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 | ||
23 | set -eu | |
24 | ||
25 | LXC_HOOK_DIR="@LXCHOOKDIR@" | |
26 | LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@" | |
27 | ||
28 | LXC_NAME= | |
29 | LXC_PATH= | |
30 | LXC_ROOTFS= | |
31 | LXC_CONFIG= | |
32 | MODE="system" | |
b80f86f2 | 33 | COMPAT_LEVEL=5 |
35444f38 CB |
34 | |
35 | # Make sure the usual locations are in PATH | |
36 | export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin | |
37 | ||
38 | in_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 | ||
58 | usage() { | |
59 | cat <<EOF | |
60 | LXC container image downloader | |
61 | ||
62 | Special 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 | ||
68 | LXC 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 | ||
73 | EOF | |
74 | return 0 | |
75 | } | |
76 | ||
77 | if ! options=$(getopt -o hm:f: -l help,metadata:,fstree:,name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@"); then | |
78 | usage | |
79 | exit 1 | |
80 | fi | |
81 | eval set -- "$options" | |
82 | ||
83 | while :; 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 | |
93 | done | |
94 | ||
95 | # Check for required binaries | |
96 | for 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 | |
101 | done | |
102 | ||
103 | cleanup() { | |
104 | if [ -d "${LOCAL_TEMP}" ]; then | |
105 | rm -Rf "${LOCAL_TEMP}" | |
106 | fi | |
107 | } | |
108 | ||
109 | # Trap all exit signals | |
110 | trap cleanup EXIT HUP INT TERM | |
111 | ||
112 | USERNS="$(in_userns)" | |
113 | ||
114 | if [ "${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 | |
124 | fi | |
125 | ||
126 | relevant_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 | |
142 | echo "Unpacking the rootfs" | |
143 | ||
144 | # Create temporary directory to | |
145 | if ! command -V mktemp >/dev/null 2>&1; then | |
146 | LOCAL_TEMP=/tmp/lxc-local.$$ | |
147 | mkdir -p "${LOCAL_TEMP}" | |
148 | else | |
149 | LOCAL_TEMP=$(mktemp -d) | |
150 | fi | |
151 | ||
152 | # Unpack file that contains meta.tar.xz | |
153 | if ! tar Jxf "${LXC_CONFIG}" -C "${LOCAL_TEMP}"; then | |
154 | echo "ERROR: Invalid metadata file" 2>&1 | |
155 | exit 1 | |
156 | fi | |
157 | ||
158 | EXCLUDES="" | |
159 | excludelist=$(relevant_file excludes) | |
160 | if [ -f "${excludelist}" ]; then | |
161 | while read -r line; do | |
162 | EXCLUDES="${EXCLUDES} --exclude=${line}" | |
163 | done < "${excludelist}" | |
164 | fi | |
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 | |
171 | tar --anchored ${EXCLUDES} --numeric-owner -xpJf "${LXC_FSTREE}" -C "${LXC_ROOTFS}" | |
172 | ||
173 | mkdir -p "${LXC_ROOTFS}/dev/pts/" | |
174 | ||
175 | # Setup the configuration | |
176 | # Setup the configuration | |
177 | configfile="$(relevant_file config)" | |
178 | if [ ! -e "${configfile}" ]; then | |
179 | echo "ERROR: meta tarball is missing the configuration file" 1>&2 | |
180 | exit 1 | |
181 | fi | |
182 | ||
183 | ## Extract all the network config entries | |
184 | sed -i -e "/lxc.net.0/{w ${LXC_PATH}/config-network" -e "d}" "${LXC_PATH}/config" | |
185 | ||
186 | ## Extract any other config entry | |
187 | sed -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 | ||
206 | fstab="$(relevant_file fstab)" | |
207 | if [ -e "${fstab}" ]; then | |
208 | echo "lxc.mount.fstab = ${LXC_PATH}/fstab" >> "${LXC_PATH}/config" | |
209 | fi | |
210 | echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_PATH}/config" | |
211 | ||
212 | ## Re-add the previously removed network config | |
213 | if [ -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" | |
220 | fi | |
221 | ||
222 | TEMPLATE_FILES="${LXC_PATH}/config" | |
223 | ||
224 | # Setup the fstab | |
225 | if [ -e "${fstab}" ]; then | |
226 | cp "${fstab}" "${LXC_PATH}/fstab" | |
227 | TEMPLATE_FILES="${TEMPLATE_FILES};${LXC_PATH}/fstab" | |
228 | fi | |
229 | ||
230 | # Look for extra templates | |
231 | if [ -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)" | |
237 | fi | |
238 | ||
239 | ||
240 | # Replace variables in all templates | |
241 | OLD_IFS=${IFS} | |
242 | IFS=";" | |
243 | for 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}" | |
251 | done | |
252 | IFS=${OLD_IFS} | |
253 | ||
254 | # prevent mingetty from calling vhangup(2) since it fails with userns on CentOS / Oracle | |
255 | if [ -f "${LXC_ROOTFS}/etc/init/tty.conf" ]; then | |
256 | sed -i 's|mingetty|mingetty --nohangup|' "${LXC_ROOTFS}/etc/init/tty.conf" | |
257 | fi | |
258 | ||
259 | ||
260 | if [ -e "$(relevant_file create-message)" ]; then | |
261 | echo "" | |
262 | echo "---" | |
263 | cat "$(relevant_file create-message)" | |
264 | fi | |
265 | ||
266 | exit 0 |