2014-12-13 12:13:30 +01:00
|
|
|
import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-15 08:58:12 +01:00
|
|
|
testVMConfig = vmName: attrs: { config, pkgs, ... }: {
|
|
|
|
boot.kernelParams = let
|
|
|
|
miniInit = ''
|
|
|
|
#!${pkgs.stdenv.shell} -xe
|
|
|
|
export PATH="${pkgs.coreutils}/bin:${pkgs.utillinux}/bin"
|
|
|
|
|
|
|
|
${pkgs.linuxPackages.virtualboxGuestAdditions}/sbin/VBoxService
|
|
|
|
${(attrs.vmScript or (const "")) pkgs}
|
|
|
|
|
|
|
|
i=0
|
|
|
|
while [ ! -e /mnt-root/shutdown ]; do
|
|
|
|
sleep 10
|
|
|
|
i=$(($i + 10))
|
|
|
|
[ $i -le 120 ] || fail
|
|
|
|
done
|
|
|
|
|
|
|
|
rm -f /mnt-root/boot-done /mnt-root/shutdown
|
|
|
|
'';
|
|
|
|
in [
|
2014-12-13 11:15:48 +01:00
|
|
|
"console=tty0" "console=ttyS0" "ignore_loglevel"
|
|
|
|
"boot.trace" "panic=1" "boot.panic_on_fail"
|
2014-12-15 08:58:12 +01:00
|
|
|
"init=${pkgs.writeScript "mini-init.sh" miniInit}"
|
2014-12-13 11:15:48 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
fileSystems."/" = {
|
|
|
|
device = "vboxshare";
|
|
|
|
fsType = "vboxsf";
|
|
|
|
};
|
|
|
|
|
|
|
|
services.virtualboxGuest.enable = true;
|
|
|
|
|
2014-12-15 08:58:12 +01:00
|
|
|
boot.initrd.kernelModules = [
|
|
|
|
"af_packet" "vboxsf"
|
|
|
|
"virtio" "virtio_pci" "virtio_ring" "virtio_net" "vboxguest"
|
|
|
|
];
|
2014-12-13 11:15:48 +01:00
|
|
|
|
|
|
|
boot.initrd.extraUtilsCommands = ''
|
|
|
|
cp -av -t "$out/bin/" \
|
2014-12-15 08:58:12 +01:00
|
|
|
"${pkgs.linuxPackages.virtualboxGuestAdditions}/sbin/mount.vboxsf" \
|
|
|
|
"${pkgs.utillinux}/bin/unshare"
|
|
|
|
${(attrs.extraUtilsCommands or (const "")) pkgs}
|
2014-12-13 11:15:48 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
boot.initrd.postMountCommands = ''
|
|
|
|
touch /mnt-root/boot-done
|
2014-12-15 08:58:12 +01:00
|
|
|
hostname "${vmName}"
|
|
|
|
mkdir -p /nix/store
|
|
|
|
unshare -m "@shell@" -c '
|
|
|
|
mount -t vboxsf nixstore /nix/store
|
|
|
|
exec "$stage2Init"
|
|
|
|
'
|
2014-12-13 11:15:48 +01:00
|
|
|
poweroff -f
|
|
|
|
'';
|
|
|
|
|
|
|
|
system.requiredKernelConfig = with config.lib.kernelConfig; [
|
|
|
|
(isYes "SERIAL_8250_CONSOLE")
|
|
|
|
(isYes "SERIAL_8250")
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2014-12-15 08:58:12 +01:00
|
|
|
testVM = vmName: vmScript: let
|
2014-12-13 11:15:48 +01:00
|
|
|
cfg = (import ../lib/eval-config.nix {
|
|
|
|
system = "i686-linux";
|
|
|
|
modules = [
|
|
|
|
../modules/profiles/minimal.nix
|
2014-12-15 08:58:12 +01:00
|
|
|
(testVMConfig vmName vmScript)
|
2014-12-13 11:15:48 +01:00
|
|
|
];
|
|
|
|
}).config;
|
|
|
|
in pkgs.vmTools.runInLinuxVM (pkgs.runCommand "virtualbox-image" {
|
|
|
|
preVM = ''
|
|
|
|
mkdir -p "$out"
|
|
|
|
diskImage="$(pwd)/qimage"
|
|
|
|
${pkgs.vmTools.qemu}/bin/qemu-img create -f raw "$diskImage" 100M
|
|
|
|
'';
|
|
|
|
|
|
|
|
postVM = ''
|
|
|
|
echo "creating VirtualBox disk image..."
|
|
|
|
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -O vdi \
|
|
|
|
"$diskImage" "$out/disk.vdi"
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildInputs = [ pkgs.utillinux pkgs.perl ];
|
|
|
|
} ''
|
|
|
|
${pkgs.parted}/sbin/parted /dev/vda mklabel msdos
|
|
|
|
${pkgs.parted}/sbin/parted /dev/vda -- mkpart primary ext2 1M -1s
|
|
|
|
. /sys/class/block/vda1/uevent
|
|
|
|
mknod /dev/vda1 b $MAJOR $MINOR
|
|
|
|
|
|
|
|
${pkgs.e2fsprogs}/sbin/mkfs.ext4 /dev/vda1
|
|
|
|
${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda1
|
|
|
|
mkdir /mnt
|
|
|
|
mount /dev/vda1 /mnt
|
|
|
|
cp "${cfg.system.build.kernel}/bzImage" /mnt/linux
|
|
|
|
cp "${cfg.system.build.initialRamdisk}/initrd" /mnt/initrd
|
|
|
|
|
|
|
|
${pkgs.grub2}/bin/grub-install --boot-directory=/mnt /dev/vda
|
|
|
|
|
|
|
|
cat > /mnt/grub/grub.cfg <<GRUB
|
|
|
|
set root=hd0,1
|
2014-12-13 12:13:30 +01:00
|
|
|
linux /linux ${concatStringsSep " " cfg.boot.kernelParams}
|
2014-12-13 11:15:48 +01:00
|
|
|
initrd /initrd
|
|
|
|
boot
|
|
|
|
GRUB
|
|
|
|
umount /mnt
|
|
|
|
'');
|
|
|
|
|
2014-12-15 08:58:12 +01:00
|
|
|
createVM = name: attrs: let
|
2014-12-13 12:13:30 +01:00
|
|
|
mkFlags = concatStringsSep " ";
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
sharePath = "/home/alice/vboxshare-${name}";
|
2014-12-13 11:15:48 +01:00
|
|
|
|
|
|
|
createFlags = mkFlags [
|
|
|
|
"--ostype Linux26"
|
|
|
|
"--register"
|
|
|
|
];
|
|
|
|
|
2014-12-15 08:58:12 +01:00
|
|
|
vmFlags = mkFlags ([
|
2014-12-13 11:15:48 +01:00
|
|
|
"--uart1 0x3F8 4"
|
2014-12-13 12:13:30 +01:00
|
|
|
"--uartmode1 client /run/virtualbox-log-${name}.sock"
|
2014-12-15 08:58:12 +01:00
|
|
|
] ++ (attrs.vmFlags or []));
|
2014-12-13 11:15:48 +01:00
|
|
|
|
|
|
|
controllerFlags = mkFlags [
|
|
|
|
"--name SATA"
|
|
|
|
"--add sata"
|
|
|
|
"--bootable on"
|
|
|
|
"--hostiocache on"
|
|
|
|
];
|
|
|
|
|
|
|
|
diskFlags = mkFlags [
|
|
|
|
"--storagectl SATA"
|
|
|
|
"--port 0"
|
|
|
|
"--device 0"
|
|
|
|
"--type hdd"
|
|
|
|
"--mtype immutable"
|
2014-12-15 08:58:12 +01:00
|
|
|
"--medium ${testVM name attrs}/disk.vdi"
|
2014-12-13 11:15:48 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
sharedFlags = mkFlags [
|
|
|
|
"--name vboxshare"
|
2014-12-13 12:13:30 +01:00
|
|
|
"--hostpath ${sharePath}"
|
2014-12-13 11:15:48 +01:00
|
|
|
];
|
2014-12-15 08:58:12 +01:00
|
|
|
|
|
|
|
nixstoreFlags = mkFlags [
|
|
|
|
"--name nixstore"
|
|
|
|
"--hostpath /nix/store"
|
|
|
|
"--readonly"
|
|
|
|
];
|
2014-12-13 12:13:30 +01:00
|
|
|
in {
|
|
|
|
machine = {
|
|
|
|
systemd.sockets = listToAttrs (singleton {
|
|
|
|
name = "vboxtestlog-${name}";
|
|
|
|
value = {
|
|
|
|
description = "VirtualBox Test Machine Log Socket";
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
before = [ "multi-user.target" ];
|
|
|
|
socketConfig.ListenStream = "/run/virtualbox-log-${name}.sock";
|
|
|
|
socketConfig.Accept = true;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
systemd.services = listToAttrs (singleton {
|
|
|
|
name = "vboxtestlog-${name}@";
|
|
|
|
value = {
|
|
|
|
description = "VirtualBox Test Machine Log";
|
|
|
|
serviceConfig.StandardInput = "socket";
|
|
|
|
serviceConfig.StandardOutput = "syslog";
|
|
|
|
serviceConfig.SyslogIdentifier = "vbox-${name}";
|
|
|
|
serviceConfig.ExecStart = "${pkgs.coreutils}/bin/cat";
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
testSubs = ''
|
2014-12-15 08:58:12 +01:00
|
|
|
sub checkRunning_${name} {
|
|
|
|
my $cmd = 'VBoxManage list runningvms | grep -q "^\"${name}\""';
|
|
|
|
my ($status, $out) = $machine->execute(ru $cmd);
|
|
|
|
return $status == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cleanup_${name} {
|
|
|
|
$machine->execute(ru "VBoxManage controlvm ${name} poweroff")
|
|
|
|
if checkRunning_${name};
|
|
|
|
$machine->succeed("rm -rf ${sharePath}");
|
|
|
|
$machine->succeed("mkdir -p ${sharePath}");
|
|
|
|
$machine->succeed("chown alice.users ${sharePath}");
|
|
|
|
}
|
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
sub createVM_${name} {
|
|
|
|
vbm("createvm --name ${name} ${createFlags}");
|
|
|
|
vbm("modifyvm ${name} ${vmFlags}");
|
2014-12-15 08:58:12 +01:00
|
|
|
vbm("setextradata ${name} VBoxInternal/PDM/HaltOnReset 1");
|
2014-12-13 12:13:30 +01:00
|
|
|
vbm("storagectl ${name} ${controllerFlags}");
|
|
|
|
vbm("storageattach ${name} ${diskFlags}");
|
|
|
|
vbm("sharedfolder add ${name} ${sharedFlags}");
|
2014-12-15 08:58:12 +01:00
|
|
|
vbm("sharedfolder add ${name} ${nixstoreFlags}");
|
2014-12-13 12:13:30 +01:00
|
|
|
vbm("showvminfo ${name} >&2");
|
2014-12-15 08:58:12 +01:00
|
|
|
cleanup_${name};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub destroyVM_${name} {
|
|
|
|
cleanup_${name};
|
|
|
|
vbm("unregistervm ${name} --delete");
|
2014-12-13 12:13:30 +01:00
|
|
|
}
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
sub waitForVMBoot_${name} {
|
|
|
|
$machine->execute(ru(
|
|
|
|
'set -e; i=0; '.
|
|
|
|
'while ! test -e ${sharePath}/boot-done; do '.
|
|
|
|
'sleep 10; i=$(($i + 10)); [ $i -le 3600 ]; '.
|
|
|
|
'VBoxManage list runningvms | grep -q "^\"${name}\""; '.
|
|
|
|
'done'
|
|
|
|
));
|
|
|
|
}
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-15 08:58:12 +01:00
|
|
|
sub waitForIP_${name} ($) {
|
|
|
|
my $property = "/VirtualBox/GuestInfo/Net/$_[0]/V4/IP";
|
|
|
|
my $getip = "VBoxManage guestproperty get ${name} $property | ".
|
|
|
|
"sed -n -e 's/^Value: //p'";
|
|
|
|
my $ip = $machine->succeed(ru(
|
|
|
|
'for i in $(seq 1000); do '.
|
|
|
|
'if ipaddr="$('.$getip.')" && [ -n "$ipaddr" ]; then '.
|
|
|
|
'echo "$ipaddr"; exit 0; '.
|
|
|
|
'fi; '.
|
|
|
|
'sleep 1; '.
|
|
|
|
'done; '.
|
|
|
|
'echo "Could not get IPv4 address for ${name}!" >&2; '.
|
|
|
|
'exit 1'
|
|
|
|
));
|
|
|
|
chomp $ip;
|
|
|
|
return $ip;
|
2014-12-13 11:15:48 +01:00
|
|
|
}
|
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
sub waitForStartup_${name} {
|
|
|
|
for (my $i = 0; $i <= 120; $i += 10) {
|
|
|
|
$machine->sleep(10);
|
|
|
|
return if checkRunning_${name};
|
|
|
|
}
|
|
|
|
die "VirtualBox VM didn't start up within 2 minutes";
|
2014-12-13 11:15:48 +01:00
|
|
|
}
|
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
sub waitForShutdown_${name} {
|
|
|
|
for (my $i = 0; $i <= 120; $i += 10) {
|
|
|
|
$machine->sleep(10);
|
|
|
|
return unless checkRunning_${name};
|
|
|
|
}
|
|
|
|
die "VirtualBox VM didn't shut down within 2 minutes";
|
|
|
|
}
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
sub shutdownVM_${name} {
|
|
|
|
$machine->succeed(ru "touch ${sharePath}/shutdown");
|
|
|
|
$machine->waitUntilSucceeds(
|
|
|
|
"test ! -e ${sharePath}/shutdown ".
|
|
|
|
" -a ! -e ${sharePath}/boot-done"
|
|
|
|
);
|
|
|
|
waitForShutdown_${name};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-15 08:58:12 +01:00
|
|
|
hostonlyVMFlags = [
|
|
|
|
"--nictype1 virtio"
|
|
|
|
"--nictype2 virtio"
|
|
|
|
"--nic2 hostonly"
|
|
|
|
"--hostonlyadapter2 vboxnet0"
|
|
|
|
];
|
|
|
|
|
|
|
|
dhcpScript = pkgs: ''
|
|
|
|
${pkgs.dhcp}/bin/dhclient \
|
|
|
|
-lf /run/dhcp.leases \
|
|
|
|
-pf /run/dhclient.pid \
|
|
|
|
-v eth0 eth1
|
|
|
|
|
|
|
|
otherIP="$(${pkgs.netcat}/bin/netcat -clp 1234 || :)"
|
|
|
|
${pkgs.iputils}/bin/ping -I eth1 -c1 "$otherIP"
|
|
|
|
echo "$otherIP reachable" | ${pkgs.netcat}/bin/netcat -clp 5678 || :
|
|
|
|
'';
|
|
|
|
|
|
|
|
vboxVMs = mapAttrs createVM {
|
|
|
|
simple = {};
|
|
|
|
|
|
|
|
test1.vmFlags = hostonlyVMFlags;
|
|
|
|
test1.vmScript = dhcpScript;
|
|
|
|
|
|
|
|
test2.vmFlags = hostonlyVMFlags;
|
|
|
|
test2.vmScript = dhcpScript;
|
|
|
|
};
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
in {
|
|
|
|
name = "virtualbox";
|
|
|
|
|
|
|
|
machine = { pkgs, ... }: {
|
|
|
|
imports = let
|
|
|
|
mkVMConf = name: val: val.machine // { key = "${name}-config"; };
|
|
|
|
vmConfigs = mapAttrsToList mkVMConf vboxVMs;
|
|
|
|
in [ ./common/user-account.nix ./common/x11.nix ] ++ vmConfigs;
|
2014-12-15 08:58:12 +01:00
|
|
|
virtualisation.memorySize = 768;
|
2014-12-13 12:13:30 +01:00
|
|
|
services.virtualboxHost.enable = true;
|
2014-12-15 08:58:12 +01:00
|
|
|
users.extraUsers.alice.extraGroups = [ "vboxusers" ];
|
2014-12-13 12:13:30 +01:00
|
|
|
};
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
testScript = ''
|
2014-12-15 08:58:12 +01:00
|
|
|
sub ru ($) {
|
|
|
|
my $esc = $_[0] =~ s/'/'\\${"'"}'/gr;
|
|
|
|
return "su - alice -c '$esc'";
|
2014-12-13 12:13:30 +01:00
|
|
|
}
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
sub vbm {
|
|
|
|
$machine->succeed(ru("VBoxManage ".$_[0]));
|
|
|
|
};
|
|
|
|
|
|
|
|
${concatStrings (mapAttrsToList (_: getAttr "testSubs") vboxVMs)}
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
$machine->waitForX;
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-15 08:58:12 +01:00
|
|
|
createVM_simple;
|
2014-12-13 12:13:30 +01:00
|
|
|
|
|
|
|
subtest "simple-gui", sub {
|
2014-12-13 11:15:48 +01:00
|
|
|
$machine->succeed(ru "VirtualBox &");
|
|
|
|
$machine->waitForWindow(qr/Oracle VM VirtualBox Manager/);
|
|
|
|
$machine->sleep(5);
|
|
|
|
$machine->screenshot("gui_manager_started");
|
|
|
|
$machine->sendKeys("ret");
|
|
|
|
$machine->screenshot("gui_manager_sent_startup");
|
2014-12-15 08:58:12 +01:00
|
|
|
waitForStartup_simple;
|
2014-12-13 11:15:48 +01:00
|
|
|
$machine->screenshot("gui_started");
|
2014-12-15 08:58:12 +01:00
|
|
|
waitForVMBoot_simple;
|
2014-12-13 11:15:48 +01:00
|
|
|
$machine->screenshot("gui_booted");
|
2014-12-15 08:58:12 +01:00
|
|
|
shutdownVM_simple;
|
2014-12-13 11:15:48 +01:00
|
|
|
$machine->sleep(5);
|
|
|
|
$machine->screenshot("gui_stopped");
|
|
|
|
$machine->sendKeys("ctrl-q");
|
|
|
|
$machine->sleep(5);
|
|
|
|
$machine->screenshot("gui_manager_stopped");
|
|
|
|
};
|
|
|
|
|
2014-12-15 08:58:12 +01:00
|
|
|
cleanup_simple;
|
2014-12-13 11:15:48 +01:00
|
|
|
|
2014-12-13 12:13:30 +01:00
|
|
|
subtest "simple-cli", sub {
|
2014-12-15 08:58:12 +01:00
|
|
|
vbm("startvm simple");
|
|
|
|
waitForStartup_simple;
|
2014-12-13 11:15:48 +01:00
|
|
|
$machine->screenshot("cli_started");
|
2014-12-15 08:58:12 +01:00
|
|
|
waitForVMBoot_simple;
|
2014-12-13 11:15:48 +01:00
|
|
|
$machine->screenshot("cli_booted");
|
2014-12-15 08:58:12 +01:00
|
|
|
shutdownVM_simple;
|
2014-12-13 11:15:48 +01:00
|
|
|
};
|
|
|
|
|
2014-12-15 08:58:12 +01:00
|
|
|
subtest "privilege-escalation", sub {
|
|
|
|
$machine->fail("test -e '/root/VirtualBox VMs'");
|
2014-12-15 19:12:58 +01:00
|
|
|
$machine->fail("test -e '/root/.config/VirtualBox'");
|
2014-12-15 08:58:12 +01:00
|
|
|
$machine->succeed("test -e '/home/alice/VirtualBox VMs'");
|
|
|
|
};
|
|
|
|
|
|
|
|
destroyVM_simple;
|
|
|
|
|
|
|
|
subtest "net-hostonlyif", sub {
|
|
|
|
createVM_test1;
|
|
|
|
createVM_test2;
|
|
|
|
|
|
|
|
vbm("startvm test1");
|
|
|
|
waitForStartup_test1;
|
|
|
|
|
|
|
|
vbm("startvm test2");
|
|
|
|
waitForStartup_test2;
|
|
|
|
|
|
|
|
waitForVMBoot_test1;
|
|
|
|
waitForVMBoot_test2;
|
|
|
|
|
|
|
|
$machine->screenshot("net_booted");
|
|
|
|
|
|
|
|
my $test1IP = waitForIP_test1 1;
|
|
|
|
my $test2IP = waitForIP_test2 1;
|
|
|
|
|
|
|
|
$machine->succeed("echo '$test2IP' | netcat -c '$test1IP' 1234");
|
|
|
|
$machine->succeed("echo '$test1IP' | netcat -c '$test2IP' 1234");
|
2014-12-13 12:13:30 +01:00
|
|
|
|
2014-12-15 08:58:12 +01:00
|
|
|
$machine->waitUntilSucceeds("netcat -c '$test1IP' 5678 >&2");
|
|
|
|
$machine->waitUntilSucceeds("netcat -c '$test2IP' 5678 >&2");
|
|
|
|
|
|
|
|
shutdownVM_test1;
|
|
|
|
shutdownVM_test2;
|
|
|
|
|
|
|
|
destroyVM_test1;
|
|
|
|
destroyVM_test2;
|
|
|
|
};
|
2014-12-13 11:15:48 +01:00
|
|
|
'';
|
|
|
|
})
|