Modifying kernel cmdline in Ubuntu

I have a number of systems on which I need to modify the kernel cmdline, adding a few options.

At the moment I do it using the following procedure:

  1. Open /etc/default/grub
  2. Modify GRUB_CMDLINE_LINUX_DEFAULT, adding the options
  3. Run update-grub
  4. Reboot

However, I would prefer to automate this process as a part of a packet installation, and avoid modifying the default file (as this is generally brittle).

What I would want to do is something like:

  1. Drop a file (in /etc/grub.d/?), overriding GRUB_CMDLINE_LINUX_DEFAULT or similar
  2. Run update-grub and reboot

There are a pile of scripts in /etc/grub.d/* which are used to build the actual menu config, however, there is no obvious way to interact with them. The script which generates the config only seems to read /etc/default/grub :(

Can somebody enlighten me if there is a way to drop a file to modify the default kernel commandline?


Solution 1:

  1. Create the directory /etc/default/grub.d if it doesn't exist already.
  2. Create a file /etc/default/grub.d/myextraoption.cfg adding to the variable you want (Append to it only, with an extra space. You want to be careful to not clobber or mangle any existing data there.):

    GRUB_CMDLINE_LINUX_DEFAULT="${GRUB_CMDLINE_LINUX_DEFAULT} extra-option"
    
  3. Run update-grub.

You should be able to safely include a yourpackage.cfg file in your package without risk of it being overwritten or clobbering something else. Any of those .cfg files are included after the main default file, so just be aware of that and plan accordingly.

You will almost certainly also want a postinst script to run update-grub when your package is installed, and just to be safe since it is in /etc you should probably also include it in conffiles in your package. I think though that this will leave it behind unless a purge of the package is done, so dealer's choice on that part.

For reference, /usr/sbin/grub-mkconfig on or around line 157 is what reads the default files, including anything matching /etc/default/grub.d/*.cfg. It seems likely to me that this situation is exactly why it does so.

I wrote this based on Trusty. I don't know how far back in releases this is still applicable. I just checked Lucid and it is not there. It is there in Precise.