]> git.proxmox.com Git - mirror_lxc.git/blame - templates/lxc-oci.in
lxc-oci: import the environment variables
[mirror_lxc.git] / templates / lxc-oci.in
CommitLineData
0ef43a5c
SH
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
23set -eu
24# set -x # debug
25
26# Make sure the usual locations are in PATH
27export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
28
29# Check for required binaries
30for 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
35done
36
37# Some useful functions
38cleanup() {
39 if [ -d "$DOWNLOAD_TEMP" ]; then
40 rm -Rf $DOWNLOAD_TEMP
41 fi
42}
43
44in_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
ce59e4ca 57getconfigpath() {
0ef43a5c
SH
58 basedir="$1"
59 q="$2"
60
0ef43a5c
SH
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
0ef43a5c
SH
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
0ef43a5c
SH
72 return
73 fi
74
75 d2=${cdigest:7}
ce59e4ca
FA
76 echo "${basedir}/blobs/sha256/${d2}"
77 return
78}
79
80# get entrypoint from oci image. Use sh if unspecified
81getep() {
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/","/" "/'`
0ef43a5c
SH
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
996202e7
FA
108# get environment from oci image.
109getenv() {
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
0ef43a5c
SH
123usage() {
124 cat <<EOF
125LXC container template for OCI images
126
127Special arguments:
128[ -h | --help ]: Print this help message and exit.
129
130Required arguments:
131[ -u | --url <url> ]: The OCI image URL
132
133LXC 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
140EOF
141 return 0
142}
143
960f15bf 144options=$(getopt -o u:h -l help,url:,name:,path:,\
0ef43a5c
SH
145rootfs:,mapped-uid:,mapped-gid: -- "$@")
146
147if [ $? -ne 0 ]; then
148 usage
149 exit 1
150fi
151eval set -- "$options"
152
153OCI_URL=""
154LXC_MAPPED_GID=
155LXC_MAPPED_UID=
156LXC_NAME=
157LXC_PATH=
158LXC_ROOTFS=
159
160while :; 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
171done
172
173# Check that we have all variables we need
174if [ -z "$LXC_NAME" ] || [ -z "$LXC_PATH" ] || [ -z "$LXC_ROOTFS" ]; then
175 echo "ERROR: Not running through LXC." 1>&2
176 exit 1
177fi
178
179if [ -z "$OCI_URL" ]; then
180 echo "ERROR: no OCI URL given"
181 exit 1
182fi
183
184USERNS=$(in_userns)
185
186if [ "$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
198fi
199
200# Trap all exit signals
201trap cleanup EXIT HUP INT TERM
202
203if ! type mktemp >/dev/null 2>&1; then
204 DOWNLOAD_TEMP=/tmp/lxc-oci.$$
205 mkdir -p $DOWNLOAD_TEMP
206else
207 DOWNLOAD_TEMP=$(mktemp -d)
208fi
209
210# Download the image - TODO - cache
211skopeo copy "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
212
213# Unpack the rootfs
214echo "Unpacking the rootfs"
215
51c80577
FA
216umoci_args=("")
217if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
218 umoci_args+=(--rootless)
219fi
220umoci unpack ${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp"
0ef43a5c
SH
221rmdir "${LXC_ROOTFS}"
222mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}"
0ef43a5c
SH
223rm -rf "${LXC_ROOTFS}.tmp"
224
ce59e4ca 225OCI_CONF_FILE=$(getconfigpath ${DOWNLOAD_TEMP} latest)
0ef43a5c 226LXC_CONF_FILE="${LXC_PATH}/config"
ce59e4ca 227entrypoint=$(getep ${OCI_CONF_FILE})
3dca1af0 228echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}"
0ef43a5c
SH
229echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}"
230
996202e7
FA
231environment=$(getenv ${OCI_CONF_FILE})
232while read -r line; do
233 echo "lxc.environment = ${line}" >> "${LXC_CONF_FILE}"
234done <<< "${environment}"
235
bc2c91ae
FA
236echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_CONF_FILE}"
237# set the hostname
238cat <<EOF > ${LXC_ROOTFS}/etc/hostname
239${LXC_NAME}
240EOF
0ef43a5c 241
b5236550
FA
242# set minimal hosts
243cat <<EOF > ${LXC_ROOTFS}/etc/hosts
244127.0.0.1 localhost
245127.0.1.1 ${LXC_NAME}
246EOF
247
0ef43a5c
SH
248if [ -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
250fi
251if [ -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
253fi
254
255exit 0