vmrun.sh (1689B)
1 #! /bin/bash 2 3 if [[ $# -lt 4 ]]; then 4 echo "Usage: $0 vm.opts port cmd files" >&2 5 exit 1 6 fi 7 8 dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 9 10 fifo=fifo 11 # should set: 12 # username: ssh login user 13 # tmp_dir: temp dir inside vm 14 # netdev: id of the user netdev this script will create 15 # qemu: qemu executable name 16 # qemu_opts: additional qemu options (bash array) 17 # should probably contain: -enable-kvm -nodefaults -m -cpu -smp -rtc 18 # -drive, -device somehing-net, -incoming 19 # ssh_opts: bash array, `-o something` flags 20 # ssh_cmd_prefix: bash array, prefix the command executed inside ssh with it 21 # for example, with freesshd you need `cmd /c` here 22 # you have to modify this script if you don't want to use user networking 23 source "$1" || exit 1 24 shift 25 26 port="$1" 27 shift 28 cmd="$1" 29 shift 30 31 [[ -n $password ]] && run_ssh="sshpass -p $password" 32 ssh_opts+=(-o StrictHostKeyChecking=no) 33 34 function retry() 35 { 36 local i res 37 for ((i=0; i<5; ++i)); do 38 { "$@"; res=$?; } || : 39 if [[ $res = 0 ]]; then return 0; fi 40 sleep $i 41 done 42 return $res 43 } 44 45 set -eEx 46 47 mkdir -p tmp 48 49 TMPDIR=tmp $qemu \ 50 "${qemu_opts[@]}" \ 51 -display none -monitor none \ 52 -netdev "user,restrict=on,ipv6=off,id=$netdev,hostfwd=tcp:127.0.0.1:$port-:22" \ 53 -daemonize -pidfile qemu.pid 54 55 retry $run_ssh scp "${ssh_opts[@]}" -P "$port" \ 56 "$@" "$username@localhost:$tmp_dir/" 57 58 # the braindead windows server closes connection on EOF, so fake something 59 rm -f "$fifo" 60 mkfifo "$fifo" 61 sleep 1h > "$fifo" & 62 $run_ssh ssh "${ssh_opts[@]}" -p "$port" "$username@localhost" \ 63 "${ssh_cmd_prefix[@]}" "cd ${tmp_dir//\//\\} && $cmd" < "$fifo" 64 kill %1 65 rm -rf "$fifo" 66 67 kill $(cat qemu.pid)