LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian
User Name
Password
Debian This forum is for the discussion of Debian Linux.

Notices


Reply
  Search this Thread
Old 06-09-2015, 01:45 PM   #1
xiongnu
Member
 
Registered: Sep 2004
Distribution: Devuan, Void
Posts: 264

Rep: Reputation: 23
Reinstall grub bootloader


hi,

I've got a Thinkpad laptop that is loaded with WinXP/Debian8. in the process of adding FreeBSD to the mix, the grub bootloader was replaced by FreeBSD's boot loader 'boot0cfg', and it doesn't detect Linux partition and I can no longer boot into Debian.


i tried to reinstall grub by using both 'netinstall' and full image of Debian8 installation.

with 'netinstall', I can get to 'rescue mode'(under 'Advanced option' on the main menu), but it returns error about not detecting a valid kernel image.

on full image (live cd version), it doesn't list rescue under 'Advanced Option' at main menu.

which image file contains 'rescue mode' so I can recover the debian bootloader?
 
Old 06-09-2015, 02:12 PM   #2
Head_on_a_Stick
Senior Member
 
Registered: Dec 2014
Location: London, England
Distribution: Debian stable (and OpenBSD-current)
Posts: 1,187

Rep: Reputation: 285Reputation: 285Reputation: 285
NOTE: The advice below presumes that Debian is not installed in EFI-mode.

Load up a live ISO and use these commands to chroot into your system from the live environment:
Code:
# mkdir /target
# mount /dev/sdXY /target
# for i in /sys /proc /dev; do mount --bind $i /target$i; done
# chroot /target /bin/bash
# source /etc/profile
# grub-install /dev/sdX
# update-grub
Replace "X" with the drive letter assigned to your Debian HDD and replace "Y" with the partition number of your root partition.

If you have a separate /boot partition, you will have to mount that as well before running the `chroot` command (and after you mount the root partition).
Code:
# mount /dev/sdXZ /target/boot
Where "Z" is the partition number of your /boot partition.

You will have to write a custom menu entry for FreeBSD -- add this to the end of the file at /etc/grub.d/40_custom
Code:
menuentry 'FreeBSD {
    set root='(hdX,A)'
    chainloader +1
}
Replace "A" with the partition containing your FreeBSD system and use `update-grub` to add the custom entry to your GRUB menu.
 
Old 06-12-2015, 01:07 PM   #3
xiongnu
Member
 
Registered: Sep 2004
Distribution: Devuan, Void
Posts: 264

Original Poster
Rep: Reputation: 23
Quote:
Originally Posted by Head_on_a_Stick View Post

If you have a separate /boot partition, you will have to mount that as well before running the `chroot` command (and after you mount the root partition).
Code:
# mount /dev/sdXZ /target/boot
Where "Z" is the partition number of your /boot partition.
here's the partition info by 'fdisk' and output of os-prober(before I installed 'boot0cfg' and can still boot into Debian). Master boot record is on windows partition. Does this mean /boot partition is 'sda1' and I need to mount it before 'chroot'?


Code:
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x9f114bc6

Device     Boot     Start       End  Sectors  Size Id Type
/dev/sda1              63  62929439 62929377   30G  7 HPFS/NTFS/exFAT
/dev/sda2  *    106027047 143775701 37748655   18G a5 FreeBSD
/dev/sda3       143806320 156295439 12489120    6G  c W95 FAT32 (LBA)
/dev/sda4        62930942 106027007 43096066 20.6G  f W95 Ext'd (LBA)
/dev/sda5        62930944  82460671 19529728  9.3G 83 Linux
/dev/sda6        82462720  85053439  2590720  1.2G 82 Linux swap / Solaris
/dev/sda7        85055488 106027007 20971520   10G 83 Linux

Partition table entries are not in disk order.
erdos@debian:~$ os-prober
unshare failed: Operation not permitted
rm: cannot remove ‘/var/lib/os-prober/labels’: Permission denied
erdos@debian:~$ sudo os-prober
/dev/sda1:Microsoft Windows XP Professional:Windows:chain
/dev/sda3:Windows NT/2000/XP:Windows1:chain
erdos@debian:~$ 
erdos@debian:~$
 
Old 06-12-2015, 01:49 PM   #4
Head_on_a_Stick
Senior Member
 
Registered: Dec 2014
Location: London, England
Distribution: Debian stable (and OpenBSD-current)
Posts: 1,187

Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Originally Posted by xiongnu View Post
Master boot record is on windows partition. Does this mean /boot partition is 'sda1' and I need to mount it before 'chroot'?
To clarify: the "Master Boot Record" is on the very first sectors of the disk and holds the partition table itself.
https://en.wikipedia.org/wiki/Master_boot_record

You can check if you have a separate /boot partition by running:
Code:
lsblk
This will show all the mountpoints in the right-most column with the block devices listed in the left-most column.

If you do not see "/boot" in the mountpoints column then you do not need to mount it.

If you do see "/boot" in the mountpoints column, then run:
Code:
# mount /dev/sdaX /target/boot
After the first `mount` command and before the `chroot` command.

