I'm using Packer to build some CentOS 7 VM's. A part of this process is installing Linux Kernel ver. 4.15. Installing the Kernel has been pretty straightforward but getting the VM to recognize the new kernel version as the default boot option has been problematic.

I have been using this article as a reference for changing the default boot option. In short it states

  1. Boot options are stored in array like order in grub.cfg and the most recently installed kernel will be at index 0.
  2. When a new kernel is installed a new grub.cfg is created.
  3. If GRUB_DEFAULT=saved in the /etc/default/grub file the value from /boot/grub2/grubenv is used as the default boot option.

The CentOS Wiki states I can change the value of /boot/grub2/grubenv using the command grub2-set-default <index> where the index is the index of the boot config option in grub.cfg.

All that being said executing these commands on an existing VM changes the default boot option as expected.

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm 
yum -y --enablerepo=elrepo-kernel install kernel-ml
grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg

Copying these commands in directly into a Packer provisioner results in a new boot option with the newly installed kernel but that boot option is not set as the default boot option.

"provisioners": [
        "type": "shell",
        "inline": [
            "rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org",
            "rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm ",
            "yum -y --enablerepo=elrepo-kernel install kernel-ml",
            "grub2-set-default 0",
            "grub2-mkconfig -o /boot/grub2/grub.cfg"

I would expect to see a boot menu that looks like this after importing the VM for the first time. Boot Options

My only thought is Packer provisioners are run in some sort of like transaction so the kernel has to be installed first and then in another provisioner script the boot option has to be changed.

I've also considered trying to bundle this as a script rather than an inline provisioner but I don't why that behave differently.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.