Saturday, March 20, 2010

[zz]How to Dual Boot Windows 7 and Linux using BCDEdit

http://www.iceflatline.com/2009/09/how-to-dual-boot-windows-7-and-linux-using-bcdedit/

Windows How to Dual Boot Windows 7 and Linux using BCDEdit

14 Comments

This post will describe how to set up a system that can boot into Windows 7 or a Linux distribution. The Window 7 Boot Configuration Data Editor (BCDEdit) will be used to configure Windows 7 to display a menu at boot time that will allow the user to choose between Windows 7 and a Linux distribution of your choice.

To help explain the steps involved, we’ll use an 80 GB hard drive with Windows 7 already installed. We’ll then re-partition the drive using Gnome Partition Editor (GParted) in order to add Ubuntu or Fedora. We’ll then use BCDedit to add a boot menu option for our Linux distribution. All steps involved assume you have a functioning CD drive (or USB drive if you’d prefer) that the system can boot from. The versions for the software used in this post were as follows:

Fedora v11
GParted v0.4.6-1
Ubuntu v9.04
Windows 7 RC Build 7100

Oh… and while I’ve never encountered a situation where GParted destroyed existing disk data, make sure you backup any critical files before you proceed.

So, let’s get started.

Partitioning for Ubuntu

The first thing we’ll want to do is re-partition our 80GB drive. Download a copy of GParted Live and burn it to a CD (or place it on a bootable USB drive). Next, boot the system using the GParted disk. After accepting the default settings for keymap, language, and X-window configuration, you should arrive at the GParted desktop (See Figure 1).

Screenshot of the GParted Desktop

Figure 1

You’ll notice that GParted is reporting ~74 GB of total disk space (1 Gigabyte = 1,073,741,824 Bytes) and that Windows 7 currently occupies all of that space using two primary partitions: one small boot partition at device /dev/hdd1 and the remaining operating system at device /dev/hdd2. Eventually, we will have five partitions in total: the two Windows 7 partitions just mentioned, as well as ones for the Ubuntu operating system and Linux swap. We’ll also create a FAT32 partition for sharing data between Windows 7 and Ubuntu. You’ll need to determine how much disk space you want to allocate to each of these partitions based on your disk size, but for our 80 GB example disk, we’ll use the following partition layout:

Windows 7: ~100MB (Windows 7 bootloader)
Windows 7: ~40 GB
Ubuntu: ~24 GB
Linux-Swap: ~1 GB
FAT32: ~9 GB

Let’s leave the Windows 7 boot partition at /dev/hdd1 alone and reduce the partition at /dev/hdd2 to ~40GB. Highlight /dev/hdd2 by left-clicking on it and then select “Resize/Move.” This will bring up a new window where the new size for the partition can be entered manually, or you can left-click and hold the graphical bar to resize the partition. Make sure the free space is following /dev/hdd2. Now hit “Resize/Move” to complete the resizing (See Figure 2).

Screenshot of a Partition Resized using GParted

Figure 2

Once the size of /dev/hdd2 is reduced, you’ll see that the remaining space is now labeled unallocated. Left-click this space to highlight it and then select “New” to create a new partition. Make this partition a primary partition and the file system ext3. Now, reduce the partition to ~29 GB – you can do this by typing in the value in the New Size field or by moving the slider to the left until you reach the desired size. When complete, select “Add” (See Figure 3).

Screenshot of a New Partition Created Using GParted

Figure 3

The disk is limited to four primary partitions, so in the remaining unallocated space we’ll need to create a new extended partition instead of a primary partition. Once the new extended partition is created, left-click to highlight it and create two new logical partitions following the steps above. One should be linux-swap and sized to 1 GB; the other (in the remaining space) will be our FAT32 partition. When complete, you should have a partition layout that resembles Figure 4.

Screenshot of New Ubuntu Partition Layout Created Using GParted

Figure 4

Review the newly created partition layout and make any necessary adjustments. You can back out of any changes you’ve made by selecting “Undo Last Operation” or “Clear All Operations” (under the Edit menu). Now select “Apply All Operations” and GParted will proceed with writing the changes to the disk. After a few minutes you should see the re-partitioned drive (See Figure 5).

Screenshot of New Ubuntu Partitions with Device Names Assigned by GParted

Figure 5

You’ll notice that GParted retained the device designations /dev/hdd1 and /dev/hdd2 for Windows 7 but has now assigned the appropriate device designations to each of our newly minted partitions:

/dev/hdd1 – Windows 7 boot (Primary partition)
/dev/hdd2 – Windows 7 (Primary partition)
/dev/hdd3 – ext3 (Primary partition)
/dev/hdd4 – (Extended partition)
/dev/hdd5 – Linux-Swap (Logical partition)
/dev/hdd6 – FAT32 (Logical partition)

