From b4b0ad7a22cbea014f24f85132590ff1bdf749f1 Mon Sep 17 00:00:00 2001 From: Stephan Maka Date: Sat, 13 Apr 2019 23:33:31 +0200 Subject: [PATCH] windows tests: move qemu/ssh utils into a shared file --- artiq/windows/default.nix | 38 +++++++------------------------- artiq/windows/install.nix | 46 ++++++++++----------------------------- artiq/windows/qemu.nix | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 65 deletions(-) create mode 100644 artiq/windows/qemu.nix diff --git a/artiq/windows/default.nix b/artiq/windows/default.nix index 1189642..2be7633 100644 --- a/artiq/windows/default.nix +++ b/artiq/windows/default.nix @@ -8,34 +8,12 @@ with pkgs; let - qemu = qemu_kvm; - runQemu = extraArgs: - let - args = [ - "-enable-kvm" - "-m" qemuMem - "-display" "none" - "-bios" "${OVMF.fd}/FV/OVMF.fd" - "-netdev" "user,id=n1,restrict=on,hostfwd=tcp::2022-:22" "-device" "e1000,netdev=n1" - ]; - argStr = builtins.concatStringsSep " " (args ++ extraArgs); - in "qemu-system-x86_64 ${argStr}"; - sshUser = "user"; - sshPassword = "user"; - sshOpts = "-o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=$TMPDIR/known_hosts"; - ssh = cmd: '' - echo "ssh windows \"${cmd}\"" - ${sshpass}/bin/sshpass -p${sshPassword} -- \ - ${openssh}/bin/ssh -np 2022 ${sshOpts} \ - ${sshUser}@localhost \ - "${cmd}" - ''; - scp = src: target: '' - echo "Copy ${src} to ${target}" - ${sshpass}/bin/sshpass -p${sshPassword} -- \ - ${openssh}/bin/scp -P 2022 ${sshOpts} \ - "${src}" "${sshUser}@localhost:${target}" - ''; + qemu = import ./qemu.nix { + inherit pkgs qemuMem; + diskImage = "c.img"; + }; + ssh = qemu.ssh; + scp = qemu.scp; condaEnv = "artiq-env"; in stdenv.mkDerivation { @@ -46,11 +24,11 @@ in mkdir $out ''; doCheck = true; - checkInputs = [ qemu sshpass openssh ]; + checkInputs = qemu.inputs; checkPhase = '' # +1 day from last modification of the disk image CLOCK=$(date -Is -d @$(expr $(stat -c %Y ${diskImage}) + 86400)) - ${runQemu [ + ${qemu.runQemu [ "-boot" "order=c" "-snapshot" "-drive" "file=${diskImage},index=0,media=disk,cache=unsafe" diff --git a/artiq/windows/install.nix b/artiq/windows/install.nix index f2352ca..2b5a20c 100644 --- a/artiq/windows/install.nix +++ b/artiq/windows/install.nix @@ -16,34 +16,14 @@ let sha256 = "1kyf03571fhxd0a9f8bcpmqfdpw7461kclfyb4yb3dsi783y4sck"; }; - qemu = qemu_kvm; - runQemu = extraArgs: - let - args = [ - "-enable-kvm" - "-m" qemuMem - "-bios" "${OVMF.fd}/FV/OVMF.fd" - "-netdev" "user,id=n1,hostfwd=tcp::2022-:22" "-device" "e1000,netdev=n1" - ]; - argStr = lib.escapeShellArgs (args ++ extraArgs); - in "${qemu}/bin/qemu-system-x86_64 ${argStr}"; + qemu = import ./qemu.nix { + inherit pkgs qemuMem; + diskImage = "c.img"; + }; + # Double-escape because we produce a script from a shell heredoc + ssh = cmd: qemu.ssh (qemu.escape cmd); + scp = qemu.scp; - sshUser = "user"; - sshPassword = "user"; - sshOpts = "-o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=\\$TMPDIR/known_hosts"; - ssh = cmd: '' - echo "ssh windows \"${cmd}\"" - ${sshpass}/bin/sshpass -p${sshPassword} -- \ - ${openssh}/bin/ssh -np 2022 ${sshOpts} \ - ${sshUser}@localhost \ - "${cmd}" - ''; - scp = src: target: '' - echo "Copy ${src} to ${target}" - ${sshpass}/bin/sshpass -p${sshPassword} -- \ - ${openssh}/bin/scp -P 2022 ${sshOpts} \ - "${src}" "${sshUser}@localhost:${target}" - ''; condaEnv = "artiq-env"; condaDepSpecs = builtins.concatStringsSep " " @@ -61,7 +41,7 @@ stdenv.mkDerivation { unpackCmd = '' ln -s $curSrc windows.iso ''; - buildInputs = [ qemu openssh sshpass ]; + buildInputs = qemu.inputs; dontBuild = true; installPhase = '' mkdir -p $out/bin $out/data @@ -70,11 +50,9 @@ stdenv.mkDerivation { #!/usr/bin/env bash set -e -m - TMPDIR=\$(mktemp -d) - if [ ! -f c.img ] ; then - ${qemu}/bin/qemu-img create -f qcow2 c.img ${diskImageSize} - ${runQemu [ + ${qemu.qemu-img} create -f qcow2 c.img ${diskImageSize} + ${qemu.runQemu [ "-boot" "order=d" "-drive" "file=c.img,index=0,media=disk,cache=unsafe" "-drive" "file=$out/data/windows.iso,index=1,media=cdrom,cache=unsafe" @@ -82,7 +60,7 @@ stdenv.mkDerivation { echo "Please perform a Windows installation." else echo "Please finalize your Windows installation (or delete c.img and restart)" - ${runQemu [ + ${qemu.runQemu [ "-boot" "order=c" "-drive" "file=c.img,index=0,media=disk" ]} & @@ -106,8 +84,6 @@ stdenv.mkDerivation { echo "Waiting for qemu exit" wait - - rm -rf \$TMPDIR EOF chmod a+x $out/bin/networked-installer.sh ''; diff --git a/artiq/windows/qemu.nix b/artiq/windows/qemu.nix new file mode 100644 index 0000000..e75ddac --- /dev/null +++ b/artiq/windows/qemu.nix @@ -0,0 +1,45 @@ +{ pkgs, + diskImage, + qemuMem, + sshUser ? "user", + sshPassword ? "user", +}: + +with pkgs; + +let + qemu = qemu_kvm; + qemu-img = "${qemu}/bin/qemu-img"; + runQemu = extraArgs: + let + args = [ + "-enable-kvm" + "-m" qemuMem + "-display" "none" + "-bios" "${OVMF.fd}/FV/OVMF.fd" + "-netdev" "user,id=n1,restrict=on,hostfwd=tcp::2022-:22" "-device" "e1000,netdev=n1" + ]; + argStr = builtins.concatStringsSep " " (args ++ extraArgs); + in "qemu-system-x86_64 ${argStr}"; + + escape = builtins.replaceStrings [ "\\" ] [ "\\\\" ]; + sshOpts = "-o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=/tmp/known_hosts"; + ssh = cmd: '' + echo ssh windows '${escape cmd}' + ${sshpass}/bin/sshpass -p${sshPassword} -- \ + ${openssh}/bin/ssh -np 2022 ${sshOpts} \ + ${sshUser}@localhost \ + '${escape cmd}' + ''; + scp = src: target: '' + echo "Copy ${src} to ${target}" + ${sshpass}/bin/sshpass -p${sshPassword} -- \ + ${openssh}/bin/scp -P 2022 ${sshOpts} \ + "${src}" "${sshUser}@localhost:${target}" + ''; + +in +{ + inherit qemu-img runQemu escape ssh scp; + inputs = [ qemu openssh sshpass ]; +}