2014-02-15 22:23:47 +00:00
|
|
|
{ sshKey
|
|
|
|
, qemuArgs ? []
|
|
|
|
, command ? "sync"
|
|
|
|
, suspendTo ? null
|
|
|
|
, resumeFrom ? null
|
|
|
|
, installMode ? false
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (import <nixpkgs> {}) lib stdenv writeScript vmTools makeInitrd;
|
2014-02-16 15:53:02 +00:00
|
|
|
inherit (import <nixpkgs> {}) samba vde2 openssh socat netcat coreutils gzip;
|
2014-02-15 22:23:47 +00:00
|
|
|
|
|
|
|
preInitScript = writeScript "preinit.sh" ''
|
|
|
|
#!${vmTools.initrdUtils}/bin/ash -e
|
|
|
|
export PATH=${vmTools.initrdUtils}/bin
|
|
|
|
mount -t proc none /proc
|
|
|
|
mount -t sysfs none /sys
|
|
|
|
for arg in $(cat /proc/cmdline); do
|
|
|
|
if [ "x''${arg#command=}" != "x$arg" ]; then
|
|
|
|
command="''${arg#command=}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
for i in $(cat ${modulesClosure}/insmod-list); do
|
|
|
|
insmod $i
|
|
|
|
done
|
|
|
|
|
2014-02-16 15:53:02 +00:00
|
|
|
mkdir -p /dev /fs
|
|
|
|
|
|
|
|
mount -t tmpfs none /dev
|
2014-02-15 22:23:47 +00:00
|
|
|
mknod /dev/null c 1 3
|
|
|
|
mknod /dev/zero c 1 5
|
|
|
|
mknod /dev/random c 1 8
|
|
|
|
mknod /dev/urandom c 1 9
|
|
|
|
mknod /dev/tty c 5 0
|
|
|
|
|
|
|
|
ifconfig lo up
|
|
|
|
ifconfig eth0 up 192.168.0.2
|
|
|
|
|
2014-02-16 15:53:02 +00:00
|
|
|
mount -t tmpfs none /fs
|
|
|
|
mkdir -p /fs/nix/store /fs/xchg /fs/dev /fs/sys /fs/proc /fs/etc /fs/tmp
|
2014-02-15 22:23:47 +00:00
|
|
|
|
2014-02-16 15:53:02 +00:00
|
|
|
mount -o bind /dev /fs/dev
|
|
|
|
mount -t sysfs none /fs/sys
|
|
|
|
mount -t proc none /fs/proc
|
2014-02-15 22:23:47 +00:00
|
|
|
|
2014-02-16 02:19:40 +00:00
|
|
|
mount -t 9p \
|
|
|
|
-o trans=virtio,version=9p2000.L,msize=262144,cache=loose \
|
2014-02-16 15:53:02 +00:00
|
|
|
store /fs/nix/store
|
2014-02-16 02:19:40 +00:00
|
|
|
|
2014-02-15 22:23:47 +00:00
|
|
|
mount -t 9p \
|
|
|
|
-o trans=virtio,version=9p2000.L,msize=262144,cache=loose \
|
2014-02-16 15:53:02 +00:00
|
|
|
xchg /fs/xchg
|
|
|
|
|
|
|
|
cat > /fs/etc/passwd <<PASSWD
|
|
|
|
root:x:0:0::/root:/bin/false
|
|
|
|
nobody:x:65534:65534::/var/empty:/bin/false
|
|
|
|
PASSWD
|
2014-02-15 22:23:47 +00:00
|
|
|
|
2014-02-16 15:53:02 +00:00
|
|
|
set +e
|
|
|
|
chroot /fs $command $out
|
|
|
|
echo $? > /fs/xchg/in-vm-exit
|
|
|
|
|
|
|
|
poweroff -f
|
2014-02-15 22:23:47 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
initrd = makeInitrd {
|
|
|
|
contents = lib.singleton {
|
|
|
|
object = preInitScript;
|
|
|
|
symlink = "/init";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-02-16 00:42:10 +00:00
|
|
|
shellEscape = x: "'${lib.replaceChars ["'"] [("'\\'" + "'")] x}'";
|
|
|
|
|
2014-02-16 02:19:40 +00:00
|
|
|
loopForever = "while :; do ${coreutils}/bin/sleep 1; done";
|
|
|
|
|
2014-02-15 22:23:47 +00:00
|
|
|
initScript = writeScript "init.sh" (''
|
|
|
|
#!${stdenv.shell}
|
2014-02-16 15:47:23 +00:00
|
|
|
${coreutils}/bin/cp -L "${sshKey}" /ssh.key
|
|
|
|
${coreutils}/bin/chmod 600 /ssh.key
|
|
|
|
'' + (if installMode then ''
|
|
|
|
echo -n "Waiting for Windows installation to finish..."
|
|
|
|
while ! ${netcat}/bin/netcat -z 192.168.0.1 22; do
|
|
|
|
echo -n .
|
|
|
|
# Print a dot every 10 seconds only to shorten line length.
|
|
|
|
${coreutils}/bin/sleep 10
|
|
|
|
done
|
|
|
|
echo " success."
|
|
|
|
# Loop forever, because this VM is going to be killed.
|
|
|
|
${loopForever}
|
|
|
|
'' else ''
|
2014-02-16 15:53:02 +00:00
|
|
|
${coreutils}/bin/mkdir -p /etc/samba /etc/samba/private \
|
|
|
|
/var/lib/samba /var/log /var/run
|
2014-02-15 22:23:47 +00:00
|
|
|
${coreutils}/bin/cat > /etc/samba/smb.conf <<CONFIG
|
|
|
|
[global]
|
|
|
|
security = user
|
|
|
|
map to guest = Bad User
|
|
|
|
workgroup = cygwin
|
|
|
|
netbios name = controller
|
|
|
|
server string = %h
|
|
|
|
log level = 1
|
|
|
|
max log size = 1000
|
|
|
|
log file = /var/log/samba.log
|
|
|
|
|
|
|
|
[nixstore]
|
|
|
|
path = /nix/store
|
|
|
|
read only = no
|
|
|
|
guest ok = yes
|
2014-02-16 02:19:40 +00:00
|
|
|
|
|
|
|
[xchg]
|
|
|
|
path = /xchg
|
|
|
|
read only = no
|
|
|
|
guest ok = yes
|
2014-02-15 22:23:47 +00:00
|
|
|
CONFIG
|
|
|
|
|
|
|
|
${samba}/sbin/nmbd -D
|
|
|
|
${samba}/sbin/smbd -D
|
2014-02-16 15:53:02 +00:00
|
|
|
|
2014-02-15 22:23:47 +00:00
|
|
|
echo -n "Waiting for Windows VM to become available..."
|
|
|
|
while ! ${netcat}/bin/netcat -z 192.168.0.1 22; do
|
|
|
|
echo -n .
|
|
|
|
${coreutils}/bin/sleep 1
|
|
|
|
done
|
|
|
|
echo " success."
|
|
|
|
|
|
|
|
${openssh}/bin/ssh \
|
|
|
|
-o UserKnownHostsFile=/dev/null \
|
|
|
|
-o StrictHostKeyChecking=no \
|
|
|
|
-i /ssh.key \
|
|
|
|
-l Administrator \
|
2014-02-16 00:42:10 +00:00
|
|
|
192.168.0.1 -- ${shellEscape command}
|
2014-02-15 22:23:47 +00:00
|
|
|
|
2014-02-16 02:19:40 +00:00
|
|
|
${lib.optionalString (suspendTo != null) ''
|
|
|
|
${coreutils}/bin/touch /xchg/suspend_now
|
|
|
|
${loopForever}
|
|
|
|
''}
|
2014-02-15 22:23:47 +00:00
|
|
|
''));
|
|
|
|
|
|
|
|
kernelAppend = lib.concatStringsSep " " [
|
|
|
|
"panic=1"
|
|
|
|
"loglevel=4"
|
|
|
|
"console=tty1"
|
|
|
|
"console=ttyS0"
|
|
|
|
"command=${initScript}"
|
|
|
|
];
|
|
|
|
|
|
|
|
controllerQemuArgs = lib.concatStringsSep " " (maybeKvm64 ++ [
|
|
|
|
"-nographic"
|
|
|
|
"-no-reboot"
|
|
|
|
"-virtfs local,path=/nix/store,security_model=none,mount_tag=store"
|
2014-02-16 02:19:40 +00:00
|
|
|
"-virtfs local,path=$XCHG_DIR,security_model=none,mount_tag=xchg"
|
2014-02-15 22:23:47 +00:00
|
|
|
"-kernel ${modulesClosure.kernel}/bzImage"
|
|
|
|
"-initrd ${initrd}/initrd"
|
|
|
|
"-append \"${kernelAppend}\""
|
|
|
|
"-net nic,vlan=0,macaddr=52:54:00:12:01:02,model=virtio"
|
|
|
|
"-net vde,vlan=0,sock=$QEMU_VDE_SOCKET"
|
|
|
|
]);
|
|
|
|
|
|
|
|
maybeKvm64 = lib.optional (stdenv.system == "x86_64-linux") "-cpu kvm64";
|
|
|
|
|
|
|
|
cygwinQemuArgs = lib.concatStringsSep " " (maybeKvm64 ++ [
|
|
|
|
"-monitor unix:$MONITOR_SOCKET,server,nowait"
|
|
|
|
"-nographic"
|
|
|
|
"-net nic,vlan=0,macaddr=52:54:00:12:01:01"
|
|
|
|
"-net vde,vlan=0,sock=$QEMU_VDE_SOCKET"
|
|
|
|
"-rtc base=2010-01-01,clock=vm"
|
|
|
|
] ++ qemuArgs ++ lib.optionals (resumeFrom != null) [
|
|
|
|
"-incoming 'exec: ${gzip}/bin/gzip -c -d \"${resumeFrom}\"'"
|
|
|
|
]);
|
|
|
|
|
|
|
|
modulesClosure = lib.overrideDerivation vmTools.modulesClosure (o: {
|
|
|
|
rootModules = o.rootModules ++ lib.singleton "virtio_net";
|
|
|
|
});
|
|
|
|
|
|
|
|
preVM = ''
|
2014-02-16 02:19:40 +00:00
|
|
|
XCHG_DIR="$(${coreutils}/bin/mktemp -d nix-vm.XXXXXXXXXX --tmpdir)"
|
2014-02-15 22:23:47 +00:00
|
|
|
QEMU_VDE_SOCKET="$(pwd)/vde.ctl"
|
|
|
|
MONITOR_SOCKET="$(pwd)/monitor"
|
|
|
|
${vde2}/bin/vde_switch -s "$QEMU_VDE_SOCKET" &
|
2014-02-15 23:27:43 +00:00
|
|
|
echo 'alive?' | ${socat}/bin/socat - \
|
|
|
|
UNIX-CONNECT:$QEMU_VDE_SOCKET/ctl,retry=20
|
2014-02-15 22:23:47 +00:00
|
|
|
'';
|
|
|
|
|
2014-02-16 02:19:40 +00:00
|
|
|
bgBoth = lib.optionalString (suspendTo != null) " &";
|
|
|
|
|
2014-02-15 22:23:47 +00:00
|
|
|
vmExec = if installMode then ''
|
|
|
|
${vmTools.qemuProg} ${controllerQemuArgs} &
|
2014-02-16 02:19:40 +00:00
|
|
|
${vmTools.qemuProg} ${cygwinQemuArgs}${bgBoth}
|
2014-02-15 22:23:47 +00:00
|
|
|
'' else ''
|
|
|
|
${vmTools.qemuProg} ${cygwinQemuArgs} &
|
2014-02-16 02:19:40 +00:00
|
|
|
${vmTools.qemuProg} ${controllerQemuArgs}${bgBoth}
|
2014-02-15 22:23:47 +00:00
|
|
|
'' + lib.optionalString (suspendTo != null) ''
|
2014-02-16 02:19:40 +00:00
|
|
|
while ! test -e "$XCHG_DIR/suspend_now"; do sleep 1; done
|
2014-02-15 22:23:47 +00:00
|
|
|
${socat}/bin/socat - UNIX-CONNECT:$MONITOR_SOCKET <<CMD
|
|
|
|
stop
|
|
|
|
migrate_set_speed 4095m
|
|
|
|
migrate "exec:${gzip}/bin/gzip -c > '${suspendTo}'"
|
|
|
|
quit
|
|
|
|
CMD
|
2014-02-16 02:19:40 +00:00
|
|
|
wait %-
|
2014-02-15 22:23:47 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
in writeScript "run-cygwin-vm.sh" ''
|
|
|
|
#!${stdenv.shell} -e
|
|
|
|
${preVM}
|
|
|
|
${vmExec}
|
|
|
|
''
|