]>
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 | |
ea8fa04f CB |
72 | [ --mapped-uid <map> ]: A uid map (user namespaces) |
73 | [ --mapped-gid <map> ]: A gid map (user namespaces) | |
35444f38 CB |
74 | EOF |
75 | return 0 | |
76 | } | |
77 | ||
78 | if ! options=$(getopt -o hm:f: -l help,metadata:,fstree:,name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@"); then | |
79 | usage | |
80 | exit 1 | |
81 | fi | |
82 | eval set -- "$options" | |
83 | ||
84 | while :; 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 | |
96 | done | |
97 | ||
98 | # Check for required binaries | |
99 | for 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 | |
104 | done | |
105 | ||
106 | cleanup() { | |
107 | if [ -d "${LOCAL_TEMP}" ]; then | |
108 | rm -Rf "${LOCAL_TEMP}" | |
109 | fi | |
110 | } | |
111 | ||
112 | # Trap all exit signals | |
113 | trap cleanup EXIT HUP INT TERM | |
114 | ||
115 | USERNS="$(in_userns)" | |
116 | ||
117 | if [ "${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 | |
127 | fi | |
128 | ||
129 | relevant_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 | |
145 | echo "Unpacking the rootfs" | |
146 | ||
147 | # Create temporary directory to | |
148 | if ! command -V mktemp >/dev/null 2>&1; then | |
149 | LOCAL_TEMP=/tmp/lxc-local.$$ | |
150 | mkdir -p "${LOCAL_TEMP}" | |
151 | else | |
152 | LOCAL_TEMP=$(mktemp -d) | |
153 | fi | |
154 | ||
155 | # Unpack file that contains meta.tar.xz | |
156 | if ! tar Jxf "${LXC_CONFIG}" -C "${LOCAL_TEMP}"; then | |
157 | echo "ERROR: Invalid metadata file" 2>&1 | |
158 | exit 1 | |
159 | fi | |
160 | ||
161 | EXCLUDES="" | |
162 | excludelist=$(relevant_file excludes) | |
163 | if [ -f "${excludelist}" ]; then | |
164 | while read -r line; do | |
165 | EXCLUDES="${EXCLUDES} --exclude=${line}" | |
166 | done < "${excludelist}" | |
167 | fi | |
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 | |
174 | tar --anchored ${EXCLUDES} --numeric-owner -xpJf "${LXC_FSTREE}" -C "${LXC_ROOTFS}" | |
175 | ||
176 | mkdir -p "${LXC_ROOTFS}/dev/pts/" | |
177 | ||
178 | # Setup the configuration | |
179 | # Setup the configuration | |
180 | configfile="$(relevant_file config)" | |
181 | if [ ! -e "${configfile}" ]; then | |
182 | echo "ERROR: meta tarball is missing the configuration file" 1>&2 | |
183 | exit 1 | |
184 | fi | |
185 | ||
186 | ## Extract all the network config entries | |
187 | sed -i -e "/lxc.net.0/{w ${LXC_PATH}/config-network" -e "d}" "${LXC_PATH}/config" | |
188 | ||
189 | ## Extract any other config entry | |
190 | sed -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 | ||
209 | fstab="$(relevant_file fstab)" | |
210 | if [ -e "${fstab}" ]; then | |
211 | echo "lxc.mount.fstab = ${LXC_PATH}/fstab" >> "${LXC_PATH}/config" | |
212 | fi | |
213 | echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_PATH}/config" | |
214 | ||
215 | ## Re-add the previously removed network config | |
216 | if [ -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" | |
223 | fi | |
224 | ||
225 | TEMPLATE_FILES="${LXC_PATH}/config" | |
226 | ||
227 | # Setup the fstab | |
228 | if [ -e "${fstab}" ]; then | |
229 | cp "${fstab}" "${LXC_PATH}/fstab" | |
230 | TEMPLATE_FILES="${TEMPLATE_FILES};${LXC_PATH}/fstab" | |
231 | fi | |
232 | ||
233 | # Look for extra templates | |
234 | if [ -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)" | |
240 | fi | |
241 | ||
242 | ||
243 | # Replace variables in all templates | |
244 | OLD_IFS=${IFS} | |
245 | IFS=";" | |
246 | for 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}" | |
254 | done | |
255 | IFS=${OLD_IFS} | |
256 | ||
257 | # prevent mingetty from calling vhangup(2) since it fails with userns on CentOS / Oracle | |
258 | if [ -f "${LXC_ROOTFS}/etc/init/tty.conf" ]; then | |
259 | sed -i 's|mingetty|mingetty --nohangup|' "${LXC_ROOTFS}/etc/init/tty.conf" | |
260 | fi | |
261 | ||
262 | ||
263 | if [ -e "$(relevant_file create-message)" ]; then | |
264 | echo "" | |
265 | echo "---" | |
266 | cat "$(relevant_file create-message)" | |
267 | fi | |
268 | ||
269 | exit 0 |