You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.6 KiB
Bash
68 lines
1.6 KiB
Bash
#! /bin/bash
|
|
|
|
if [[ $# -lt 4 ]]; then
|
|
echo "Usage: $0 vm.opts port cmd files" >&2
|
|
exit 1
|
|
fi
|
|
|
|
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
fifo=fifo
|
|
# should set:
|
|
# username: ssh login user
|
|
# tmp_dir: temp dir inside vm
|
|
# netdev: id of the user netdev this script will create
|
|
# qemu: qemu executable name
|
|
# qemu_opts: additional qemu options (bash array)
|
|
# should probably contain: -enable-kvm -nodefaults -m -cpu -smp -rtc
|
|
# -drive, -device somehing-net, -incoming
|
|
# ssh_opts: bash array, `-o something` flags
|
|
# ssh_cmd_prefix: bash array, prefix the command executed inside ssh with it
|
|
# for example, with freesshd you need `cmd /c` here
|
|
# you have to modify this script if you don't want to use user networking
|
|
source "$1" || exit 1
|
|
shift
|
|
|
|
port="$1"
|
|
shift
|
|
cmd="$1"
|
|
shift
|
|
|
|
[[ -n $password ]] && run_ssh="sshpass -p $password"
|
|
ssh_opts+=(-o StrictHostKeyChecking=no)
|
|
|
|
function retry()
|
|
{
|
|
local i res
|
|
for ((i=0; i<5; ++i)); do
|
|
{ "$@"; res=$?; } || :
|
|
if [[ $res = 0 ]]; then return 0; fi
|
|
sleep $i
|
|
done
|
|
return $res
|
|
}
|
|
|
|
set -eEx
|
|
|
|
mkdir -p tmp
|
|
|
|
TMPDIR=tmp $qemu \
|
|
"${qemu_opts[@]}" \
|
|
-display none -monitor none \
|
|
-netdev "user,restrict=on,ipv6=off,id=$netdev,hostfwd=tcp:127.0.0.1:$port-:22" \
|
|
-daemonize -pidfile qemu.pid
|
|
|
|
retry $run_ssh scp "${ssh_opts[@]}" -P "$port" \
|
|
"$@" "$username@localhost:$tmp_dir/"
|
|
|
|
# the braindead windows server closes connection on EOF, so fake something
|
|
rm -f "$fifo"
|
|
mkfifo "$fifo"
|
|
sleep 1h > "$fifo" &
|
|
$run_ssh ssh "${ssh_opts[@]}" -p "$port" "$username@localhost" \
|
|
"${ssh_cmd_prefix[@]}" "cd ${tmp_dir//\//\\} && $cmd" < "$fifo"
|
|
kill %1
|
|
rm -rf "$fifo"
|
|
|
|
kill $(cat qemu.pid)
|