]> git.proxmox.com Git - mirror_lxc.git/blobdiff - templates/lxc-oci.in
Merge pull request #2055 from marcosps/cgfsng_debug
[mirror_lxc.git] / templates / lxc-oci.in
index 13decc74009ac3272a029404c222844c8d7d6688..daa4241f4af8ddd43059534852594737de00dd20 100755 (executable)
@@ -34,10 +34,17 @@ for bin in skopeo umoci jq; do
     fi
 done
 
+LOCALSTATEDIR="@LOCALSTATEDIR@"
+LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
+LXC_HOOK_DIR="@LXCHOOKDIR@"
+
 # Some useful functions
 cleanup() {
-    if [ -d "$DOWNLOAD_TEMP" ]; then
-        rm -Rf $DOWNLOAD_TEMP
+    if [ -d "${DOWNLOAD_TEMP}" ]; then
+        rm -Rf "${DOWNLOAD_TEMP}"
+    fi
+    if [ -d "${LXC_ROOTFS}.tmp" ]; then
+        rm -Rf "${LXC_ROOTFS}.tmp"
     fi
 }
 
@@ -58,7 +65,7 @@ getconfigpath() {
        basedir="$1"
        q="$2"
 
-       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'`
+       digest=`cat "${basedir}/index.json" | jq -c -r --arg q "$q" '.manifests[] | if .annotations."org.opencontainers.image.ref.name" == $q then .digest else empty end'`
        if [ -z "${digest}" ]; then
                echo "$q not found in index.json" >&2
                return
@@ -66,7 +73,7 @@ getconfigpath() {
 
        # Ok we have the image config digest, now get the config from that,
        d=${digest:7}
-       cdigest=`cat "${basedir}/blobs/sha256/${d}" | jq '.config.digest' | sed -e 's/"//g'`
+       cdigest=`cat "${basedir}/blobs/sha256/${d}" | jq -c -r '.config.digest'`
        if [ -z "${cdigest}" ]; then
                echo "container config not found" >&2
                return
@@ -86,22 +93,66 @@ getep() {
 
        configpath="$1"
 
-       ep=`cat "${configpath}" | jq -c '.config.Entrypoint' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
-       cmd=`cat "${configpath}" | jq -c '.config.Cmd' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
-       if [ "${ep}" = "null" ]; then
+       ep=`cat "${configpath}" | jq -c -r '.config.Entrypoint[]?'`
+       cmd=`cat "${configpath}" | jq -c -r '.config.Cmd[]?'`
+       if [ -z "${ep}" ]; then
                ep="${cmd}"
-               if [ "${ep}" = "null" ]; then
+               if [ -z "${ep}" ]; then
                        ep="/bin/sh"
                fi
-       elif [ "${cmd}" != "null" ]; then
+       elif [ -n "${cmd}" ]; then
                ep="${ep} ${cmd}"
        fi
 
-       if [ -z "${ep}" ]; then
-               echo "/bin/sh"
+       echo ${ep}
+       return
+}
+
+# get environment from oci image.
+getenv() {
+       if [ "$#" -eq 0 ]; then
                return
        fi
-       echo "${ep}"
+
+       configpath="$1"
+
+       env=`cat "${configpath}" | jq -c -r '.config.Env[]'`
+
+       echo "${env}"
+       return
+}
+
+# FIXME 1: only support numerical values in the configuration file.
+# FIXME 2: from the OCI image spec: "If group/gid is not specified,
+# the default group and supplementary groups of the given user/uid in
+# /etc/passwd from the container are applied."
+getuidgid() {
+       if [ "$#" -eq 0 ]; then
+               echo "0 0"
+               return
+       fi
+
+       configpath="$1"
+
+       uidgid=`cat "${configpath}" | jq -c -r '.config.User // "0:0"'`
+       uidgid=(${uidgid//:/ })
+
+       printf '%d %d' ${uidgid[0]:-0} ${uidgid[1]:-0} 2>/dev/null || true
+       return
+}
+
+# get cwd from oci image.
+getcwd() {
+       if [ "$#" -eq 0 ]; then
+               echo "/"
+               return
+       fi
+
+       configpath="$1"
+
+       cwd=`cat "${configpath}" | jq -c -r '.config.WorkingDir // "/"'`
+
+       echo "${cwd}"
        return
 }
 
@@ -115,6 +166,10 @@ Special arguments:
 Required arguments:
 [ -u | --url <url> ]: The OCI image URL
 
+Optional arguments:
+[ --username <username> ]: The username for the registry
+[ --password <password> ]: The password for the registry
+
 LXC internal arguments (do not pass manually!):
 [ --name <name> ]: The container name
 [ --path <path> ]: The path to the container
@@ -126,8 +181,8 @@ EOF
     return 0
 }
 
-options=$(getopt -o u:h -l help,url:,name:,path:,\
-rootfs:,mapped-uid:,mapped-gid: -- "$@")
+options=$(getopt -o u:h -l help,url:,username:,password:,no-cache,dhcp,\
+name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@")
 
 if [ $? -ne 0 ]; then
     usage
@@ -136,6 +191,11 @@ fi
 eval set -- "$options"
 
 OCI_URL=""
+OCI_USERNAME=
+OCI_PASSWORD=
+OCI_USE_CACHE="true"
+OCI_USE_DHCP="false"
+
 LXC_MAPPED_GID=
 LXC_MAPPED_UID=
 LXC_NAME=
@@ -146,6 +206,10 @@ while :; do
     case "$1" in
         -h|--help)          usage && exit 1;;
         -u|--url)           OCI_URL=$2; shift 2;;
+        --username)         OCI_USERNAME=$2; shift 2;;
+        --password)         OCI_PASSWORD=$2; shift 2;;
+        --no-cache)         OCI_USE_CACHE="false"; shift 1;;
+        --dhcp)             OCI_USE_DHCP="true"; shift 1;;
         --name)             LXC_NAME=$2; shift 2;;
         --path)             LXC_PATH=$2; shift 2;;
         --rootfs)           LXC_ROOTFS=$2; shift 2;;