Make note of these partition designations because you will need to refer to them when we install Ubuntu. Now, let’s exit out of GParted and reboot the system. If all went well, you will arrive at the Windows 7 logon screen. Along the way, you may see Windows perform a disk check – don’t worry that’s normal. If you log into Windows 7 and access Explorer or Disk Manager you should see your FAT32 partition.

Partitioning for Fedora

The process for creating partitions for an Fedora 11 install is essentially the same as described for Ubuntu. What does differ, however, is the number and type of the partitions we’ll need. The Fedora 11 operating system utilizes the ext4 file system. Unfortunately, Fedora is incapable of placing its bootloader on the ext4 partition. Therefore, we’ll need to create a small primary partition that Fedora 11 can use to install the bootloader. For our 80 GB example disk, we’ll use the following partition layout. Note the addition of a small 100 MB partition that we will eventually format as ext3 and use for the Fedora bootloader:

Windows 7: ~100MB (Windows 7 bootloader)
Windows 7: ~40 GB
Fedora: ~100MB (Fedora bootloader)
Fedora: ~24 GB
Linux-Swap: ~1 GB
FAT32: ~9 GB

Now, using GParted and following similar steps described above for Ubuntu, create the following partitions (See Figure 6):

/dev/hdd1 – Windows 7 boot (Primary partition)
/dev/hdd2 – Windows 7 (Primary partition)
/dev/hdd3 – ext3 (Primary partition)
/dev/hdd4 – (Extended partition)
/dev.hdd5 – ext4 (Logical partition)
/dev/hdd6 – Linux-Swap (Logical partition)
/dev/hdd7 – FAT32 (Logical partition)

Screenshot of a Fedora Partition Layout Using GParted

Figure 6

Installing Ubuntu

Now it’s time to install Ubuntu on to the partitions we created with Gparted. Download a copy of Ubuntu Desktop and burn it to a CD (or place it on a bootable USB drive). Boot the system using the Ubuntu disk. Double-click the install icon on the desktop to get started. Continue through the installation process until you arrive at “Prepare disk space” and select “Specify partitions manually (advanced)” to advance to the “Prepare partitions” screen (See Figure 7). This is where we’ll instruct Ubuntu which mount points and file systems to use on the partitions we created. You’ll notice that the Ubuntu installer references the partitions using the /dev/sda1, 2, 3…, etc. device labels typically used for SCSI, SATA, and USB drives. These correspond directly to the /dev/hdd1, 2, 3…, etc. device labels used by Gparted (e.g., device /dev/hdd3 = device /dev/sda3).

Screenshot of the Ubuntu Partition Preparation Screen

Figure 7

Left-click to highlight /dev/sda3 and then select “Edit partition.” On the pop-up screen make sure that the ext3 file system is selected and the check box instructing Ubuntu to format the partition is checked. Device /dev/sda3 will serve as the root partition for Ubuntu so we’ll set the mount point to / from the list of choices in the drop down menu. Now select “Okay” to accept the changes (See Figure 8).

Screenshot of the Ubuntu Edit Partition Screen

Figure 8

Following similar steps, let’s move on to /dev/sda5 and set the mount point to linux-swap. There is no need to format this partition. Finally, device /dev/sda6 will serve as our FAT32 partition to share files between our Windows 7 and Linux systems so make sure the FAT32 file system is selected and set the mount point to /os-share (you can choose and alternative name if desired). There is also no need to format this partition. Do a final review and then select “Forward” to continue to the “Ready to install” screen (See Figure 9).

Screenshot of the Ubuntu Confirm Partition Screen

Figure 9

This next step is critical. We need to tell Ubuntu where to install its bootloader (GRUB). We DO NOT want to install GRUB on /dev/sda, as that would overwrite our disk’s Master Boot Record, nor do we want to install it on /dev/sda1, as that is being used for the Windows 7 boot configuration data. Instead, let’s have Ubuntu install GRUB on the partition that will contain the Ubuntu operating system – in our case /dev/sda3. To do this, left-click on the tab that says “Advanced.” This will bring up a pop-up screen where we can specify the location for the bootloader (See Figure 10).

Screenshot of the Ubuntu Bootloader Install Screen

Figure 10

First, make sure that the check box for installing the bootloader is checked. Next, we need to place GRUB on the same partition Ubuntu will be installed on. GRUB starts counting disk partitions from the beginning of the disk starting with 0 so let’s change (hd0) to (hd0,2). That will tell Ubuntu to install GRUB on the third partition of our disk (/dev/sda3). Now select “Okay” and continue with the Ubuntu installation. When the install completes you’ll be asked to reboot the system. If all went well, you will arrive once again arrive at the Windows 7 logon screen.

Installing Fedora

If you’re installing Fedora you’ll follow similar steps described in the Ubuntu install above.Download a copy of Fedora Desktop and burn it to a CD (or place it on a bootable USB drive). Boot the system using the Fedora disk and double-click on the install icon on the desktop to get started. Continue through the installation process until you arrive at Fedora’s disk partitioner, then select “Create custom layout” from the drop down menu list and select “Next.” (See Figure 11).

