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