# Microcode initrd, genkernel and Grub2

## marco.difresco

Hi all,

as I was re-installing my system, I got to the point of installing the Intel microcode and I found this article.

At the end it mention to load the microcode at boot and specifically:

 *Quote:*   

> If you're already using another initrd, you can specify several of them in your bootloader or, if your bootloader does not support that, merge them yourself using cat. The early microcode might be ignored if it's not the first initrd, though.

 

It is something I would like to test, especially since in the past I had issues loading the microcode as a module.

I compile the kernel with genkernel and it creates an initrd; therefore I am wondering if sys-boot/grub-2.02_beta2-r3 support the specification of several initrd.

If so, where do I configure it to pickup the microcode initrd and put it with along the genkernel inside /boot/grub/grub.cfg? I assume it is somewhere /etc/grub.d/ in order to make the integration automatic every time I execute:

```
LANG=C grub2-mkconfig -o /boot/grub/grub.cfg
```

If Grub doesn't support the use of several initrd, the above quote mentions the possibility of merging the microcode initrd and the other (genkernel in my case) initrd. Searching around I found this article that mention how to do it, and in specific:

```
# cp /boot/initramfs-genkernel-x86_64-3.17.1-gentoo-r1 /home/fitzcarraldo/initramfs-genkernel-x86_64-3.17.1-gentoo-r1.bak.early # Backup the recently-built initramfs first.

# cat ucode.cpio /boot/initramfs-genkernel-x86_64-3.17.1-gentoo-r1 >/boot/initramfs-genkernel-x86_64-3.17.1-gentoo-r1.ucode

# cp /boot/initramfs-genkernel-x86_64-3.17.1-gentoo-r1.ucode /boot/initramfs-genkernel-x86_64-3.17.1-gentoo-r1

# rm /boot/initramfs-genkernel-x86_64-3.17.1-gentoo-r1.ucode

```

I tried to do it for practice sake and it is doable, but I am wondering if there is a way to "generalize" it so I can put it in a script that automatically pick up the last kernel I compiled and it merges the micorode initrd in it - for example I am using the following script to compile the kernel:

```
#!/bin/bash

export CONCURRENCY_LEVEL=9

cd /usr/src/linux

make silentoldconfig

genkernel --oldconfig --menuconfig --save-config --install --symlink --no-dmraid --no-zfs all

LANG=C grub2-mkconfig -o /boot/grub/grub.cfg

emerge @module-rebuild
```

and it would be nice to put some line at the end of it to merge the microcode initrd.

Thanks in advance

----------

## frostschutz

isn't it just "initrd a b c d ..." in grub?

I don't know if there is a genkernel --microcode...

----------

## Sprchkn

This is old, but if you name the microcode file as "early-ucode.cpio" as given in the Gentoo Wiki, you can just add the text:

```
"early-ucode.cpio" \
```

into the /etc/grub.d/10-linux file.

For instance, mine read:

```

initrd=

for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \

```

and I changed it to:

```

initrd=

for i in "early-ucode.cpio" \

         "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \

```

I'm not sure how bash sorts files, so that may not work if early-ucode.cpio isn't picked up before another ramdisk image, since several sources state that the ucode should be loaded first. That should at least point you in the right direction though.

----------

## dweezil-n0xad

Arch Linux has a patch to make grub2-mkconfig detect the microcode and put it first in initrd.

I've edited and put this patch in /etc/portage/patches/sys-boot/grub/grub-intel-microcode.patch and emerged grub.

```

--- a/util/grub.d/10_linux.in

+++ b/util/grub.d/10_linux.in

@@ -65,6 +65,12 @@ case x"$GRUB_FS" in

    ;;

 esac

 

+intel_ucode=

+if test -e "/boot/microcode.cpio" ; then

+    gettext_printf "Found Intel Microcode\n" >&2

+    intel_ucode="$(make_system_path_relative_to_its_root /boot/microcode.cpio)"

+fi

+

 title_correction_code=

 

 linux_entry ()

@@ -136,7 +142,7 @@ EOF

     message="$(gettext_printf "Loading initial ramdisk ...")"

     sed "s/^/$submenu_indentation/" << EOF

    echo   '$(echo "$message" | grub_quote)'

-   initrd   ${rel_dirname}/${initrd}

+   initrd   ${intel_ucode} ${rel_dirname}/${initrd}

 EOF

   fi

   sed "s/^/$submenu_indentation/" << EOF

```

I use this script to compile new kernels

```
#!/bin/sh

zcat /proc/config.gz > /usr/src/linux/.config

cd /usr/src/linux

make oldconfig

make -j8 && make modules_install

mount /boot

make install

genkernel --install --no-ramdisk-modules initramfs

cp /lib/firmware/microcode.cpio /boot

grub2-mkconfig -o /boot/grub/grub.cfg

emerge @module-rebuild
```

the result in /boot/grub/grub.cfg:

```
...

echo   'Loading initial ramdisk ...'

initrd   /microcode.cpio /initramfs-genkernel-x86_64-4.6.3-gentoo

...
```

----------

## alinefr

As you are already using genkernel, it can do the things in the right way for you.

I use genkernel for luks & zfs and my initramfs is generated in-kernel (no grub), but it should work in a separated initramfs file as well.

So my genkernel.conf has:

INITRAMFS_OVERLAY="/usr/share/microcode"

And my /usr/share/microcode:

```

/usr/share/microcode/

/usr/share/microcode/kernel

/usr/share/microcode/kernel/x86

/usr/share/microcode/kernel/x86/microcode

/usr/share/microcode/kernel/x86/microcode/GenuineIntel.bin

```

It just works, out of the box. Genkernel is clever enough to put this overlay at the start of the initramfs.

----------

## candrews

 *dweezil-n0xad wrote:*   

> Arch Linux has a patch to make grub2-mkconfig detect the microcode and put it first in initrd.
> 
> I've edited and put this patch in /etc/portage/patches/sys-boot/grub/grub-intel-microcode.patch and emerged grub.
> 
> ```
> ...

 

I reported an issue with Gentoo at https://bugs.gentoo.org/show_bug.cgi?id=587422 including a pull request. Hopefully at some point this all just works  :Smile: 

----------

## dweezil-n0xad

 *alinefr wrote:*   

> As you are already using genkernel, it can do the things in the right way for you.
> 
> I use genkernel for luks & zfs and my initramfs is generated in-kernel (no grub), but it should work in a separated initramfs file as well.
> 
> So my genkernel.conf has:
> ...

 This only works on my system when the generated seperate initramfs is not compressed (COMPRESS_INITRD="no").

I tried with lzop, xz and gzip. No microcode update.

With no initramfs compression:

```
[    0.000000] microcode: microcode updated early to revision 0x20, date = 2016-03-16
```

edit: a little research confirms that the ucode has to be in uncompressed initramfs

----------

