Add "--copy SRC DEST" param, allow to pass more arguments to the container command

This commit is contained in:
selfhoster selfhoster 2023-04-23 21:35:25 +02:00
parent 0648d481bc
commit 99be1f49af

View File

@ -5,19 +5,58 @@ KEEP=0
SYSDIR="/ramdisk/sys"
TMPDIR="/ramdisk/tmp"
help() {
echo "testvm.sh DISTRO [CMD] [ARGS]"
echo " -k|--keep: Don't destroy the VM after running"
echo " --copy PATHSRC PATHDEST: copy PATHSRC into the container as PATHDEST"
}
if [ $# -lt 2 ]; then
help
exit 0
fi
# Files to copy into container
COPY=()
COPYDEST=()
while true; do
case "$1" in
"-h"|"--help")
echo "testvm.sh DISTRO [CMD]"
echo " -k|--keep: Don't destroy the VM after running"
help
exit 0
;;
"-k"|"--keep")
KEEP=1
shift
;;
"--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
TYPE="$1"
TEMPLATE=/var/lib/lxc/"$TYPE".template
if [ ! -f "$TEMPLATE" ]; then
@ -36,10 +75,23 @@ ROOTFS="$TMPDIR"/"$VMNAME"
mkdir -p "$ROOTFS"/etc/
echo "$VMNAME" > "$ROOTFS"/etc/hostname
CMD="$2"
# Copy files into the VM
for fileIndex in ${!COPY[@]}; do
PATHSRC="${COPY[$fileIndex]}"
PATHDEST="$ROOTFS""${COPYDEST[$fileIndex]}"
# Create parent dirs
#DESTPATH="$(realpath "$file")"
#DESTPATH="$ROOTFS""$DESTPATH"
PARENTPATHDEST="$(dirname "$PATHDEST")"
mkdir -p "$PARENTPATHDEST"
cp -r "$PATHSRC" "$PATHDEST"
done
CMD="$1"
shift
if [[ "$CMD" != "" ]]; then
lxc-execute -n "$VMNAME" -- "$CMD"
lxc-execute -n "$VMNAME" -- "$CMD" "$@"
[ $KEEP -eq 0 ] && lxc-destroy -n "$VMNAME"
else
lxc-start -n "$VMNAME"