about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/virtualisation/virtualbox-image.nix
blob: 0da217fd1cb0cb5ca32b8e2cfa8618258cfa8bb7 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.virtualbox;

in {

  options = {
    virtualbox = {
      baseImageSize = mkOption {
        type = with types; either (enum [ "auto" ]) int;
        default = "auto";
        example = 50 * 1024;
        description = lib.mdDoc ''
          The size of the VirtualBox base image in MiB.
        '';
      };
      baseImageFreeSpace = mkOption {
        type = with types; int;
        default = 30 * 1024;
        description = lib.mdDoc ''
          Free space in the VirtualBox base image in MiB.
        '';
      };
      memorySize = mkOption {
        type = types.int;
        default = 1536;
        description = lib.mdDoc ''
          The amount of RAM the VirtualBox appliance can use in MiB.
        '';
      };
      vmDerivationName = mkOption {
        type = types.str;
        default = "nixos-ova-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
        description = lib.mdDoc ''
          The name of the derivation for the VirtualBox appliance.
        '';
      };
      vmName = mkOption {
        type = types.str;
        default = "${config.system.nixos.distroName} ${config.system.nixos.label} (${pkgs.stdenv.hostPlatform.system})";
        description = lib.mdDoc ''
          The name of the VirtualBox appliance.
        '';
      };
      vmFileName = mkOption {
        type = types.str;
        default = "nixos-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.ova";
        description = lib.mdDoc ''
          The file name of the VirtualBox appliance.
        '';
      };
      params = mkOption {
        type = with types; attrsOf (oneOf [ str int bool (listOf str) ]);
        example = {
          audio = "alsa";
          rtcuseutc = "on";
          usb = "off";
        };
        description = lib.mdDoc ''
          Parameters passed to the Virtualbox appliance.

          Run `VBoxManage modifyvm --help` to see more options.
        '';
      };
      exportParams = mkOption {
        type = with types; listOf (oneOf [ str int bool (listOf str) ]);
        example = [
          "--vsys" "0" "--vendor" "ACME Inc."
        ];
        default = [];
        description = lib.mdDoc ''
          Parameters passed to the Virtualbox export command.

          Run `VBoxManage export --help` to see more options.
        '';
      };
      extraDisk = mkOption {
        description = lib.mdDoc ''
          Optional extra disk/hdd configuration.
          The disk will be an 'ext4' partition on a separate file.
        '';
        default = null;
        example = {
          label = "storage";
          mountPoint = "/home/demo/storage";
          size = 100 * 1024;
        };
        type = types.nullOr (types.submodule {
          options = {
            size = mkOption {
              type = types.int;
              description = lib.mdDoc "Size in MiB";
            };
            label = mkOption {
              type = types.str;
              default = "vm-extra-storage";
              description = lib.mdDoc "Label for the disk partition";
            };
            mountPoint = mkOption {
              type = types.str;
              description = lib.mdDoc "Path where to mount this disk.";
            };
          };
        });
      };
      postExportCommands = mkOption {
        type = types.lines;
        default = "";
        example = ''
          ${pkgs.cot}/bin/cot edit-hardware "$fn" \
            -v vmx-14 \
            --nics 2 \
            --nic-types VMXNET3 \
            --nic-names 'Nic name' \
            --nic-networks 'Nic match' \
            --network-descriptions 'Nic description' \
            --scsi-subtypes VirtualSCSI
        '';
        description = lib.mdDoc ''
          Extra commands to run after exporting the OVA to `$fn`.
        '';
      };
      storageController = mkOption {
        type = with types; attrsOf (oneOf [ str int bool (listOf str) ]);
        example = {
          name = "SCSI";
          add = "scsi";
          portcount = 16;
          bootable = "on";
          hostiocache = "on";
        };
        default = {
          name = "SATA";
          add = "sata";
          portcount = 4;
          bootable = "on";
          hostiocache = "on";
        };
        description = lib.mdDoc ''
          Parameters passed to the VirtualBox appliance. Must have at least
          `name`.

          Run `VBoxManage storagectl --help` to see more options.
        '';
      };
    };
  };

  config = {

    virtualbox.params = mkMerge [
      (mapAttrs (name: mkDefault) {
        acpi = "on";
        vram = 32;
        nictype1 = "virtio";
        nic1 = "nat";
        audiocontroller = "ac97";
        audio = "alsa";
        audioout = "on";
        graphicscontroller = "vmsvga";
        rtcuseutc = "on";
        usb = "on";
        usbehci = "on";
        mouse = "usbtablet";
      })
      (mkIf (pkgs.stdenv.hostPlatform.system == "i686-linux") { pae = "on"; })
    ];

    system.build.virtualBoxOVA = import ../../lib/make-disk-image.nix {
      name = cfg.vmDerivationName;

      inherit pkgs lib config;
      partitionTableType = "legacy";
      diskSize = cfg.baseImageSize;
      additionalSpace = "${toString cfg.baseImageFreeSpace}M";

      postVM =
        ''
          export HOME=$PWD
          export PATH=${pkgs.virtualbox}/bin:$PATH

          echo "converting image to VirtualBox format..."
          VBoxManage convertfromraw $diskImage disk.vdi

          ${optionalString (cfg.extraDisk != null) ''
            echo "creating extra disk: data-disk.raw"
            dataDiskImage=data-disk.raw
            truncate -s ${toString cfg.extraDisk.size}M $dataDiskImage

            parted --script $dataDiskImage -- \
              mklabel msdos \
              mkpart primary ext4 1MiB -1
            eval $(partx $dataDiskImage -o START,SECTORS --nr 1 --pairs)
            mkfs.ext4 -F -L ${cfg.extraDisk.label} $dataDiskImage -E offset=$(sectorsToBytes $START) $(sectorsToKilobytes $SECTORS)K
            echo "creating extra disk: data-disk.vdi"
            VBoxManage convertfromraw $dataDiskImage data-disk.vdi
          ''}

          echo "creating VirtualBox VM..."
          vmName="${cfg.vmName}";
          VBoxManage createvm --name "$vmName" --register \
            --ostype ${if pkgs.stdenv.hostPlatform.system == "x86_64-linux" then "Linux26_64" else "Linux26"}
          VBoxManage modifyvm "$vmName" \
            --memory ${toString cfg.memorySize} \
            ${lib.cli.toGNUCommandLineShell { } cfg.params}
          VBoxManage storagectl "$vmName" ${lib.cli.toGNUCommandLineShell { } cfg.storageController}
          VBoxManage storageattach "$vmName" --storagectl ${cfg.storageController.name} --port 0 --device 0 --type hdd \
            --medium disk.vdi
          ${optionalString (cfg.extraDisk != null) ''
            VBoxManage storageattach "$vmName" --storagectl ${cfg.storageController.name} --port 1 --device 0 --type hdd \
            --medium data-disk.vdi
          ''}

          echo "exporting VirtualBox VM..."
          mkdir -p $out
          fn="$out/${cfg.vmFileName}"
          VBoxManage export "$vmName" --output "$fn" --options manifest ${escapeShellArgs cfg.exportParams}
          ${cfg.postExportCommands}

          rm -v $diskImage

          mkdir -p $out/nix-support
          echo "file ova $fn" >> $out/nix-support/hydra-build-products
        '';
    };

    fileSystems = {
      "/" = {
        device = "/dev/disk/by-label/nixos";
        autoResize = true;
        fsType = "ext4";
      };
    } // (lib.optionalAttrs (cfg.extraDisk != null) {
      ${cfg.extraDisk.mountPoint} = {
        device = "/dev/disk/by-label/" + cfg.extraDisk.label;
        autoResize = true;
        fsType = "ext4";
      };
    });

    boot.growPartition = true;
    boot.loader.grub.device = "/dev/sda";

    swapDevices = [{
      device = "/var/swap";
      size = 2048;
    }];

    virtualisation.virtualbox.guest.enable = true;

  };
}