File update_UUID_in_grub.pl of Package Vj_Latesttar
#!/usr/bin/env perl
# Author: Vijay Tripathi <v-vijayt@microsoft.com>
use File::Basename;
use File::Copy;
#
# Declare subroutines.
#
sub update_grub_file;
sub GetUUID;
#
# Declare globals.
#
my $grubfile = "/boot/grub/grub.conf";
my $kernel_ver = `uname -r`;
my $matchkernel = "vmlinuz-" . $kernel_ver;
#
# main entry point
#
chomp $matchkernel;
if (-e "/etc/SuSE-release") {
$grubfile = "/boot/grub/menu.lst";
}
update_grub_file();
print "\tDone.\n";
#
# Subroutine Update Grub File
#
sub update_grub_file() {
my $grubfiletmp = $grubfile . ".tmp123";
my $modified = 0;
if ( -e $grubfiletmp) {
die "\tError $grubfiletmp exists. Please rename the file and rerun this.\n";
}
if (!open(GRUB_CONF_FILE_TMP, ">$grubfiletmp")) {
die "\tError opening $grubfiletmp. Running as root?\n";
}
if (!open(GRUB_CONF_FILE, "<$grubfile")) {
close GRUB_CONF_FILE_TMP;
unlink $grubfiletmp;
die "\tError opening $grubfile. Running as root?\n";
}
#print "\tLooking for kernel match - $matchkernel.\n";
while ($line = <GRUB_CONF_FILE>) {
if ($line =~ /$matchkernel/) {
#print "\tKernel match found - $matchkernel.\n";
if ($line =~ /\/dev\/*/) {
$tmp=$line;
chomp $line;
@disk = split(/ */,$line);
@alldevs = ();
foreach(@disk)
{
if($_ =~ /\/dev\//){
push(@alldevs,$_);
}
}
foreach(@alldevs){
@var = split(/=/,$_);
$uuid=GetUUID($var[1]);
$line =~ s/$var[1]/$uuid/gi;
}
printf GRUB_CONF_FILE_TMP $line."\n";
$modified = 1;
}
} else {
printf GRUB_CONF_FILE_TMP $line;
}
}
close GRUB_CONF_FILE;
close GRUB_CONF_FILE_TMP;
if ($modified == 1) {
move($grubfiletmp, $grubfile);
} else {
unlink $grubfiletmp;
}
}
#Subroutine to get uuid of device
sub GetUUID{
#BLKID COMMAND OUTPUT
$uuidfstr=`blkid $_[0]`;
#SPLITTING O/P IN THE STRINGS
@uuid_arr = split(/ */,$uuidfstr);
$uuid = 0;
#REMOVING "" FROM UUID STRING
foreach(@uuid_arr){
if($_ =~ /^U/){
$uuid = $_;
$uuid =~ s/"//gi;
}
}
$uuid;
}