@@ -166,38 +230,65 @@ if [ -z "$OCI_URL" ]; then
     exit 1
 fi
 
+if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
+    echo "ERROR: password given but no username specified"
+    exit 1
+fi
+
+if [ "${OCI_USE_CACHE}" = "true" ]; then
+    if ! skopeo copy --help | grep -q 'dest-shared-blob-dir'; then
+        echo "INFO: skopeo doesn't support blob caching"
+        OCI_USE_CACHE="false"
+    fi
+fi
+
 USERNS=$(in_userns)
 
-if [ "$USERNS" != "no" ]; then
+if [ "$USERNS" = "yes" ]; then
+    if [ -z "$LXC_MAPPED_UID" ] || [ "$LXC_MAPPED_UID" = "-1" ]; then
+        echo "ERROR: In a user namespace without a map." 1>&2
+        exit 1
+    fi
+fi
+
+if [ "${OCI_USE_CACHE}" = "true" ]; then
     if [ "$USERNS" = "yes" ]; then
-        if [ -z "$LXC_MAPPED_UID" ] || [ "$LXC_MAPPED_UID" = "-1" ]; then
-            echo "ERROR: In a user namespace without a map." 1>&2
-            exit 1
-        fi
-        DOWNLOAD_MODE="user"
-        DOWNLOAD_TARGET="user"
+        DOWNLOAD_BASE="${HOME}/.cache/lxc"
     else
-        DOWNLOAD_MODE="user"
-        DOWNLOAD_TARGET="system"
+        DOWNLOAD_BASE="${LOCALSTATEDIR}/cache/lxc"
     fi
+else
+    DOWNLOAD_BASE=/tmp
 fi
 
 # Trap all exit signals
 trap cleanup EXIT HUP INT TERM
 
 if ! type mktemp >/dev/null 2>&1; then
