121 lines
2.5 KiB
Bash
Executable File
121 lines
2.5 KiB
Bash
Executable File
#! /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
|