]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-oci.in
lxc-oci: add support for WorkingDir
[mirror_lxc.git] / templates / lxc-oci.in
1 #!/bin/bash
2
3 # Create application containers from OCI images
4
5 # Copyright © 2014 Stéphane Graber <stgraber@ubuntu.com>
6 # Copyright © 2017 Serge Hallyn <serge@hallyn.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 # set -x # debug
25
26 # Make sure the usual locations are in PATH
27 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
28
29 # Check for required binaries
30 for bin in skopeo umoci jq; do
31 if ! type $bin >/dev/null 2>&1; then
32 echo "ERROR: Missing required tool: $bin" 1>&2
33 exit 1
34 fi
35 done
36
37 LOCALSTATEDIR="@LOCALSTATEDIR@"
38 LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
39
40 # Some useful functions
41 cleanup() {
42 if [ -d "${DOWNLOAD_TEMP}" ]; then
43 rm -Rf "${DOWNLOAD_TEMP}"
44 fi
45 if [ -d "${LXC_ROOTFS}.tmp" ]; then
46 rm -Rf "${LXC_ROOTFS}.tmp"
47 fi
48 }
49
50 in_userns() {
51 [ -e /proc/self/uid_map ] || { echo no; return; }
52 while read line; do
53 fields=$(echo $line | awk '{ print $1 " " $2 " " $3 }')
54 [ "$fields" = "0 0 4294967295" ] && { echo no; return; } || true
55 echo $fields | grep -q " 0 1$" && { echo userns-root; return; } || true
56 done < /proc/self/uid_map
57
58 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
59 { echo userns-root; return; }
60 echo yes
61 }
62
63 getconfigpath() {
64 basedir="$1"
65 q="$2"
66
67 digest=`cat "${basedir}/index.json" | jq -c -r --arg q "$q" '.manifests[] | if .annotations."org.opencontainers.image.ref.name" == $q then .digest else empty end'`
68 if [ -z "${digest}" ]; then
69 echo "$q not found in index.json" >&2
70 return
71 fi
72
73 # Ok we have the image config digest, now get the config from that,
74 d=${digest:7}
75 cdigest=`cat "${basedir}/blobs/sha256/${d}" | jq -c -r '.config.digest'`
76 if [ -z "${cdigest}" ]; then
77 echo "container config not found" >&2
78 return
79 fi
80
81 d2=${cdigest:7}
82 echo "${basedir}/blobs/sha256/${d2}"
83 return
84 }
85
86 # get entrypoint from oci image. Use sh if unspecified
87 getep() {
88 if [ "$#" -eq 0 ]; then
89 echo "/bin/sh"
90 return
91 fi
92
93 configpath="$1"
94
95 ep=`cat "${configpath}" | jq -c -r '.config.Entrypoint[]?'`
96 cmd=`cat "${configpath}" | jq -c -r '.config.Cmd[]?'`
97 if [ -z "${ep}" ]; then
98 ep="${cmd}"
99 if [ -z "${ep}" ]; then
100 ep="/bin/sh"
101 fi
102 elif [ -n "${cmd}" ]; then
103 ep="${ep} ${cmd}"
104 fi
105
106 echo ${ep}
107 return
108 }
109
110 # get environment from oci image.
111 getenv() {
112 if [ "$#" -eq 0 ]; then
113 return
114 fi
115
116 configpath="$1"
117
118 env=`cat "${configpath}" | jq -c -r '.config.Env[]'`
119
120 echo "${env}"
121 return
122 }
123
124 # FIXME 1: only support numerical values in the configuration file.
125 # FIXME 2: from the OCI image spec: "If group/gid is not specified,
126 # the default group and supplementary groups of the given user/uid in
127 # /etc/passwd from the container are applied."
128 getuidgid() {
129 if [ "$#" -eq 0 ]; then
130 echo "0 0"
131 return
132 fi
133
134 configpath="$1"
135
136 uidgid=`cat "${configpath}" | jq -c -r '.config.User // "0:0"'`
137 uidgid=(${uidgid//:/ })
138
139 printf '%d %d' ${uidgid[0]:-0} ${uidgid[1]:-0} 2>/dev/null || true
140 return
141 }
142
143 # get cwd from oci image.
144 getcwd() {
145 if [ "$#" -eq 0 ]; then
146 echo "/"
147 return
148 fi
149
150 configpath="$1"
151
152 cwd=`cat "${configpath}" | jq -c -r '.config.WorkingDir // "/"'`
153
154 echo "${cwd}"
155 return
156 }
157
158 usage() {
159 cat <<EOF
160 LXC container template for OCI images
161
162 Special arguments:
163 [ -h | --help ]: Print this help message and exit.
164
165 Required arguments:
166 [ -u | --url <url> ]: The OCI image URL
167
168 Optional arguments:
169 [ --username <username> ]: The username for the registry
170 [ --password <password> ]: The password for the registry
171
172 LXC internal arguments (do not pass manually!):
173 [ --name <name> ]: The container name
174 [ --path <path> ]: The path to the container
175 [ --rootfs <rootfs> ]: The path to the container's rootfs
176 [ --mapped-uid <map> ]: A uid map (user namespaces)
177 [ --mapped-gid <map> ]: A gid map (user namespaces)
178
179 EOF
180 return 0
181 }
182
183 options=$(getopt -o u:h -l help,url:,username:,password:,no-cache,\
184 name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@")
185
186 if [ $? -ne 0 ]; then
187 usage
188 exit 1
189 fi
190 eval set -- "$options"
191
192 OCI_URL=""
193 OCI_USERNAME=
194 OCI_PASSWORD=
195 OCI_USE_CACHE="true"
196
197 LXC_MAPPED_GID=
198 LXC_MAPPED_UID=
199 LXC_NAME=
200 LXC_PATH=
201 LXC_ROOTFS=
202
203 while :; do
204 case "$1" in
205 -h|--help) usage && exit 1;;
206 -u|--url) OCI_URL=$2; shift 2;;
207 --username) OCI_USERNAME=$2; shift 2;;
208 --password) OCI_PASSWORD=$2; shift 2;;
209 --no-cache) OCI_USE_CACHE="false"; shift 1;;
210 --name) LXC_NAME=$2; shift 2;;
211 --path) LXC_PATH=$2; shift 2;;
212 --rootfs) LXC_ROOTFS=$2; shift 2;;
213 --mapped-uid) LXC_MAPPED_UID=$2; shift 2;;
214 --mapped-gid) LXC_MAPPED_GID=$2; shift 2;;
215 *) break;;
216 esac
217 done
218
219 # Check that we have all variables we need
220 if [ -z "$LXC_NAME" ] || [ -z "$LXC_PATH" ] || [ -z "$LXC_ROOTFS" ]; then
221 echo "ERROR: Not running through LXC." 1>&2
222 exit 1
223 fi
224
225 if [ -z "$OCI_URL" ]; then
226 echo "ERROR: no OCI URL given"
227 exit 1
228 fi
229
230 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
231 echo "ERROR: password given but no username specified"
232 exit 1
233 fi
234
235 if [ "${OCI_USE_CACHE}" = "true" ]; then
236 if ! skopeo copy --help | grep -q 'dest-shared-blob-dir'; then
237 echo "INFO: skopeo doesn't support blob caching"
238 OCI_USE_CACHE="false"
239 fi
240 fi
241
242 USERNS=$(in_userns)
243
244 if [ "$USERNS" = "yes" ]; then
245 if [ -z "$LXC_MAPPED_UID" ] || [ "$LXC_MAPPED_UID" = "-1" ]; then
246 echo "ERROR: In a user namespace without a map." 1>&2
247 exit 1
248 fi
249 fi
250
251 if [ "${OCI_USE_CACHE}" = "true" ]; then
252 if [ "$USERNS" = "yes" ]; then
253 DOWNLOAD_BASE="${HOME}/.cache/lxc"
254 else
255 DOWNLOAD_BASE="${LOCALSTATEDIR}/cache/lxc"
256 fi
257 else
258 DOWNLOAD_BASE=/tmp
259 fi
260
261 # Trap all exit signals
262 trap cleanup EXIT HUP INT TERM
263
264 if ! type mktemp >/dev/null 2>&1; then
265 DOWNLOAD_TEMP="${DOWNLOAD_BASE}/lxc-oci.$$"
266 mkdir -p $DOWNLOAD_TEMP
267 else
268 DOWNLOAD_TEMP=$(mktemp -d -p "${DOWNLOAD_BASE}")
269 fi
270
271 # Download the image
272 skopeo_args=("")
273 if [ -n "$OCI_USERNAME" ]; then
274 CREDENTIALS="${OCI_USERNAME}"
275 if [ -n "$OCI_PASSWORD" ]; then
276 CREDENTIALS="${CREDENTIALS}:${OCI_PASSWORD}"
277 fi
278 skopeo_args+=(--src-creds "${CREDENTIALS}")
279 fi
280 if [ "${OCI_USE_CACHE}" = "true" ]; then
281 skopeo_args+=(--dest-shared-blob-dir "${DOWNLOAD_BASE}")
282 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
283 ln -s "${DOWNLOAD_BASE}/sha256" "${DOWNLOAD_TEMP}/blobs/sha256"
284 else
285 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
286 fi
287
288 echo "Unpacking the rootfs"
289 umoci_args=("")
290 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
291 umoci_args+=(--rootless)
292 fi
293 umoci unpack ${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp"
294 rmdir "${LXC_ROOTFS}"
295 mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}"
296
297 OCI_CONF_FILE=$(getconfigpath ${DOWNLOAD_TEMP} latest)
298 LXC_CONF_FILE="${LXC_PATH}/config"
299 entrypoint=$(getep ${OCI_CONF_FILE})
300 echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}"
301 echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}"
302
303 environment=$(getenv ${OCI_CONF_FILE})
304 while read -r line; do
305 echo "lxc.environment = ${line}" >> "${LXC_CONF_FILE}"
306 done <<< "${environment}"
307
308 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf" ]; then
309 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/common.conf" >> "${LXC_CONF_FILE}"
310 fi
311
312 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf" ]; then
313 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/userns.conf" >> "${LXC_CONF_FILE}"
314 fi
315
316 echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_CONF_FILE}"
317 # set the hostname
318 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
319 ${LXC_NAME}
320 EOF
321
322 # set minimal hosts
323 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
324 127.0.0.1 localhost
325 127.0.1.1 ${LXC_NAME}
326 ::1 localhost ip6-localhost ip6-loopback
327 fe00::0 ip6-localnet
328 ff00::0 ip6-mcastprefix
329 ff02::1 ip6-allnodes
330 ff02::2 ip6-allrouters
331 EOF
332
333 uidgid=($(getuidgid ${OCI_CONF_FILE}))
334 echo "lxc.init.uid = ${uidgid[0]}" >> "${LXC_CONF_FILE}"
335 echo "lxc.init.gid = ${uidgid[1]}" >> "${LXC_CONF_FILE}"
336
337 cwd=$(getcwd ${OCI_CONF_FILE})
338 echo "lxc.init.cwd = ${cwd}" >> "${LXC_CONF_FILE}"
339
340 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
341 chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
342 fi
343 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
344 chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
345 fi
346
347 exit 0