-    DOWNLOAD_TEMP=/tmp/lxc-oci.$$
+    DOWNLOAD_TEMP="${DOWNLOAD_BASE}/lxc-oci.$$"
     mkdir -p $DOWNLOAD_TEMP
 else
-    DOWNLOAD_TEMP=$(mktemp -d)
+    DOWNLOAD_TEMP=$(mktemp -d -p "${DOWNLOAD_BASE}")
 fi
 
-# Download the image - TODO - cache
-skopeo copy "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
+# Download the image
+skopeo_args=("")
+if [ -n "$OCI_USERNAME" ]; then
+    CREDENTIALS="${OCI_USERNAME}"
+    if [ -n "$OCI_PASSWORD" ]; then
+        CREDENTIALS="${CREDENTIALS}:${OCI_PASSWORD}"
+    fi
+    skopeo_args+=(--src-creds "${CREDENTIALS}")
+fi
+if [ "${OCI_USE_CACHE}" = "true" ]; then
+    skopeo_args+=(--dest-shared-blob-dir "${DOWNLOAD_BASE}")
+    skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
+    ln -s "${DOWNLOAD_BASE}/sha256" "${DOWNLOAD_TEMP}/blobs/sha256"
+else
+    skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
+fi
 
-# Unpack the rootfs
 echo "Unpacking the rootfs"
-
 umoci_args=("")
 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
     umoci_args+=(--rootless)
@@ -205,7 +296,6 @@ fi
 umoci unpack ${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp"
 rmdir "${LXC_ROOTFS}"
 mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}"
-rm -rf "${LXC_ROOTFS}.tmp"
 
 OCI_CONF_FILE=$(getconfigpath ${DOWNLOAD_TEMP} latest)
 LXC_CONF_FILE="${LXC_PATH}/config"
@@ -213,6 +303,28 @@ entrypoint=$(getep ${OCI_CONF_FILE})
 echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}"
 echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}"
 
+environment=$(getenv ${OCI_CONF_FILE})
+while read -r line; do
+    echo "lxc.environment = ${line}" >> "${LXC_CONF_FILE}"
+done <<< "${environment}"
+
+if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf" ]; then
+    echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/common.conf" >> "${LXC_CONF_FILE}"
+fi
+
+if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf" ]; then
+    echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/userns.conf" >> "${LXC_CONF_FILE}"
+fi
+
+if [ -e "${LXC_TEMPLATE_CONFIG}/oci.common.conf" ]; then
+    echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/oci.common.conf" >> "${LXC_CONF_FILE}"
+fi
+
+if [ "${OCI_USE_DHCP}" = "true" ]; then
+    echo "lxc.hook.start-host = ${LXC_HOOK_DIR}/dhclient" >> "${LXC_CONF_FILE}"
+    echo "lxc.hook.stop = ${LXC_HOOK_DIR}/dhclient" >> "${LXC_CONF_FILE}"
+fi
+
 echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_CONF_FILE}"
 # set the hostname
 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
@@ -223,8 +335,20 @@ EOF
 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
 127.0.0.1   localhost
 127.0.1.1   ${LXC_NAME}
+::1     localhost ip6-localhost ip6-loopback
+fe00::0 ip6-localnet
+ff00::0 ip6-mcastprefix
+ff02::1 ip6-allnodes
+ff02::2 ip6-allrouters
 EOF
 
+uidgid=($(getuidgid ${OCI_CONF_FILE}))
+echo "lxc.init.uid = ${uidgid[0]}" >> "${LXC_CONF_FILE}"
+echo "lxc.init.gid = ${uidgid[1]}" >> "${LXC_CONF_FILE}"
+
+cwd=$(getcwd ${OCI_CONF_FILE})
+echo "lxc.init.cwd = ${cwd}" >> "${LXC_CONF_FILE}"
+
 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
     chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
 fi