Hack31.Edit Your GRUB Configuration with grubby


Hack 31. Edit Your GRUB Configuration with grubby

Save tons of typing (and typos), by using a ready-made tool for editing grub. conf.

A machine that doesn't boot doesn't work. And there are many environments in which the grub.conf file supplied with the distribution just doesn't cut it. Whether you're using kickstart to install a server farm, or just hacking around with new kernel builds on your web server, you can leave your scripting skills on the back burner by making use of grubby, a simple command-line tool that will edit your kernel definitions for you.

Here's an example of a very simple kernel definition from a grub.conf file on a Red Hat Enterprise Linux server:

 title Red Hat Enterprise Linux AS (2.4.21-32.0.1.EL) root (hd0,0) kernel /vmlinuz-2.4.21-32.0.1.EL ro root=LABEL=/ initrd /nitrd-2.4.21-32.0.1.EL.img 

This is a fairly standard stanza referred to in the GRUB documentation as an "OS definition" (owing to GRUB's ability to boot seemingly any operating system in existence). Occasionally, it becomes necessary to alter the grub.conf file to pass arguments to the kernel at boot time. For example, if you kickstart a server farm and later add serial console connections to the servers, the kernels will not automatically detect the console, and GRUB will not automagically add the arguments necessary to redirect output to the serial terminal device. How to do this is covered in "Use a Serial Console for Centralized Access to Your Systems" [Hack #76].

This would normally mean hand-editing the grub.conf file to add the argumentsunless you happen to know about grubby. Here's the command, run as root, that you would use to add the requisite arguments to all kernels to allow for console redirection:

 # grubby-update-kernel=ALL --args=console=ttyS0,9600 

The ALL keyword works with several flags and, in this case, it will add the arguments to every kernel line in the configuration file. There's also a DEFAULT keyword that will alter only the kernel line of the default kernel, as per the grub.conf default parameter.

grubby can also alter options to the grub bootloader itself. Using the following commands, you can add a new kernel to the grub.conf file and make it the kernel that grub will boot by default:

 # grubby -add-kernel=/boot/vmlinuz-2.4.21-new -make-default # grubby -set-default=/boot/vmlinuz-2.4.21-32.0.1.ELsmp 

I used the -make-default flag to set the vmlinuz-2.4.21-new kernel to be the default. If you tell grubby to change the default kernel to one that the config file doesn't know about, it'll try to do it, fail, remove the "default" parameter from your config file entirely, and not complain about it one bit. Since I failed to put my new kernel in place, in the second command, I've reset the default kernel back to one that was defined earlier by using the set-default parameter.

So how has this saved you any typing? Changing a default kernel is as simple as changing a single digit in the grub.conf file, right? Well, yes, assuming that you're doing this on a single machine. However, if you need to run a scripted update to your grub.conf file on all the machines you manage or you're altering grub.conf during an automated installation to make a customized kernel the default, I'd much rather use grubby than sed, awk, vi, ed, and/or Perl to do the work. In these cases, it does save you some typing, not to mention saving you from reinventing the wheel!



Linux Server Hacks (Vol. 2)
BSD Sockets Programming from a Multi-Language Perspective (Programming Series)
ISBN: N/A
EAN: 2147483647
Year: 2003
Pages: 162
Authors: M. Tim Jones

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net