summary refs log tree commit diff
path: root/nixos/modules/virtualisation/xen-dom0.nix
blob: 566059472c9f214fc7f129b393aa34efed16ff89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Xen hypervisor (Dom0) support.

{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.virtualisation.xen;

  xen = pkgs.xen;

  xendConfig = pkgs.writeText "xend-config.sxp"
    ''
      (loglevel DEBUG)
      (network-script network-bridge)
      (vif-script vif-bridge)
    '';

in

{
  ###### interface

  options = {

    virtualisation.xen.enable =
      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.
          '';
      };

    virtualisation.xen.bootParams =
      mkOption {
        default = "";
        description =
          ''
            Parameters passed to the Xen hypervisor at boot time.
          '';
      };

    virtualisation.xen.domain0MemorySize =
      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.
          '';
      };

  };


  ###### implementation

  config = mkIf cfg.enable {

    environment.systemPackages = [ xen ];

    # Domain 0 requires a pvops-enabled kernel.
    boot.kernelPackages = pkgs.linuxPackages_3_2_xen;

    boot.kernelModules =
      [ "xen_evtchn" "xen_gntdev" "xen_blkback" "xen_netback" "xen_pciback"
        "blktap" "tun"
      ];

    # The radeonfb kernel module causes the screen to go black as soon
    # as it's loaded, so don't load it.
    boot.blacklistedKernelModules = [ "radeonfb" ];

    # 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
      '';

    virtualisation.xen.bootParams =
      [ "loglvl=all" "guest_loglvl=all" ] ++
      optional (cfg.domain0MemorySize != 0) "dom0_mem=${toString cfg.domain0MemorySize}M";

    system.extraSystemBuilderCmds =
      ''
        ln -s ${xen}/boot/xen.gz $out/xen.gz
        echo "${toString cfg.bootParams}" > $out/xen-params
      '';

    # Mount the /proc/xen pseudo-filesystem.
    system.activationScripts.xen =
      ''
        if [ -d /proc/xen ]; then
            ${pkgs.sysvtools}/bin/mountpoint -q /proc/xen || \
                ${pkgs.utillinux}/bin/mount -t xenfs none /proc/xen
        fi
      '';

    jobs.xend =
      { description = "Xen Control Daemon";

        startOn = "stopped udevtrigger";

        path =
          [ pkgs.bridge_utils pkgs.gawk pkgs.iproute pkgs.nettools
            pkgs.utillinux pkgs.bash xen pkgs.pciutils pkgs.procps
          ];

        environment.XENCONSOLED_TRACE = "hv";

        preStart =
          ''
            mkdir -p /var/log/xen/console -m 0700

            ${xen}/sbin/xend start

            # Wait until Xend is running.
            for ((i = 0; i < 60; i++)); do echo "waiting for xend..."; ${xen}/sbin/xend status && break; done

            ${xen}/sbin/xend status || exit 1
          '';

        postStop = "${xen}/sbin/xend stop";
      };

    jobs.xendomains =
      { description = "Automatically starts, saves and restores Xen domains on startup/shutdown";

        startOn = "started xend";

        stopOn = "starting shutdown and stopping xend";

        restartIfChanged = false;
        
        path = [ pkgs.xen ];

        environment.XENDOM_CONFIG = "${xen}/etc/sysconfig/xendomains";

        preStart =
          ''
            mkdir -p /var/lock/subsys -m 755
            ${xen}/etc/init.d/xendomains start
          '';

        postStop = "${xen}/etc/init.d/xendomains stop";
      };

    # 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.
    jobs.dhcpcd.startOn = mkOverride 50 "started xend";

    environment.etc =
      [ { source = xendConfig;
          target = "xen/xend-config.sxp";
        }
        { source = "${xen}/etc/xen/scripts";
          target = "xen/scripts";
        }
      ];

    # Xen provides udev rules.
    services.udev.packages = [ xen ];

    services.udev.path = [ pkgs.bridge_utils pkgs.iproute ];

  };

}