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:
- Open /etc/default/grub
- Modify GRUB_CMDLINE_LINUX_DEFAULT, adding the options
- Run update-grub
- 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:
- Drop a file (in /etc/grub.d/?), overriding GRUB_CMDLINE_LINUX_DEFAULT or similar
- 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?
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.):
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.
grub 2.02 appears to source files matching /etc/default/grub.d/*.cfg in addition to /etc/default/grub.
Maybe this works on whatever version your Ubuntu has, too.
As per the grub documentation, you may find it useful to edit the
/etc/grub.d/40_custom
file withgrub-mkconfig
;Perhaps you could grab one of the first entries and copy it to the end of that config, and append your desired options? This should be future-proof as it's basically preserving the existing config and appending your custom config as a supplementary menu-option.
Edit: I realise that you requested that it be the default line, but the above is a compromise of the edit with a less potentially-destructive methodology (and in any case, you could choose to use
grub-mkconfig
to perform a more bold task such as changing the default)