This commit is contained in:
josephsmendoza
2022-11-04 17:21:23 +00:00
parent a75f138b10
commit d80152e9e4
4 changed files with 109 additions and 57 deletions
+20
View File
@@ -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.
-36
View File
@@ -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"
-21
View File
@@ -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"
Executable
+89
View File
@@ -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