50 lines
865 B
Bash
Executable File
50 lines
865 B
Bash
Executable File
#! /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
|
|
|