LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   grub2: Skipping one partition from OS detection (http://www.linuxquestions.org/questions/linux-software-2/grub2-skipping-one-partition-from-os-detection-741100/)

eantoranz 07-18-2009 09:38 PM

grub2: Skipping one partition from OS detection
 
I have this laptop where I have GNU/Linux, Windows and one recovery partition that I haven't removed.

When I run update-grub2, I get the instances of GNU/Linux, memtest, Windows and another for the recovery partition. Is there a way to make os-prober skip one partition?

Simon Bridge 07-19-2009 02:34 AM

http://pwet.fr/man/linux/administrat...me/update_grub
You edit /boot/grub/menu.lst and remove the entry for that partition.

eantoranz 07-19-2009 10:26 AM

yea, simon, but then if I run update-grub2 it'd be back, wouldn't it?

Simon Bridge 07-20-2009 01:10 AM

Hmmm... looking closer: The old update-grub just added entries for each /boot/vmlinuz-* it saw and did nothing to probe partitions.

The main issue here is lack of documentation:
http://osdir.com/ml/debian-bugs-dist.../msg03754.html

Here is an attempt to rectify this:
http://ubuntuforums.org/showthread.php?t=1195275


You issue is that you want to skip the search for non-linux OSs
On a quick look -

* 10_linux searches for installed linux kernels.
* 30_os-prober searches for other Operating Systems.

So you don't want to run the prober.

However, grub2 uses different paradigms to grub and you should look through that link. eg. to remove an entry, change the permissions of the grub.d file ... I can see how you may create a bash alias which will remove the extraneous entry each time update-grub2 is run.


Is it possible that the bootable flag is set for more than one partition?

johanfa 12-15-2009 03:24 PM

Hello eantoranz,

If you want to skip one (or some) partition/s (not only recovery or memtest entries), this is a simple easy trick that worked for me.

SIMPLE EASY TRICK: EDIT 3 FILES TO PREVENT SOME PARTITIONS BEING AUTODETECTED BY GRUB2

STEP 1)

Add the variable
GRUB_DONT_ AUTODETECT_PART
to the file
/etc/default/grub

This variable must contain the name of the partitions you don't want to be autodetected.
It's the list of these space-separated names.
The partitions naming is the usual one in linux.
If you are not sure, run:
update-grub
as superuser (root),
i.e. if your distribution is, for example, Ubuntu, run:
sudo update-grub
and take a note of the names (without the /dev/)

Example:
This is my /etc/default/grub:

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.

GRUB_DEFAULT=0
GRUB_TIMEOUT=7
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="noresume quiet splash verbose vga=788"
GRUB_CMDLINE_LINUX=

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
GRUB_GFXMODE=800x600

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entrys
#GRUB_DISABLE_LINUX_RECOVERY="true"

# This variable has been added by me
# It prevents some partitions to be autodetected
GRUB_DONT_AUTODETECT_PART="hda1 hda5"

As you can see, in my case I don't want hda1 and hda5 to be autodetected.



STEP 2)

Apply a little patch to the file /usr/sbin/grub-mkconfig.
(This is the really executed program when you run update-grub)

After the second export command, add another one (more or less, in the line 229) export with this new created variable:
export GRUP_DONT_AUTODETECT_PART

Example:
This is my /usr/sbin/grub-mkconfig:

... ...
# These are optional, user-defined variables.
export GRUB_DEFAULT \
GRUB_HIDDEN_TIMEOUT \
GRUB_HIDDEN_TIMEOUT_QUIET \
GRUB_TIMEOUT \
GRUB_DISTRIBUTOR \
GRUB_CMDLINE_LINUX \
GRUB_CMDLINE_LINUX_DEFAULT \
GRUB_TERMINAL_INPUT \
GRUB_TERMINAL_OUTPUT \
GRUB_SERIAL_COMMAND \
GRUB_DISABLE_LINUX_UUID \
GRUB_DISABLE_LINUX_RECOVERY \
GRUB_GFXMODE \
GRUB_DISABLE_OS_PROBER