Replace "X" with the partition number shown by `lsblk` (if it shows "/boot" as a mountpoint).

If this all seems a bit too much for you, you could also try the "SuperGRUB2" rescue disk:
http://www.supergrubdisk.org/

You will still need to add the FreeBSD custom entry yourself though as GRUB cannot read the BSD filesystems.

Last edited by Head_on_a_Stick; 06-12-2015 at 01:49 PM. Reason: typo
 
Old 06-12-2015, 04:15 PM   #5
xiongnu
Member
 
Registered: Sep 2004
Distribution: Devuan, Void
Posts: 264

Original Poster
Rep: Reputation: 23
success! first part works flawlessly, I got 'grub' back and now I'm in Debian typing this message.
Code:
# mkdir /target
# mount /dev/sda5 /target
# for i in /sys /proc /dev; do mount --bind $i /target$i; done
# chroot /target /bin/bash
# source /etc/profile
# grub-install /dev/sda
# update-grub

second part generates error, here's my /etc/grub.d/40_custom:
Code:
menuentry 'FreeBSD {
    set root='(hda,2)'
    chainloader +1
}
error while run 'update-grub'
Code:
erdos@debian:~$ sudo update-grub
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.16.0-4-amd64
Found initrd image: /boot/initrd.img-3.16.0-4-amd64
Found Microsoft Windows XP Professional on /dev/sda1
Found unknown Linux distribution on /dev/sda2
Found Windows NT/2000/XP on /dev/sda3
error: out of memory.
error: syntax error.
error: Incorrect command.
error: syntax error.
Syntax error at line 216
Syntax errors are detected in generated GRUB config file.
Ensure that there are no errors in /etc/default/grub
and /etc/grub.d/* files or please file a bug report with
/boot/grub/grub.cfg.new file attached.
done
erdos@debian:~$
 
Old 06-12-2015, 04:26 PM   #6
Head_on_a_Stick
Senior Member
 
Registered: Dec 2014
Location: London, England
Distribution: Debian stable (and OpenBSD-current)
Posts: 1,187

Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Originally Posted by xiongnu View Post
second part generates error, here's my /etc/grub.d/40_custom:
Code:
menuentry 'FreeBSD {
    set root='(hda,2)'
    chainloader +1
}
I did say to add that to the end of 40_custom -- you need these lines at the top of the file:
Code:
#!/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.
If it still doesn't work, you should probably mark this thread [SOLVED] and start a new thread about adding a FreeBSD entry to GRUB2.

I used to have a FreeBSD system booting from GRUB but it's gone now and I can't remember exactly what I used to create the entry.

EDIT: Also, it should be "set root='(hd0,2)'" rather than '(hda,2)'

Last edited by Head_on_a_Stick; 06-12-2015 at 04:28 PM.
 
Old 06-12-2015, 04:38 PM   #7
xiongnu
Member
 
Registered: Sep 2004
Distribution: Devuan, Void
Posts: 264

Original Poster
Rep: Reputation: 23
that's correct, i appended the menuentry to the end of /etc/grub.d/40_custom.

Code:
#!/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 'FreeBSD {
    set root='(hd0,2)'
    chainloader +1
}
the update-grub returned the same error when i use 'set root='(hd0,2)'

I've marked the thread solved, thanks a lot for the help!
 
Old 06-12-2015, 04:49 PM   #8
Head_on_a_Stick
Senior Member
 
Registered: Dec 2014
Location: London, England
Distribution: Debian stable (and OpenBSD-current)
Posts: 1,187

Rep: Reputation: 285Reputation: 285Reputation: 285
You're welcome
 
Old 06-12-2015, 04:53 PM   #9
xiongnu
Member
 
Registered: Sep 2004
Distribution: Devuan, Void
Posts: 264

Original Poster
Rep: Reputation: 23
Smile

after i put freebsd in between double quotation " ", 'update-grub' runs without error. upon reboot, freebsd appears in grub and I can boot into it as well.

Code:
menuentry "FreeBSD" {
    set root='(hd0,2)'
    chainloader +1
}
thanks a lot, Head_on_a_Stick, you saved my day!
 
Old 06-12-2015, 05:00 PM   #10
Head_on_a_Stick
Senior Member
 
Registered: Dec 2014
Location: London, England
Distribution: Debian stable (and OpenBSD-current)
Posts: 1,187

Rep: Reputation: 285Reputation: 285Reputation: 285
Excellent news -- glad you got it sorted!
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Grub bootloader overwritten by WinXP bootloader, all other os' gone! craigforsure Linux - Newbie 5 03-29-2010 02:49 AM
Replace GRUB Bootloader with WIN NT Bootloader wombat53 Linux - Software 1 09-04-2009 08:56 AM
How can I disable the grub bootloader and enable the lilo bootloader? abefroman Linux - General 1 08-06-2005 08:53 AM
Need to reinstall WinXP, How do I keep my GRUB bootloader? jesusphreak Linux - Software 2 04-17-2005 09:38 PM
reinstall Windows bootloader extrasolar Slackware 7 02-06-2004 03:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration