2010-10-05 16:44:40 +01:00
|
|
|
# Xen hypervisor (Dom0) support.
|
2010-09-12 23:43:45 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2010-09-12 23:43:45 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2010-09-12 23:43:45 +01:00
|
|
|
|
2011-09-14 19:20:50 +01:00
|
|
|
let
|
|
|
|
cfg = config.virtualisation.xen;
|
2010-09-16 16:24:46 +01:00
|
|
|
xen = pkgs.xen;
|
|
|
|
in
|
2010-09-12 23:43:45 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2011-09-14 19:20:50 +01:00
|
|
|
virtualisation.xen.enable =
|
2010-09-12 23:43:45 +01:00
|
|
|
mkOption {
|
|
|
|
default = false;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Setting this option enables the Xen hypervisor, a
|
|
|
|
virtualisation technology that allows multiple virtual
|
|
|
|
machines, known as <emphasis>domains</emphasis>, to run
|
|
|
|
concurrently on the physical machine. NixOS runs as the
|
|
|
|
privileged <emphasis>Domain 0</emphasis>. This option
|
|
|
|
requires a reboot to take effect.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-09-14 19:20:50 +01:00
|
|
|
virtualisation.xen.bootParams =
|
2010-09-12 23:43:45 +01:00
|
|
|
mkOption {
|
|
|
|
default = "";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Parameters passed to the Xen hypervisor at boot time.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-09-14 19:20:50 +01:00
|
|
|
virtualisation.xen.domain0MemorySize =
|
2010-09-12 23:43:45 +01:00
|
|
|
mkOption {
|
|
|
|
default = 0;
|
|
|
|
example = 512;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Amount of memory (in MiB) allocated to Domain 0 on boot.
|
|
|
|
If set to 0, all memory is assigned to Domain 0.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-01-29 08:58:28 +00:00
|
|
|
virtualisation.xen.bridge =
|
|
|
|
mkOption {
|
|
|
|
default = "xenbr0";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Create a bridge for the Xen domUs to connect to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
virtualisation.xen.stored =
|
|
|
|
mkOption {
|
2015-03-05 11:46:50 +00:00
|
|
|
type = types.path;
|
2015-01-29 08:58:28 +00:00
|
|
|
description =
|
|
|
|
''
|
2015-03-04 11:05:04 +00:00
|
|
|
Xen Store daemon to use. Defaults to oxenstored of the xen package.
|
2015-01-29 08:58:28 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
virtualisation.xen.trace =
|
|
|
|
mkOption {
|
|
|
|
default = false;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Enable Xen tracing.
|
|
|
|
'';
|
|
|
|
};
|
2010-09-12 23:43:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2015-01-29 08:58:28 +00:00
|
|
|
assertions = [ {
|
|
|
|
assertion = pkgs.stdenv.isx86_64;
|
|
|
|
message = "Xen currently not supported on ${pkgs.stdenv.system}";
|
|
|
|
} {
|
|
|
|
assertion = config.boot.loader.grub.enable && (config.boot.loader.grub.efiSupport == false);
|
|
|
|
message = "Xen currently does not support EFI boot";
|
|
|
|
} ];
|
2010-09-12 23:43:45 +01:00
|
|
|
|
2015-03-05 11:46:50 +00:00
|
|
|
virtualisation.xen.stored = mkDefault "${xen}/bin/oxenstored";
|
|
|
|
|
2010-09-16 16:24:46 +01:00
|
|
|
environment.systemPackages = [ xen ];
|
2010-09-12 23:43:45 +01:00
|
|
|
|
2015-01-29 08:58:28 +00:00
|
|
|
# Make sure Domain 0 gets the required configuration
|
|
|
|
#boot.kernelPackages = pkgs.boot.kernelPackages.override { features={xen_dom0=true;}; };
|
2010-09-12 23:43:45 +01:00
|
|
|
|
2011-09-14 19:20:50 +01:00
|
|
|
boot.kernelModules =
|
2015-01-29 08:58:28 +00:00
|
|
|
[ "xen-evtchn" "xen-gntdev" "xen-gntalloc" "xen-blkback" "xen-netback"
|
|
|
|
"xen-pciback" "evtchn" "gntdev" "netbk" "blkbk" "xen-scsibk"
|
|
|
|
"usbbk" "pciback" "xen-acpi-processor" "blktap2" "tun" "netxen_nic"
|
|
|
|
"xen_wdt" "xen-acpi-processor" "xen-privcmd" "xen-scsiback"
|
|
|
|
"xenfs"
|
2010-09-27 17:32:42 +01:00
|
|
|
];
|
2010-09-14 12:22:50 +01:00
|
|
|
|
2015-01-29 08:58:28 +00:00
|
|
|
|
2010-09-12 23:43:45 +01:00
|
|
|
# The radeonfb kernel module causes the screen to go black as soon
|
|
|
|
# as it's loaded, so don't load it.
|
|
|
|
boot.blacklistedKernelModules = [ "radeonfb" ];
|
|
|
|
|
2010-12-06 09:54:08 +00:00
|
|
|
# Increase the number of loopback devices from the default (8),
|
|
|
|
# which is way too small because every VM virtual disk requires a
|
|
|
|
# loopback device.
|
|
|
|
boot.extraModprobeConfig =
|
|
|
|
''
|
|
|
|
options loop max_loop=64
|
|
|
|
'';
|
|
|
|
|
2015-01-29 08:58:28 +00:00
|
|
|
virtualisation.xen.bootParams = [] ++
|
|
|
|
optionals cfg.trace [ "loglvl=all" "guest_loglvl=all" ] ++
|
2010-09-12 23:43:45 +01:00
|
|
|
optional (cfg.domain0MemorySize != 0) "dom0_mem=${toString cfg.domain0MemorySize}M";
|
|
|
|
|
|
|
|
system.extraSystemBuilderCmds =
|
|
|
|
''
|
2010-09-16 16:24:46 +01:00
|
|
|
ln -s ${xen}/boot/xen.gz $out/xen.gz
|
2010-09-12 23:43:45 +01:00
|
|
|
echo "${toString cfg.bootParams}" > $out/xen-params
|
|
|
|
'';
|
|
|
|
|
2010-09-12 23:56:54 +01:00
|
|
|
# Mount the /proc/xen pseudo-filesystem.
|
2010-09-13 16:41:38 +01:00
|
|
|
system.activationScripts.xen =
|
2010-09-12 23:56:54 +01:00
|
|
|
''
|
|
|
|
if [ -d /proc/xen ]; then
|
2015-01-29 08:58:28 +00:00
|
|
|
${pkgs.kmod}/bin/modprobe xenfs 2> /dev/null
|
|
|
|
${pkgs.utillinux}/bin/mountpoint -q /proc/xen || \
|
2010-09-12 23:56:54 +01:00
|
|
|
${pkgs.utillinux}/bin/mount -t xenfs none /proc/xen
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2015-01-29 08:58:28 +00:00
|
|
|
# Domain 0 requires a pvops-enabled kernel.
|
|
|
|
system.requiredKernelConfig = with config.lib.kernelConfig;
|
|
|
|
[ (isYes "XEN")
|
|
|
|
(isYes "X86_IO_APIC")
|
|
|
|
(isYes "ACPI")
|
|
|
|
(isYes "XEN_DOM0")
|
|
|
|
(isYes "PCI_XEN")
|
|
|
|
(isYes "XEN_DEV_EVTCHN")
|
|
|
|
(isYes "XENFS")
|
|
|
|
(isYes "XEN_COMPAT_XENFS")
|
|
|
|
(isYes "XEN_SYS_HYPERVISOR")
|
|
|
|
(isYes "XEN_GNTDEV")
|
|
|
|
(isYes "XEN_BACKEND")
|
|
|
|
(isModule "XEN_NETDEV_BACKEND")
|
|
|
|
(isModule "XEN_BLKDEV_BACKEND")
|
|
|
|
(isModule "XEN_PCIDEV_BACKEND")
|
|
|
|
(isYes "XEN_BALLOON")
|
|
|
|
(isYes "XEN_SCRUB_PAGES")
|
|
|
|
];
|
2010-10-06 17:07:16 +01:00
|
|
|
|
2010-09-14 12:58:06 +01:00
|
|
|
|
2010-09-16 16:24:46 +01:00
|
|
|
environment.etc =
|
2015-01-29 08:58:28 +00:00
|
|
|
[ { source = "${xen}/etc/xen/xl.conf";
|
|
|
|
target = "xen/xl.conf";
|
2010-09-16 16:24:46 +01:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
# Xen provides udev rules.
|
|
|
|
services.udev.packages = [ xen ];
|
|
|
|
|
2015-02-20 20:43:09 +00:00
|
|
|
services.udev.path = [ pkgs.bridge-utils pkgs.iproute ];
|
2010-09-16 16:24:46 +01:00
|
|
|
|
2015-01-29 08:58:28 +00:00
|
|
|
systemd.services.xen-store = {
|
|
|
|
description = "Xen Store Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" "xen-store.socket" ];
|
|
|
|
requires = [ "xen-store.socket" ];
|
|
|
|
preStart = ''
|
|
|
|
export XENSTORED_ROOTDIR="/var/lib/xenstored"
|
|
|
|
rm -f "$XENSTORED_ROOTDIR"/tdb* &>/dev/null
|
|
|
|
|
|
|
|
mkdir -p /var/run
|
|
|
|
${optionalString cfg.trace "mkdir -p /var/log/xen"}
|
|
|
|
grep -q control_d /proc/xen/capabilities
|
|
|
|
'';
|
|
|
|
serviceConfig.ExecStart = ''
|
|
|
|
${cfg.stored}${optionalString cfg.trace " -T /var/log/xen/xenstored-trace.log"} --no-fork
|
|
|
|
'';
|
|
|
|
postStart = ''
|
|
|
|
time=0
|
|
|
|
timeout=30
|
|
|
|
# Wait for xenstored to actually come up, timing out after 30 seconds
|
|
|
|
while [ $time -lt $timeout ] && ! `${pkgs.xen}/bin/xenstore-read -s / >/dev/null 2>&1` ; do
|
|
|
|
time=$(($time+1))
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
# Exit if we timed out
|
|
|
|
if ! [ $time -lt $timeout ] ; then
|
|
|
|
echo "Could not start Xenstore Daemon"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
${pkgs.xen}/bin/xenstore-write "/local/domain/0/name" "Domain-0"
|
|
|
|
${pkgs.xen}/bin/xenstore-write "/local/domain/0/domid" 0
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.sockets.xen-store = {
|
|
|
|
description = "XenStore Socket for userspace API";
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
socketConfig = {
|
|
|
|
ListenStream = [ "/var/run/xenstored/socket" "/var/run/xenstored/socket_ro" ];
|
|
|
|
SocketMode = "0660";
|
|
|
|
SocketUser = "root";
|
|
|
|
SocketGroup = "root";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
systemd.services.xen-console = {
|
|
|
|
description = "Xen Console Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "xen-store.service" ];
|
|
|
|
preStart = ''
|
|
|
|
mkdir -p /var/run/xen
|
|
|
|
${optionalString cfg.trace "mkdir -p /var/log/xen"}
|
|
|
|
grep -q control_d /proc/xen/capabilities
|
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = ''
|
|
|
|
${pkgs.xen}/bin/xenconsoled${optionalString cfg.trace " --log=all --log-dir=/var/log/xen"}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
systemd.services.xen-qemu = {
|
|
|
|
description = "Xen Qemu Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "xen-console.service" ];
|
|
|
|
serviceConfig.ExecStart = ''
|
|
|
|
${pkgs.xen}/lib/xen/bin/qemu-system-i386 -xen-domid 0 -xen-attach -name dom0 -nographic -M xenpv \
|
|
|
|
-monitor /dev/null -serial /dev/null -parallel /dev/null
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
systemd.services.xen-watchdog = {
|
|
|
|
description = "Xen Watchdog Daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "xen-qemu.service" ];
|
|
|
|
serviceConfig.ExecStart = "${pkgs.xen}/bin/xenwatchdogd 30 15";
|
|
|
|
serviceConfig.Type = "forking";
|
|
|
|
serviceConfig.RestartSec = "1";
|
|
|
|
serviceConfig.Restart = "on-failure";
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
systemd.services.xen-bridge = {
|
|
|
|
description = "Xen bridge";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
before = [ "xen-domains.service" ];
|
|
|
|
serviceConfig.RemainAfterExit = "yes";
|
|
|
|
serviceConfig.ExecStart = ''
|
|
|
|
${pkgs.bridge-utils}/bin/brctl addbr ${cfg.bridge}
|
|
|
|
${pkgs.inetutils}/bin/ifconfig ${cfg.bridge} up
|
|
|
|
'';
|
|
|
|
serviceConfig.ExecStop = ''
|
|
|
|
${pkgs.inetutils}/bin/ifconfig ${cfg.bridge} down
|
|
|
|
${pkgs.bridge-utils}/bin/brctl delbr ${cfg.bridge}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.xen-domains = {
|
|
|
|
description = "Xen domains - automatically starts, saves and restores Xen domains";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "xen-bridge.service" "xen-qemu.service" ];
|
|
|
|
## To prevent a race between dhcpcd and xend's bridge setup script
|
|
|
|
## (which renames eth* to peth* and recreates eth* as a virtual
|
|
|
|
## device), start dhcpcd after xend.
|
|
|
|
before = [ "dhcpd.service" ];
|
|
|
|
restartIfChanged = false;
|
|
|
|
serviceConfig.RemainAfterExit = "yes";
|
|
|
|
path = [ pkgs.xen ];
|
|
|
|
environment.XENDOM_CONFIG = "${pkgs.xen}/etc/sysconfig/xendomains";
|
|
|
|
preStart = "mkdir -p /var/lock/subsys -m 755";
|
|
|
|
serviceConfig.ExecStart = "${pkgs.xen}/etc/init.d/xendomains start";
|
|
|
|
serviceConfig.ExecStop = "${pkgs.xen}/etc/init.d/xendomains stop";
|
|
|
|
};
|
|
|
|
|
2010-09-12 23:43:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|