Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Regenerating grub.conf

by Tanktalus (Canon)
on Feb 28, 2008 at 21:00 UTC ( [id://671011]=sourcecode: print w/replies, xml ) Need Help??
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 (Archbishop) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://671011]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-26 04:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found