summary refs log tree commit diff
path: root/pkgs/applications/virtualization/xen/generic.nix
blob: fcd7aab808b3bf35f1f8a92a9714c22517b92e81 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{ stdenv, fetchurl, which, zlib, pkgconfig, SDL, openssl
, libuuid, gettext, ncurses, dev86, iasl, pciutils, bzip2
, lvm2, utillinux, procps, texinfo, perl, pythonPackages
, glib, bridge-utils, xorg, pixman, iproute, udev, bison
, flex, cmake, ocaml, ocamlPackages, figlet, libaio, yajl
, checkpolicy, transfig, glusterfs, acl, fetchgit, xz, spice
, spice_protocol, usbredir, alsaLib, quilt
, coreutils, gawk, gnused, gnugrep, diffutils, multipath-tools
, inetutils, iptables, openvswitch, nbd, drbd, xenConfig
, xenserverPatched ? false, ... }:

with stdenv.lib;

let

  libDir = if stdenv.is64bit then "lib64" else "lib";

  # Sources needed to build the stubdoms and tools
  # These sources are already rather old and probably do not change frequently
  xenExtfiles = [
      { url = http://xenbits.xensource.com/xen-extfiles/lwip-1.3.0.tar.gz;
        sha256 = "13wlr85s1hnvia6a698qpryyy12lvmqw0a05xmjnd0h71ralsbkp";
      }
      { url = http://xenbits.xensource.com/xen-extfiles/zlib-1.2.3.tar.gz;
        sha256 = "0pmh8kifb6sfkqfxc23wqp3f2wzk69sl80yz7w8p8cd4cz8cg58p";
      }
      { url = http://xenbits.xensource.com/xen-extfiles/newlib-1.16.0.tar.gz;
        sha256 = "01rxk9js833mwadq92jx0flvk9jyjrnwrq93j39c2j2wjsa66hnv";
      }
      { url = http://xenbits.xensource.com/xen-extfiles/grub-0.97.tar.gz;
        sha256 = "02r6b52r0nsp6ryqfiqchnl7r1d9smm80sqx24494gmx5p8ia7af";
      }
      { url = http://xenbits.xensource.com/xen-extfiles/pciutils-2.2.9.tar.bz2;
        sha256 = "092v4q478i1gc7f3s2wz6p4xlf1wb4gs5shbkn21vnnmzcffc2pn";
      }
      { url = http://xenbits.xensource.com/xen-extfiles/tpm_emulator-0.7.4.tar.gz;
        sha256 = "0nd4vs48j0zfzv1g5jymakxbjqf9ss6b2jph3b64356xhc6ylj2f";
      }
      { url = http://xenbits.xensource.com/xen-extfiles/tboot-20090330.tar.gz;
        sha256 = "0rl1b53g019w2c268pyxhjqsj9ls37i4p74bdv1hdi2yvs0r1y81";
      }
      { url = http://xenbits.xensource.com/xen-extfiles/ipxe-git-9a93db3f0947484e30e753bbd61a10b17336e20e.tar.gz;
        sha256 = "0p206zaxlhda60ci33h9gipi5gm46fvvsm6k5c0w7b6cjg0yhb33";
      }
      { url = http://xenbits.xensource.com/xen-extfiles/polarssl-1.1.4-gpl.tgz;
        sha256 = "1dl4fprpwagv9akwqpb62qwqvh24i50znadxwvd2kfnhl02gsa9d";
      }
      { url = http://xenbits.xensource.com/xen-extfiles/gmp-4.3.2.tar.bz2;
        sha256 = "0x8prpqi9amfcmi7r4zrza609ai9529pjaq0h4aw51i867064qck";
      }
    ];

  scriptEnvPath = stdenv.lib.concatStrings (stdenv.lib.intersperse ":" (map (x: "${x}/bin")
    [ coreutils gawk gnused gnugrep which perl diffutils utillinux multipath-tools
      iproute inetutils iptables bridge-utils openvswitch nbd drbd ]));
in



