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