1use strict;
2use warnings;
3use Class::Struct;
4use XML::LibXML;
5use File::Basename;
6use File::Path;
7use File::stat;
8use File::Copy;
9use File::Slurp;
10use File::Temp;
11require List::Compare;
12use POSIX;
13use Cwd;
14
15# system.build.toplevel path
16my $defaultConfig = $ARGV[1] or die;
17
18# Grub config XML generated by grubConfig function in grub.nix
19my $dom = XML::LibXML->load_xml(location => $ARGV[0]);
20
21sub get { my ($name) = @_; return $dom->findvalue("/expr/attrs/attr[\@name = '$name']/*/\@value"); }
22
23sub readFile {
24 my ($fn) = @_; local $/ = undef;
25 open FILE, "<$fn" or return undef; my $s = <FILE>; close FILE;
26 local $/ = "\n"; chomp $s; return $s;
27}
28
29sub writeFile {
30 my ($fn, $s) = @_;
31 open FILE, ">$fn" or die "cannot create $fn: $!\n";
32 print FILE $s or die;
33 close FILE or die;
34}
35
36sub runCommand {
37 my ($cmd) = @_;
38 open FILE, "$cmd 2>/dev/null |" or die "Failed to execute: $cmd\n";
39 my @ret = <FILE>;
40 close FILE;
41 return ($?, @ret);
42}
43
44my $grub = get("grub");
45my $grubVersion = int(get("version"));
46my $grubTarget = get("grubTarget");
47my $extraConfig = get("extraConfig");
48my $extraPrepareConfig = get("extraPrepareConfig");
49my $extraPerEntryConfig = get("extraPerEntryConfig");
50my $extraEntries = get("extraEntries");
51my $extraEntriesBeforeNixOS = get("extraEntriesBeforeNixOS") eq "true";
52my $extraInitrd = get("extraInitrd");
53my $splashImage = get("splashImage");
54my $configurationLimit = int(get("configurationLimit"));
55my $copyKernels = get("copyKernels") eq "true";
56my $timeout = int(get("timeout"));
57my $defaultEntry = int(get("default"));
58my $fsIdentifier = get("fsIdentifier");
59my $grubEfi = get("grubEfi");
60my $grubTargetEfi = get("grubTargetEfi");
61my $bootPath = get("bootPath");
62my $storePath = get("storePath");
63my $canTouchEfiVariables = get("canTouchEfiVariables");
64my $efiInstallAsRemovable = get("efiInstallAsRemovable");
65my $efiSysMountPoint = get("efiSysMountPoint");
66my $gfxmodeEfi = get("gfxmodeEfi");
67my $gfxmodeBios = get("gfxmodeBios");
68my $bootloaderId = get("bootloaderId");
69my $forceInstall = get("forceInstall");
70my $font = get("font");
71$ENV{'PATH'} = get("path");
72
73die "unsupported GRUB version\n" if $grubVersion != 1 && $grubVersion != 2;
74
75print STDERR "updating GRUB $grubVersion menu...\n";
76
77mkpath("$bootPath/grub", 0, 0700);
78
79# Discover whether the bootPath is on the same filesystem as / and
80# /nix/store. If not, then all kernels and initrds must be copied to
81# the bootPath.
82if (stat($bootPath)->dev != stat("/nix/store")->dev) {
83 $copyKernels = 1;
84}
85
86# Discover information about the location of the bootPath
87struct(Fs => {
88 device => '$',
89 type => '$',
90 mount => '$',
91});
92sub PathInMount {
93 my ($path, $mount) = @_;
94 my @splitMount = split /\//, $mount;
95 my @splitPath = split /\//, $path;
96 if ($#splitPath < $#splitMount) {
97 return 0;
98 }
99 for (my $i = 0; $i <= $#splitMount; $i++) {
100 if ($splitMount[$i] ne $splitPath[$i]) {
101 return 0;
102 }
103 }
104 return 1;
105}
106
107# Figure out what filesystem is used for the directory with init/initrd/kernel files
108sub GetFs {
109 my ($dir) = @_;
110 my $bestFs = Fs->new(device => "", type => "", mount => "");
111 foreach my $fs (read_file("/proc/self/mountinfo")) {
112 chomp $fs;
113 my @fields = split / /, $fs;
114 my $mountPoint = $fields[4];
115 next unless -d $mountPoint;
116 my @mountOptions = split /,/, $fields[5];
117
118 # Skip the optional fields.
119 my $n = 6; $n++ while $fields[$n] ne "-"; $n++;
120 my $fsType = $fields[$n];
121 my $device = $fields[$n + 1];
122 my @superOptions = split /,/, $fields[$n + 2];
123
124 # Skip the bind-mount on /nix/store.
125 next if $mountPoint eq "/nix/store" && (grep { $_ eq "rw" } @superOptions);
126 # Skip mount point generated by systemd-efi-boot-generator?
127 next if $fsType eq "autofs";
128
129 # Ensure this matches the intended directory
130 next unless PathInMount($dir, $mountPoint);
131
132 # Is it better than our current match?
133 if (length($mountPoint) > length($bestFs->mount)) {
134 $bestFs = Fs->new(device => $device, type => $fsType, mount => $mountPoint);
135 }
136 }
137 return $bestFs;
138}
139struct (Grub => {
140 path => '$',
141 search => '$',
142});
143my $driveid = 1;
144sub GrubFs {
145 my ($dir) = @_;
146 my $fs = GetFs($dir);
147 my $path = substr($dir, length($fs->mount));
148 if (substr($path, 0, 1) ne "/") {
149 $path = "/$path";
150 }
151 my $search = "";
152
153 if ($grubVersion > 1) {
154 # ZFS is completely separate logic as zpools are always identified by a label
155 # or custom UUID
156 if ($fs->type eq 'zfs') {
157 my $sid = index($fs->device, '/');
158
159 if ($sid < 0) {
160 $search = '--label ' . $fs->device;
161 $path = '/@' . $path;
162 } else {
163 $search = '--label ' . substr($fs->device, 0, $sid);
164 $path = '/' . substr($fs->device, $sid) . '/@' . $path;
165 }
166 } else {
167 my %types = ('uuid' => '--fs-uuid', 'label' => '--label');
168
169 if ($fsIdentifier eq 'provided') {
170 # If the provided dev is identifying the partition using a label or uuid,
171 # we should get the label / uuid and do a proper search
172 my @matches = $fs->device =~ m/\/dev\/disk\/by-(label|uuid)\/(.*)/;
173 if ($#matches > 1) {
174 die "Too many matched devices"
175 } elsif ($#matches == 1) {
176 $search = "$types{$matches[0]} $matches[1]"
177 }
178 } else {
179 # Determine the identifying type
180 $search = $types{$fsIdentifier} . ' ';
181
182 # Based on the type pull in the identifier from the system
183 my ($status, @devInfo) = runCommand("@utillinux@/bin/blkid -o export @{[$fs->device]}");
184 if ($status != 0) {
185 die "Failed to get blkid info for @{[$fs->mount]} on @{[$fs->device]}";
186 }
187 my @matches = join("", @devInfo) =~ m/@{[uc $fsIdentifier]}=([^\n]*)/;
188 if ($#matches != 0) {
189 die "Couldn't find a $types{$fsIdentifier} for @{[$fs->device]}\n"
190 }
191 $search .= $matches[0];
192 }
193
194 # BTRFS is a special case in that we need to fix the referrenced path based on subvolumes
195 if ($fs->type eq 'btrfs') {
196 my ($status, @id_info) = runCommand("@btrfsprogs@/bin/btrfs subvol show @{[$fs->mount]}");
197 if ($status != 0) {
198 die "Failed to retrieve subvolume info for @{[$fs->mount]}\n";
199 }
200 my @ids = join("\n", @id_info) =~ m/^(?!\/\n).*Subvolume ID:[ \t\n]*([0-9]+)/s;
201 if ($#ids > 0) {
202 die "Btrfs subvol name for @{[$fs->device]} listed multiple times in mount\n"
203 } elsif ($#ids == 0) {
204 my ($status, @path_info) = runCommand("@btrfsprogs@/bin/btrfs subvol list @{[$fs->mount]}");
205 if ($status != 0) {
206 die "Failed to find @{[$fs->mount]} subvolume id from btrfs\n";
207 }
208 my @paths = join("", @path_info) =~ m/ID $ids[0] [^\n]* path ([^\n]*)/;
209 if ($#paths > 0) {
210 die "Btrfs returned multiple paths for a single subvolume id, mountpoint @{[$fs->mount]}\n";
211 } elsif ($#paths != 0) {
212 die "Btrfs did not return a path for the subvolume at @{[$fs->mount]}\n";
213 }
214 $path = "/$paths[0]$path";
215 }
216 }
217 }
218 if (not $search eq "") {
219 $search = "search --set=drive$driveid " . $search;
220 $path = "(\$drive$driveid)$path";
221 $driveid += 1;
222 }
223 }
224 return Grub->new(path => $path, search => $search);
225}
226my $grubBoot = GrubFs($bootPath);
227my $grubStore;
228if ($copyKernels == 0) {
229 $grubStore = GrubFs($storePath);
230}
231my $extraInitrdPath;
232if ($extraInitrd) {
233 if (! -f $extraInitrd) {
234 print STDERR "Warning: the specified extraInitrd " . $extraInitrd . " doesn't exist. Your system won't boot without it.\n";
235 }
236 $extraInitrdPath = GrubFs($extraInitrd);
237}
238
239# Generate the header.
240my $conf .= "# Automatically generated. DO NOT EDIT THIS FILE!\n";
241
242if ($grubVersion == 1) {
243 $conf .= "
244 default $defaultEntry
245 timeout $timeout
246 ";
247 if ($splashImage) {
248 copy $splashImage, "$bootPath/background.xpm.gz" or die "cannot copy $splashImage to $bootPath\n";
249 $conf .= "splashimage " . $grubBoot->path . "/background.xpm.gz\n";
250 }
251}
252
253else {
254 if ($copyKernels == 0) {
255 $conf .= "
256 " . $grubStore->search;
257 }
258 # FIXME: should use grub-mkconfig.
259 $conf .= "
260 " . $grubBoot->search . "
261 if [ -s \$prefix/grubenv ]; then
262 load_env
263 fi
264
265 # ‘grub-reboot’ sets a one-time saved entry, which we process here and
266 # then delete.
267 if [ \"\${next_entry}\" ]; then
268 set default=\"\${next_entry}\"
269 set next_entry=
270 save_env next_entry
271 set timeout=1
272 else
273 set default=$defaultEntry
274 set timeout=$timeout
275 fi
276
277 # Setup the graphics stack for bios and efi systems
278 if [ \"\${grub_platform}\" = \"efi\" ]; then
279 insmod efi_gop
280 insmod efi_uga
281 else
282 insmod vbe
283 fi
284 insmod font
285 if loadfont " . $grubBoot->path . "/converted-font.pf2; then
286 insmod gfxterm
287 if [ \"\${grub_platform}\" = \"efi\" ]; then
288 set gfxmode=$gfxmodeEfi
289 set gfxpayload=keep
290 else
291 set gfxmode=$gfxmodeBios
292 set gfxpayload=text
293 fi
294 terminal_output gfxterm
295 fi
296 ";
297
298 if ($font) {
299 copy $font, "$bootPath/converted-font.pf2" or die "cannot copy $font to $bootPath\n";
300 }
301 if ($splashImage) {
302 # FIXME: GRUB 1.97 doesn't resize the background image if it
303 # doesn't match the video resolution.
304 copy $splashImage, "$bootPath/background.png" or die "cannot copy $splashImage to $bootPath\n";
305 $conf .= "
306 insmod png
307 if background_image " . $grubBoot->path . "/background.png; then
308 set color_normal=white/black
309 set color_highlight=black/white
310 else
311 set menu_color_normal=cyan/blue
312 set menu_color_highlight=white/blue
313 fi
314 ";
315 }
316}
317
318$conf .= "$extraConfig\n";
319
320
321# Generate the menu entries.
322$conf .= "\n";
323
324my %copied;
325mkpath("$bootPath/kernels", 0, 0755) if $copyKernels;
326
327sub copyToKernelsDir {
328 my ($path) = @_;
329 return $grubStore->path . substr($path, length("/nix/store")) unless $copyKernels;
330 $path =~ /\/nix\/store\/(.*)/ or die;
331 my $name = $1; $name =~ s/\//-/g;
332 my $dst = "$bootPath/kernels/$name";
333 # Don't copy the file if $dst already exists. This means that we
334 # have to create $dst atomically to prevent partially copied
335 # kernels or initrd if this script is ever interrupted.
336 if (! -e $dst) {
337 my $tmp = "$dst.tmp";
338 copy $path, $tmp or die "cannot copy $path to $tmp\n";
339 rename $tmp, $dst or die "cannot rename $tmp to $dst\n";
340 }
341 $copied{$dst} = 1;
342 return $grubBoot->path . "/kernels/$name";
343}
344
345sub addEntry {
346 my ($name, $path) = @_;
347 return unless -e "$path/kernel" && -e "$path/initrd";
348
349 my $kernel = copyToKernelsDir(Cwd::abs_path("$path/kernel"));
350 my $initrd = copyToKernelsDir(Cwd::abs_path("$path/initrd"));
351 if ($extraInitrd) {
352 $initrd .= " " .$extraInitrdPath->path;
353 }
354 my $xen = -e "$path/xen.gz" ? copyToKernelsDir(Cwd::abs_path("$path/xen.gz")) : undef;
355
356 # FIXME: $confName
357
358 my $kernelParams =
359 "systemConfig=" . Cwd::abs_path($path) . " " .
360 "init=" . Cwd::abs_path("$path/init") . " " .
361 readFile("$path/kernel-params");
362 my $xenParams = $xen && -e "$path/xen-params" ? readFile("$path/xen-params") : "";
363
364 if ($grubVersion == 1) {
365 $conf .= "title $name\n";
366 $conf .= " $extraPerEntryConfig\n" if $extraPerEntryConfig;
367 $conf .= " kernel $xen $xenParams\n" if $xen;
368 $conf .= " " . ($xen ? "module" : "kernel") . " $kernel $kernelParams\n";
369 $conf .= " " . ($xen ? "module" : "initrd") . " $initrd\n\n";
370 } else {
371 $conf .= "menuentry \"$name\" {\n";
372 $conf .= $grubBoot->search . "\n";
373 if ($copyKernels == 0) {
374 $conf .= $grubStore->search . "\n";
375 }
376 if ($extraInitrd) {
377 $conf .= $extraInitrdPath->search . "\n";
378 }
379 $conf .= " $extraPerEntryConfig\n" if $extraPerEntryConfig;
380 $conf .= " multiboot $xen $xenParams\n" if $xen;
381 $conf .= " " . ($xen ? "module" : "linux") . " $kernel $kernelParams\n";
382 $conf .= " " . ($xen ? "module" : "initrd") . " $initrd\n";
383 $conf .= "}\n\n";
384 }
385}
386
387
388# Add default entries.
389$conf .= "$extraEntries\n" if $extraEntriesBeforeNixOS;
390
391addEntry("NixOS - Default", $defaultConfig);
392
393$conf .= "$extraEntries\n" unless $extraEntriesBeforeNixOS;
394
395my $grubBootPath = $grubBoot->path;
396# extraEntries could refer to @bootRoot@, which we have to substitute
397$conf =~ s/\@bootRoot\@/$grubBootPath/g;
398
399# Emit submenus for all system profiles.
400sub addProfile {
401 my ($profile, $description) = @_;
402
403 # Add entries for all generations of this profile.
404 $conf .= "submenu \"$description\" {\n" if $grubVersion == 2;
405
406 sub nrFromGen { my ($x) = @_; $x =~ /\/\w+-(\d+)-link/; return $1; }
407
408 my @links = sort
409 { nrFromGen($b) <=> nrFromGen($a) }
410 (glob "$profile-*-link");
411
412 my $curEntry = 0;
413 foreach my $link (@links) {
414 last if $curEntry++ >= $configurationLimit;
415 if (! -e "$link/nixos-version") {
416 warn "skipping corrupt system profile entry ‘$link’\n";
417 next;
418 }
419 my $date = strftime("%F", localtime(lstat($link)->mtime));
420 my $version =
421 -e "$link/nixos-version"
422 ? readFile("$link/nixos-version")
423 : basename((glob(dirname(Cwd::abs_path("$link/kernel")) . "/lib/modules/*"))[0]);
424 addEntry("NixOS - Configuration " . nrFromGen($link) . " ($date - $version)", $link);
425 }
426
427 $conf .= "}\n" if $grubVersion == 2;
428}
429
430addProfile "/nix/var/nix/profiles/system", "NixOS - All configurations";
431
432if ($grubVersion == 2) {
433 for my $profile (glob "/nix/var/nix/profiles/system-profiles/*") {
434 my $name = basename($profile);
435 next unless $name =~ /^\w+$/;
436 addProfile $profile, "NixOS - Profile '$name'";
437 }
438}
439
440# Run extraPrepareConfig in sh
441if ($extraPrepareConfig ne "") {
442 system((get("shell"), "-c", $extraPrepareConfig));
443}
444
445# write the GRUB config.
446my $confFile = $grubVersion == 1 ? "$bootPath/grub/menu.lst" : "$bootPath/grub/grub.cfg";
447my $tmpFile = $confFile . ".tmp";
448writeFile($tmpFile, $conf);
449
450
451# check whether to install GRUB EFI or not
452sub getEfiTarget {
453 if ($grubVersion == 1) {
454 return "no"
455 } elsif (($grub ne "") && ($grubEfi ne "")) {
456 # EFI can only be installed when target is set;
457 # A target is also required then for non-EFI grub
458 if (($grubTarget eq "") || ($grubTargetEfi eq "")) { die }
459 else { return "both" }
460 } elsif (($grub ne "") && ($grubEfi eq "")) {
461 # TODO: It would be safer to disallow non-EFI grub installation if no taget is given.
462 # If no target is given, then grub auto-detects the target which can lead to errors.
463 # E.g. it seems as if grub would auto-detect a EFI target based on the availability
464 # of a EFI partition.
465 # However, it seems as auto-detection is currently relied on for non-x86_64 and non-i386
466 # architectures in NixOS. That would have to be fixed in the nixos modules first.
467 return "no"
468 } elsif (($grub eq "") && ($grubEfi ne "")) {
469 # EFI can only be installed when target is set;
470 if ($grubTargetEfi eq "") { die }
471 else {return "only" }
472 } else {
473 # prevent an installation if neither grub nor grubEfi is given
474 return "neither"
475 }
476}
477
478my $efiTarget = getEfiTarget();
479
480# Append entries detected by os-prober
481if (get("useOSProber") eq "true") {
482 my $targetpackage = ($efiTarget eq "no") ? $grub : $grubEfi;
483 system(get("shell"), "-c", "pkgdatadir=$targetpackage/share/grub $targetpackage/etc/grub.d/30_os-prober >> $tmpFile");
484}
485
486# Atomically switch to the new config
487rename $tmpFile, $confFile or die "cannot rename $tmpFile to $confFile\n";
488
489
490# Remove obsolete files from $bootPath/kernels.
491foreach my $fn (glob "$bootPath/kernels/*") {
492 next if defined $copied{$fn};
493 print STDERR "removing obsolete file $fn\n";
494 unlink $fn;
495}
496
497
498#
499# Install GRUB if the parameters changed from the last time we installed it.
500#
501
502struct(GrubState => {
503 name => '$',
504 version => '$',
505 efi => '$',
506 devices => '$',
507 efiMountPoint => '$',
508});
509sub readGrubState {
510 my $defaultGrubState = GrubState->new(name => "", version => "", efi => "", devices => "", efiMountPoint => "" );
511 open FILE, "<$bootPath/grub/state" or return $defaultGrubState;
512 local $/ = "\n";
513 my $name = <FILE>;
514 chomp($name);
515 my $version = <FILE>;
516 chomp($version);
517 my $efi = <FILE>;
518 chomp($efi);
519 my $devices = <FILE>;
520 chomp($devices);
521 my $efiMountPoint = <FILE>;
522 chomp($efiMountPoint);
523 close FILE;
524 my $grubState = GrubState->new(name => $name, version => $version, efi => $efi, devices => $devices, efiMountPoint => $efiMountPoint );
525 return $grubState
526}
527
528sub getDeviceTargets {
529 my @devices = ();
530 foreach my $dev ($dom->findnodes('/expr/attrs/attr[@name = "devices"]/list/string/@value')) {
531 $dev = $dev->findvalue(".") or die;
532 push(@devices, $dev);
533 }
534 return @devices;
535}
536my @deviceTargets = getDeviceTargets();
537my $prevGrubState = readGrubState();
538my @prevDeviceTargets = split/,/, $prevGrubState->devices;
539
540my $devicesDiffer = scalar (List::Compare->new( '-u', '-a', \@deviceTargets, \@prevDeviceTargets)->get_symmetric_difference());
541my $nameDiffer = get("fullName") ne $prevGrubState->name;
542my $versionDiffer = get("fullVersion") ne $prevGrubState->version;
543my $efiDiffer = $efiTarget ne $prevGrubState->efi;
544my $efiMountPointDiffer = $efiSysMountPoint ne $prevGrubState->efiMountPoint;
545if (($ENV{'NIXOS_INSTALL_GRUB'} // "") eq "1") {
546 warn "NIXOS_INSTALL_GRUB env var deprecated, use NIXOS_INSTALL_BOOTLOADER";
547 $ENV{'NIXOS_INSTALL_BOOTLOADER'} = "1";
548}
549my $requireNewInstall = $devicesDiffer || $nameDiffer || $versionDiffer || $efiDiffer || $efiMountPointDiffer || (($ENV{'NIXOS_INSTALL_BOOTLOADER'} // "") eq "1");
550
551# install a symlink so that grub can detect the boot drive
552my $tmpDir = File::Temp::tempdir(CLEANUP => 1) or die "Failed to create temporary space";
553symlink "$bootPath", "$tmpDir/boot" or die "Failed to symlink $tmpDir/boot";
554
555# install non-EFI GRUB
556if (($requireNewInstall != 0) && ($efiTarget eq "no" || $efiTarget eq "both")) {
557 foreach my $dev (@deviceTargets) {
558 next if $dev eq "nodev";
559 print STDERR "installing the GRUB $grubVersion boot loader on $dev...\n";
560 my @command = ("$grub/sbin/grub-install", "--recheck", "--root-directory=$tmpDir", Cwd::abs_path($dev));
561 if ($forceInstall eq "true") {
562 push @command, "--force";
563 }
564 if ($grubTarget ne "") {
565 push @command, "--target=$grubTarget";
566 }
567 (system @command) == 0 or die "$0: installation of GRUB on $dev failed\n";
568 }
569}
570
571
572# install EFI GRUB
573if (($requireNewInstall != 0) && ($efiTarget eq "only" || $efiTarget eq "both")) {
574 print STDERR "installing the GRUB $grubVersion EFI boot loader into $efiSysMountPoint...\n";
575 my @command = ("$grubEfi/sbin/grub-install", "--recheck", "--target=$grubTargetEfi", "--boot-directory=$bootPath", "--efi-directory=$efiSysMountPoint");
576 if ($forceInstall eq "true") {
577 push @command, "--force";
578 }
579 if ($canTouchEfiVariables eq "true") {
580 push @command, "--bootloader-id=$bootloaderId";
581 } else {
582 push @command, "--no-nvram";
583 push @command, "--removable" if $efiInstallAsRemovable eq "true";
584 }
585
586 (system @command) == 0 or die "$0: installation of GRUB EFI into $efiSysMountPoint failed\n";
587}
588
589
590# update GRUB state file
591if ($requireNewInstall != 0) {
592 open FILE, ">$bootPath/grub/state" or die "cannot create $bootPath/grub/state: $!\n";
593 print FILE get("fullName"), "\n" or die;
594 print FILE get("fullVersion"), "\n" or die;
595 print FILE $efiTarget, "\n" or die;
596 print FILE join( ",", @deviceTargets ), "\n" or die;
597 print FILE $efiSysMountPoint, "\n" or die;
598 close FILE or die;
599}