Skip to content

Instantly share code, notes, and snippets.

@jasraj
Last active January 12, 2017 13:43
Show Gist options
  • Save jasraj/646bdfc93aa6d8e2fca33fe1b5692e54 to your computer and use it in GitHub Desktop.
Save jasraj/646bdfc93aa6d8e2fca33fe1b5692e54 to your computer and use it in GitHub Desktop.
Updates GRUB to boot with the previous good kernel version (4.4.0-57) for zfs-native 0.6.5.7
# zfs-native 0.6.5.7 - Kernel Downgrade to 4.4.0-57
# Does not build with 4.4.0-59
# Install previous good kernel
apt-get install linux-image-4.4.0-57-generic linux-image-extra-4.4.0-57-generic
# Replace primary boot kernel (vmlinuz and initrd.img) in grub.cfg
sed -E "s/\/(vmlinuz|initrd\.img)-4\.4\.0-59-generic/\/\1-4.4.0-57-generic/g" /boot/grub/grub.cfg > /tmp/grub-modified-4.4.0-57.cfg
# CHECK THE GENERATED FILE (look for code within "menuentry 'Ubuntu'") for updated kernel version
# Update grub.cfg
cp -v /boot/grub/grub.cfg /tmp/grub.$(date +%Y-%m-%d-%H-%M-%S)-.bak
cp -v /tmp/grub-modified-4.4.0-57.cfg /boot/grub/grub.cfg
# Reboot
shutdown -r now
@leskanic
Copy link

leskanic commented Jan 12, 2017

While this works, grub.cfg gets overwritten with every kernel update or grub update. More persistent way is to change GRUB_DEFAULT value in /etc/default/grub from default value of 0 (meaning 'boot first item from the boot menu') to the new value. How to figure out that value?
Looking into /boot/grub/grub.cfg, mark down $menuentry_id_option string of the menu item you want to boot. This, in typical Ubuntu installation, will be a sub-item of 'Advanced options for Ubuntu' menu item and therefore has to be provided in the form of item>sub-item.

This is an example of /boot/grub/grub.cfg contents:

menuentry 'Ubuntu' <...> $menuentry_id_option 'gnulinux-simple-blah' {
<...>
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-blah' {
menuentry 'Ubuntu, with Linux 4.4.0-59-generic' <...> $menuentry_id_option'gnulinux-4.4.0-59-generic-advanced-blah' {
<...>
menuentry 'Ubuntu, with Linux 4.4.0-59-generic (upstart)' <...> $menuentry_id_option 'gnulinux-4.4.0-59-generic-init-upstart-blah' {
<...>
menuentry 'Ubuntu, with Linux 4.4.0-59-generic (recovery mode)' <...> $menuentry_id_option 'gnulinux-4.4.0-59-generic-recovery-blah' {
<...>
menuentry 'Ubuntu, with Linux 4.4.0-57-generic' <...> $menuentry_id_option'gnulinux-4.4.0-57-generic-advanced-blah' {

Where blah is rather long string I trimmed for brevity.

Since kernel 4.4.0-57 is in submenu, I need to pull both its id as well as id of its parent item (in reversed order) and separate them by > sign. The resulting string therefore is gnulinux-advanced-blah>gnulinux-4.4.0-57-generic-advanced-blah. Note the single quotes have to be omitted. This string, enclosed in (double) quotes, is to be provided in /etc/default/grub GRUB_DEFAULT item: instead of default
GRUB_DEFAULT=0
the line would now read
GRUB_DEFAULT="gnulinux-advanced-blah>gnulinux-4.4.0-57-generic-advanced-blah"

Once /etc/default/grub is updated, sudo update-grub has to be run to update /boot/grub/grub.cfg and the system can be rebooted and updated as needed without risking loss of these modifications due to kernel/grub update. Once new and "ZFS-working" kernel is out, /etc/default/grub can be reverted back to GRUB_DEFAULT=0, followed by sudo update-grub and the system will again boot the most recent kernel version.

@jasraj
Copy link
Author

jasraj commented Jan 12, 2017

I didn't realise you could use the entry ID for the GRUB_DEFAULT configuration (I only saw it using list index, e.g. 1>2 which didn't work for me). Thanks for sharing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment