]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-oci.in
lxc-oci: add basic handling of numerical uid/gid
[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 usage() {
144 cat <<EOF
145 LXC container template for OCI images
146
147 Special arguments:
148 [ -h | --help ]: Print this help message and exit.
149
150 Required arguments:
151 [ -u | --url <url> ]: The OCI image URL
152
153 Optional arguments:
154 [ --username <username> ]: The username for the registry
155 [ --password <password> ]: The password for the registry
156
157 LXC internal arguments (do not pass manually!):
158 [ --name <name> ]: The container name
159 [ --path <path> ]: The path to the container
160 [ --rootfs <rootfs> ]: The path to the container's rootfs
161 [ --mapped-uid <map> ]: A uid map (user namespaces)
162 [ --mapped-gid <map> ]: A gid map (user namespaces)
163
164 EOF
165 return 0
166 }
167
168 options=$(getopt -o u:h -l help,url:,username:,password:,no-cache,\
169 name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@")
170
171 if [ $? -ne 0 ]; then
172 usage
173 exit 1
174 fi
175 eval set -- "$options"
176
177 OCI_URL=""
178 OCI_USERNAME=
179 OCI_PASSWORD=
180 OCI_USE_CACHE="true"
181
182 LXC_MAPPED_GID=
183 LXC_MAPPED_UID=
184 LXC_NAME=
185 LXC_PATH=
186 LXC_ROOTFS=
187
188 while :; do
189 case "$1" in
190 -h|--help) usage && exit 1;;
191 -u|--url) OCI_URL=$2; shift 2;;
192 --username) OCI_USERNAME=$2; shift 2;;
193 --password) OCI_PASSWORD=$2; shift 2;;
194 --no-cache) OCI_USE_CACHE="false"; shift 1;;
195 --name) LXC_NAME=$2; shift 2;;
196 --path) LXC_PATH=$2; shift 2;;
197 --rootfs) LXC_ROOTFS=$2; shift 2;;
198 --mapped-uid) LXC_MAPPED_UID=$2; shift 2;;
199 --mapped-gid) LXC_MAPPED_GID=$2; shift 2;;
200 *) break;;
201 esac
202 done
203
204 # Check that we have all variables we need
205 if [ -z "$LXC_NAME" ] || [ -z "$LXC_PATH" ] || [ -z "$LXC_ROOTFS" ]; then
206 echo "ERROR: Not running through LXC." 1>&2
207 exit 1
208 fi
209
210 if [ -z "$OCI_URL" ]; then
211 echo "ERROR: no OCI URL given"
212 exit 1
213 fi
214
215 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
216 echo "ERROR: password given but no username specified"
217 exit 1
218 fi
219
220 if [ "${OCI_USE_CACHE}" = "true" ]; then
221 if ! skopeo copy --help | grep -q 'dest-shared-blob-dir'; then
222 echo "INFO: skopeo doesn't support blob caching"
223 OCI_USE_CACHE="false"
224 fi
225 fi
226
227 USERNS=$(in_userns)
228
229 if [ "$USERNS" = "yes" ]; then
230 if [ -z "$LXC_MAPPED_UID" ] || [ "$LXC_MAPPED_UID" = "-1" ]; then
231 echo "ERROR: In a user namespace without a map." 1>&2
232 exit 1
233 fi
234 fi
235
236 if [ "${OCI_USE_CACHE}" = "true" ]; then
237 if [ "$USERNS" = "yes" ]; then
238 DOWNLOAD_BASE="${HOME}/.cache/lxc"
239 else
240 DOWNLOAD_BASE="${LOCALSTATEDIR}/cache/lxc"
241 fi
242 else
243 DOWNLOAD_BASE=/tmp
244 fi
245
246 # Trap all exit signals
247 trap cleanup EXIT HUP INT TERM
248
249 if ! type mktemp >/dev/null 2>&1; then
250 DOWNLOAD_TEMP="${DOWNLOAD_BASE}/lxc-oci.$$"
251 mkdir -p $DOWNLOAD_TEMP
252 else
253 DOWNLOAD_TEMP=$(mktemp -d -p "${DOWNLOAD_BASE}")
254 fi
255
256 # Download the image
257 skopeo_args=("")
258 if [ -n "$OCI_USERNAME" ]; then
259 CREDENTIALS="${OCI_USERNAME}"
260 if [ -n "$OCI_PASSWORD" ]; then
261 CREDENTIALS="${CREDENTIALS}:${OCI_PASSWORD}"
262 fi
263 skopeo_args+=(--src-creds "${CREDENTIALS}")
264 fi
265 if [ "${OCI_USE_CACHE}" = "true" ]; then
266 skopeo_args+=(--dest-shared-blob-dir "${DOWNLOAD_BASE}")
267 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
268 ln -s "${DOWNLOAD_BASE}/sha256" "${DOWNLOAD_TEMP}/blobs/sha256"
269 else
270 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
271 fi
272
273 echo "Unpacking the rootfs"
274 umoci_args=("")
275 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
276 umoci_args+=(--rootless)
277 fi
278 umoci unpack ${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp"
279 rmdir "${LXC_ROOTFS}"
280 mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}"
281
282 OCI_CONF_FILE=$(getconfigpath ${DOWNLOAD_TEMP} latest)
283 LXC_CONF_FILE="${LXC_PATH}/config"
284 entrypoint=$(getep ${OCI_CONF_FILE})
285 echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}"
286 echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}"
287
288 environment=$(getenv ${OCI_CONF_FILE})
289 while read -r line; do
290 echo "lxc.environment = ${line}" >> "${LXC_CONF_FILE}"
291 done <<< "${environment}"
292
293 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf" ]; then
294 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/common.conf" >> "${LXC_CONF_FILE}"
295 fi
296
297 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf" ]; then
298 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/userns.conf" >> "${LXC_CONF_FILE}"
299 fi
300
301 echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_CONF_FILE}"
302 # set the hostname
303 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
304 ${LXC_NAME}
305 EOF
306
307 # set minimal hosts
308 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
309 127.0.0.1 localhost
310 127.0.1.1 ${LXC_NAME}
311 EOF
312
313 uidgid=($(getuidgid ${OCI_CONF_FILE}))
314 echo "lxc.init.uid = ${uidgid[0]}" >> "${LXC_CONF_FILE}"
315 echo "lxc.init.gid = ${uidgid[1]}" >> "${LXC_CONF_FILE}"
316
317 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
318 chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
319 fi
320 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
321 chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
322 fi
323
324 exit 0