Skip to content

Instantly share code, notes, and snippets.

@glance-
Created July 1, 2020 07:40
Show Gist options
  • Save glance-/44f5c9495de565fddca6fefc6a9fa69e to your computer and use it in GitHub Desktop.
Save glance-/44f5c9495de565fddca6fefc6a9fa69e to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# vim: ts=4 sw=4 et
use warnings;
use strict;
use autodie;
use Data::Dumper;
exit(0) unless ($ENV{FAI_ACTION} // '' eq 'install' and $ENV{disklist} // '' eq 'nvme0n1');
# if we're installing, try to ensure we're in best preformance mode for nvme
# Ok, parse cmd to figure out in which mode we are and which modes are available
open(my $nvme_cmd, '-|', 'nvme', 'id-ns', '-H', '/dev/nvme0n1');
my @nvme_lbas;
while(my $line = <$nvme_cmd>) {
my %nvme_lba;
next unless ($line =~ m/^LBA Format\s+(\d+) : Metadata.+ Data Size: (\d+).+Relative Performance: (0x\d+) (\w+)\s(.*)$/);
$nvme_lba{format} = int($1);
$nvme_lba{data_size} = $2;
$nvme_lba{performance} = hex($3);
$nvme_lba{performance_str} = $4;
$nvme_lba{in_use} = $5 ne '';
push @nvme_lbas, \%nvme_lba;
}
# Sort the list
my $best_nvme_lba = (sort { $a->{performance} <=> $b->{performance} } @nvme_lbas)[0];
if ($best_nvme_lba->{in_use}) {
print "Best lba already in use, not re-formatting\n";
exit(0);
}
print "Better lba found, re-formatting to:\n";
print Dumper($best_nvme_lba);
exit(system('nvme', 'format', '--lbaf', $best_nvme_lba->{format}, '/dev/nvme0n1'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment