GRUB2: set default kernel by name

Other talk about Salix
Post Reply
User avatar
damNageHack
Posts: 663
Joined: 24. Sep 2009, 17:07

GRUB2: set default kernel by name

Post 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 ;)
Last edited by damNageHack on 10. Dec 2009, 18:49, edited 1 time in total.
Image
This is the oppinion of the author, it does not force you to share and is signed automatically.
You are free to keep them all errors for your own. Linux is the best game I ever played.
Post Reply