Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Tutorials & Tips
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Tutorials & Tips
The place to find Ubuntu related Tips & Tricks.

 
Thread Tools Display Modes
Old July 27th, 2006   #1
catlett
Chocolate Ubuntu Mocha Blend
 
catlett's Avatar
 
Join Date: Mar 2006
Location: Boston, MA
Beans: 1,862
Ubuntu 6.10 Edgy
How to restore Grub from a live Ubuntu cd.

This will restore grub if you already had grub installed but lost it to a windows install or some other occurence that erased/changed your MBR so that grub no longer appears at start up or it returns an error.

(This how to is written for Ubuntu but should work on other systems. The only thing to take note of, when you see "sudo" that will mean to you that the following command should be entered at a root terminal.)

Boot into the live Ubuntu cd. This can be the live installer cd or the older live session Ubuntu cds.

When you get to the desktop open a terminal and enter. (I am going to give you the commands and then I will explain them later)

Code:
sudo grub
This will get you a "grub>" prompt (i.e. the grub shell). At grub>. enter these commands

Code:
find /boot/grub/stage1
This will return a location. If you have more than one, select the installation that you want to provide the grub files.
Next, THIS IS IMPORTANT, whatever was returned for the find command use it in the next line (you are still at grub>. when you enter the next 3 commands)

Code:
root (hd?,?)
Again use the value from the find command i.e. if find returned (hd0,1) then you would enter root (hd0,1)

Next enter the command to install grub to the mbr

Code:
setup (hd0)
Finally exit the grub shell
Code:
quit
That is it. Grub will be installed to the mbr.
When you reboot, you will have the grub menu at startup.

Now the explanation.
Sudo grub gets you the grub shell.
Find /boot/grub/stage1 has grub locate the file stage1. What this does is tell us where grub's files are. Only a small part of grub is located on the mbr, the rest of grub is in your boot folder. Grub needs those files to run the setup. So you find the files and then you tell grub where to locate the files it will need for setup.
So root (hd?,?) tells grub it's files are on that partition.
Finally setup (hd0) tells grub to setup on hd0. When you give grub the parameter hd0 with no following value for a partition, grub will use the mbr. hd0 is the grub label for the first drive's mbr.
Quit will exit you from the grub shell.



THIS IS AN EDIT. 5-HT MADE A GOOD POINT AND I AM JUST GOING TO COPY/PASTE IT HERE

Quote:
Just have recommendation to add that may be irrelevant: it might be of benefit to give an explicit warning (though it is mentioned) that this guide will write GRUB to the MBR (just in case someone is using a different boot loader on their MBR and would like to reinstall GRUB to a partition).

If someone wants GRUB on a partition, the 'setup (hd0)' step can be modified to 'setup (hdX,Y)'. Where X is the hard disk, and Y the partition using GRUB's nomenclature of starting from 0 (first partition=0, second=1,...).
THIS IS ANOTHER EDIT. TOSK POSTED A WAY TO MOUNT PROC AND UDEV. THIS WAS NEEDED BECAUSE GRUB WASN'T RECOGNISING THE DRIVE. I THOUGHT IT WAS A VALUABLE COMMENT AND DECIDED TO PUT IT IN THE ORIGINAL POST SO PEOPLE WILL SEE IT AT THE TOP. IT MAY BE MISSED AS JUST A REPLY POST DOWN THE PAGE.
ALL KNOWLEDGE IS WELCOME!


Quote:
Mine was a slightly different story. I couldn't get grub to find the stage1 file or even recognize my drive. So I borrowed some knowledge I picked up while using Gentoo:

You have to mount your root partition using the livecd:
Code:

$
Code:
sudo mkdir /mnt/root
$
Code:
sudo mount -t ext3 /dev/sda6 /mnt/root
Then you have to mount the proc subsystem and udev inside /mnt/root also:
Code:

$
Code:
sudo mount -t proc none /mnt/root/proc
$
Code:
sudo mount -o bind /dev /mnt/root/dev
Doing this allows grub to discover your drives. Next you have to chroot:
Code:

$
Code:
sudo chroot /mnt/root /bin/bash
Now that you're chrooted into your drive as root everything should work.
Code:

