Merge pull request #223418 from Mic92/grub

nixos/grub-install: don't rely on shell to run commands
This commit is contained in:
Jörg Thalheim 2023-05-04 09:48:41 +01:00 committed by GitHub
commit be8e3128b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -34,23 +34,29 @@ sub getList {
} }
sub readFile { sub readFile {
my ($fn) = @_; local $/ = undef; my ($fn) = @_;
open FILE, "<$fn" or return undef; my $s = <FILE>; close FILE; # enable slurp mode: read entire file in one go
local $/ = "\n"; chomp $s; return $s; local $/ = undef;
open my $fh, "<$fn" or return undef;
my $s = <$fh>;
close $fh;
# disable slurp mode
local $/ = "\n";
chomp $s;
return $s;
} }
sub writeFile { sub writeFile {
my ($fn, $s) = @_; my ($fn, $s) = @_;
open FILE, ">$fn" or die "cannot create $fn: $!\n"; open my $fh, ">$fn" or die "cannot create $fn: $!\n";
print FILE $s or die; print $fh $s or die "cannot write to $fn: $!\n";
close FILE or die; close $fh or die "cannot close $fn: $!\n";
} }
sub runCommand { sub runCommand {
my ($cmd) = @_; open(my $fh, "-|", @_) or die "Failed to execute: $@_\n";
open FILE, "$cmd 2>/dev/null |" or die "Failed to execute: $cmd\n"; my @ret = $fh->getlines();
my @ret = <FILE>; close $fh;
close FILE;
return ($?, @ret); return ($?, @ret);
} }
@ -200,7 +206,7 @@ sub GrubFs {
$search = $types{$fsIdentifier} . ' '; $search = $types{$fsIdentifier} . ' ';
# Based on the type pull in the identifier from the system # Based on the type pull in the identifier from the system
my ($status, @devInfo) = runCommand("@utillinux@/bin/blkid -o export @{[$fs->device]}"); my ($status, @devInfo) = runCommand("@utillinux@/bin/blkid", "-o", "export", @{[$fs->device]});
if ($status != 0) { if ($status != 0) {
die "Failed to get blkid info (returned $status) for @{[$fs->mount]} on @{[$fs->device]}"; die "Failed to get blkid info (returned $status) for @{[$fs->mount]} on @{[$fs->device]}";
} }
@ -213,7 +219,7 @@ sub GrubFs {
# BTRFS is a special case in that we need to fix the referrenced path based on subvolumes # BTRFS is a special case in that we need to fix the referrenced path based on subvolumes
if ($fs->type eq 'btrfs') { if ($fs->type eq 'btrfs') {
my ($status, @id_info) = runCommand("@btrfsprogs@/bin/btrfs subvol show @{[$fs->mount]}"); my ($status, @id_info) = runCommand("@btrfsprogs@/bin/btrfs", "subvol", "show", @{[$fs->mount]});
if ($status != 0) { if ($status != 0) {
die "Failed to retrieve subvolume info for @{[$fs->mount]}\n"; die "Failed to retrieve subvolume info for @{[$fs->mount]}\n";
} }
@ -221,7 +227,7 @@ sub GrubFs {
if ($#ids > 0) { if ($#ids > 0) {
die "Btrfs subvol name for @{[$fs->device]} listed multiple times in mount\n" die "Btrfs subvol name for @{[$fs->device]} listed multiple times in mount\n"
} elsif ($#ids == 0) { } elsif ($#ids == 0) {
my ($status, @path_info) = runCommand("@btrfsprogs@/bin/btrfs subvol list @{[$fs->mount]}"); my ($status, @path_info) = runCommand("@btrfsprogs@/bin/btrfs", "subvol", "list", @{[$fs->mount]});
if ($status != 0) { if ($status != 0) {
die "Failed to find @{[$fs->mount]} subvolume id from btrfs\n"; die "Failed to find @{[$fs->mount]} subvolume id from btrfs\n";
} }