]> git.proxmox.com Git - mirror_lxc.git/blame - templates/lxc-oci.in
lxc-oci: write /etc/hosts
[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
57# get entrypoint from oci image. Use sh if unspecified
58# TODO - we can get other things like resource limits here
59getep() {
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
100usage() {
101 cat <<EOF
102LXC container template for OCI images
103
104Special arguments:
105[ -h | --help ]: Print this help message and exit.
106
107Required arguments:
108[ -u | --url <url> ]: The OCI image URL
109
110LXC 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
117EOF
118 return 0
119}
120
960f15bf 121options=$(getopt -o u:h -l help,url:,name:,path:,\
0ef43a5c
SH
122rootfs:,mapped-uid:,mapped-gid: -- "$@")
123
124if [ $? -ne 0 ]; then
125 usage
126 exit 1
127fi
128eval set -- "$options"
129
130OCI_URL=""
131LXC_MAPPED_GID=
132LXC_MAPPED_UID=
133LXC_NAME=
134LXC_PATH=
135LXC_ROOTFS=
136
137while :; 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
148done
149
150# Check that we have all variables we need
151if [ -z "$LXC_NAME" ] || [ -z "$LXC_PATH" ] || [ -z "$LXC_ROOTFS" ]; then
152 echo "ERROR: Not running through LXC." 1>&2
153 exit 1
154fi
155
156if [ -z "$OCI_URL" ]; then
157 echo "ERROR: no OCI URL given"
158 exit 1
159fi
160
161USERNS=$(in_userns)
162
163if [ "$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
175fi
176
177# Trap all exit signals
178trap cleanup EXIT HUP INT TERM
179
180if ! type mktemp >/dev/null 2>&1; then
181 DOWNLOAD_TEMP=/tmp/lxc-oci.$$
182 mkdir -p $DOWNLOAD_TEMP
183else
184 DOWNLOAD_TEMP=$(mktemp -d)
185fi
186
187# Download the image - TODO - cache
188skopeo copy "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
189
190# Unpack the rootfs
191echo "Unpacking the rootfs"
192
51c80577
FA
193umoci_args=("")
194if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
195 umoci_args+=(--rootless)
196fi
197umoci unpack ${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp"
0ef43a5c
SH
198rmdir "${LXC_ROOTFS}"
199mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}"
200entrypoint=$(getep ${DOWNLOAD_TEMP} latest)
201rm -rf "${LXC_ROOTFS}.tmp"
202
203LXC_CONF_FILE="${LXC_PATH}/config"
3dca1af0 204echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}"
0ef43a5c
SH
205echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}"
206
bc2c91ae
FA
207echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_CONF_FILE}"
208# set the hostname
209cat <<EOF > ${LXC_ROOTFS}/etc/hostname
210${LXC_NAME}
211EOF
0ef43a5c 212
b5236550
FA
213# set minimal hosts
214cat <<EOF > ${LXC_ROOTFS}/etc/hosts
215127.0.0.1 localhost
216127.0.1.1 ${LXC_NAME}
217EOF
218
0ef43a5c
SH
219if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
220 chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
221fi
222if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
223 chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
224fi
225
226exit 0