# This variable is intended to prevent some partitions to be detected
export GRUB_DONT_AUTODETECT_PART

if test "x${grub_cfg}" != "x"; then
... ...



STEP 3)

And lastly, you must apply 2 little patchs to the file
/etc/grub.d/30_os-prober
Note.- In my package the name begins by 30. It's not mandatory.
This number establishes the execution order (sequence) for all the files (executable files) in the directory.

a) Fisrt patch:

# My patch to prevent some partitions being autodetected
PARTITIONNAME="`echo ${DEVICE} | cut -c 6- 2> /dev/null`"
if [ "`echo ${GRUB_DONT_AUTODETECT_PART} | grep -e ${PARTITIONNAME} 2> /dev/null`" ] ; then
BOOT="banned"
fi
# End of first part of the patch

Example:
Here you can see where it must be placed (in the line 45, more or less):

for OS in ${OSPROBED} ; do
DEVICE="`echo ${OS} | cut -d ':' -f 1`"
LONGNAME="`echo ${OS} | cut -d ':' -f 2 | tr '^' ' '`"
LABEL="`echo ${OS} | cut -d ':' -f 3 | tr '^' ' '`"
BOOT="`echo ${OS} | cut -d ':' -f 4`"

# My patch to prevent some partitions being autodetected
PARTITIONNAME="`echo ${DEVICE} | cut -c 6- 2> /dev/null`"
if [ "`echo ${GRUB_DONT_AUTODETECT_PART} | grep -e ${PARTITIONNAME} 2> /dev/null`" ] ; then
BOOT="banned"
fi
# End of first part of the patch

if [ -z "${LONGNAME}" ] ; then
LONGNAME="${LABEL}"
fi


b) Second patch:

# My patch to prevent some partitions being autodetected: 2n part
banned)
echo " ${DEVICE} has been prevented to be autodetected." >&2
;;
# End of the 2n part of my patch

Example:
Here you can see where it must be placed (in the line 159, more or less, nearly at the end of the file):

EOF
;;
# My patch to prevent some partitions being autodetected: 2n part
banned)
echo " ${DEVICE} has been prevented to be autodetected." >&2
;;
# End of the 2n part of my patch
hurd|*)
echo " ${LONGNAME} is not yet supported by grub-mkconfig." >&2
;;
esac
done



Et voilą!
At this moment you only must run:

update-grub

as superuser (root)


This trick has been applied to the files provided by the package
grub-common 1.97~beta3-1
and checked successfuly on
Debian squeeze (testing)

Regards,

Johan

syg00 12-15-2009 05:34 PM

Interesting. What about if the package gets updated ?.

johanfa 12-16-2009 12:59 PM

Hello syg00,

Unfortunately grub2 is under development yet and this trick is only a workaround.

Well, in short, if the package gets updated, you'll have to reedit the updated files if grub2 developers won’t have included this feature yet.

Anyway, reedit the files is not difficult at all (it's only a pain) and it may be worthwhile if:

a) you have installed a distribution that is not well recognized by grub2 (i.e. Mandriva): in this case the straightforward workaround is preventing its autodetection and booting the system by means of a customized entry in the file
/etc/grub.d/40_custom

Example:
This is my /etc/grub.d/40_custom:

#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
menuentry "Mandriva Linux release 2010.0 (Official) for x86_64 (on /dev/hda5)" {
insmod ext2
set root=(hd0,5)
linux (hd0,5)/boot/vmlinuz-2.6.31.5-desktop-1mnb
initrd (hd0,5)/boot/initrd-2.6.31.5-desktop-1mnb.img
}

b) you have a partition you don't want to be accessed or booted by users (i.e. Vista recovery partition): in this case the partition only needs to be ignored.

Regards,

Johan

syg00 12-16-2009 04:02 PM

The question was meant as a warning to others.
Have you submitted the change upstream ? - seems like a worthwhile addition.

justin.perkins 04-16-2012 11:24 AM

Quick update. works on natty 11.04
 
I used this method successfully with 11.04, thanks for this. Just wondered if it was still going to work and when I discovered it did, I figured I'd put a note up (which I looked for but didn't find initially)

