Skip to content

Commit 5be6db6

Browse files
authored
grub modernize (#374844)
2 parents 1d182bf + 33c2472 commit 5be6db6

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

nixos/modules/system/boot/loader/grub/install-grub.pl

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Class::Struct;
44
use XML::LibXML;
55
use File::Basename;
6-
use File::Path;
6+
use File::Path qw(make_path);
77
use File::stat;
88
use File::Copy;
99
use File::Copy::Recursive qw(rcopy pathrm);
@@ -37,7 +37,8 @@ sub readFile {
3737
my ($fn) = @_;
3838
# enable slurp mode: read entire file in one go
3939
local $/ = undef;
40-
open my $fh, "<$fn" or return undef;
40+
open my $fh, "<", $fn
41+
or return;
4142
my $s = <$fh>;
4243
close $fh;
4344
# disable slurp mode
@@ -48,7 +49,7 @@ sub readFile {
4849

4950
sub writeFile {
5051
my ($fn, $s) = @_;
51-
open my $fh, ">$fn" or die "cannot create $fn: $!\n";
52+
open my $fh, ">", $fn or die "cannot create $fn: $!\n";
5253
print $fh $s or die "cannot write to $fn: $!\n";
5354
close $fh or die "cannot close $fn: $!\n";
5455
}
@@ -98,7 +99,7 @@ sub runCommand {
9899

99100
print STDERR "updating GRUB 2 menu...\n";
100101

101-
mkpath("$bootPath/grub", 0, 0700);
102+
make_path("$bootPath/grub", { mode => 0700 });
102103

103104
# Discover whether the bootPath is on the same filesystem as / and
104105
# /nix/store. If not, then all kernels and initrds must be copied to
@@ -438,7 +439,7 @@ sub GrubFs {
438439
$conf .= "\n";
439440

440441
my %copied;
441-
mkpath("$bootPath/kernels", 0, 0755) if $copyKernels;
442+
make_path("$bootPath/kernels", { mode => 0755 }) if $copyKernels;
442443

443444
sub copyToKernelsDir {
444445
my ($path) = @_;
@@ -471,7 +472,7 @@ sub addEntry {
471472
my $systemName = basename(Cwd::abs_path("$path"));
472473
my $initrdSecretsPath = "$bootPath/kernels/$systemName-secrets";
473474

474-
mkpath(dirname($initrdSecretsPath), 0, 0755);
475+
make_path(dirname($initrdSecretsPath), { mode => 0755 });
475476
my $oldUmask = umask;
476477
# Make sure initrd is not world readable (won't work if /boot is FAT)
477478
umask 0137;
@@ -690,25 +691,25 @@ sub getEfiTarget {
690691
# because it is read line-by-line.
691692
sub readGrubState {
692693
my $defaultGrubState = GrubState->new(name => "", version => "", efi => "", devices => "", efiMountPoint => "", extraGrubInstallArgs => () );
693-
open FILE, "<$bootPath/grub/state" or return $defaultGrubState;
694+
open my $fh, "<", "$bootPath/grub/state" or return $defaultGrubState;
694695
local $/ = "\n";
695-
my $name = <FILE>;
696+
my $name = <$fh>;
696697
chomp($name);
697-
my $version = <FILE>;
698+
my $version = <$fh>;
698699
chomp($version);
699-
my $efi = <FILE>;
700+
my $efi = <$fh>;
700701
chomp($efi);
701-
my $devices = <FILE>;
702+
my $devices = <$fh>;
702703
chomp($devices);
703-
my $efiMountPoint = <FILE>;
704+
my $efiMountPoint = <$fh>;
704705
chomp($efiMountPoint);
705706
# Historically, arguments in the state file were one per each line, but that
706707
# gets really messy when newlines are involved, structured arguments
707708
# like lists are needed (they have to have a separator encoding), or even worse,
708709
# when we need to remove a setting in the future. Thus, the 6th line is a JSON
709710
# object that can store structured data, with named keys, and all new state
710711
# should go in there.
711-
my $jsonStateLine = <FILE>;
712+
my $jsonStateLine = <$fh>;
712713
# For historical reasons we do not check the values above for un-definedness
713714
# (that is, when the state file has too few lines and EOF is reached),
714715
# because the above come from the first version of this logic and are thus
@@ -720,7 +721,7 @@ sub readGrubState {
720721
}
721722
my %jsonState = %{decode_json($jsonStateLine)};
722723
my @extraGrubInstallArgs = exists($jsonState{'extraGrubInstallArgs'}) ? @{$jsonState{'extraGrubInstallArgs'}} : ();
723-
close FILE;
724+
close $fh;
724725
my $grubState = GrubState->new(name => $name, version => $version, efi => $efi, devices => $devices, efiMountPoint => $efiMountPoint, extraGrubInstallArgs => \@extraGrubInstallArgs );
725726
return $grubState
726727
}
@@ -787,18 +788,18 @@ sub readGrubState {
787788
my $stateFile = "$bootPath/grub/state";
788789
my $stateFileTmp = $stateFile . ".tmp";
789790

790-
open FILE, ">$stateFileTmp" or die "cannot create $stateFileTmp: $!\n";
791-
print FILE get("fullName"), "\n" or die;
792-
print FILE get("fullVersion"), "\n" or die;
793-
print FILE $efiTarget, "\n" or die;
794-
print FILE join( ",", @deviceTargets ), "\n" or die;
795-
print FILE $efiSysMountPoint, "\n" or die;
791+
open my $fh, ">", "$stateFileTmp" or die "cannot create $stateFileTmp: $!\n";
792+
print $fh get("fullName"), "\n" or die;
793+
print $fh get("fullVersion"), "\n" or die;
794+
print $fh $efiTarget, "\n" or die;
795+
print $fh join( ",", @deviceTargets ), "\n" or die;
796+
print $fh $efiSysMountPoint, "\n" or die;
796797
my %jsonState = (
797798
extraGrubInstallArgs => \@extraGrubInstallArgs
798799
);
799800
my $jsonStateLine = encode_json(\%jsonState);
800-
print FILE $jsonStateLine, "\n" or die;
801-
close FILE or die;
801+
print $fh $jsonStateLine, "\n" or die;
802+
close $fh or die;
802803

803804
# Atomically switch to the new state file
804805
rename $stateFileTmp, $stateFile or die "cannot rename $stateFileTmp to $stateFile: $!\n";

0 commit comments

Comments
 (0)