Extracting Ubuntu (Grub2) boot entries.

Here’s a short script which looks for Ubuntu boot entries in ./boot/grub/grub.cfg on a mounted Ubuntu partition and displays them to standard output in legacy Grub format. If you’re happy with the output you can redirect it to a file and paste it in openSUSE /boot/grub/menu.lst
Run it from the directory where your Ubuntu root or boot partition is mounted.


#!/bin/bash
#: Title       : ugrub
#: Date Created: Wed Nov  3 03:32:36 PDT 2010
#: Last Edit   : Wed Nov  3 03:32:36 PDT 2010
#: Author      : please_try_again
#: Version     : 0.1
#: Description : convert Ubuntu Grub2 to Legacy Grub entry to standard output
#
# Copy and paste this text into a text file and save it in your home area, bin folder (~/bin) as the file ugrub
# Once the file is saved, you must make the file executable using the terminal command: chmod +u ~/bin/ugrub
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Usage: 
# mount your Ubuntu / partition in /mnt 
# or, if you have a separate one,
# mount your Ubuntu /boot partition in /mnt 
# and comment out the line below:
BOOT="/boot"   
# cd to /mnt and run the script from there.
# If the output does make sense, redirect it to a file
# and paste it in your /boot/grub/menu.lst 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  -e .$BOOT/grub/grub.cfg ] || exec echo "grub.cfg not found"

distro=$(lsb_release -s -i 2> /dev/null)
distro=${distro:-linux}

cat << EOFSED | sed -f - .$BOOT/grub/grub.cfg  | awk -F "," '{ if ( $2 ~ /[0-9])/) { ID=$2-1 ; print $1","ID")" } else print }'
1,/### BEGIN \/etc\/grub\.d\/10_linux ###/d
/### END/,\$d
/menuentry/ i\

/menuentry/ i\
###Don't change this comment - YaST2 identifier: Original name: ${distro}###
/recordfail/d
/insmod/d
/search --no-floppy/d
/echo/d
s/menuentry/title/
s/}//
s/	]*set root='\(.*\)'/    root \1/
s/msdos//
s/ 	]*linux 	]/    kernel /
s/ 	]*initrd 	]/    initrd /
/{/s/' .*/'/
EOFSED

  • There are tabs inside regular expressions. Make sure you don’t remove them from the code.

Well you gave me a reason to load Ubuntu on something so I can try out your script. Once again, thanks for taking the time to share this with us please_try_again.

Thank You,