]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-oci.in
lxc-oci: cleanup temporary download directory if umoci is interrupted
[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 LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
38
39 # Some useful functions
40 cleanup() {
41 if [ -d "${DOWNLOAD_TEMP}" ]; then
42 rm -Rf "${DOWNLOAD_TEMP}"
43 fi
44 if [ -d "${LXC_ROOTFS}.tmp" ]; then
45 rm -Rf "${LXC_ROOTFS}.tmp"
46 fi
47 }
48
49 in_userns() {
50 [ -e /proc/self/uid_map ] || { echo no; return; }
51 while read line; do
52 fields=$(echo $line | awk '{ print $1 " " $2 " " $3 }')
53 [ "$fields" = "0 0 4294967295" ] && { echo no; return; } || true
54 echo $fields | grep -q " 0 1$" && { echo userns-root; return; } || true
55 done < /proc/self/uid_map
56
57 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
58 { echo userns-root; return; }
59 echo yes
60 }
61
62 getconfigpath() {
63 basedir="$1"
64 q="$2"
65
66 digest=`cat "${basedir}/index.json" | jq --arg q "$q" '.manifests[] | if .annotations."org.opencontainers.image.ref.name" == $q then .digest else null end' | sed -e 's/"//g'`
67 if [ -z "${digest}" ]; then
68 echo "$q not found in index.json" >&2
69 return
70 fi
71
72 # Ok we have the image config digest, now get the config from that,
73 d=${digest:7}
74 cdigest=`cat "${basedir}/blobs/sha256/${d}" | jq '.config.digest' | sed -e 's/"//g'`
75 if [ -z "${cdigest}" ]; then
76 echo "container config not found" >&2
77 return
78 fi
79
80 d2=${cdigest:7}
81 echo "${basedir}/blobs/sha256/${d2}"
82 return
83 }
84
85 # get entrypoint from oci image. Use sh if unspecified
86 getep() {
87 if [ "$#" -eq 0 ]; then
88 echo "/bin/sh"
89 return
90 fi
91
92 configpath="$1"
93
94 ep=`cat "${configpath}" | jq -c '.config.Entrypoint' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
95 cmd=`cat "${configpath}" | jq -c '.config.Cmd' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
96 if [ "${ep}" = "null" ]; then
97 ep="${cmd}"
98 if [ "${ep}" = "null" ]; then
99 ep="/bin/sh"
100 fi
101 elif [ "${cmd}" != "null" ]; then
102 ep="${ep} ${cmd}"
103 fi
104
105 if [ -z "${ep}" ]; then
106 echo "/bin/sh"
107 return
108 fi
109 echo "${ep}"
110 return
111 }
112
113 # get environment from oci image.
114 getenv() {
115 if [ "$#" -eq 0 ]; then
116 return
117 fi
118
119 configpath="$1"
120
121 cat "${configpath}" > /tmp/config
122 env=`cat "${configpath}" | jq -c '.config.Env[]'`
123
124 echo "${env}"
125 return
126 }
127
128 usage() {
129 cat <<EOF
130 LXC container template for OCI images
131
132 Special arguments:
133 [ -h | --help ]: Print this help message and exit.
134
135 Required arguments:
136 [ -u | --url <url> ]: The OCI image URL
137
138 Optional arguments:
139 [ --username <username> ]: The username for the registry
140 [ --password <password> ]: The password for the registry
141
142 LXC internal arguments (do not pass manually!):
143 [ --name <name> ]: The container name
144 [ --path <path> ]: The path to the container
145 [ --rootfs <rootfs> ]: The path to the container's rootfs
146 [ --mapped-uid <map> ]: A uid map (user namespaces)
147 [ --mapped-gid <map> ]: A gid map (user namespaces)
148
149 EOF
150 return 0
151 }
152
153 options=$(getopt -o u:h -l help,url:,username:,password:,\
154 name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@")
155
156 if [ $? -ne 0 ]; then
157 usage
158 exit 1
159 fi
160 eval set -- "$options"
161
162 OCI_URL=""
163 OCI_USERNAME=
164 OCI_PASSWORD=
165
166 LXC_MAPPED_GID=
167 LXC_MAPPED_UID=
168 LXC_NAME=
169 LXC_PATH=
170 LXC_ROOTFS=
171
172 while :; do
173 case "$1" in
174 -h|--help) usage && exit 1;;
175 -u|--url) OCI_URL=$2; shift 2;;
176 --username) OCI_USERNAME=$2; shift 2;;
177 --password) OCI_PASSWORD=$2; shift 2;;
178 --name) LXC_NAME=$2; shift 2;;
179 --path) LXC_PATH=$2; shift 2;;
180 --rootfs) LXC_ROOTFS=$2; shift 2;;
181 --mapped-uid) LXC_MAPPED_UID=$2; shift 2;;
182 --mapped-gid) LXC_MAPPED_GID=$2; shift 2;;
183 *) break;;
184 esac
185 done
186
187 # Check that we have all variables we need
188 if [ -z "$LXC_NAME" ] || [ -z "$LXC_PATH" ] || [ -z "$LXC_ROOTFS" ]; then
189 echo "ERROR: Not running through LXC." 1>&2
190 exit 1
191 fi
192
193 if [ -z "$OCI_URL" ]; then
194 echo "ERROR: no OCI URL given"
195 exit 1
196 fi
197
198 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
199 echo "ERROR: password given but no username specified"
200 exit 1
201 fi
202
203 USERNS=$(in_userns)
204
205 if [ "$USERNS" != "no" ]; then
206 if [ "$USERNS" = "yes" ]; then
207 if [ -z "$LXC_MAPPED_UID" ] || [ "$LXC_MAPPED_UID" = "-1" ]; then
208 echo "ERROR: In a user namespace without a map." 1>&2
209 exit 1
210 fi
211 DOWNLOAD_MODE="user"
212 DOWNLOAD_TARGET="user"
213 else
214 DOWNLOAD_MODE="user"
215 DOWNLOAD_TARGET="system"
216 fi
217 fi
218
219 # Trap all exit signals
220 trap cleanup EXIT HUP INT TERM
221
222 if ! type mktemp >/dev/null 2>&1; then
223 DOWNLOAD_TEMP=/tmp/lxc-oci.$$
224 mkdir -p $DOWNLOAD_TEMP
225 else
226 DOWNLOAD_TEMP=$(mktemp -d)
227 fi
228
229 # Download the image - TODO - cache
230 skopeo_args=("")
231 if [ -n "$OCI_USERNAME" ]; then
232 CREDENTIALS="${OCI_USERNAME}"
233 if [ -n "$OCI_PASSWORD" ]; then
234 CREDENTIALS="${CREDENTIALS}:${OCI_PASSWORD}"
235 fi
236 skopeo_args+=(--src-creds "${CREDENTIALS}")
237 fi
238 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
239
240 # Unpack the rootfs
241 echo "Unpacking the rootfs"
242
243 umoci_args=("")
244 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
245 umoci_args+=(--rootless)
246 fi
247 umoci unpack ${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp"
248 rmdir "${LXC_ROOTFS}"
249 mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}"
250
251 OCI_CONF_FILE=$(getconfigpath ${DOWNLOAD_TEMP} latest)
252 LXC_CONF_FILE="${LXC_PATH}/config"
253 entrypoint=$(getep ${OCI_CONF_FILE})
254 echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}"
255 echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}"
256
257 environment=$(getenv ${OCI_CONF_FILE})
258 while read -r line; do
259 echo "lxc.environment = ${line}" >> "${LXC_CONF_FILE}"
260 done <<< "${environment}"
261
262 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf" ]; then
263 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/common.conf" >> "${LXC_CONF_FILE}"
264 fi
265
266 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf" ]; then
267 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/userns.conf" >> "${LXC_CONF_FILE}"
268 fi
269
270 echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_CONF_FILE}"
271 # set the hostname
272 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
273 ${LXC_NAME}
274 EOF
275
276 # set minimal hosts
277 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
278 127.0.0.1 localhost
279 127.0.1.1 ${LXC_NAME}
280 EOF
281
282 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
283 chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
284 fi
285 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
286 chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
287 fi
288
289 exit 0