From d80152e9e4bca2c88dd55f8e48a12f2ba2e17250 Mon Sep 17 00:00:00 2001 From: josephsmendoza Date: Fri, 4 Nov 2022 17:21:23 +0000 Subject: [PATCH] polish --- README.md | 20 +++++++++ machinectl-oci-import.sh | 36 ---------------- nspawn-oci-import.sh | 21 ---------- oci-import.sh | 89 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 57 deletions(-) create mode 100644 README.md delete mode 100644 machinectl-oci-import.sh delete mode 100644 nspawn-oci-import.sh create mode 100755 oci-import.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..665e48a --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# NSpawn-OCI +Script to import OCI (Docker) containers to systemd-nspawn +## Usage +```bash +import.sh $SOURCE $HOSTNAME [--convert|-c] +``` +`$SOURCE` can be any supported [container transport](https://github.com/containers/image/blob/main/docs/containers-transports.5.md) + +`$HOSTNAME` may consist of only letters, digits, and the hyphen (`-`) symbol, and has a maximum length of 15 characters. + +`--convert` will attempt to convert the container from OCI to nspawn with the following caveats: +- the nspawn container uses host networking. If you wish to use bridged or nat, there are many ways to do so, and you will need to decide for yourself. +- a small subset of common options are converted, including: + 1. Environment Variables + 2. Working Directory + 3. Process Parameters + 4. Bind Mounts + - when a container has declared a mount point, you will be prompted for the host bind point + +After importing, you may need to edit either `config.json` or `name.nspawn`. The appropriate config path will be printed at the end of the script. Keep in mind when using converted containers that guest networking is likely to be completely unconfigured, so if you want to change from host networking mode to use port mapping or bridge networking you will need to configure both a bridge interface on the host and guest networking inside each container. \ No newline at end of file diff --git a/machinectl-oci-import.sh b/machinectl-oci-import.sh deleted file mode 100644 index 55f8561..0000000 --- a/machinectl-oci-import.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail -mkdir -p /run/nspawn-oci - -skopeo_output=$(echo /run/nspawn-oci/$1 | sed s.://./.g) -mkdir -p $skopeo_output -skopeo copy $1 oci:$skopeo_output:latest - -umoci_output=/run/nspawn-oci/$2 -rm -rf $umoci_output -umoci --verbose unpack --image $skopeo_output $umoci_output - -mkdir -p /etc/systemd/nspawn -oci_config=$umoci_output/config.json -nspawn_config=/etc/systemd/nspawn/$2.nspawn - -cat << end > $nspawn_config -[Exec] -ProcessTwo=true -end - -for environment in $(jq --compact-output '.process.env' $oci_config | sed "s/[][]//g;s/\"//g;s/,/ /g") -do -echo $environment -cat << end >> $nspawn_config -Environment=$environment -end -done - -cat << end >> $nspawn_config -WorkingDirectory=$(jq --compact-output '.process.cwd' $oci_config | sed "s/[][]//g;s/\"//g;s/,/ /g") -Parameters=$(jq --compact-output '.process.args' $oci_config | sed "s/[][]//g;s/,/ /g") -end - -machinectl import-fs $umoci_output/rootfs $2 -echo "$2 created from $1. start/enable via systemd-nspawn@$2.service" \ No newline at end of file diff --git a/nspawn-oci-import.sh b/nspawn-oci-import.sh deleted file mode 100644 index 6ec6b0a..0000000 --- a/nspawn-oci-import.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail -mkdir -p /run/nspawn-oci - -skopeo_output=$(echo /run/nspawn-oci/$1 | sed s.://./.g) -mkdir -p $skopeo_output -skopeo copy $1 oci:$skopeo_output:latest - -umoci_output=/run/nspawn-oci/$2 -rm -rf $umoci_output -umoci --verbose unpack --image $skopeo_output $umoci_output - -mkdir -p /etc/systemd/system/systemd-nspawn@$2.service.d/ -override=/etc/systemd/system/systemd-nspawn@$2.service.d/override.conf -cat << end > $override -[Service] -ExecStart=systemd-nspawn --oci-bundle=/var/lib/machines/%i --machine %i -end - -machinectl import-fs $umoci_output $2 -echo "$2 created from $1. start/enable via systemd-nspawn@$2.service" \ No newline at end of file diff --git a/oci-import.sh b/oci-import.sh new file mode 100755 index 0000000..5ea89de --- /dev/null +++ b/oci-import.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +set -euo pipefail + +source=$1 +hostname=$2 +if [[ -v 3 ]];then +convert=true;else +convert=false;fi + +cat << end +source=$source +hostname=$hostname +convert=$convert +end + +mkdir -p /run/nspawn-oci + +skopeo_output=$(echo /run/nspawn-oci/$source | sed s.://./.g) +#mkdir -p $skopeo_output +#skopeo copy $source oci:$skopeo_output:latest + +umoci_output=/run/nspawn-oci/$hostname +#rm -rf $umoci_output +#umoci --verbose unpack --image $skopeo_output $umoci_output + +oci_config=$umoci_output/config.json + +if $convert;then +# nspawn mode +mkdir -p /etc/systemd/nspawn +nspawn_config=/etc/systemd/nspawn/$hostname.nspawn + +cat << end > $nspawn_config +[Exec] +ProcessTwo=true +end + +for environment in $(jq --compact-output '.process.env' $oci_config | sed "s/[][]//g;s/\"//g;s/,/ /g") +do +echo $environment +cat << end >> $nspawn_config +Environment=$environment +end +done + +cat << end >> $nspawn_config +WorkingDirectory=$(jq --compact-output '.process.cwd' $oci_config | sed "s/[][]//g;s/\"//g;s/,/ /g") +Parameters=$(jq --compact-output '.process.args' $oci_config | sed "s/[][]//g;s/,/ /g") + +[Network] +VirtualEthernet=no + +[Files] +end + +for mount in $(jq -c '.mounts[]' $oci_config); do +source=$(echo $mount | jq -r '.source' ) +echo $source +if [ $source == 'none' ]; then +echo 'container requires bind mount' +destination=$(echo $mount | jq -r '.destination') +read -p "$destination:" source +cat << end >> $nspawn_config +Bind=$source:$destination +end +fi +done + +machinectl import-fs $umoci_output/rootfs $2 +echo "Edit $nspawn_config before starting" + +else +# oci mode +mkdir -p /etc/systemd/system/systemd-nspawn@$hostname.service.d/ +override=/etc/systemd/system/systemd-nspawn@$hostname.service.d/override.conf +cat << end > $override +[Service] +ExecStart=systemd-nspawn --oci-bundle=/var/lib/machines/%i --machine %i +end + +machinectl import-fs $umoci_output $hostname +echo "Edit /var/lib/machines/$hostname/config.json before starting" + +fi + +cat << end +Start/Enable with systemd-nspawn@$hostname.service +$hostname created from $source +end \ No newline at end of file