nixos/grub: Refactor perl script to remove the Switch module

This commit is contained in:
William A. Kennington III 2014-04-30 18:45:44 -05:00
parent 1f460e00ef
commit 99b4792554

View File

@ -8,7 +8,6 @@ use File::stat;
use File::Copy; use File::Copy;
use POSIX; use POSIX;
use Cwd; use Cwd;
use Switch;
my $defaultConfig = $ARGV[1] or die; my $defaultConfig = $ARGV[1] or die;
@ -78,9 +77,13 @@ sub GrubFs {
my $fs = GetFs($dir); my $fs = GetFs($dir);
my $path = "/" . substr($dir, length($fs->mount)); my $path = "/" . substr($dir, length($fs->mount));
my $search = ""; my $search = "";
if ($grubVersion > 1) { if ($grubVersion > 1) {
# ZFS is completely separate logic as zpools are always identified by a label
# or custom UUID
if ($fs->type eq "zfs") { if ($fs->type eq "zfs") {
my $sid = index($fs->device, "/"); my $sid = index($fs->device, "/");
if ($sid < 0) { if ($sid < 0) {
$search = "--label " . $fs->device; $search = "--label " . $fs->device;
$path = "/@" . $path; $path = "/@" . $path;
@ -89,16 +92,17 @@ sub GrubFs {
$path = "/" . substr($fs->device, $sid) . "/@" . $path; $path = "/" . substr($fs->device, $sid) . "/@" . $path;
} }
} else { } else {
my $idCmd = "\$(blkid -o export " . $fs->device . ") 2>/dev/null; echo" my $idCmd = "\$(blkid -o export " . $fs->device . ") 2>/dev/null; echo";
switch ($fsIdentifier) {
case "uuid" { if ($fsIdentifier eq "uuid") {
$search = "--fs-uuid " . `$idCmd \$UUID`; $search = "--fs-uuid " . `$idCmd \$UUID`;
} } elsif ($fsIdentifier eq "label") {
case "label" {
$search = "--label " . `$idCmd \$LABEL`; $search = "--label " . `$idCmd \$LABEL`;
} } elsif ($fsIdentifier eq "provided") {
case "provided" {
my $lbl = "/dev/disk/by-label/"; my $lbl = "/dev/disk/by-label/";
# If the provided dev is identifying the partition using a label or uuid,
# we should get the label / uuid and do a proper search
if (index($fs->device, $lbl) == 0) { if (index($fs->device, $lbl) == 0) {
$search = "--label " . substr($fs->device, length($lbl)); $search = "--label " . substr($fs->device, length($lbl));
} }
@ -106,15 +110,15 @@ sub GrubFs {
if (index($fs->device, $uuid) == 0) { if (index($fs->device, $uuid) == 0) {
$search = "--fs-uuid " . substr($fs->device, length($uuid)); $search = "--fs-uuid " . substr($fs->device, length($uuid));
} }
} } else {
else {
die "invalid fs identifier type\n"; die "invalid fs identifier type\n";
} }
}
# 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") {
$subvol = `mount | sed -n 's,^@{[$fs->device]} on .*subvol=\([^,)]*\).*\$,\1,p'` my $subvol = `mount | sed -n 's,^@{[$fs->device]} on .*subvol=\([^,)]*\).*\$,\1,p'`;
if ($subvol eq "") { if ($subvol eq "") {
$subvol = `btrfs subvol get-default @{[$fs->mount]} | sed -n 's,^.*path \([^ ]*\) .*\$,\1,p'` $subvol = `btrfs subvol get-default @{[$fs->mount]} | sed -n 's,^.*path \([^ ]*\) .*\$,\1,p'`;
} }
$path = "/$subvol"; $path = "/$subvol";
} }