Page 1 of 1

GRUB2: set default kernel by name

Posted: 10. Dec 2009, 18:42
by damNageHack
I don't know how to ingegrate this into /etc/grub.d/ for grub-mkconfig cause default value is set before all kernels are parsed. In my opinion a design problem of grub, setting a static default value but parsing then for kernels dynamically makes not really much sense to me.

Code: Select all

#! /bin/sh -e

# grub-mkconfig helper script.
# Copyright (C) 2006,2007,2008,2009  Free Software Foundation, Inc.
#
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

# Parse the grub config for linux kernels
# and choose preferred kernel by name as default.
# Caution! If there is no version string in the name available,
# you have to add a space, e.g. like this "vmlinuz ".
# Example: GRUB_PREFERRED_KERNEL="vmlinuz-huge-smp-testing-2.6.31.5-smp"

. /etc/default/grub

cfg="/boot/grub/grub.cfg"

declare -i preferred=`grep "linux" $cfg | grep -m1 -n "$GRUB_PREFERRED_KERNEL" | awk 'BEGIN {FS = ":"} {print $1}'`
if [ $preferred > 1 ] ; then
  declare -i preferred_default=$preferred-2
  echo "Changing default to preferred '$GRUB_PREFERRED_KERNEL' = $preferred_default" >&2
  sed -e "s/^set default=[0-9]*$/set default=$preferred_default/" $cfg  > $cfg.new ;
  mv $cfg.new $cfg
fi
Oh ... and thanks to Shador & thenktor for the nice help ;)