summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorWilliam A. Kennington III <william@wkennington.com>2014-05-01 19:08:54 -0500
committerWilliam A. Kennington III <william@wkennington.com>2014-08-28 13:35:35 -0700
commit769d2dc6bf7bba3867e13c125fc0a6d5c76aa19a (patch)
tree27691f31c0d426568d3d6683a2dc5d82dadd5d61 /nixos
parentd4e2040099b33f62971aee2f8530e693115cd680 (diff)
downloadnixlib-769d2dc6bf7bba3867e13c125fc0a6d5c76aa19a.tar
nixlib-769d2dc6bf7bba3867e13c125fc0a6d5c76aa19a.tar.gz
nixlib-769d2dc6bf7bba3867e13c125fc0a6d5c76aa19a.tar.bz2
nixlib-769d2dc6bf7bba3867e13c125fc0a6d5c76aa19a.tar.lz
nixlib-769d2dc6bf7bba3867e13c125fc0a6d5c76aa19a.tar.xz
nixlib-769d2dc6bf7bba3867e13c125fc0a6d5c76aa19a.tar.zst
nixlib-769d2dc6bf7bba3867e13c125fc0a6d5c76aa19a.zip
nixos/grub: Catch errors from command execution
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/system/boot/loader/grub/install-grub.pl34
1 files changed, 28 insertions, 6 deletions
diff --git a/nixos/modules/system/boot/loader/grub/install-grub.pl b/nixos/modules/system/boot/loader/grub/install-grub.pl
index f95e8f75431c..6748a5ca08fa 100644
--- a/nixos/modules/system/boot/loader/grub/install-grub.pl
+++ b/nixos/modules/system/boot/loader/grub/install-grub.pl
@@ -28,6 +28,14 @@ sub writeFile {
     close FILE or die;
 }
 
+sub runCommand {
+    my ($cmd) = @_;
+    open FILE, "$cmd 2>/dev/null |" or die "Failed to execute: $cmd\n";
+    my @ret = <FILE>;
+    close FILE;
+    return ($?, @ret);
+}
+
 my $grub = get("grub");
 my $grubVersion = int(get("version"));
 my $extraConfig = get("extraConfig");
@@ -64,7 +72,11 @@ struct(Fs => {
 });
 sub GetFs {
     my ($dir) = @_;
-    my @boot = split(/[ \n\t]+/, `df -T "$dir" | tail -n 1`);
+	my ($status, @dfOut) = runCommand("df -T $dir");
+	if ($status != 0 || $#dfOut != 1) {
+		die "Failed to retrieve output about $dir from `df`";
+	}
+    my @boot = split(/[ \n\t]+/, $dfOut[1]);
     return Fs->new(device => $boot[0], type => $boot[1], mount => $boot[6]);
 }
 struct (Grub => {
@@ -110,8 +122,11 @@ sub GrubFs {
                 $search = $types{$fsIdentifier} . ' ';
 
                 # Based on the type pull in the identifier from the system
-                my $devInfo = `blkid -o export @{[$fs->device]}`;
-                my @matches = $devInfo =~ m/@{[uc $fsIdentifier]}=([^\n]*)/;
+                my ($status, @devInfo) = runCommand("blkid -o export @{[$fs->device]}");
+				if ($status != 0) {
+					die "Failed to get blkid info for @{[$fs->device]}";
+				}
+                my @matches = join("", @devInfo) =~ m/@{[uc $fsIdentifier]}=([^\n]*)/;
                 if ($#matches != 0) {
                     die "Couldn't find a $types{$fsIdentifier} for @{[$fs->device]}\n"
                 }
@@ -122,14 +137,21 @@ sub GrubFs {
             if ($fs->type eq 'btrfs') {
                 my $subvol = "";
 
-                my @subvols = `mount` =~ m/@{[$fs->device]} on [^\n]*subvol=([^,)]*)/;
+                my ($status, @mounts) = runCommand('mount');
+                if ($status != 0) {
+                    die "Failed to retreive mount info";
+                }
+                my @subvols = join("", @mounts) =~ m/@{[$fs->device]} on [^\n]*subvol=([^,)]*)/;
                 if ($#subvols > 0) {
                     die "Btrfs device @{[$fs->device]} listed multiple times in mount\n"
                 } elsif ($#subvols == 0) {
                     $subvol = $subvols[0];
                 } else {
-                    my $btrfsDefault = `btrfs subvol get-default @{[$fs->mount]}`;
-                    my @results = $btrfsDefault =~ m/path ([^ ]*)/;
+                    my ($status, @btrfsOut) = runCommand("btrfs subvol get-default @{[$fs->mount]}");
+                    if ($status != 0) {
+                        die "Failed to retrieve btrfs default subvolume"
+                    }
+                    my @results = join("", @btrfsOut) =~ m/path ([^ ]*)/;
                     if ($#results > 0) {
                         die "Btrfs device @{[$fs->device]} has multiple default subvolumes\n";
                     } elsif ($#results == 1) {