diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-04-04 18:56:17 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-04-04 18:56:17 +0200 |
| commit | 07a7cc3166965c1e36ee0254a917c3e632b3de23 (patch) | |
| tree | 1d2135615236d5690cbd0e9e5a0803ff5517fa20 | |
first sketch
| -rw-r--r-- | .#archlinux.raw.lck | 0 | ||||
| -rw-r--r-- | README.md | 3 | ||||
| -rwxr-xr-x | spawnctl | 136 |
3 files changed, 139 insertions, 0 deletions
diff --git a/.#archlinux.raw.lck b/.#archlinux.raw.lck new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/.#archlinux.raw.lck diff --git a/README.md b/README.md new file mode 100644 index 0000000..d64658a --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# SpawnCTL + +This script is a script to control your systemd containers. diff --git a/spawnctl b/spawnctl new file mode 100755 index 0000000..bf02180 --- /dev/null +++ b/spawnctl @@ -0,0 +1,136 @@ +#!/bin/sh + + +export IMAGE_DIR='~/.cache/images' +export MACHINE_DIR='/var/lib/machines' + +mkdir -p "$IMAGE_DIR" + +help() { + echo 'Usage: spawnctl' + echo 'Manage container and their usage.' + echo + echo ' init <archlinux|debian>' + echo ' create <archlinux|debian> <name>' + echo ' start <name>' + echo ' enable <name>' + echo ' shell <name>' + echo ' reboot <name>' + echo ' poweroff <name>' + echo ' remove <name>' + echo ' update' + echo ' list' + echo ' help' + echo + echo ' storage <name>' + echo ' resize [+-]<num><unit>' + echo ' usage' + echo +} + + +init() { + mkdir -p /tmp/spawnctl-image-build/ + qemu-img create "$IMAGE_DIR/$1.template" 4G + mkfs.ext4 "$IMAGE_DIR/$1.template" + mount "$IMAGE_DIR/$1.template" /tmp/spawnctl-image-build/ + case "$1" in + "archlinux") + pacstrap -K -c /tmp/spawnctl-image-build/ + systemd-nspawn -D /tmp/spawnctl-image-build/ systemctl enable systemd-networkd systemd-resolved + killall gpg-agent + ;; + "debian") debootstrap --include=dbus-broker,systemd-container --components=main,universe stable /tmp/spawnctl-image-build/ https://deb.debian.org/debian;; + *) rm "$IMAGE_DIR/$1.template";; + esac + umount /tmp/spawnctl-image-build/ + rmdir /tmp/spawnctl-image-build/ +} + + +update() { + machinectl list | grep container | while read container; do + cmd="" + case "$(echo "$container" | awk '{ print $4 }')" in + "arch") cmd="pacman -Syyu";; + "debian") cmd="apt update && apt upgrade";; + esac + systemd-run -M "$(echo "$container" | awk '{ print $1 }')" "$cmd" + done +} + + +create() { + [ ! -e "$IMAGE_DIR/$1.template" ] && echo "Run first 'spawnctl init $1'" && exit + machinectl import-raw "$IMAGE_DIR/$1.template" "$2" +} + + +start() { + machinectl start "$1" +} + + +enable() { + machinectl enable "$1" +} + + +shell() { + machinectl shell "$1" +} + + +reboot() { + machinectl reboot "$1" +} + + +poweroff() { + machinectl poweroff "$1" +} + + +remove() { + poweroff "$1" + machinectl remove "$1" +} + + +list() { + machinectl list-images +} + + +storage() { + if [ "$1" = "resize" ]; then + qemu-img resize -f raw "$MACHINE_DIR/$2.raw" "$3" + e2fsck -f "$MACHINE_DIR/$2.raw" + resize2fs "$MACHINE_DIR/$2.raw" + else + printf "Storage of $1: %s\n" "$(ls -lh "$MACHINE_DIR/$1.raw" | awk '{ print $5 }')" + fi +} + +cmd=$1 +[ "$#" != "0" ] && shift 1 + +case "$cmd" in + "init") init $@;; + "update") update;; + "create") create $@;; + "start") start $@;; + "enable") enable $@;; + "shell") shell $@;; + "reboot") reboot $@;; + "poweroff") poweroff $@;; + "remove") remove $@;; + "list") list $@;; + "storage") storage $@;; + "help") help;; + "") help;; + *) + echo "Error: unknown command '$cmd'" + echo + help;; +esac |