#
Code:
sudo grub
I edited in the sudo, just to be safe. When I enter grub and not sudo grub, grub cannot find the file. I do not know if the chroot changes this because I did not try it that way. In the end I figured it was better to err on the side of caution. Tosk I hope you don't mind my editing of your reply.
grub>
Code:
find /boot/grub/stage1
It found mine on (hd0,5)
Code:

grub>
Code:
root (hd0,5)
It successfully scanned the partition and recognized the filesystem-type
Code:

grub>
Code:
setup (hd0)
That was it. It installed and on reboot I was thrown back into Ubuntu.

This might help some people who are having issues so I thought I would post it.

PLEASE NOTE: My Ubuntu was installed to /dev/sda6. This may not be the same for everyone. /dev/sdaX means it's SCSI/SATA/USB/FireWire drive. And it's partition 6 because I have a weird partitioning scheme in place.

--Tosk


**This set of instruction is a combination of 2 guides I saw before. One was a Mepis grub how to but I never found it again. The other is the grub manual. It's section explained the find, root, setup process but never mentioned it could be done from a live session.

This is the grub link http://www.gnu.org/software/grub/man...-GRUB-natively

Post script;
Just to post as much information as possible, this is an older how to for resoring grub to the mbr. The original post is directions for using the install cd and then there are replies that mention the method I posted here, as well as the chroot method mlind mentioned. If this method fails, you may want to try this http://ubuntuforums.org/showthread.php?t=24113

Last edited by catlett; September 20th, 2006 at 08:46 PM..
catlett is offline   Reply With Quote
Old July 27th, 2006   #2
mlind
Skinny Extra Sweet Ubuntu
 
mlind's Avatar
 
Join Date: Nov 2005
Beans: 2,325
Ubuntu Intrepid Ibex (testing)
Re: How to install Grub from a live Ubuntu cd.

This looks very useful info. Is the result same as running grub-install /dev/xxx ?
__________________
Building on Ubuntu using [pbuilder]
Other resources [Upgrading from CVS, Gaim2, Anjuta, Rhythmbox]
mlind is offline   Reply With Quote
Old July 27th, 2006   #3
catlett
Chocolate Ubuntu Mocha Blend
 
catlett's Avatar
 
Join Date: Mar 2006
Location: Boston, MA
Beans: 1,862
Ubuntu 6.10 Edgy
Re: How to install Grub from a live Ubuntu cd.

The end result is the same but grub-install /dev/hd? doesn't work with a live cd. When I run grub-install from a live cd, I get an error about grub not being able to read the bios.
When I first installed ubuntu there was a grub re-install guide. It said something about mounting the ubuntu partition. Then cd to the directory and chroot. At the time it was over my head and I didn't pay much attention to it. Now I can't find it.
The other guide was the Breezy guide where you trick the install cd into re-installing grub.I gues it worked for people but I thought there had to be a simpler way to do it.
I finally found the grub manual entry on native installs and tried it from a live cd and it worked.
Hopefully reinstalling grub will now be a simple thing to do from a live cd.
catlett is offline   Reply With Quote
Old July 28th, 2006   #4
mlind
Skinny Extra Sweet Ubuntu
 
mlind's Avatar
 
Join Date: Nov 2005
Beans: 2,325
Ubuntu Intrepid Ibex (testing)
Re: How to install Grub from a live Ubuntu cd.

You probably mean something like this as root
Code:
mkdir /mnt/root
mount /dev/xxx? /mnt/root
chroot /mnt/root /bin/bash
grub-install /dev/xxx
Thanks for your HOWTO though, I'll try it next time I screw up my grub. Which is probably at weekend when I testdrive Edgy.
__________________
Building on Ubuntu using [pbuilder]
Other resources [Upgrading from CVS, Gaim2, Anjuta, Rhythmbox]
mlind is offline   Reply With Quote
Old July 28th, 2006   #5
catlett
Chocolate Ubuntu Mocha Blend
 
catlett's Avatar
 
Join Date: Mar 2006
Location: Boston, MA
Beans: 1,862
Ubuntu 6.10 Edgy
Re: How to install Grub from a live Ubuntu cd.

