]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | # get entrypoint from oci image. Use sh if unspecified | |
58 | # TODO - we can get other things like resource limits here | |
59 | getep() { | |
60 | basedir="$1" | |
61 | q="$2" | |
62 | ||
63 | ||
64 | 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'` | |
65 | if [ -z "${digest}" ]; then | |
66 | echo "$q not found in index.json" >&2 | |
67 | echo "/bin/sh" | |
68 | return | |
69 | fi | |
70 | ||
71 | # Ok we have the image config digest, now get the config from that, | |
72 | d=${digest:7} | |
73 | cdigest=`cat "${basedir}/blobs/sha256/${d}" | jq '.config.digest' | sed -e 's/"//g'` | |
74 | if [ -z "${cdigest}" ]; then | |
75 | echo "container config not found" >&2 | |
76 | echo "/bin/sh" | |
77 | return | |
78 | fi | |
79 | ||
80 | d2=${cdigest:7} | |
81 | ep=`cat "${basedir}/blobs/sha256/${d2}" | jq -c '.config.Entrypoint' | sed -e 's/^\[//; s/\]$//; s/","/" "/'` | |
82 | cmd=`cat "${basedir}/blobs/sha256/${d2}" | jq -c '.config.Cmd' | sed -e 's/^\[//; s/\]$//; s/","/" "/'` | |
83 | if [ "${ep}" = "null" ]; then | |
84 | ep="${cmd}" | |
85 | if [ "${ep}" = "null" ]; then | |
86 | ep="/bin/sh" | |
87 | fi | |
88 | elif [ "${cmd}" != "null" ]; then | |
89 | ep="${ep} ${cmd}" | |
90 | fi | |
91 | ||
92 | if [ -z "${ep}" ]; then | |
93 | echo "/bin/sh" | |
94 | return | |
95 | fi | |
96 | echo "${ep}" | |
97 | return | |
98 | } | |
99 | ||
100 | usage() { | |
101 | cat <<EOF | |
102 | LXC container template for OCI images | |
103 | ||
104 | Special arguments: | |
105 | [ -h | --help ]: Print this help message and exit. | |
106 | ||
107 | Required arguments: | |
108 | [ -u | --url <url> ]: The OCI image URL | |
109 | ||
110 | LXC internal arguments (do not pass manually!): | |
111 | [ --name <name> ]: The container name | |
112 | [ --path <path> ]: The path to the container | |
113 | [ --rootfs <rootfs> ]: The path to the container's rootfs | |
114 | [ --mapped-uid <map> ]: A uid map (user namespaces) | |
115 | [ --mapped-gid <map> ]: A gid map (user namespaces) | |
116 | ||
117 | EOF | |
118 | return 0 | |
119 | } | |
120 | ||
121 | options=$(getopt -o u:h -l help,name:,path:,\ | |
122 | rootfs:,mapped-uid:,mapped-gid: -- "$@") | |
123 | ||
124 | if [ $? -ne 0 ]; then | |
125 | usage | |
126 | exit 1 | |
127 | fi | |
128 | eval set -- "$options" | |
129 | ||
130 | OCI_URL="" | |
131 | LXC_MAPPED_GID= | |
132 | LXC_MAPPED_UID= | |
133 | LXC_NAME= | |
134 | LXC_PATH= | |
135 | LXC_ROOTFS= | |
136 | ||
137 | while :; do | |
138 | case "$1" in | |
139 | -h|--help) usage && exit 1;; | |
140 | -u|--url) OCI_URL=$2; shift 2;; | |
141 | --name) LXC_NAME=$2; shift 2;; | |
142 | --path) LXC_PATH=$2; shift 2;; | |
143 | --rootfs) LXC_ROOTFS=$2; shift 2;; | |
144 | --mapped-uid) LXC_MAPPED_UID=$2; shift 2;; | |
145 | --mapped-gid) LXC_MAPPED_GID=$2; shift 2;; | |
146 | *) break;; | |
147 | esac | |
148 | done | |
149 | ||
150 | # Check that we have all variables we need | |
151 | if [ -z "$LXC_NAME" ] || [ -z "$LXC_PATH" ] || [ -z "$LXC_ROOTFS" ]; then | |
152 | echo "ERROR: Not running through LXC." 1>&2 | |
153 | exit 1 | |
154 | fi | |
155 | ||
156 | if [ -z "$OCI_URL" ]; then | |
157 | echo "ERROR: no OCI URL given" | |
158 | exit 1 | |
159 | fi | |
160 | ||
161 | USERNS=$(in_userns) | |
162 | ||
163 | if [ "$USERNS" != "no" ]; then | |
164 | if [ "$USERNS" = "yes" ]; then | |
165 | if [ -z "$LXC_MAPPED_UID" ] || [ "$LXC_MAPPED_UID" = "-1" ]; then | |
166 | echo "ERROR: In a user namespace without a map." 1>&2 | |
167 | exit 1 | |
168 | fi | |
169 | DOWNLOAD_MODE="user" | |
170 | DOWNLOAD_TARGET="user" | |
171 | else | |
172 | DOWNLOAD_MODE="user" | |
173 | DOWNLOAD_TARGET="system" | |
174 | fi | |
175 | fi | |
176 | ||
177 | # Trap all exit signals | |
178 | trap cleanup EXIT HUP INT TERM | |
179 | ||
180 | if ! type mktemp >/dev/null 2>&1; then | |
181 | DOWNLOAD_TEMP=/tmp/lxc-oci.$$ | |
182 | mkdir -p $DOWNLOAD_TEMP | |
183 | else | |
184 | DOWNLOAD_TEMP=$(mktemp -d) | |
185 | fi | |
186 | ||
187 | # Download the image - TODO - cache | |
188 | skopeo copy "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest" | |
189 | ||
190 | # Unpack the rootfs | |
191 | echo "Unpacking the rootfs" | |
192 | ||
193 | umoci unpack --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp" | |
194 | rmdir "${LXC_ROOTFS}" | |
195 | mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}" | |
196 | entrypoint=$(getep ${DOWNLOAD_TEMP} latest) | |
197 | rm -rf "${LXC_ROOTFS}.tmp" | |
198 | ||
199 | LXC_CONF_FILE="${LXC_PATH}/config" | |
3dca1af0 | 200 | echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}" |
0ef43a5c SH |
201 | echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}" |
202 | ||
203 | echo "lxc.uts.name = ${LXC_NAME}" >> ${LXC_PATH}/config | |
204 | ||
205 | if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then | |
206 | chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true | |
207 | fi | |
208 | if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then | |
209 | chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true | |
210 | fi | |
211 | ||
212 | exit 0 |