2018-11-11 11:41:11 +03:00
|
|
|
{ system ? builtins.currentSystem,
|
|
|
|
config ? {},
|
|
|
|
pkgs ? import ../.. { inherit system config; },
|
|
|
|
debug ? false,
|
virtualbox: 5.2.28 -> 6.0.6
Quite some fixing was needed to get this to work.
Changes in VirtualBox and additions:
- VirtualBox is no longer officially supported on 32-bit hosts so i686-linux is removed from platforms
for VirtualBox and the extension pack. 32-bit additions still work.
- There was a refactoring of kernel module makefiles and two resulting bugs affected us which had to be patched.
These bugs were reported to the bug tracker (see comments near patches).
- The Qt5X11Extras makefile patch broke. Fixed it to apply again, making the libraries logic simpler
and more correct (it just uses a different base path instead of always linking to Qt5X11Extras).
- Added a patch to remove "test1" and "test2" kernel messages due to forgotten debugging code.
- virtualbox-host NixOS module: the VirtualBoxVM executable should be setuid not VirtualBox.
This matches how the official installer sets it up.
- Additions: replaced a for loop for installing kernel modules with just a "make install",
which seems to work without any of the things done in the previous code.
- Additions: The package defined buildCommand which resulted in phases not running, including RUNPATH
stripping in fixupPhase, and installPhase was defined which was not even run. Fixed this by
refactoring using phases. Had to set dontStrip otherwise binaries were broken by stripping.
The libdbus path had to be added later in fixupPhase because it is used via dlopen not directly linked.
- Additions: Added zlib and libc to patchelf, otherwise runtime library errors result from some binaries.
For some reason the missing libc only manifested itself for mount.vboxsf when included in the initrd.
Changes in nixos/tests/virtualbox:
- Update the simple-gui test to send the right keys to start the VM. With VirtualBox 5
it was enough to just send "return", but with 6 the Tools thing may be selected by
default. Send "home" to reliably select Tools, "down" to move to the VM and "return"
to start it.
- Disable the VirtualBox UART by default because it causes a crash due to a regression
in VirtualBox (specific to software virtualization and serial port usage). It can
still be enabled using an option but there is an assert that KVM nested virtualization
is enabled, which works around the problem (see below).
- Add an option to enable nested KVM virtualization, allowing VirtualBox to use hardware
virtualization. This works around the UART problem and also allows using 64-bit
guests, but requires a kernel module parameter.
- Add an option to run 64-bit guests. Tested that the tests pass with that. As mentioned
this requires KVM nested virtualization.
2019-05-04 17:18:39 +03:00
|
|
|
enableUnfree ? false,
|
2024-02-14 20:40:46 +03:00
|
|
|
enableKvm ? false,
|
2022-06-26 14:41:05 +03:00
|
|
|
use64bitGuest ? true
|
2018-11-11 11:41:11 +03:00
|
|
|
}:
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
with import ../lib/testing-python.nix { inherit system pkgs; };
|
2016-02-29 21:15:36 +03:00
|
|
|
with pkgs.lib;
|
2014-12-18 15:43:18 +03:00
|
|
|
|
2016-02-29 21:15:36 +03:00
|
|
|
let
|
2016-08-23 14:05:14 +03:00
|
|
|
testVMConfig = vmName: attrs: { config, pkgs, lib, ... }: let
|
2015-09-17 12:02:43 +03:00
|
|
|
guestAdditions = pkgs.linuxPackages.virtualboxGuestAdditions;
|
|
|
|
|
|
|
|
miniInit = ''
|
2020-04-07 09:25:48 +03:00
|
|
|
#!${pkgs.runtimeShell} -xe
|
2020-11-24 18:29:28 +03:00
|
|
|
export PATH="${lib.makeBinPath [ pkgs.coreutils pkgs.util-linux ]}"
|
2015-09-17 12:02:43 +03:00
|
|
|
|
2022-06-25 20:11:34 +03:00
|
|
|
mkdir -p /run/dbus /var
|
|
|
|
ln -s /run /var
|
2015-09-17 12:02:43 +03:00
|
|
|
cat > /etc/passwd <<EOF
|
|
|
|
root:x:0:0::/root:/bin/false
|
2016-10-24 13:43:42 +03:00
|
|
|
messagebus:x:1:1::/run/dbus:/bin/false
|
2015-09-17 12:02:43 +03:00
|
|
|
EOF
|
|
|
|
cat > /etc/group <<EOF
|
|
|
|
root:x:0:
|
|
|
|
messagebus:x:1:
|
|
|
|
EOF
|
2016-05-26 17:45:43 +03:00
|
|
|
|
2022-11-21 05:03:50 +03:00
|
|
|
"${pkgs.dbus}/bin/dbus-daemon" --fork \
|
|
|
|
--config-file="${pkgs.dbus}/share/dbus-1/system.conf"
|
2015-09-17 12:02:43 +03:00
|
|
|
|
|
|
|
${guestAdditions}/bin/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 {
|
|
|
|
boot.kernelParams = [
|
2014-12-13 13:15:48 +03:00
|
|
|
"console=tty0" "console=ttyS0" "ignore_loglevel"
|
|
|
|
"boot.trace" "panic=1" "boot.panic_on_fail"
|
2014-12-15 10:58:12 +03:00
|
|
|
"init=${pkgs.writeScript "mini-init.sh" miniInit}"
|
2014-12-13 13:15:48 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
fileSystems."/" = {
|
|
|
|
device = "vboxshare";
|
|
|
|
fsType = "vboxsf";
|
|
|
|
};
|
|
|
|
|
2015-08-13 13:17:32 +03:00
|
|
|
virtualisation.virtualbox.guest.enable = true;
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2014-12-15 10:58:12 +03:00
|
|
|
boot.initrd.kernelModules = [
|
|
|
|
"af_packet" "vboxsf"
|
|
|
|
"virtio" "virtio_pci" "virtio_ring" "virtio_net" "vboxguest"
|
|
|
|
];
|
2014-12-13 13:15:48 +03:00
|
|
|
|
|
|
|
boot.initrd.extraUtilsCommands = ''
|
2015-09-17 12:02:43 +03:00
|
|
|
copy_bin_and_libs "${guestAdditions}/bin/mount.vboxsf"
|
2020-11-24 18:29:28 +03:00
|
|
|
copy_bin_and_libs "${pkgs.util-linux}/bin/unshare"
|
2014-12-15 10:58:12 +03:00
|
|
|
${(attrs.extraUtilsCommands or (const "")) pkgs}
|
2014-12-13 13:15:48 +03:00
|
|
|
'';
|
|
|
|
|
|
|
|
boot.initrd.postMountCommands = ''
|
|
|
|
touch /mnt-root/boot-done
|
2014-12-15 10:58:12 +03:00
|
|
|
hostname "${vmName}"
|
|
|
|
mkdir -p /nix/store
|
2020-04-07 09:25:48 +03:00
|
|
|
unshare -m ${escapeShellArg pkgs.runtimeShell} -c '
|
2014-12-15 10:58:12 +03:00
|
|
|
mount -t vboxsf nixstore /nix/store
|
|
|
|
exec "$stage2Init"
|
|
|
|
'
|
2014-12-13 13:15:48 +03:00
|
|
|
poweroff -f
|
|
|
|
'';
|
|
|
|
|
|
|
|
system.requiredKernelConfig = with config.lib.kernelConfig; [
|
|
|
|
(isYes "SERIAL_8250_CONSOLE")
|
|
|
|
(isYes "SERIAL_8250")
|
|
|
|
];
|
2020-08-17 14:27:07 +03:00
|
|
|
|
|
|
|
networking.usePredictableInterfaceNames = false;
|
2014-12-13 13:15:48 +03:00
|
|
|
};
|
|
|
|
|
2014-12-15 21:17:38 +03:00
|
|
|
mkLog = logfile: tag: let
|
|
|
|
rotated = map (i: "${logfile}.${toString i}") (range 1 9);
|
|
|
|
all = concatMapStringsSep " " (f: "\"${f}\"") ([logfile] ++ rotated);
|
|
|
|
logcmd = "tail -F ${all} 2> /dev/null | logger -t \"${tag}\"";
|
2020-08-07 13:31:26 +03:00
|
|
|
in if debug then "machine.execute(ru('${logcmd} & disown'))" else "pass";
|
2014-12-15 21:17:38 +03:00
|
|
|
|
2014-12-15 10:58:12 +03:00
|
|
|
testVM = vmName: vmScript: let
|
2014-12-13 13:15:48 +03:00
|
|
|
cfg = (import ../lib/eval-config.nix {
|
virtualbox: 5.2.28 -> 6.0.6
Quite some fixing was needed to get this to work.
Changes in VirtualBox and additions:
- VirtualBox is no longer officially supported on 32-bit hosts so i686-linux is removed from platforms
for VirtualBox and the extension pack. 32-bit additions still work.
- There was a refactoring of kernel module makefiles and two resulting bugs affected us which had to be patched.
These bugs were reported to the bug tracker (see comments near patches).
- The Qt5X11Extras makefile patch broke. Fixed it to apply again, making the libraries logic simpler
and more correct (it just uses a different base path instead of always linking to Qt5X11Extras).
- Added a patch to remove "test1" and "test2" kernel messages due to forgotten debugging code.
- virtualbox-host NixOS module: the VirtualBoxVM executable should be setuid not VirtualBox.
This matches how the official installer sets it up.
- Additions: replaced a for loop for installing kernel modules with just a "make install",
which seems to work without any of the things done in the previous code.
- Additions: The package defined buildCommand which resulted in phases not running, including RUNPATH
stripping in fixupPhase, and installPhase was defined which was not even run. Fixed this by
refactoring using phases. Had to set dontStrip otherwise binaries were broken by stripping.
The libdbus path had to be added later in fixupPhase because it is used via dlopen not directly linked.
- Additions: Added zlib and libc to patchelf, otherwise runtime library errors result from some binaries.
For some reason the missing libc only manifested itself for mount.vboxsf when included in the initrd.
Changes in nixos/tests/virtualbox:
- Update the simple-gui test to send the right keys to start the VM. With VirtualBox 5
it was enough to just send "return", but with 6 the Tools thing may be selected by
default. Send "home" to reliably select Tools, "down" to move to the VM and "return"
to start it.
- Disable the VirtualBox UART by default because it causes a crash due to a regression
in VirtualBox (specific to software virtualization and serial port usage). It can
still be enabled using an option but there is an assert that KVM nested virtualization
is enabled, which works around the problem (see below).
- Add an option to enable nested KVM virtualization, allowing VirtualBox to use hardware
virtualization. This works around the UART problem and also allows using 64-bit
guests, but requires a kernel module parameter.
- Add an option to run 64-bit guests. Tested that the tests pass with that. As mentioned
this requires KVM nested virtualization.
2019-05-04 17:18:39 +03:00
|
|
|
system = if use64bitGuest then "x86_64-linux" else "i686-linux";
|
2014-12-13 13:15:48 +03:00
|
|
|
modules = [
|
2014-12-15 10:58:12 +03:00
|
|
|
(testVMConfig vmName vmScript)
|
2014-12-13 13:15:48 +03: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"
|
|
|
|
'';
|
|
|
|
|
2020-11-24 18:29:28 +03:00
|
|
|
buildInputs = [ pkgs.util-linux pkgs.perl ];
|
2014-12-13 13:15:48 +03:00
|
|
|
} ''
|
2017-10-08 16:35:47 +03:00
|
|
|
${pkgs.parted}/sbin/parted --script /dev/vda mklabel msdos
|
|
|
|
${pkgs.parted}/sbin/parted --script /dev/vda -- mkpart primary ext2 1M -1s
|
2014-12-13 13:15:48 +03:00
|
|
|
${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 14:13:30 +03:00
|
|
|
linux /linux ${concatStringsSep " " cfg.boot.kernelParams}
|
2014-12-13 13:15:48 +03:00
|
|
|
initrd /initrd
|
|
|
|
boot
|
|
|
|
GRUB
|
|
|
|
umount /mnt
|
|
|
|
'');
|
|
|
|
|
2014-12-15 10:58:12 +03:00
|
|
|
createVM = name: attrs: let
|
2014-12-13 14:13:30 +03:00
|
|
|
mkFlags = concatStringsSep " ";
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2014-12-13 14:13:30 +03:00
|
|
|
sharePath = "/home/alice/vboxshare-${name}";
|
2014-12-13 13:15:48 +03:00
|
|
|
|
|
|
|
createFlags = mkFlags [
|
virtualbox: 5.2.28 -> 6.0.6
Quite some fixing was needed to get this to work.
Changes in VirtualBox and additions:
- VirtualBox is no longer officially supported on 32-bit hosts so i686-linux is removed from platforms
for VirtualBox and the extension pack. 32-bit additions still work.
- There was a refactoring of kernel module makefiles and two resulting bugs affected us which had to be patched.
These bugs were reported to the bug tracker (see comments near patches).
- The Qt5X11Extras makefile patch broke. Fixed it to apply again, making the libraries logic simpler
and more correct (it just uses a different base path instead of always linking to Qt5X11Extras).
- Added a patch to remove "test1" and "test2" kernel messages due to forgotten debugging code.
- virtualbox-host NixOS module: the VirtualBoxVM executable should be setuid not VirtualBox.
This matches how the official installer sets it up.
- Additions: replaced a for loop for installing kernel modules with just a "make install",
which seems to work without any of the things done in the previous code.
- Additions: The package defined buildCommand which resulted in phases not running, including RUNPATH
stripping in fixupPhase, and installPhase was defined which was not even run. Fixed this by
refactoring using phases. Had to set dontStrip otherwise binaries were broken by stripping.
The libdbus path had to be added later in fixupPhase because it is used via dlopen not directly linked.
- Additions: Added zlib and libc to patchelf, otherwise runtime library errors result from some binaries.
For some reason the missing libc only manifested itself for mount.vboxsf when included in the initrd.
Changes in nixos/tests/virtualbox:
- Update the simple-gui test to send the right keys to start the VM. With VirtualBox 5
it was enough to just send "return", but with 6 the Tools thing may be selected by
default. Send "home" to reliably select Tools, "down" to move to the VM and "return"
to start it.
- Disable the VirtualBox UART by default because it causes a crash due to a regression
in VirtualBox (specific to software virtualization and serial port usage). It can
still be enabled using an option but there is an assert that KVM nested virtualization
is enabled, which works around the problem (see below).
- Add an option to enable nested KVM virtualization, allowing VirtualBox to use hardware
virtualization. This works around the UART problem and also allows using 64-bit
guests, but requires a kernel module parameter.
- Add an option to run 64-bit guests. Tested that the tests pass with that. As mentioned
this requires KVM nested virtualization.
2019-05-04 17:18:39 +03:00
|
|
|
"--ostype ${if use64bitGuest then "Linux26_64" else "Linux26"}"
|
2014-12-13 13:15:48 +03:00
|
|
|
"--register"
|
|
|
|
];
|
|
|
|
|
2019-09-03 22:59:50 +03:00
|
|
|
vmFlags = mkFlags ([
|
|
|
|
"--uart1 0x3F8 4"
|
|
|
|
"--uartmode1 client /run/virtualbox-log-${name}.sock"
|
2015-09-22 10:24:10 +03:00
|
|
|
"--memory 768"
|
2016-09-14 00:17:47 +03:00
|
|
|
"--audio none"
|
2014-12-15 10:58:12 +03:00
|
|
|
] ++ (attrs.vmFlags or []));
|
2014-12-13 13:15:48 +03: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 10:58:12 +03:00
|
|
|
"--medium ${testVM name attrs}/disk.vdi"
|
2014-12-13 13:15:48 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
sharedFlags = mkFlags [
|
|
|
|
"--name vboxshare"
|
2014-12-13 14:13:30 +03:00
|
|
|
"--hostpath ${sharePath}"
|
2014-12-13 13:15:48 +03:00
|
|
|
];
|
2014-12-15 10:58:12 +03:00
|
|
|
|
|
|
|
nixstoreFlags = mkFlags [
|
|
|
|
"--name nixstore"
|
|
|
|
"--hostpath /nix/store"
|
|
|
|
"--readonly"
|
|
|
|
];
|
2014-12-13 14:13:30 +03:00
|
|
|
in {
|
|
|
|
machine = {
|
2019-09-03 22:59:50 +03:00
|
|
|
systemd.sockets."vboxtestlog-${name}" = {
|
2015-09-17 12:32:05 +03:00
|
|
|
description = "VirtualBox Test Machine Log Socket For ${name}";
|
2015-09-17 12:25:37 +03:00
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
before = [ "multi-user.target" ];
|
|
|
|
socketConfig.ListenStream = "/run/virtualbox-log-${name}.sock";
|
|
|
|
socketConfig.Accept = true;
|
|
|
|
};
|
|
|
|
|
2019-09-03 22:59:50 +03:00
|
|
|
systemd.services."vboxtestlog-${name}@" = {
|
2015-09-17 12:32:05 +03:00
|
|
|
description = "VirtualBox Test Machine Log For ${name}";
|
2015-09-17 12:25:37 +03:00
|
|
|
serviceConfig.StandardInput = "socket";
|
2022-06-22 00:35:51 +03:00
|
|
|
serviceConfig.StandardOutput = "journal";
|
2015-09-17 12:25:37 +03:00
|
|
|
serviceConfig.SyslogIdentifier = "GUEST-${name}";
|
|
|
|
serviceConfig.ExecStart = "${pkgs.coreutils}/bin/cat";
|
|
|
|
};
|
2014-12-13 14:13:30 +03:00
|
|
|
};
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2014-12-13 14:13:30 +03:00
|
|
|
testSubs = ''
|
2020-08-07 13:31:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
${name}_sharepath = "${sharePath}"
|
|
|
|
|
|
|
|
|
|
|
|
def check_running_${name}():
|
|
|
|
cmd = "VBoxManage list runningvms | grep -q '^\"${name}\"'"
|
|
|
|
(status, _) = machine.execute(ru(cmd))
|
|
|
|
return status == 0
|
|
|
|
|
|
|
|
|
|
|
|
def cleanup_${name}():
|
|
|
|
if check_running_${name}():
|
|
|
|
machine.execute(ru("VBoxManage controlvm ${name} poweroff"))
|
|
|
|
machine.succeed("rm -rf ${sharePath}")
|
|
|
|
machine.succeed("mkdir -p ${sharePath}")
|
2022-05-05 23:09:00 +03:00
|
|
|
machine.succeed("chown alice:users ${sharePath}")
|
2020-08-07 13:31:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
def create_vm_${name}():
|
2022-06-23 23:02:10 +03:00
|
|
|
cleanup_${name}()
|
2021-05-09 01:43:41 +03:00
|
|
|
vbm("createvm --name ${name} ${createFlags}")
|
|
|
|
vbm("modifyvm ${name} ${vmFlags}")
|
|
|
|
vbm("setextradata ${name} VBoxInternal/PDM/HaltOnReset 1")
|
|
|
|
vbm("storagectl ${name} ${controllerFlags}")
|
|
|
|
vbm("storageattach ${name} ${diskFlags}")
|
|
|
|
vbm("sharedfolder add ${name} ${sharedFlags}")
|
|
|
|
vbm("sharedfolder add ${name} ${nixstoreFlags}")
|
2020-08-07 13:31:26 +03:00
|
|
|
|
|
|
|
${mkLog "$HOME/VirtualBox VMs/${name}/Logs/VBox.log" "HOST-${name}"}
|
|
|
|
|
|
|
|
|
|
|
|
def destroy_vm_${name}():
|
|
|
|
cleanup_${name}()
|
|
|
|
vbm("unregistervm ${name} --delete")
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_vm_boot_${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"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_ip_${name}(interface):
|
|
|
|
property = f"/VirtualBox/GuestInfo/Net/{interface}/V4/IP"
|
|
|
|
getip = f"VBoxManage guestproperty get ${name} {property} | sed -n -e 's/^Value: //p'"
|
|
|
|
|
|
|
|
ip = machine.succeed(
|
|
|
|
ru(
|
|
|
|
"for i in $(seq 1000); do "
|
|
|
|
f'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"
|
|
|
|
)
|
|
|
|
).strip()
|
|
|
|
return ip
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_startup_${name}(nudge=lambda: None):
|
|
|
|
for _ in range(0, 130, 10):
|
|
|
|
machine.sleep(10)
|
|
|
|
if check_running_${name}():
|
|
|
|
return
|
|
|
|
nudge()
|
|
|
|
raise Exception("VirtualBox VM didn't start up within 2 minutes")
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_shutdown_${name}():
|
|
|
|
for _ in range(0, 130, 10):
|
|
|
|
machine.sleep(10)
|
|
|
|
if not check_running_${name}():
|
|
|
|
return
|
|
|
|
raise Exception("VirtualBox VM didn't shut down within 2 minutes")
|
|
|
|
|
|
|
|
|
|
|
|
def shutdown_vm_${name}():
|
|
|
|
machine.succeed(ru("touch ${sharePath}/shutdown"))
|
|
|
|
machine.execute(
|
|
|
|
"set -e; i=0; "
|
|
|
|
"while test -e ${sharePath}/shutdown "
|
|
|
|
" -o -e ${sharePath}/boot-done; do "
|
|
|
|
"sleep 1; i=$(($i + 1)); [ $i -le 3600 ]; "
|
|
|
|
"done"
|
|
|
|
)
|
|
|
|
wait_for_shutdown_${name}()
|
2014-12-13 14:13:30 +03:00
|
|
|
'';
|
|
|
|
};
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2014-12-15 10:58:12 +03:00
|
|
|
hostonlyVMFlags = [
|
|
|
|
"--nictype1 virtio"
|
|
|
|
"--nictype2 virtio"
|
|
|
|
"--nic2 hostonly"
|
|
|
|
"--hostonlyadapter2 vboxnet0"
|
|
|
|
];
|
|
|
|
|
2018-08-13 03:25:43 +03:00
|
|
|
# The VirtualBox Oracle Extension Pack lets you use USB 3.0 (xHCI).
|
|
|
|
enableExtensionPackVMFlags = [
|
|
|
|
"--usbxhci on"
|
|
|
|
];
|
|
|
|
|
2014-12-15 10:58:12 +03:00
|
|
|
dhcpScript = pkgs: ''
|
2022-06-25 20:11:34 +03:00
|
|
|
${pkgs.dhcpcd}/bin/dhcpcd eth0 eth1
|
2014-12-15 10:58:12 +03:00
|
|
|
|
2016-10-30 03:52:47 +03:00
|
|
|
otherIP="$(${pkgs.netcat}/bin/nc -l 1234 || :)"
|
2014-12-15 10:58:12 +03:00
|
|
|
${pkgs.iputils}/bin/ping -I eth1 -c1 "$otherIP"
|
2016-10-30 03:52:47 +03:00
|
|
|
echo "$otherIP reachable" | ${pkgs.netcat}/bin/nc -l 5678 || :
|
2014-12-15 10:58:12 +03:00
|
|
|
'';
|
|
|
|
|
2015-09-17 15:12:24 +03:00
|
|
|
sysdDetectVirt = pkgs: ''
|
|
|
|
${pkgs.systemd}/bin/systemd-detect-virt > /mnt-root/result
|
|
|
|
'';
|
|
|
|
|
2014-12-15 10:58:12 +03:00
|
|
|
vboxVMs = mapAttrs createVM {
|
|
|
|
simple = {};
|
|
|
|
|
2015-09-17 15:12:24 +03:00
|
|
|
detectvirt.vmScript = sysdDetectVirt;
|
|
|
|
|
2014-12-15 10:58:12 +03:00
|
|
|
test1.vmFlags = hostonlyVMFlags;
|
|
|
|
test1.vmScript = dhcpScript;
|
|
|
|
|
|
|
|
test2.vmFlags = hostonlyVMFlags;
|
|
|
|
test2.vmScript = dhcpScript;
|
2016-09-01 21:54:58 +03:00
|
|
|
|
|
|
|
headless.virtualisation.virtualbox.headless = true;
|
|
|
|
headless.services.xserver.enable = false;
|
2014-12-15 10:58:12 +03:00
|
|
|
};
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2018-08-13 03:25:43 +03:00
|
|
|
vboxVMsWithExtpack = mapAttrs createVM {
|
|
|
|
testExtensionPack.vmFlags = enableExtensionPackVMFlags;
|
|
|
|
};
|
|
|
|
|
2024-02-14 19:29:06 +03:00
|
|
|
mkVBoxTest = vboxHostConfig: vms: name: testScript: makeTest {
|
2016-02-29 21:15:36 +03:00
|
|
|
name = "virtualbox-${name}";
|
|
|
|
|
2022-03-21 02:15:30 +03:00
|
|
|
nodes.machine = { lib, config, ... }: {
|
2016-02-29 21:15:36 +03:00
|
|
|
imports = let
|
|
|
|
mkVMConf = name: val: val.machine // { key = "${name}-config"; };
|
2018-08-13 03:25:43 +03:00
|
|
|
vmConfigs = mapAttrsToList mkVMConf vms;
|
2016-02-29 21:15:36 +03:00
|
|
|
in [ ./common/user-account.nix ./common/x11.nix ] ++ vmConfigs;
|
|
|
|
virtualisation.memorySize = 2048;
|
2024-02-14 20:40:46 +03:00
|
|
|
|
|
|
|
virtualisation.qemu.options = let
|
|
|
|
# IvyBridge is reasonably ancient to be compatible with recent
|
|
|
|
# Intel/AMD hosts and sufficient for the KVM flavor.
|
|
|
|
guestCpu = if config.virtualisation.virtualbox.host.enableKvm then "IvyBridge" else "kvm64";
|
|
|
|
in ["-cpu" "${guestCpu},svm=on,vmx=on"];
|
|
|
|
|
2020-01-27 01:41:19 +03:00
|
|
|
test-support.displayManager.auto.user = "alice";
|
2018-06-30 02:55:42 +03:00
|
|
|
users.users.alice.extraGroups = let
|
2016-02-29 21:15:36 +03:00
|
|
|
inherit (config.virtualisation.virtualbox.host) enableHardening;
|
2024-02-14 19:29:06 +03:00
|
|
|
in lib.mkIf enableHardening [ "vboxusers" ];
|
|
|
|
|
|
|
|
virtualisation.virtualbox.host = {
|
|
|
|
enable = true;
|
|
|
|
} // vboxHostConfig;
|
|
|
|
|
|
|
|
nixpkgs.config.allowUnfree = config.virtualisation.virtualbox.host.enableExtensionPack;
|
2016-02-29 21:15:36 +03:00
|
|
|
};
|
2014-12-13 14:13:30 +03:00
|
|
|
|
2016-02-29 21:15:36 +03:00
|
|
|
testScript = ''
|
2020-08-07 13:31:26 +03:00
|
|
|
from shlex import quote
|
|
|
|
${concatStrings (mapAttrsToList (_: getAttr "testSubs") vms)}
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
def ru(cmd: str) -> str:
|
|
|
|
return f"su - alice -c {quote(cmd)}"
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2014-12-13 14:13:30 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
def vbm(cmd: str) -> str:
|
|
|
|
return machine.succeed(ru(f"VBoxManage {cmd}"))
|
|
|
|
|
|
|
|
|
|
|
|
def remove_uuids(output: str) -> str:
|
|
|
|
return "\n".join(
|
|
|
|
[line for line in (output or "").splitlines() if not line.startswith("UUID:")]
|
|
|
|
)
|
|
|
|
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
machine.wait_for_x()
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2016-02-29 21:15:36 +03:00
|
|
|
${mkLog "$HOME/.config/VirtualBox/VBoxSVC.log" "HOST-SVC"}
|
2014-12-15 21:17:38 +03:00
|
|
|
|
2016-02-29 21:15:36 +03:00
|
|
|
${testScript}
|
2020-08-07 13:31:26 +03:00
|
|
|
# (keep black happy)
|
2016-02-29 21:15:36 +03:00
|
|
|
'';
|
2014-12-13 14:13:30 +03:00
|
|
|
|
2021-01-10 22:08:30 +03:00
|
|
|
meta = with pkgs.lib.maintainers; {
|
2023-06-02 01:44:51 +03:00
|
|
|
maintainers = [ aszlig ];
|
2014-12-13 13:15:48 +03:00
|
|
|
};
|
2016-02-29 21:15:36 +03:00
|
|
|
};
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2024-02-14 19:29:06 +03:00
|
|
|
unfreeTests = mapAttrs (mkVBoxTest { enableExtensionPack = true; } vboxVMsWithExtpack) {
|
2018-08-28 23:28:47 +03:00
|
|
|
enable-extension-pack = ''
|
2020-08-07 13:31:26 +03:00
|
|
|
create_vm_testExtensionPack()
|
|
|
|
vbm("startvm testExtensionPack")
|
|
|
|
wait_for_startup_testExtensionPack()
|
|
|
|
machine.screenshot("cli_started")
|
|
|
|
wait_for_vm_boot_testExtensionPack()
|
|
|
|
machine.screenshot("cli_booted")
|
|
|
|
|
|
|
|
with machine.nested("Checking for privilege escalation"):
|
|
|
|
machine.fail("test -e '/root/VirtualBox VMs'")
|
|
|
|
machine.fail("test -e '/root/.config/VirtualBox'")
|
|
|
|
machine.succeed("test -e '/home/alice/VirtualBox VMs'")
|
|
|
|
|
|
|
|
shutdown_vm_testExtensionPack()
|
|
|
|
destroy_vm_testExtensionPack()
|
2018-08-28 23:28:47 +03:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-02-14 20:40:46 +03:00
|
|
|
kvmTests = mapAttrs (mkVBoxTest {
|
|
|
|
enableKvm = true;
|
|
|
|
|
|
|
|
# Once the KVM version supports these, we can enable them.
|
|
|
|
addNetworkInterface = false;
|
|
|
|
enableHardening = false;
|
|
|
|
} vboxVMs) {
|
|
|
|
kvm-headless = ''
|
|
|
|
create_vm_headless()
|
|
|
|
machine.succeed(ru("VBoxHeadless --startvm headless >&2 & disown %1"))
|
|
|
|
wait_for_startup_headless()
|
|
|
|
wait_for_vm_boot_headless()
|
|
|
|
shutdown_vm_headless()
|
|
|
|
destroy_vm_headless()
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-02-14 19:29:06 +03:00
|
|
|
in mapAttrs (mkVBoxTest {} vboxVMs) {
|
2016-02-29 21:15:36 +03:00
|
|
|
simple-gui = ''
|
virtualbox: 5.2.28 -> 6.0.6
Quite some fixing was needed to get this to work.
Changes in VirtualBox and additions:
- VirtualBox is no longer officially supported on 32-bit hosts so i686-linux is removed from platforms
for VirtualBox and the extension pack. 32-bit additions still work.
- There was a refactoring of kernel module makefiles and two resulting bugs affected us which had to be patched.
These bugs were reported to the bug tracker (see comments near patches).
- The Qt5X11Extras makefile patch broke. Fixed it to apply again, making the libraries logic simpler
and more correct (it just uses a different base path instead of always linking to Qt5X11Extras).
- Added a patch to remove "test1" and "test2" kernel messages due to forgotten debugging code.
- virtualbox-host NixOS module: the VirtualBoxVM executable should be setuid not VirtualBox.
This matches how the official installer sets it up.
- Additions: replaced a for loop for installing kernel modules with just a "make install",
which seems to work without any of the things done in the previous code.
- Additions: The package defined buildCommand which resulted in phases not running, including RUNPATH
stripping in fixupPhase, and installPhase was defined which was not even run. Fixed this by
refactoring using phases. Had to set dontStrip otherwise binaries were broken by stripping.
The libdbus path had to be added later in fixupPhase because it is used via dlopen not directly linked.
- Additions: Added zlib and libc to patchelf, otherwise runtime library errors result from some binaries.
For some reason the missing libc only manifested itself for mount.vboxsf when included in the initrd.
Changes in nixos/tests/virtualbox:
- Update the simple-gui test to send the right keys to start the VM. With VirtualBox 5
it was enough to just send "return", but with 6 the Tools thing may be selected by
default. Send "home" to reliably select Tools, "down" to move to the VM and "return"
to start it.
- Disable the VirtualBox UART by default because it causes a crash due to a regression
in VirtualBox (specific to software virtualization and serial port usage). It can
still be enabled using an option but there is an assert that KVM nested virtualization
is enabled, which works around the problem (see below).
- Add an option to enable nested KVM virtualization, allowing VirtualBox to use hardware
virtualization. This works around the UART problem and also allows using 64-bit
guests, but requires a kernel module parameter.
- Add an option to run 64-bit guests. Tested that the tests pass with that. As mentioned
this requires KVM nested virtualization.
2019-05-04 17:18:39 +03:00
|
|
|
# Home to select Tools, down to move to the VM, enter to start it.
|
2020-08-07 13:31:26 +03:00
|
|
|
def send_vm_startup():
|
|
|
|
machine.send_key("home")
|
|
|
|
machine.send_key("down")
|
|
|
|
machine.send_key("ret")
|
|
|
|
|
|
|
|
|
|
|
|
create_vm_simple()
|
2021-11-05 03:43:22 +03:00
|
|
|
machine.succeed(ru("VirtualBox >&2 &"))
|
2020-08-07 13:31:26 +03:00
|
|
|
machine.wait_until_succeeds(ru("xprop -name 'Oracle VM VirtualBox Manager'"))
|
|
|
|
machine.sleep(5)
|
|
|
|
machine.screenshot("gui_manager_started")
|
|
|
|
send_vm_startup()
|
|
|
|
machine.screenshot("gui_manager_sent_startup")
|
|
|
|
wait_for_startup_simple(send_vm_startup)
|
|
|
|
machine.screenshot("gui_started")
|
|
|
|
wait_for_vm_boot_simple()
|
|
|
|
machine.screenshot("gui_booted")
|
|
|
|
shutdown_vm_simple()
|
|
|
|
machine.sleep(5)
|
|
|
|
machine.screenshot("gui_stopped")
|
|
|
|
machine.send_key("ctrl-q")
|
|
|
|
machine.sleep(5)
|
|
|
|
machine.screenshot("gui_manager_stopped")
|
|
|
|
destroy_vm_simple()
|
2016-02-29 21:15:36 +03:00
|
|
|
'';
|
2014-12-13 13:15:48 +03:00
|
|
|
|
2016-02-29 21:15:36 +03:00
|
|
|
simple-cli = ''
|
2020-08-07 13:31:26 +03:00
|
|
|
create_vm_simple()
|
|
|
|
vbm("startvm simple")
|
|
|
|
wait_for_startup_simple()
|
|
|
|
machine.screenshot("cli_started")
|
|
|
|
wait_for_vm_boot_simple()
|
|
|
|
machine.screenshot("cli_booted")
|
|
|
|
|
|
|
|
with machine.nested("Checking for privilege escalation"):
|
|
|
|
machine.fail("test -e '/root/VirtualBox VMs'")
|
|
|
|
machine.fail("test -e '/root/.config/VirtualBox'")
|
|
|
|
machine.succeed("test -e '/home/alice/VirtualBox VMs'")
|
|
|
|
|
|
|
|
shutdown_vm_simple()
|
|
|
|
destroy_vm_simple()
|
2016-02-29 21:15:36 +03:00
|
|
|
'';
|
2015-09-18 00:47:33 +03:00
|
|
|
|
2016-09-01 21:54:58 +03:00
|
|
|
headless = ''
|
2020-08-07 13:31:26 +03:00
|
|
|
create_vm_headless()
|
2022-06-24 13:22:35 +03:00
|
|
|
machine.succeed(ru("VBoxHeadless --startvm headless >&2 & disown %1"))
|
2020-08-07 13:31:26 +03:00
|
|
|
wait_for_startup_headless()
|
|
|
|
wait_for_vm_boot_headless()
|
|
|
|
shutdown_vm_headless()
|
|
|
|
destroy_vm_headless()
|
2016-09-01 21:54:58 +03:00
|
|
|
'';
|
|
|
|
|
2016-02-29 21:15:36 +03:00
|
|
|
host-usb-permissions = ''
|
2022-06-25 20:35:51 +03:00
|
|
|
import sys
|
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
user_usb = remove_uuids(vbm("list usbhost"))
|
|
|
|
print(user_usb, file=sys.stderr)
|
|
|
|
root_usb = remove_uuids(machine.succeed("VBoxManage list usbhost"))
|
|
|
|
print(root_usb, file=sys.stderr)
|
|
|
|
|
|
|
|
if user_usb != root_usb:
|
|
|
|
raise Exception("USB host devices differ for root and normal user")
|
|
|
|
if "<none>" in user_usb:
|
|
|
|
raise Exception("No USB host devices found")
|
2016-02-29 21:15:36 +03:00
|
|
|
'';
|
2015-09-18 00:47:33 +03:00
|
|
|
|
2016-02-29 21:15:36 +03:00
|
|
|
systemd-detect-virt = ''
|
2020-08-07 13:31:26 +03:00
|
|
|
create_vm_detectvirt()
|
|
|
|
vbm("startvm detectvirt")
|
|
|
|
wait_for_startup_detectvirt()
|
|
|
|
wait_for_vm_boot_detectvirt()
|
|
|
|
shutdown_vm_detectvirt()
|
|
|
|
result = machine.succeed(f"cat '{detectvirt_sharepath}/result'").strip()
|
|
|
|
destroy_vm_detectvirt()
|
|
|
|
if result != "oracle":
|
|
|
|
raise Exception(f'systemd-detect-virt returned "{result}" instead of "oracle"')
|
2016-02-29 21:15:36 +03:00
|
|
|
'';
|
2015-09-17 15:12:24 +03:00
|
|
|
|
2016-02-29 21:15:36 +03:00
|
|
|
net-hostonlyif = ''
|
2020-08-07 13:31:26 +03:00
|
|
|
create_vm_test1()
|
|
|
|
create_vm_test2()
|
2014-12-15 10:58:12 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
vbm("startvm test1")
|
|
|
|
wait_for_startup_test1()
|
|
|
|
wait_for_vm_boot_test1()
|
2014-12-15 10:58:12 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
vbm("startvm test2")
|
|
|
|
wait_for_startup_test2()
|
|
|
|
wait_for_vm_boot_test2()
|
2014-12-15 10:58:12 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
machine.screenshot("net_booted")
|
2014-12-15 10:58:12 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
test1_ip = wait_for_ip_test1(1)
|
|
|
|
test2_ip = wait_for_ip_test2(1)
|
2014-12-15 10:58:12 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
machine.succeed(f"echo '{test2_ip}' | nc -N '{test1_ip}' 1234")
|
|
|
|
machine.succeed(f"echo '{test1_ip}' | nc -N '{test2_ip}' 1234")
|
2014-12-13 14:13:30 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
machine.wait_until_succeeds(f"nc -N '{test1_ip}' 5678 < /dev/null >&2")
|
|
|
|
machine.wait_until_succeeds(f"nc -N '{test2_ip}' 5678 < /dev/null >&2")
|
2014-12-15 10:58:12 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
shutdown_vm_test1()
|
|
|
|
shutdown_vm_test2()
|
2014-12-15 10:58:12 +03:00
|
|
|
|
2020-08-07 13:31:26 +03:00
|
|
|
destroy_vm_test1()
|
|
|
|
destroy_vm_test2()
|
2014-12-13 13:15:48 +03:00
|
|
|
'';
|
2024-02-14 20:40:46 +03:00
|
|
|
}
|
|
|
|
// (optionalAttrs enableKvm kvmTests)
|
|
|
|
// (optionalAttrs enableUnfree unfreeTests)
|