stdenv.mkDerivation {
  inherit (xenConfig) name version src;

  dontUseCmakeConfigure = true;

  buildInputs =
    [ which zlib pkgconfig SDL openssl libuuid gettext ncurses
      dev86 iasl pciutils bzip2 xz texinfo perl yajl
      pythonPackages.python pythonPackages.wrapPython
      glib bridge-utils pixman iproute udev bison xorg.libX11
      flex ocaml ocamlPackages.findlib figlet libaio
      checkpolicy pythonPackages.markdown transfig
      glusterfs acl cmake spice spice_protocol usbredir
      alsaLib quilt
    ];

  pythonPath = [ pythonPackages.curses ];

  patches = stdenv.lib.optionals ((xenserverPatched == false) && (builtins.hasAttr "xenPatches" xenConfig)) xenConfig.xenPatches;

  postPatch = ''
      ${stdenv.lib.optionalString ((xenserverPatched == true) && (builtins.hasAttr "xenserverPatches" xenConfig)) xenConfig.xenserverPatches}

      # Xen's tools and firmares need various git repositories that it
      # usually checks out at time using git.  We can't have that.
      ${flip concatMapStrings xenConfig.toolsGits (x: let src = fetchgit x.git; in ''
        cp -r ${src} tools/${src.name}-dir-remote
        chmod -R +w tools/${src.name}-dir-remote
      '' + stdenv.lib.optionalString (builtins.hasAttr "patches" x) ''
        ( cd tools/${src.name}-dir-remote; ${concatStringsSep "; " (map (p: "patch -p1 < ${p}") x.patches)} )
      '')}
      ${flip concatMapStrings xenConfig.firmwareGits (x: let src = fetchgit x.git; in ''
        cp -r ${src} tools/firmware/${src.name}-dir-remote
        chmod -R +w tools/firmware/${src.name}-dir-remote
      '' + stdenv.lib.optionalString (builtins.hasAttr "patches" x) ''
        ( cd tools/firmware/${src.name}-dir-remote; ${concatStringsSep "; " (map (p: "patch -p1 < ${p}") x.patches)} )
      '')}

      # Xen's stubdoms and firmwares need various sources that are usually fetched
      # at build time using wget. We can't have that, so we prefetch Xen's ext_files.
      mkdir xen_ext_files
      ${flip concatMapStrings xenExtfiles (x: let src = fetchurl x; in ''
        cp ${src} xen_ext_files/${src.name}
      '')}
  '';

  preConfigure = ''
    # Fake wget: copy prefetched downloads instead
    mkdir wget
    echo "#!/bin/sh" > wget/wget
    echo "echo ===== Not fetching \$*, copy pre-fetched file instead" >> wget/wget
    echo "cp \$4 \$3" >> wget/wget
    chmod +x wget/wget
    export PATH=$PATH:$PWD/wget
    export EXTRA_QEMUU_CONFIGURE_ARGS="--enable-spice --enable-usb-redir --enable-linux-aio"
  '';

  postConfigure =
    ''
      substituteInPlace tools/libfsimage/common/fsimage_plugin.c \
        --replace /usr $out

      substituteInPlace tools/blktap2/lvm/lvm-util.c \
        --replace /usr/sbin/vgs ${lvm2}/sbin/vgs \
        --replace /usr/sbin/lvs ${lvm2}/sbin/lvs

      substituteInPlace tools/hotplug/Linux/network-bridge \
        --replace /usr/bin/logger ${utillinux}/bin/logger

      substituteInPlace tools/xenmon/xenmon.py \
        --replace /usr/bin/pkill ${procps}/bin/pkill

      substituteInPlace tools/xenstat/Makefile \
        --replace /usr/include/curses.h ${ncurses.dev}/include/curses.h

      substituteInPlace tools/qemu-xen-traditional/xen-hooks.mak \
        --replace /usr/include/pci ${pciutils}/include/pci

      substituteInPlace tools/qemu-xen-traditional-dir-remote/xen-hooks.mak \
        --replace /usr/include/pci ${pciutils}/include/pci

      substituteInPlace tools/hotplug/Linux/xen-backend.rules \
        --replace /etc/xen/scripts $out/etc/xen/scripts

      # blktap is not provided by xen, but by xapi
      sed -i '/blktap/d' tools/hotplug/Linux/xen-backend.rules

      # Work around a bug in our GCC wrapper: `gcc -MF foo -v' doesn't
      # print the GCC version number properly.
      substituteInPlace xen/Makefile \
        --replace '$(CC) $(CFLAGS) -v' '$(CC) -v'

      # Allow the location of the xendomains config file to be
      # overriden at runtime.
      substituteInPlace tools/hotplug/Linux/init.d/xendomains \
        --replace 'XENDOM_CONFIG=/etc/sysconfig/xendomains' "" \
        --replace 'XENDOM_CONFIG=/etc/default/xendomains' "" \
        --replace /etc/xen/scripts/hotplugpath.sh $out/etc/xen/scripts/hotplugpath.sh \
        --replace /bin/ls ls

      substituteInPlace tools/hotplug/Linux/xendomains \
        --replace /bin/ls ls

      # Hack to get `gcc -m32' to work without having 32-bit Glibc headers.
      mkdir -p tools/include/gnu
      touch tools/include/gnu/stubs-32.h
    '';

  # TODO: Flask needs more testing before enabling it by default.
  #makeFlags = "XSM_ENABLE=y FLASK_ENABLE=y PREFIX=$(out) CONFIG_DIR=/etc XEN_EXTFILES_URL=\\$(XEN_ROOT)/xen_ext_files ";
  makeFlags = "PREFIX=$(out) CONFIG_DIR=/etc XEN_EXTFILES_URL=\\$(XEN_ROOT)/xen_ext_files ";

  buildFlags = "xen tools stubdom";

  postBuild =
    ''
      make -C docs man-pages

      (cd tools/xen-libhvm-dir-remote; make)
      (cd tools/xen-libhvm-dir-remote/biospt; cc -Wall -g -D_LINUX -Wstrict-prototypes biospt.c -o biospt -I../libhvm -L../libhvm -lxenhvm)
    '';

  installPhase =
    ''
      mkdir -p $out $out/share
      cp -prvd dist/install/nix/store/*/* $out/
      cp -prvd dist/install/boot $out/boot
      cp -prvd dist/install/etc $out
      cp -dR docs/man1 docs/man5 $out/share/man/
      wrapPythonPrograms
      substituteInPlace $out/etc/xen/scripts/hotplugpath.sh --replace SBINDIR=\"$out/sbin\" SBINDIR=\"$out/bin\"

      shopt -s extglob
      for i in $out/etc/xen/scripts/!(*.sh); do
        sed -i "2s@^@export PATH=$out/bin:${scriptEnvPath}\n@" $i
      done

      (cd tools/xen-libhvm-dir-remote; make install)
      cp tools/xen-libhvm-dir-remote/biospt/biospt $out/bin/.
    '';

  meta = {
    homepage = http://www.xen.org/;
    description = "Xen hypervisor and management tools for Dom0";
    platforms = [ "x86_64-linux" ];
    maintainers = with stdenv.lib.maintainers; [ eelco tstrobel ];
  };
}