How to switch a Windows MBR to Grub2


Results 1 to 2 of 2

Thread: How to switch a Windows MBR to Grub2

  1. #1
    Join Date
    Jun 2004
    Location
    Newcastle upon Tyne
    Posts
    2,951

    How to switch a Windows MBR to Grub2

    Keyword in a nutshell : Grub2 can be placed inside any MS Windows to enhance the booting power.

    What is the switch all about?

    There is no necessity to change. The alteration is merely adding a more powerful and easier boot loader Grub2 as an additional tool.

    The original boot loader is untouched and is still the one that boots the MS Windows.

    What version of MS Windows does the switching applied?

    Done it with Win2k, Xp, Vista and Win7 before writing this Howto.

    Why is it possible?

    Grub2 has been engineered to read NTFS partitions. Therefore Grub2 can be installed inside a MS Windows partition that has a NTFS filing system. Linux may or may not be present elsewhere in the computer. In any case the switching is for Grub2 to work as a stand alone boot loader. Linux is only required to put it there.

    What actually are the changes in my Windows system?

    Only two items are involved.

    (1) The MBR, which is exactly 512 bytes long and is the first sector of the first boot disk, is replaced by Grub2's own version. Every Windows system can be booted without its MBR.

    (2) A new directory \boot is added to the "C" drive. Inside it \boot\grub has Grub2's system files. Win2k and Xp do not use a \boot partition at all. Vista and Win7 do have a \Boot partition which can be used to house a subdirectory for grub. Please note the \boot in Windows appears as /boot in Linux. The slash changes angle.

    How much hard disk space do I lose to get Grub2 in my Windows?

    The \boot\grub directory can have about 180 items with a total storage of 1Mb or 1000kb. All versions of Grub can be put inside a floppy and Grub2 is no different.

    Why Grub2? what's wrong with Grub1? what are their differences

    Grub1 cannot read a NTFS partition so It cannot be installed inside a MS Windows. Grub1 can be added if a separate partition is made available for its residence.

    As Grub2 can read NTFS partitions it is more useful. For example the \Boot is a hidden directory invisible to the Windows users but with Grub2 every hidden file and folder is visible.

    Grub1 is up to version 0.97 and Grub2 started from version 1.9x. Grub1 is fully mature, totally capable but difficult to maintain and expand. It maintainers decided it was better to re-write everything and called it Grub2. Grub1 has not been maintained for a few years already.

    What do I need to get Grub2 into my Windows?

    Just a Linux Live CD that has Grub2 inside. Currently Ubuntu and LinuxMint are shipped with Grub2 as their default boot loader. Grub2 can be downloaded directly from GNU/Grub but to use it one has to build it into a Linux so the easy way is just boot up a Linux Live CD that has Grub2.

    Is it safe and can I reverse the process?

    It is perfectly safe as the MS system MBR (remember it is only 512 bytes long) can be restored using the Windows installation CD or DVD thereby overwriting the Grub2. The command with a Win2k/Xp in Recovery Console is "fixmbr" whereas for a Vista/Win7 the command prompt off an installation DVD is "bootrec /fixmbr".

    To remove Grub2 folder is no different from removing any folder.

    What happen if I don't have the original installation CD or DVD?

    It is always the installation CD or DVD that provide the utility to alter the MBR. Any Windows installation CD or DVD of the similar Windows version will do as only one utility program is run.

    OK so what are the steps?

    Step 1 - Boot up a Linux Live CD that has Grub2. I assume Ubuntu above V9.04 is used.

    Step 2 - Select a terminal as the work is safer to be executed in a terminal. Become a Super user (equivalent to Admin in a MS Windows) with these terminal command
    Code:
    sudo su
    Step 3 - Do a sanity check with the hard disk partitions by command
    Code:
    fdisk -l
    It is vital to identify the partition that boots a MS Windows in Linux.

    Generally two conditions would have to be satisfied;

    (i) The partition must have been marked "active". This is shown normally with a "*" in Linux terminal

    (ii) The boot partition of any MS Windows must be a primary partition. In Linux terms only the first 4 partitions, from sda1 to sda4, are reserved primaries.

    Here is an output from command "fdisk -l" of my own PC as an example
    Code:
    [root@localhost ~]# fdisk -l
    
    Disk /dev/sda: 1500.3 GB, 1500300828160 bytes
    255 heads, 63 sectors/track, 182401 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0xdb426a9c
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1       30394   244139773+   7  HPFS/NTFS
    /dev/sda2           30395       42553    97667167+  83  Linux
    /dev/sda3           42554       48633    48837600   83  Linux
    /dev/sda4           48634      182401  1074491460    5  Extended
    /dev/sda5           48634       48756      987966   82  Linux swap / Solaris
    /dev/sda6           48757       73072   195318238+  83  Linux
    /dev/sda7           73073       85231    97667136    7  HPFS/NTFS
    /dev/sda8           85232       97390    97667136    7  HPFS/NTFS
    /dev/sda9           97391      109549    97667136    7  HPFS/NTFS
    /dev/sda10         109550      170338   488287611   83  Linux
    /dev/sda11         170339      172771    19543041   83  Linux
    /dev/sda12         172772      175204    19543041   83  Linux
    /dev/sda13         175205      177637    19543041   83  Linux
    /dev/sda14         177638      180070    19543041   83  Linux
    /dev/sda15         180071      182401    18723726   83  Linux
    The two colored code condition are highlighted confirming my sda1 is the partition that boots a MS Windows.

    Step 4 - Create a mounting point in /mnt of the booted up Linux, mount the Windows partition on it. The commands are
    Code:
    mkdir /mnt/sda1
    mount /dev/sda1 /mnt/sda1
    ls /mnt/sda1
    The last command is optional for viewing the content of the Windows partition. As a sanity check Win2k or Xp always has boot.ini, NTLDR and ntdetect.com inside whereas for a Vista or Win7 there should always be a file called bootmgr and the /Boot directory present.

    Step 5 - Install Grub2. This is just by a simply Linux command
    Code:
    grub-install --root-directory=/mnt/sda1 /dev/sda
    Only this command implement the change. The consequence is the MBR, denoted by /dev/sda, will be allowed Grub2 to occupy. In so doing Grub2 will create a /boot/grub and populate it with the system files. At this stage Grub2 only boots to a Grub prompt and the user has to boot each operating system manually. To arrange the Windows automatically booted with a selectable menu a configuration file /boot/grub/grub.cfg must be created as per Step 6.

    Step 6 - Use an editor to create /boot/grub/grub.cfg in the mounted Windows paritition /mnt/sda1 by command
    Code:
    nano /mnt/sda1/boot/grub/grub.cfg
    with the following entries
    Code:
    menuentry 'My Windows in sda1 now booted by Grub2' {
    set root=(hd0,1)
    chainloader +1
    }
    The above entries can boot Windows in any partition by declaring the relevant partition in the statement "set root=". Grub2 counts disk from 0 and partitions from1 (same as a MS Windows) therefore (hd0,1) is the first partition of the first hard disk. Anything shown in menuentry only get displayed and not acted on.

    The above steps work perfectly for Win2k and Xp.

    For Vista and Win7 there is a small complication that /Boot and not /boot could be used in some versions of these two MS Windows. The /boot/grub therefore must be duplicated in /Boot, if it exists, by this command in the Linux terminal

    Step 7 - To duplicate /boot/grub into /Boot if it exists
    Code:
    cp -r /mnt/sda1/boot/grub /mnt/sda1/Boot
    OK now I have Grub2, what does it do for me?The commands in a Grub prompt below have been colored in blue.

    This is difficult to answer as it depends how much you use it. The power of Grub2 is that you can talk to it directly and instruct it to do things. You do this in a Grub prompt which is available if instead of booting a system you press the "c" key. You will then see a blank screen with "Sh: Grub" and a blinking cursor. This is a Grub prompt that you can use to boot manually every installed operating system in a PC.

    You can go back to the hard disk booting menu by this Grub2 command
    Code:
    set root=(hd0)
    chainloader +1
    boot
    If you want to see the partitions in your PC the Grub2 command is
    Code:
    ls
    You can see all the hidden files in your Windows partition. For example if your Xp Windows is in (hd0,1) you can list its directories by
    Code:
    ls (hd0,1)/
    and view the contents of the hidden file boot.ini by
    Code:
    cat (hd0,1)/boot.ini
    If you want to boot an operating system say in a USB flash drive (hd1,1) and it has a Grub2 you can do so by command
    Code:
    set root=(hd1,1)
    configfile /boot/grub/grub.cfg
    What Grub2 can do for you is how much use the commands available to you. These commands can be viewed by
    Code:
    help
    Last edited by saikee; 07-28-2010 at 10:59 AM.
    Linux user started Jun 2004 - No. 361921
    Using a Linux live CD to clone XP
    To install Linux and keep Windows MBR untouched
    Adding extra Linux & Doing it in a lazy way
    A Grub menu booting 100+ systems & A "Howto" to install and boot 145 systems
    Just cloning tips Just booting tips A collection of booting tips

    Judge asked Linux "You are being charged murdering Windoze by stabbing its heart with a weapon, what was it?" Replied Linux "A Live CD"

  2. #2
    Join Date
    Jun 2004
    Location
    Newcastle upon Tyne
    Posts
    2,951

    Some technical details

    A little explanation why Grub2 can be used in inside a MS Windows formatted to the NTFS filing system.

    Grub1 and Grub2 are very simple boot loaders that have only two essential system files. In Grub1 they are the stage1 and stage2. The equivalent in Grub2 are boot.img and core.img. The two stages of each Grub can be put together in a floppy and used to boot any installed operating system in a PC. The first part stage1 of Grub1 and boot.img of Grub2 is exactly 512 bytes long for controlling the MBR. There is not much written on how the first stage is connected with second stage.

    Only recently I discovered Grub1's stage2 is hard-coded into stage1 when Grub1 is set up. For certain hard disk geometries the stage2 may be too big to occupy the boot sector, which is just the first sector but extending to the first of the 255 heads (LBA mode), of a partition reserved for the boot loader. A reserved boot sector is used by most filing systems. Thus the second stage of Grub it is always inside /boot/grub directory.

    Since it is a hard disk address then it doesn't matter if the /boot/grub directory has disappeared. In fact I proved the whole partition, where Grub1 resides, can be deleted and Grub1 will still boot, as documented here.

    This is possible because the deletion of a partition only affects the entry in the 64-byte partition table in the MBR and the partition interior is not touched. It is just once the partition has been deleted there is no means to find the files again unless the partition table is rebuilt to the former setting. This is not a problem for Grub1 if its stage1 already knows the exact spot in the hard disk to load the stage2. This of course will not work if this hard disk area is overwritten by the formation of a new partition. In any case the stage2 only provides a Grub prompt but the proof is that Grub1 does not reside in the boot sector if its stage1 is in the MBR.

    Grub2 is a bit different as its boot.img (stage 1) actually relies on the filing system to find the core.img (stage 2). I tried the same trick as Grub1 by removing Grub2's partition but ending up with Grub-rescue prompt which is the indication of the core.img not available.

    Thus if the first stage of Grub2 is in the MBR and Grub2 can find its second stage from the /boot/grub directory then the boot sector will be left uninhabited. Therefore we can afford to have a MS Windows in the same partition.

    A MS Windows booting process differs from Grub by having three stages.

    Possibly trying to be backward compatible a MS system from Dos to Win7 always uses a MBR which, instead of booting a specified partition it only searches the 4 primary partitions boot whichever one that has the booting flag switched on.

    MS system's second stage is to implant a booting code inside the boot sector of the "active" (in LInux it is called "bootable") partition. Thus the MBR will load the boot sector code automatically into the memory. There is no name for the boot sector code but for Win2k and Xp it is known as /nt52 whereas for Vista and Win7 it is called /nt60 selectable by an utility program bootsect.exe.

    The third stage of a MS Windows booting process is the loaded boot sector code will find its own boot loader. For Win2k/Xp it will be NTLDR whereas for Vista/Win7 the corresponding boot loader to be loaded is bootmgr. It is the NTDLR or bootmgr that provides the booting menu and ultimately to loads the Windows operating system.

    The 3-stage booting process mandates the boot sector code must be implanted with a boot sector of every MS Windows. This is why a Linux installer can be so successful in dual booting a MS Windows simply by including a partition to boot if its boot sector has been inhabited.

    When Grub boots a MS system it basically substitutes only Windows first stage.

    The traditional use of Grub2 is having it outside the MS Windows partition in its own partition or as part of a Linux distro. However there is no technical reason why we can't place Grub2 inside the MS Windows.
    Linux user started Jun 2004 - No. 361921
    Using a Linux live CD to clone XP
    To install Linux and keep Windows MBR untouched
    Adding extra Linux & Doing it in a lazy way
    A Grub menu booting 100+ systems & A "Howto" to install and boot 145 systems
    Just cloning tips Just booting tips A collection of booting tips

    Judge asked Linux "You are being charged murdering Windoze by stabbing its heart with a weapon, what was it?" Replied Linux "A Live CD"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •