parse a line for a particular value (specifically crashkernel)

Latest response

We are reviewing our environment and best-practices. One of the items of consideration is crashkernel. I am trying to figure out how to parse grub.conf to determine what the crashkernel is set to at boot time via grub (I'm not interested in what /proc/cmdline has at that time).

Consider the following 2 scenarios:
# grep crash /boot/grub/grub.conf
kernel /vmlinuz-2.6.18-348.12.1.el5 ro root=/dev/VolGroup00/LogVol01 quiet crashkernel=auto

# grep crash /boot/grub/grub.conf
kernel /vmlinuz-2.6.18-348.4.1.el5 ro root=/dev/VolGroup00/LogVol01 rhgb quiet crashkernel=128M@16M

I would like to return either
crashkernel=auto or crashkernel=128M@16M
or.. better yet
auto or 128M@16M

Responses

Couple questions:
1) what's your preferred programming language (shell, Perl, Python, etc.)
2) do your systems have more than one boot-entry configured in grub
2a) If 'yes' to '2', do you want to parse all of the bootable options or just a specific one?

Thanks Tom.
- Preffered is shell (bash), followed by Perl, then python.
- Generally the systems will have more than one boot-entry, and worse yet, we may, or may-not be booted off the current one. However, I believe we can reasonably expect the crashkernel=blah value to be the same for each entry.
- We can parse the "top" one, or all - that won't matter much, I believe.

To provide a bit more detail - we are basically having to clean up a bunch of hosts (over 300) and I would like to have some idea of what I am looking at. Some boxes have invalid strings, others have no crashkernel defined ... and then there's the hosts with auto (which seems to work universally). Once I understand all the variants of possible entries, I can then formulate what my implementation plan will look like.

Thanks again for your reply.

Well then, here are some ideas:

  1. I'd start with the following to ensure you only get uncommented kernel lines containing crashkernel

    egrep '^[[:space:]]*kernel /vmlinuz.*crashkernel=' /etc/grub.conf
    
  2. Then I'd pass that to either sed or awk (or grep + cut), e.g.:

    egrep -o crashkernel=[[:graph:]]+ | cut -d= -f2 | head -1
        ... or ...
    awk 'NR==1 { print gensub(/.*crashkernel=([[:graph:]]+).*/, "\\1", 1) }'
        ... or ...
    sed -r '1!d;s/.*crashkernel=([[:graph:]]+).*/\1/'
    
  3. If you'd prefer to see all results and not just the first one, get rid of the leading NR==1 in the awk command and the leading 1!d; in the sed command.

Hi James,

If you are looking to get a list of crashkernel arguments, you could try:
crashkernelargs=$(grep crashkernel /etc/grub.conf | sed 's/^.crashkernel=(\w).*/\1/')

I apologize for not putting the thanks out there earlier (still getting up to speed with the new forum format).

Thanks!!! kudos will follow this post ;-)

I went with a combination of the suggestions and here are the results (for others that would like to see).

[root@rhvsrv01 ~]# egrep '^[[:space:]]kernel /vmlinuz.crashkernel=' /boot/grub/grub.conf | sed -r '1!d;s/.crashkernel=([[:graph:]]+)./\1/'
auto

In case you were wondering what my file looks like without the regex goodness...
[root@rhvsrv01 ~]# grep vmlinuz /boot/grub/grub.conf | grep -v #
kernel /vmlinuz-2.6.32-358.2.1.el6.x86_64 ro root=/dev/mapper/VolGroup00-LogVol01 rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup00/LogVol01 rd_LVM_LV=VolGroup00/LogVol00 KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet elevator=deadline processor.max_cstate=1
kernel /vmlinuz-2.6.32-358.el6.x86_64 ro root=/dev/mapper/VolGroup00-LogVol01 rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup00/LogVol01 rd_LVM_LV=VolGroup00/LogVol00 KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet

Coolio! Glad you got it worked out James.

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.