Screenshot of the Fedora Partition Selection Screen

Figure 11

Scroll down the list of devices until you find /dev/sda3 (that’s the small primary partition we created using Gparted), left-click to highlight it and select “Edit.” In the pop-up screen chose /boot as the mount point and format the partition as ext3 (See Figure 12). When finished, select “Okay.” Next, highlight and edit /dev/sda5. This partition will contain the Fedora operating system so make sure to choose / for the mount point and format as ext4. Repeat these steps for the /dev/sda6 and /dev/sda7 – these will be used for our swap and FAT32 partitions respectively. Make sure to choose vfat (FAT32) for the /dev/sda7 partition and set the mount point to /os-share (you can choose and alternative name if desired).

Screenshot of the Fedora Edit Partition Screen

Figure 12

When complete, select “Next” and do a final review of these partition changes in the pop-up screen, then select “Format.” Confirm the choices by selecting “Write changes to disk.” At this point you will be presented with some choices for where Fedora should install its bootloader (GRUB). This next step is critical. We DO NOT want to install the GRUB to /dev/sda as that will overwrite our disk’s Master Boot Record. Instead, let’s have Fedora install GRUB on the same partition as the Fedora boot partition – in our case /dev/sda3. To do this, left-click on the tab that says “Change device.” This will bring up a pop-up screen where we can specify location for the boot partition (See Figure 13).

Screenshot of the Fedora Bootloader Installation Screen

Figure 13

When finished, select “Next” and Fedora will write the changes to disk and complete the install. After you reboot, if all went well, you will arrive at the Windows 7 logon screen.

Configuring for Dual Boot

Now that we have our disk partitioned and Fedora or Ubuntu installed, let’s set up our system to boot Windows 7 or the Linux distribution. This will involve copying the first 512 bytes of our Linux boot partition to Windows 7 and then use that file, and BCDEdit, to configure Windows 7 to display a menu at boot time that will give you a choice between Windows 7 and Linux.

Boot the system again using the Gparted disk and let’s make a mount point for the FAT32 share partition we created. Open up a terminal in Gparted and enter the following:

mkdir /mnt/os-share

Next, let’s mount the correct device to this directory. Recall from the Gparted partitioning steps above that the FAT32 partition is located at device /dev/hdd6 in the case of Ubuntu and device /dev/hdd7 in the case of Fedora. Assuming Ubuntu then for this example, enter the following:

mount /dev/hdd6 /mnt/os-share

Finally, let’s write the first 512 bytes from our Linux boot partition to a file and copy that file to our FAT32 partition. Note that the boot partition in the case of both Ubuntu and Fedora was the same – /dev/sda3:

dd if=/dev/hdd3 of=/mnt/os-share/linux.bin bs=512 count=1

Exit Gparted now and reboot to Windows 7. Open the FAT32 partition and you should see the linux.bin file. Copy that file to the root of the Windows boot (active) partition (e.g., C:\). Next, let’s use BCDEdit to modify Windows 7’s boot configuration data to add a boot menu option for our Linux distribution. Administrative privileges are required to use BCDEdit, so navigate to Start->All Programs->Accessories, Right-click on Command Prompt and select “Run as administrator.” Okay, now let’s start by creating an entry for our Linux distribution. Note here that you are free to choose another entry name if desired:

bcdedit /create /d “Linux” /application BOOTSECTOR

BCDEdit will return an alphanumeric identifier for this entry that I will refer to as {ID} in the remaining steps. You’ll need to replace {ID} by the actual returned identifier. An example of {ID} is {d7294d4e-9837-11de-99ac-f3f3a79e3e93}. Next, let’s specify which partition hosts a copy of the linux.bin file:

bcdedit /set {ID} device partition=c:

The path to our linux.bin file:

bcdedit /set {ID} path \linux.bin

An entry to the displayed menu at boot time:

bcdedit /displayorder {ID} /addlast

and finally, let’s specify how long the menu choices will be displayed:

bcdedit /timeout 30

That’s it! Now reboot and you will be presented with menu where you can choose to boot to Windows 7 or Linux. When you choose Linux, you’ll be taken to the GRUB menu where you can choose to continue booting your Linux distribution or return to the previous menu.

Windows XP?

If you plan to dual boot using Windows XP then you will still partition and install Ubuntu or Fedora as described above (Note, however, that XP does not use a small primary partition for boot configuration data like Windows 7 does), but you won’t be needing BCDEdit. Instead, you can simply open the boot.ini file located at C:\ and add the following entry:

c:\linux.bin="Linux"

Conclusion

With a minimal amount time, the free and open source disk partitioning tool Gparted, and a little command line foo, you can easily set up a system that can dual boot Windows 7 and your choice of Linux distributions.

References

http://technet.microsoft.com/en-us/library/cc709667%28WS.10%29.aspx

0 comments:

Post a Comment