blob: c3580274b4a6da51db1445419017af81d880a46f (
plain)
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
|
{
config,
lib,
pkgs,
...
}:
with lib;
let
cfg = config.virtualisation.azureImage;
in
{
imports = [
./azure-common.nix
./disk-size-option.nix
../image/file-options.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
"virtualisation"
"azureImage"
"diskSize"
];
to = [
"virtualisation"
"diskSize"
];
})
];
options.virtualisation.azureImage = {
bootSize = mkOption {
type = types.int;
default = 256;
description = ''
ESP partition size. Unit is MB.
Only effective when vmGeneration is `v2`.
'';
};
contents = mkOption {
type = with types; listOf attrs;
default = [ ];
description = ''
Extra contents to add to the image.
'';
};
label = mkOption {
type = types.str;
default = "nixos";
description = ''
NixOS partition label.
'';
};
vmGeneration = mkOption {
type =
with types;
enum [
"v1"
"v2"
];
default = "v1";
description = ''
VM Generation to use.
For v2, secure boot needs to be turned off during creation.
'';
};
additionalSpace = mkOption {
type = types.str;
default = "512M";
example = "2048M";
description = ''
additional disk space to be added to the image if diskSize "auto"
is used.
'';
};
};
config = {
image.extension = "vhd";
system.nixos.tags = [ "azure" ];
system.build.image = config.system.build.azureImage;
system.build.azureImage = import ../../lib/make-disk-image.nix {
name = "azure-image";
inherit (config.image) baseName;
# Azure expects vhd format with fixed size,
# generating raw format and convert with subformat args afterwards
format = "raw";
postVM = ''
${lib.getExe' pkgs.vmTools.qemu "qemu-img"} convert -f raw -o subformat=fixed,force_size -O vpc $diskImage $out/${config.image.fileName}
rm $diskImage
''
+ lib.optionalString (cfg.diskSize == "auto") ''
truncate -s +${cfg.additionalSpace} "$out/${config.image.fileName}"
${lib.getExe' pkgs.cloud-utils "growpart"} "$out/${config.image.fileName}" 1
'';
configFile = ./azure-config-user.nix;
bootSize = "${toString cfg.bootSize}M";
partitionTableType = if (cfg.vmGeneration == "v2") then "efi" else "legacy";
inherit (cfg) contents label;
inherit (config.virtualisation) diskSize;
inherit config lib pkgs;
};
boot.growPartition = true;
boot.loader.grub = rec {
efiSupport = (cfg.vmGeneration == "v2");
device = if efiSupport then "nodev" else "/dev/sda";
efiInstallAsRemovable = efiSupport;
# Force grub to run in text mode and output to console
# by disabling font and splash image
font = null;
splashImage = null;
# For Gen 1 VM, configurate grub output to serial_com0.
# Not needed for Gen 2 VM wbere serial_com0 does not exist,
# and outputting to console is enough to make Azure Serial Console working
extraConfig = lib.mkIf (!efiSupport) ''
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
terminal_input --append serial
terminal_output --append serial
'';
};
fileSystems = {
"/" = {
device = "/dev/disk/by-label/${cfg.label}";
inherit (cfg) label;
fsType = "ext4";
autoResize = true;
};
"/boot" = lib.mkIf (cfg.vmGeneration == "v2") {
device = "/dev/disk/by-label/ESP";
fsType = "vfat";
};
};
};
}
|