Quote:
Originally Posted by mlind View Post
You probably mean something like this as root
Code:
mkdir /mnt/root
mount /dev/xxx? /mnt/root
chroot /mnt/root /bin/bash
grub-install /dev/xxx
Thanks for your HOWTO though, I'll try it next time I screw up my grub. Which is probably at weekend when I testdrive Edgy.
Please try the chroot grub-install and post what happens. Like I said I saw the how to when I didn't understand commands and never saw it again. Then I saw the grub manual describing the find, root, setup commands and I went with that.I think find, root, setup doesn't return the bios error because they are given from the grub shell.
Just for FYI, this is the error I get with grub-install from the live session.
Code:
ubuntu@ubuntu:~$ sudo grub-install /dev/hda
Probing devices to guess BIOS drives. This may take a long time.
Could not find device for /boot: Not found or not a block device.
This is the output of find, root, setup

Code:
grub> find /boot/grub/stage1
 (hd0,1)
 (hd0,10)

grub> root (hd0,10)
 Filesystem type is ext2fs, partition type 0x83

grub> setup (hd0)
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/e2fs_stage1_5" exists... yes
 Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... failed (this is not fatal)
 Running "embed /boot/grub/e2fs_stage1_5 (hd0,10)"... failed (this is not fatal
)
 Running "install /boot/grub/stage1 d (hd0) /boot/grub/stage2 p /boot/grub/menu
.lst "... succeeded
Done.
catlett is offline   Reply With Quote
Old August 1st, 2006   #6
catlett
Chocolate Ubuntu Mocha Blend
 
catlett's Avatar
 
Join Date: Mar 2006
Location: Boston, MA
Beans: 1,862
Ubuntu 6.10 Edgy
Re: How to install Grub from a live Ubuntu cd.

