From 07a7cc3166965c1e36ee0254a917c3e632b3de23 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Tue, 4 Apr 2023 18:56:17 +0200 Subject: first sketch --- .#archlinux.raw.lck | 0 README.md | 3 ++ spawnctl | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 .#archlinux.raw.lck create mode 100644 README.md create mode 100755 spawnctl diff --git a/.#archlinux.raw.lck b/.#archlinux.raw.lck new file mode 100644 index 0000000..e69de29 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 ' + echo ' create ' + echo ' start ' + echo ' enable ' + echo ' shell ' + echo ' reboot ' + echo ' poweroff ' + echo ' remove ' + echo ' update' + echo ' list' + echo ' help' + echo + echo ' storage ' + echo ' resize [+-]' + 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 -- cgit v1.2.3-70-g09d2