Subject: "update-grub" patch to prioritize VServer kernels before the others on Debian
From: Jean Weisbuch <jean@phpnet.org>
Date: Mon, 30 Nov 2015 18:47:51 +0100
Mon, 30 Nov 2015 18:47:51 +0100
Here are the simples patches i apply during the post install of my 
Debian preseeded installations to prioritize VServer-Beng, Proxmox and 
OpenVZ kernels (you just have to modify the egrep regex if you want to 
match other kernels) to be put before the other ones when "grub-update" 
is ran on the system and thus put at first on "/boot/grub/grub.cfg".

No need to mess anymore with the "GRUB_DEFAULT" on /etc/debian/grub-pc 
nor to remove the Debian kernels if you dont want to have problems in 
case you would use older VServer/Proxmox/OpenVZ kernels than the ones 
from Debian.


As the kernel version comparison is made using "dpkg 
--compare-versions", the trick is to simply add an epoch of "1:" before 
the kernel version on the comparison variable on 
"/usr/share/grub/grub-mkconfig_lib" (which is used by "update-grub).
The kernels with the epoch as a prefix will have an absolute priority 
over the ones without it but the order between "prioritized ones" will 
still be the same (newer VServer ones will be kept before older VServers 
and before the newest non-VServer ones).


--
"grub-pc_vz-pmx_priority.diff" is for both Squeeze and Wheezy and 
"grub-pc_vz-pmx_priority.jessie.diff" is for Jessie, it might also work 
on Ubuntu but i havent tried it.



How to apply :

    patch -d /usr/share/grub/ -p0 < grub-pc_vz-pmx_priority.diff
    update-grub



Before :

    # update-grub

    Generating grub configuration file ...

    Found linux image: /boot/vmlinuz-4.2.0-0.bpo.1-amd64

    Found initrd image: /boot/initrd.img-4.2.0-0.bpo.1-amd64

    Found linux image: /boot/vmlinuz-3.18.21-vs2.3.7.4-beng

    Found initrd image: /boot/initrd.img-3.18.21-vs2.3.7.4-beng

    Found linux image: /boot/vmlinuz-3.16.0-4-amd64

    Found initrd image: /boot/initrd.img-3.16.0-4-amd64

       No volume groups found

    done

_

_After :

    # update-grub

    Generating grub configuration file ...

    Found linux image: /boot/vmlinuz-3.18.21-vs2.3.7.4-beng

    Found initrd image: /boot/initrd.img-3.18.21-vs2.3.7.4-beng

    Found linux image: /boot/vmlinuz-4.2.0-0.bpo.1-amd64

    Found initrd image: /boot/initrd.img-4.2.0-0.bpo.1-amd64

    Found linux image: /boot/vmlinuz-3.16.0-4-amd64

    Found initrd image: /boot/initrd.img-3.16.0-4-amd64

       No volume groups found

    done



Here are the simples patches i apply during the post install of my Debian preseeded installations to prioritize VServer-Beng, Proxmox and OpenVZ kernels (you just have to modify the egrep regex if you want to match other kernels) to be put before the other ones when "grub-update" is ran on the system and thus put at first on "/boot/grub/grub.cfg".

No need to mess anymore with the "GRUB_DEFAULT" on /etc/debian/grub-pc nor to remove the Debian kernels if you dont want to have problems in case you would use older VServer/Proxmox/OpenVZ kernels than the ones from Debian.


As the kernel version comparison is made using "dpkg --compare-versions", the trick is to simply add an epoch of "1:" before the kernel version on the comparison variable on "/usr/share/grub/grub-mkconfig_lib" (which is used by "update-grub).
The kernels with the epoch as a prefix will have an absolute priority over the ones without it but the order between "prioritized ones" will still be the same (newer VServer ones will be kept before older VServers and before the newest non-VServer ones).


--
"grub-pc_vz-pmx_priority.diff" is for both Squeeze and Wheezy and "grub-pc_vz-pmx_priority.jessie.diff" is for Jessie, it might also work on Ubuntu but i havent tried it.



How to apply :
patch -d /usr/share/grub/ -p0 < grub-pc_vz-pmx_priority.diff
update-grub


Before :
# update-grub                                                                                                                                                                                                                                                                 
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.2.0-0.bpo.1-amd64
Found initrd image: /boot/initrd.img-4.2.0-0.bpo.1-amd64
Found linux image: /boot/vmlinuz-3.18.21-vs2.3.7.4-beng
Found initrd image: /boot/initrd.img-3.18.21-vs2.3.7.4-beng
Found linux image: /boot/vmlinuz-3.16.0-4-amd64
Found initrd image: /boot/initrd.img-3.16.0-4-amd64
  No volume groups found
done


After :
# update-grub
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.18.21-vs2.3.7.4-beng
Found initrd image: /boot/initrd.img-3.18.21-vs2.3.7.4-beng
Found linux image: /boot/vmlinuz-4.2.0-0.bpo.1-amd64
Found initrd image: /boot/initrd.img-4.2.0-0.bpo.1-amd64
Found linux image: /boot/vmlinuz-3.16.0-4-amd64
Found initrd image: /boot/initrd.img-3.16.0-4-amd64
  No volume groups found
done

--- grub-mkconfig_lib	2013-07-03 05:39:21.000000000 +0200
+++ grub-mkconfig_lib	2015-10-14 17:02:53.232135149 +0200
@@ -216,6 +216,12 @@
     *.old:*) a="`echo -n "$a" | sed -e 's/\.old$//'`" ; cmp=gt ;;
     *:*.old) b="`echo -n "$b" | sed -e 's/\.old$//'`" ; cmp=ge ;;
   esac
+
+  # --PHPNET-- an epoch of "1:" is added in prefix of the kernel version so dpkg will
always see these kernel versions as the most recent and put them before the others in
grub.cfg
+  echo "$a" |grep -Eq -- '-openvz-|-pve|-beng' && a="1:$a"
+  echo "$b" |grep -Eq -- '-openvz-|-pve|-beng' && b="1:$b"
+  # --PHPNET--
+
   dpkg --compare-versions "$a" $cmp "$b"
   return "$?"
 }


--- grub-mkconfig_lib	2015-03-23 17:33:03.000000000 +0100
+++ grub-mkconfig_lib	2015-11-12 15:54:17.605943726 +0100
@@ -274,6 +274,12 @@
     *.old:*) version_test_gt_a="`echo -n "$version_test_gt_a" | sed -e 's/\.old$//'`"
; version_test_gt_cmp=gt ;;
     *:*.old) version_test_gt_b="`echo -n "$version_test_gt_b" | sed -e 's/\.old$//'`"
; version_test_gt_cmp=ge ;;
   esac
+
+  # --PHPNET-- an epoch of "1:" is added in prefix of the kernel version so dpkg will
always see these kernel versions as the most recent and put them before the others in
grub.cfg
+  echo "$version_test_gt_a" |grep -Eq -- '-openvz-|-pve|-beng' && version_test_gt_a="1:$version_test_gt_a"
+  echo "$version_test_gt_b" |grep -Eq -- '-openvz-|-pve|-beng' && version_test_gt_b="1:$version_test_gt_b"
+  # --PHPNET--
+
   dpkg --compare-versions "$version_test_gt_a" "$version_test_gt_cmp" "$version_test_gt_b"
   return "$?"
 }