*8^)

WallyWorld 05-24-2012 01:48 PM

Quote:

Originally Posted by johanfa (Post 3792729)
this is a simple easy trick that worked for me.
SIMPLE EASY TRICK: EDIT 3 FILES TO PREVENT SOME PARTITIONS BEING AUTODETECTED BY GRUB2

Thanks for this, I simplified it down to 5 lines.

So for anyone coming here from Google, as I did, all you need to do is modify your '30_os-prober' file
For me it is located at '/etc/grub.d/30_os-prober'

So open it with a text editor (I used nano)
then go to line 139, which should look like this

Code:

for OS in ${OSPROBED} ; do
  DEVICE="`echo ${OS} | cut -d ':' -f 1`"
  LONGNAME="`echo ${OS} | cut -d ':' -f 2 | tr '^' ' '`"
  LABEL="`echo ${OS} | cut -d ':' -f 3 | tr '^' ' '`"
  BOOT="`echo ${OS} | cut -d ':' -f 4`"
  #### ::: Line 139 ::: ####
  if [ -z "${LONGNAME}" ] ; then
    LONGNAME="${LABEL}"
  fi


then modify it to add the following lines, that are highlighted red.

Code:

for OS in ${OSPROBED} ; do
  DEVICE="`echo ${OS} | cut -d ':' -f 1`"
  LONGNAME="`echo ${OS} | cut -d ':' -f 2 | tr '^' ' '`"
  LABEL="`echo ${OS} | cut -d ':' -f 3 | tr '^' ' '`"
  BOOT="`echo ${OS} | cut -d ':' -f 4`"

  # Patch to prevent some partitions being autodetected
  SKIP_THESE_DEVICES="sda7 sda1"

  PARTITIONNAME="`echo ${DEVICE} | cut -c 6- 2> /dev/null`"
  if [ "`echo ${SKIP_THESE_DEVICES} | grep -e ${PARTITIONNAME} 2> /dev/null`" ] ; then
    continue
  fi
  # End of patch


  if [ -z "${LONGNAME}" ] ; then
    LONGNAME="${LABEL}"
  fi

You can move the line 'SKIP_THESE_DEVICES="sda7 sda1"' to the top of the '30_os-prober' file if you want so you can easily add or remove devices without scrolling down

syg00 05-24-2012 06:06 PM

Better to generate a patch. Looks like it should also fit on F16 which now also uses grub2.
I am responsible for a netbook that has been booted to Windows recovery twice - this automatically starts a rebuild. Might just apply this ... :banghead:

Wim Sturkenboom 09-04-2012 03:16 AM

I'm aware that the original post is very old ;) Just an update to say thanks and update for Ubuntu 12.04

For Ubuntu 12.04, the line from WallyWorld's patch has changed to 158. Everybody thanks for posting the solutions.

Code:

    152 for OS in ${OSPROBED} ; do
    153  DEVICE="`echo ${OS} | cut -d ':' -f 1`"
    154  LONGNAME="`echo ${OS} | cut -d ':' -f 2 | tr '^' ' '`"
    155  LABEL="`echo ${OS} | cut -d ':' -f 3 | tr '^' ' '`"
    156  BOOT="`echo ${OS} | cut -d ':' -f 4`"
    157
    158 # WimS
    159 # based on http://www.linuxquestions.org/questions/linux-software-2/grub2-skipping-one-partition-from-os-detection-741100/ post #10
    160
    161  # Patch to prevent some partitions being autodetected
    162  SKIP_THESE_DEVICES="sdb7 sdb1"
    163
    164  PARTITIONNAME="`echo ${DEVICE} | cut -c 6- 2> /dev/null`"
    165  if [ "`echo ${SKIP_THESE_DEVICES} | grep -e ${PARTITIONNAME} 2> /dev/null`" ] ; then
    166    continue
    167  fi
    168  # End of patch
    169
    170 # WimS End
    171
    172  if [ -z "${LONGNAME}" ] ; then
    173    LONGNAME="${LABEL}"
    174  fi
    175



All times are GMT -5. The time now is 11:04 PM.