Add editvm.sh TEMPLATE CMD command

This commit is contained in:
selfhoster selfhoster 2023-06-06 12:28:58 +02:00
parent 6d81507a99
commit 787a678a79
6 changed files with 139 additions and 2 deletions

View File

@ -67,6 +67,18 @@ The `mkvm.sh` script takes the type of container to run as first argument. That
Unless the `--keep` argument is passed, the container is also destroyed after running.
## Edit the template filesystem
If you need to change a template's rootfs, for running updates or adding packages, you can use the `editvm.sh` script:
```
editvm.sh archlinux bash
```
This will fail if the ramdisk service is still running, so first stop all your containers, stop the ramdisk service, and try again. Don't forget to restart the ramdisk service after you're done.
If the LXC you want to use does not use a rootfs with its own name in the `/ramdisk/persist/` folder, the script will also error out.
## License
GNU aGPL v3

120
bin/editvm.sh Executable file
View File

@ -0,0 +1,120 @@
#! /usr/bin/env bash
help() {
echo "editvm.sh DISTRO [CMD] [ARGS]"
echo " Stops the ramdisk and start editing a VM template"
echo " --copy PATHSRC PATHDEST: copy PATHSRC into the container as PATHDEST"
}
KEEP=0
SYSDIR="/ramdisk/sys"
TMPDIR="/ramdisk/tmp"
if [ $# -lt 2 ]; then
help
exit 0
fi
# Files to copy into container
COPY=()
COPYDEST=()
while true; do
case "$1" in
"-h"|"--help")
help
exit 0
;;
"--copy")
shift
PATHSRC="$1"
if [ ! -e "$PATHSRC" ]; then
echo "COPY ERROR: File/Folder "$PATHSRC" does not exist."
exit 1
fi
shift
PATHDEST="$1"
COPY+=("$PATHSRC")
COPYDEST+=("$PATHDEST")
shift
;;
*)
# Optional args processing finished
# If no first argument is passed, syntax error
if [[ "$1" = "" ]]; then
help
exit 1
fi
TYPE="$1"
shift
break
;;
esac
done
if systemctl is-active ramdisk; then
echo "ERROR: Please stop ramdisk.service first"
exit 1
fi
TEMPLATE=/var/lib/lxc/"$TYPE".template
if [ ! -f "$TEMPLATE" ]; then
echo "ERROR: No such VM type "$TYPE""
exit 1
fi
export VMNAME="$TYPE-TEMPLATE"
echo "$VMNAME"
export LXCROOTFS=/ramdisk/persist/$TYPE
# Check if the rootfs exists... maybe the template actually calls another rootfs
if [ ! -d "$LXCROOTFS" ]; then
echo "ERROR: The container template "$TYPE" does not have its own rootfs. Maybe it's using another rootfs?"
echo "Check the lxc.rootfs.path line in "$TEMPLATE" file to be sure."
exit 1
fi
LXCDIR=/var/lib/lxc/"$VMNAME"
mkdir "$LXCDIR"
envsubst < "$TEMPLATE" > "$LXCDIR"/config
ROOTFS="$LXCROOTFS"
echo "template" > "$ROOTFS"/etc/hostname
# Copy files into the VM
for fileIndex in ${!COPY[@]}; do
PATHSRC="${COPY[$fileIndex]}"
PATHDEST="$ROOTFS""${COPYDEST[$fileIndex]}"
# Create parent dirs
PARENTPATHDEST="$(dirname "$PATHDEST")"
mkdir -p "$PARENTPATHDEST"
cp --dereference -r "$PATHSRC" "$PATHDEST"
done
CMD="$1"
shift
if [[ "$CMD" != "" ]]; then
#lxc-execute -n "$VMNAME" -- "$CMD" "$@"
lxc-start -n "$VMNAME"
lxc-attach -n "$VMNAME" -- "$CMD" "$@"
if [ $KEEP -eq 0 ]; then
lxc-stop --kill "$VMNAME"
# Don't destroy VM because we want to keep the rootfs
# (we are just editing it live)
# lxc-destroy -n "$VMNAME"
rm -R /var/lib/lxc/"$VMNAME"
fi
else
lxc-start -n "$VMNAME"
lxc-attach -n "$VMNAME" bash
if [ $KEEP -eq 0 ]; then
lxc-stop --kill "$VMNAME"
# Don't destroy VM because we want to keep the rootfs
# (we are just editing it live)
#lxc-destroy -n "$VMNAME"
rm -R /var/lib/lxc/"$VMNAME"
fi
fi

View File

@ -67,6 +67,8 @@ fi
export VMNAME="$TYPE-$RANDOM"
echo "$VMNAME"
export LXCROOTFS="overlayfs:/ramdisk/sys/$TYPE:/ramdisk/tmp/$VMNAME"
echo "$LXCROOTFS"
LXCDIR=/var/lib/lxc/"$VMNAME"
mkdir "$LXCDIR"
envsubst < "$TEMPLATE" > "$LXCDIR"/config

View File

@ -9,6 +9,7 @@ mkdir -p /ramdisk/persist
ln -sf "$(pwd)"/bin/ramdisk.sh /usr/sbin/
ln -sf "$(pwd)"/bin/mkvm.sh /usr/sbin/
ln -sf "$(pwd)"/bin/editvm.sh /usr/sbin/
ln -sf "$(pwd)"/ramdisk.service /etc/systemd/system/
systemctl daemon-reload

View File

@ -12,7 +12,8 @@ lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.apparmor.profile = generated
lxc.apparmor.allow_nesting = 1
lxc.rootfs.path = overlayfs:/ramdisk/sys/archlinux:/ramdisk/tmp/$VMNAME
# Use $LXCROOTFS variable which is different between mkvm.sh/editvm.sh
lxc.rootfs.path = $LXCROOTFS
# Common configuration
lxc.include = /usr/share/lxc/config/archlinux.common.conf

View File

@ -12,7 +12,8 @@ lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.apparmor.profile = generated
lxc.apparmor.allow_nesting = 1
lxc.rootfs.path = overlayfs:/ramdisk/sys/bullseye:/ramdisk/tmp/$VMNAME
# Use $LXCROOTFS variable which is different between mkvm.sh/editvm.sh
lxc.rootfs.path = $LXCROOTFS
# Common configuration
lxc.include = /usr/share/lxc/config/debian.common.conf