Category: Utility Scripts
Author/Contact Info
Description: Automatically modifying grub.conf when inserting a new kernel or removing an old one on linux can be difficult. Even just modifying it in vi can be annoying. So, after getting some advice from other Linux users, I settled on the idea of regenerating it. When I add a new kernel, I always rename it to /boot/kernel-$version-gentoo$release (where $release =~ /-r\d+/ or it's blank) to make it easy to view. So, using that personal convention, combined with a bit of Template Toolkit, I get this script. I think all my personal conventions are at the top of the script, or in the template at the bottom. Hope this helps other linux users out there that may be building their own kernels (and thus modifying grub.conf themselves).
#!/usr/bin/perl

use Template;
use File::Spec;

my $version_extract_re = qr/kernel-([\d.]+)-gentoo(-r\d+)?/;
my $kernel_glob = '/boot/kernel-*';
my $grub_conf = '/boot/grub/grub.conf';

my $template = Template->new();

my $vars = {
    kernels => [
                map {
                    /$version_extract_re/ and
                    {
                        ver => $1,
                        rev => $2 || '',
                    }
                } reverse sort glob $kernel_glob
               ],
    zero => File::Spec->rel2abs($0),
};

my $out = '';
$template->process(\*DATA, $vars, \$out);

open my $grub, '>', $grub_conf or die "Can't write to $grub_conf: $!";
print $grub $out;


__DATA__
#
# generated by [%zero%]
#

# Boot automatically after 10 secs.
timeout 10

# By default, boot the first entry.
default 0

# Fallback to the second entry.
fallback 1

[%- FOREACH kernels %]
title  Gentoo [%ver%][%rev%]
root   (hd0,0)
kernel /kernel-[%ver%]-gentoo[%rev%] root=/dev/sda3
[% END %]
Replies are listed 'Best First'.
Re: Regenerating grub.conf
by zentara (Cardinal) on Feb 29, 2008 at 16:05 UTC
    modifying grub.conf ......... can be difficult.

    That's why I still use lilo. :-) I never figured out the appeal of grub, but all the distros seem to be switching to it as default. This old dog dosn't wanna learn the new tricks.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum