]>
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 | ||
3cc0ec84 CB |
25 | LXC_HOOK_DIR=@LXCHOOKDIR@ |
26 | LXC_TEMPLATE_CONFIG=@LXCTEMPLATECONFIG@ | |
35444f38 CB |
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 | |
655d10ed | 36 | export PATH="$PATH:/usr/sbin:/usr/bin:/sbin:/bin" |
35444f38 CB |
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 | ||
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 | ||
63 | usage() { | |
64 | cat <<EOF | |
65 | LXC container image downloader | |
66 | ||
67 | Special 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 | ||
72 | LXC 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 |
78 | EOF |
79 | return 0 | |
80 | } | |
81 | ||
82 | if ! options=$(getopt -o hm:f: -l help,metadata:,fstree:,name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@"); then | |
83 | usage | |
84 | exit 1 | |
85 | fi | |
86 | eval set -- "$options" | |
87 | ||
88 | while :; 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 | |
100 | done | |
101 | ||
102 | # Check for required binaries | |
103 | for 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 | |
108 | done | |
109 | ||
110 | cleanup() { | |
111 | if [ -d "${LOCAL_TEMP}" ]; then | |
112 | rm -Rf "${LOCAL_TEMP}" | |
113 | fi | |
114 | } | |
115 | ||
116 | # Trap all exit signals | |
117 | trap cleanup EXIT HUP INT TERM | |
118 | ||
119 | USERNS="$(in_userns)" | |
120 | ||
121 | if [ "${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 | |
131 | fi | |
132 | ||
133 | relevant_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 | |
149 | echo "Unpacking the rootfs" | |
150 | ||
151 | # Create temporary directory to | |
152 | if ! command -V mktemp >/dev/null 2>&1; then | |
153 | LOCAL_TEMP=/tmp/lxc-local.$$ | |
154 | mkdir -p "${LOCAL_TEMP}" | |
155 | else | |
156 | LOCAL_TEMP=$(mktemp -d) | |
157 | fi | |
158 | ||
159 | # Unpack file that contains meta.tar.xz | |
160 | if ! tar Jxf "${LXC_CONFIG}" -C "${LOCAL_TEMP}"; then | |
161 | echo "ERROR: Invalid metadata file" 2>&1 | |
162 | exit 1 | |
163 | fi | |
164 | ||
165 | EXCLUDES="" | |
166 | excludelist=$(relevant_file excludes) | |
167 | if [ -f "${excludelist}" ]; then | |
168 | while read -r line; do | |
169 | EXCLUDES="${EXCLUDES} --exclude=${line}" | |
170 | done < "${excludelist}" | |
171 | fi | |
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 | |
178 | tar --anchored ${EXCLUDES} --numeric-owner -xpJf "${LXC_FSTREE}" -C "${LXC_ROOTFS}" | |
179 | ||
180 | mkdir -p "${LXC_ROOTFS}/dev/pts/" | |
181 | ||
182 | # Setup the configuration | |
183 | # Setup the configuration | |
184 | configfile="$(relevant_file config)" | |
185 | if [ ! -e "${configfile}" ]; then | |
186 | echo "ERROR: meta tarball is missing the configuration file" 1>&2 | |
187 | exit 1 | |
188 | fi | |
189 | ||
190 | ## Extract all the network config entries | |
191 | sed -i -e "/lxc.net.0/{w ${LXC_PATH}/config-network" -e "d}" "${LXC_PATH}/config" | |
192 | ||
193 | ## Extract any other config entry | |
194 | sed -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 | ||
213 | fstab="$(relevant_file fstab)" | |
214 | if [ -e "${fstab}" ]; then | |
215 | echo "lxc.mount.fstab = ${LXC_PATH}/fstab" >> "${LXC_PATH}/config" | |
216 | fi | |
217 | echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_PATH}/config" | |
218 | ||
219 | ## Re-add the previously removed network config | |
220 | if [ -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" | |
227 | fi | |
228 | ||
229 | TEMPLATE_FILES="${LXC_PATH}/config" | |
230 | ||
231 | # Setup the fstab | |
232 | if [ -e "${fstab}" ]; then | |
233 | cp "${fstab}" "${LXC_PATH}/fstab" | |
234 | TEMPLATE_FILES="${TEMPLATE_FILES};${LXC_PATH}/fstab" | |
235 | fi | |
236 | ||
237 | # Look for extra templates | |
238 | if [ -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)" | |
244 | fi | |
245 | ||
246 | ||
247 | # Replace variables in all templates | |
248 | OLD_IFS=${IFS} | |
249 | IFS=";" | |
250 | for 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}" | |
258 | done | |
259 | IFS=${OLD_IFS} | |
260 | ||
261 | # prevent mingetty from calling vhangup(2) since it fails with userns on CentOS / Oracle | |
262 | if [ -f "${LXC_ROOTFS}/etc/init/tty.conf" ]; then | |
263 | sed -i 's|mingetty|mingetty --nohangup|' "${LXC_ROOTFS}/etc/init/tty.conf" | |
264 | fi | |
265 | ||
266 | ||
267 | if [ -e "$(relevant_file create-message)" ]; then | |
268 | echo "" | |
269 | echo "---" | |
270 | cat "$(relevant_file create-message)" | |
271 | fi | |
272 | ||
273 | exit 0 |