From 99be1f49afe75fdc6e0fc370b74fcda78c1f81ec Mon Sep 17 00:00:00 2001 From: selfhoster1312 Date: Sun, 23 Apr 2023 21:35:25 +0200 Subject: [PATCH] Add "--copy SRC DEST" param, allow to pass more arguments to the container command --- bin/mkvm.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/bin/mkvm.sh b/bin/mkvm.sh index a988547..a41ae1f 100755 --- a/bin/mkvm.sh +++ b/bin/mkvm.sh @@ -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"