Just wanted to post with more concrete proof. I knew it was right from the documentation and I got feedback from one success story plus I ran it but it was just replacing the same list so it was hard to tell.
BUT NOW, I have absolute proof. I just installed Fedora Core 5 yesterday. I allowed Fedora to install grub. Fedora's grub didn't recognise Ubuntu's installation.
I thought to myself, Perfect! Lets put the guide to work. This is just the scenario I was hoping to help people with. You had grub but lost it to another install (or another issue that caused grub to return an error or what not. The main thing is that grub is already installed somewhere. The issue is you can't access it)
This is actually how anyone could handle this. I copied down Fedora's grub entry , then I rebooted and put the Ubuntu Live installation cd. When I got to the desktop I opened the terminal and did the 4 commands. One from the regular ubuntu terminal and 3 at the grub shell.
Code:
sudo grub
then at grub>
Code:
find /boot/grub/stage1
It returned 2 listings, hd0,2 and hd0,6. I forgot Ubuntu's root partition believe it or not but I knew I just put Fedora on /dev/hda3 (that is hd0,2 to grub)
Now that I know Ubuntu's grub is on hd0,6 I let the grub shell know what grub files I wanted to use
Still at grub>
Code:
root (hd0,6)
Now the command to install grub on the mbr with hd0,6's grub files
Code:
setup (hd0)
Grub then prints a little read out in the terminal about how it found the kernel and everything else until it says "succeeded". Now all I have to do is get out of the shell
grub>
Code:
quit
Then I shutdown Ubuntu and restarted the computer.
Gone was Fedora's grub and in it's place was my Ubuntu grub menu. Success.
(As for accessing Fedora, I just inserted the Fedora entries I copied from Fedora's grub before I booted the live cd. Now I have Ubuntu's grub back and it is booting to fedora.)

To sum up, hd0,6's grub installed flawlessly on the mbr. It took all of 2 minutes from putting the cd in to restarting.

Here it is again just to put it to bed.
Code:
sudo grub
Code:
find /boot/grub/stage1
whatever the find command returns use for root
Code:
root (hd?,?)
Code:
setup (hd0)
hd0 is the mbr of the first drive (as far as grub is concerned)
Code:
quit

Last edited by catlett; August 2nd, 2006 at 08:29 PM..
catlett is offline   Reply With Quote
Old July 24th, 2009   #7
kuppalli
First Cup of Ubuntu
 
Join Date: May 2009
Beans: 2
Smile Re: How to install Grub from a live Ubuntu cd.

This worked with me.
Thanks a lot.

Quote:
Originally Posted by catlett View Post
Please try the chroot grub-install and post what happens. Like I said I saw the how to when I didn't understand commands and never saw it again. Then I saw the grub manual describing the find, root, setup commands and I went with that.I think find, root, setup doesn't return the bios error because they are given from the grub shell.
Just for FYI, this is the error I get with grub-install from the live session.
Code:
ubuntu@ubuntu:~$ sudo grub-install /dev/hda
Probing devices to guess BIOS drives. This may take a long time.
Could not find device for /boot: Not found or not a block device.
This is the output of find, root, setup

Code:
grub> find /boot/grub/stage1
 (hd0,1)
 (hd0,10)

grub> root (hd0,10)
 Filesystem type is ext2fs, partition type 0x83

grub> setup (hd0)
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/e2fs_stage1_5" exists... yes
 Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... failed (this is not fatal)
 Running "embed /boot/grub/e2fs_stage1_5 (hd0,10)"... failed (this is not fatal
)
 Running "install /boot/grub/stage1 d (hd0) /boot/grub/stage2 p /boot/grub/menu
.lst "... succeeded
Done.
kuppalli is offline   Reply With Quote
Old August 5th, 2006   #8
kurup
5 Cups of Ubuntu
 
Join Date: Aug 2006
Beans: 20
Re: How to install Grub from a live Ubuntu cd.

I follwed your procedure to the T, infact the live cd (6.06 LTS) took everything in beautifully. It even gave me the output that the grub has been succesfully loaded.

I have a dual boot (windows 2000 and dapper). This is how my menu.1st looks like

# menu.lst - See: grub(, info grub, update-grub(
# grub-install(, grub-floppy(,
# grub-md5-crypt, /usr/share/doc/grub
# and /usr/share/doc/grub-doc/.

## default num
# Set the default entry to the entry number NUM. Numbering starts from 0, and
# the entry number 0 is the default if the command is not used.
#
# You can specify 'saved' instead of a number. In this case, the default entry
# is the entry saved with the command 'savedefault'.
# WARNING: If you are using dmraid do not change this entry to 'saved' or your
# array will desync and will not let you boot your system.
default 0

## timeout sec
# Set a timeout, in SEC seconds, before automatically booting the default entry
# (normally the first entry defined).
timeout 10

## hiddenmenu
# Hides the menu by default (press ESC to see the menu)
#hiddenmenu

# Pretty colours
#color cyan/blue white/blue

## password ['--md5'] passwd
# If used in the first section of a menu file, disable all interactive editing
# control (menu entry editor and command-line) and entries protected by the
# command 'lock'
# e.g. password topsecret
# password --md5 $1$gLhU0/$aW78kHK1QfV3P2b2znUoe/
# password topsecret

#
# examples
#
# title Windows 95/98/NT/2000
# root (hd0,0)
# makeactive
# chainloader +1
#
# title Linux
# root (hd0,1)
# kernel /vmlinuz root=/dev/hda2 ro
#

#
# Put static boot stanzas before and/or after AUTOMAGIC KERNEL LIST

### BEGIN AUTOMAGIC KERNELS LIST
## lines between the AUTOMAGIC KERNELS LIST markers will be modified
## by the debian update-grub script except for the default options below

## DO NOT UNCOMMENT THEM, Just edit them to your needs

## ## Start Default Options ##
## default kernel options
## default kernel options for automagic boot options
## If you want special options for specific kernels use kopt_x_y_z
## where x.y.z is kernel version. Minor versions can be omitted.
## e.g. kopt=root=/dev/hda1 ro
## kopt_2_6_8=root=/dev/hdc1 ro
## kopt_2_6_8_2_686=root=/dev/hdc2 ro
# kopt=root=/dev/hda2 ro

## default grub root device
## e.g. groot=(hd0,0)
# groot=(hd0,1)

## should update-grub create alternative automagic boot options
## e.g. alternative=true
## alternative=false
# alternative=true

## should update-grub lock alternative automagic boot options
## e.g. lockalternative=true
## lockalternative=false
# lockalternative=false

## additional options to use with the default boot option, but not with the
## alternatives
## e.g. defoptions=vga=791 resume=/dev/hda5
# defoptions=quiet splash

## altoption boot targets option
## multiple altoptions lines are allowed
## e.g. altoptions=(extra menu suffix) extra boot options
## altoptions=(recovery mode) single
# altoptions=(recovery mode) single

## controls how many kernels should be put into the menu.lst
## only counts the first occurence of a kernel, not the
## alternative kernel options
## e.g. howmany=all
## howmany=7
# howmany=all

## should update-grub create memtest86 boot option
## e.g. memtest86=true
## memtest86=false
# memtest86=true

## should update-grub adjust the value of the default booted system
## can be true or false
# updatedefaultentry=false

## ## End Default Options ##

title Ubuntu, kernel 2.6.15-26-386
root (hd0,1)
kernel /boot/vmlinuz-2.6.15-26-386 root=/dev/hda2 ro quiet splash
initrd /boot/initrd.img-2.6.15-26-386
savedefault
boot

title Ubuntu, kernel 2.6.15-26-386 (recovery mode)
root (hd0,1)
kernel /boot/vmlinuz-2.6.15-26-386 root=/dev/hda2 ro single
initrd /boot/initrd.img-2.6.15-26-386
boot

title Ubuntu, kernel 2.6.15-23-386
root (hd0,1)
kernel /boot/vmlinuz-2.6.15-23-386 root=/dev/hda2 ro quiet splash
initrd /boot/initrd.img-2.6.15-23-386
savedefault
boot

title Ubuntu, kernel 2.6.15-23-386 (recovery mode)
root (hd0,1)
kernel /boot/vmlinuz-2.6.15-23-386 root=/dev/hda2 ro single
initrd /boot/initrd.img-2.6.15-23-386
boot

title Ubuntu, memtest86+
root (hd0,1)
kernel /boot/memtest86+.bin
boot

### END DEBIAN AUTOMAGIC KERNELS LIST

# This is a divider, added to separate the menu items below from the Debian
# ones.
title Other operating systems:
root


# This entry automatically added by the Debian installer for a non-linux OS
# on /dev/hda1
title Windows 2000
root (hd0,0)
savedefault
#makeactive
chainloader +1


Could I have a corrupted grub? If so..how do I go about rectifying it?

Thanks in advance.
kurup is offline   Reply With Quote
Old August 5th, 2006   #9
catlett
Chocolate Ubuntu Mocha Blend
 
catlett's Avatar
 
Join Date: Mar 2006
Location: Boston, MA
Beans: 1,862
Ubuntu 6.10 Edgy
Re: How to install Grub from a live Ubuntu cd.

That appears to be a perfectly fine grub menu. Is it having an issue?
Right now it is booting with the 2.6.15-23-386 kernel to the second partition of your first hard drive, where it expects to find ubuntu. Are you getting into Ubuntu?
It is also booting to the first partition of your first hard drive where it expects to find windows. Are you getting into windows?

This file can be edited by any text editor as long as you have root priveleges. The file's path is /boot/grub/menu.lst.
If you needed to access this document from Ubuntu, the commmand would be
Code:
sudo gedit /boot/grub/menu.lst
But this appears to be a perfectly fine menu. It has windows on the first partition and ubuntu on the second partition.
catlett is offline   Reply With Quote
Old August 6th, 2006   #10
kurup
5 Cups of Ubuntu
 
Join Date: Aug 2006
Beans: 20
Re: How to install Grub from a live Ubuntu cd.

Quote:
Originally Posted by catlett View Post
That appears to be a perfectly fine grub menu. Is it having an issue?
That is correct. It gives me the grub menu on boot-up..but when I scroll down to the 'Windows 2000' line..it flips me back to the first line. Strange..have no clue why?!

Quote:
Originally Posted by catlett View Post
Right now it is booting with the 2.6.15-23-386 kernel to the second partition of your first hard drive, where it expects to find ubuntu. Are you getting into Ubuntu?
Ubuntu is booting up just fine..no problems there whatsoever

Quote:
Originally Posted by catlett View Post
It is also booting to the first partition of your first hard drive where it expects to find windows. Are you getting into windows?
No. That is really the problem!

Here's what my fdisk looks like
Quote:
Disk /dev/hda: 40.0 GB, 40060403712 bytes
255 heads, 63 sectors/track, 4870 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 1305 10482381 7 HPFS/NTFS
/dev/hda2 1306 4780 27912937+ 83 Linux
/dev/hda3 4781 4870 722925 5 Extended
/dev/hda5 4781 4870 722893+ 82 Linux swap / Solaris
Its supposed to be set to boot thru Windows.

Any suggestions for me?
kurup is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:14 AM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry