lxc-ramdisk/bin/mkvm.sh

50 lines
865 B
Bash
Raw Normal View History

2023-04-10 20:33:27 +02:00
#! /usr/bin/env bash
KEEP=0
SYSDIR="/ramdisk/sys"
TMPDIR="/ramdisk/tmp"
case "$1" in
"-h"|"--help")
echo "testvm.sh DISTRO [CMD]"
echo " -k|--keep: Don't destroy the VM after running"
exit 0
;;
"-k"|"--keep")
KEEP=1
shift
;;
esac
TYPE="$1"
TEMPLATE=/var/lib/lxc/"$TYPE".template
if [ ! -f "$TEMPLATE" ]; then
echo "ERROR: No such VM type "$TYPE""
exit 1
fi
export VMNAME="$TYPE-$RANDOM"
echo "$VMNAME"
LXCDIR=/var/lib/lxc/"$VMNAME"
mkdir "$LXCDIR"
envsubst < "$TEMPLATE" > "$LXCDIR"/config
ROOTFS="$TMPDIR"/"$VMNAME"
mkdir -p "$ROOTFS"/etc/
echo "$VMNAME" > "$ROOTFS"/etc/hostname
CMD="$2"
if [[ "$CMD" != "" ]]; then
lxc-execute -n "$VMNAME" -- "$CMD"
[ $KEEP -eq 0 ] && lxc-destroy -n "$VMNAME"
else
lxc-start -n "$VMNAME"
lxc-attach -n "$VMNAME" bash
[ $KEEP -eq 0 ] && lxc-stop --kill "$VMNAME" && lxc-destroy -n "$VMNAME"
fi