### Dual booting Windows and Ubuntu [MBR]

Do you use both Linux and Windows? See how to dual boot Windows and Linux Ubuntu, what you do to modify or reinstall each operating system without affecting the other and how to remove one of them.

Linux distributions are becoming more and more popular. But before completely giving up Windows, many users want to have both OSes on their PCs. That's because they can't find an alternative for their everyday apps on Linux or can't play some games. Several manufacturers also started to sell preinstalled Linux computers. Before wiping the whole HDD to install Windows, consider dual booting.

This post will give solutions to some common situations. Because Ubuntu is one of the most popular Linux distributions, it will be used during this tutorial.

Here are some common situations that we will talk about:

1. My PC came preinstalled with Windows. I want to try Ubuntu (Linux).
2. My PC came preinstalled with Ubuntu. I bought Windows and want to install it.
3. I have successfully set up dual boot. But now I have to reinstall one of the OSes. What will happen to the other?
4. I want to get rid of one of the OSes. How to keep the other bootable?
Before discussing each of these situations, it should be mentioned that, unless you want to wipe your entire HDD (and of course the existing OS install), you should know whether it is partitioned with the classical MS-DOS scheme or with the modern GPT.

## MS-DOS or GPT

Of course I'm not talking about the operating system. It's just the same partition table information it used, also known as MBR (master boot record). If the OS has been installed in EFI mode, the partition table is GPT. Otherwise, it is very probable to be MBR, no matter if the PC supports UEFI.

What is the problem here? Well, if you have a GPT partitioned disk (by an installation started in EFI mode) and try to install Windows by booting it in compatibility mode (MBR), it will recognize the drive partitions but will ask you to format the entire HDD.

Modern OSes (Windows newer than 7 and 64 bits; Ubuntu newer than 12.04.2 and 64 bits) can be booted and installed in both EFI and BIOS modes. You just have to know how the primary OS was installed and boot from the installation media of the second one in the right mode.
 Windows booted from MBR cannot be installed on GPT disk.
Here is how you can check the HDD in Windows. Launch diskpart from cmd and type list disk. If you see an asterisk in the Gpt column corresponding to the HDD, then is GPT. Otherwise it is MBR.
 Check GPT/MBR on Windows using diskpart. This disk is MBR partitioned.
On Ubuntu you can use diskpart equivalent, parted. Run it from a terminal as superuser: sudo parted. Then type print all. It will tell you the partition table type.
 parted showing disk information and partition table.

## 1. Install Ubuntu on a Windows PC

You have a Windows PC and want to install Ubuntu. First of all you must decide where you install it and make some room for it. To do that open Disk Management by right clicking Computer and choosing Manage. On the left column choose Disk Management. Let's have a look at a common (yet not so good) configuration.
 Windows OS installed on a single partition
Why is this a bad configuration? There are two reasons. One is that whenever you'll want to do a clean install of the OS, you'll have to backup all your personal files and folders on a different drive, otherwise you'll lose everything. And there is the dual boot issue. You'll need to access your files from both the operating systems. This is no problem with Ubuntu, because it reads NTFS partitions. But it doesn't take into account specific file attributes of Windows, so by frequently accessing Windows partition (C:) from Ubuntu you may accidentally delete a system file and break the installation. That is why it is recommended to have an extra partition, which will hold your personal data and can be kept when changing OS. This partition can be accessed from both OSes and does not contain sensitive boot data. Even without a dual boot computer it is recommended to keep your data on a separate partition, unless you do backups on external media. If you like Windows' user folder structure, don't worry. Personal folders (Documents, Pictures, Videos, Downloads) can be moved to another partition as simply as right clicking them and choosing Location from Properties.

Let's get back to the point. In order to install Ubuntu, we need to make free space. Yes, I know you have free space on the partition, but it needs to be outside of partition. So, right-click the right-most partition! It is highly recommended to choose the last partition in order not to lose data. Select Shrink Volume....
 Shrinking a partition in Disk Management
After determining the maximum available shrink space, you will be prompted with a dialog where you can choose the shrink size. Ignore the disk and partition values from screenshots (they are made on a virtual machine).

Now, if you already have a secondary partition for your data, you only need space for Ubuntu. 15 - 30 GB is enough, considering that we will not make a separate /home partition on Ubuntu. Shrink with that and you'll get an unallocated space (marked in black).

If you only have the C: partition you'll need to make another partition and leave some free space. I suggest you leave for Windows (C:) about 40 - 80 GB. Substract this value from your HDD size and shrink the partition with the result. You have a big unused space. Of course this can be used to install Ubuntu, but we need also a partition. So, let's make a new partition (volume). Right click the unallocated space and select New Simple Volume.
 Making a new partition in Disk Management
Click Next once and you will be prompted to choose the new partition space. By default Windows selected all available space. But you want to keep an unallocated space, so you'll write there the difference between unallocated space and the space you want for Ubuntu (all values are in MB; don't forget that 1 GB = 1024 MB). Have a look at my example. I made the partition 20000 MB (19.5 GB) in order to keep 18819 MB (18.3 GB) for Ubuntu.
 Choosing the new partition size in Disk Management
Click Next a few times leaving options as default. Windows will format and assign a drive letter to your new partition. At the end, you should have a mandatory unallocated space.
 The unallocated space will be used to install Ubuntu
If you got the point, in order to install Ubuntu or other Linux, you need an unallocated space. You can make this by shrinking or completely deleting an existing partition. You can't have more than 4 primary partitions on a MS-DOS partition table! Looking at the screenshot above, we reached maximum: 1. System Reserved, 2. C:, 3. My Data (E:), 4. Will be the partition that holds the extended partition table. If your disk is GPT, you don't have to worry. The maximum is 128 partitions.

We got the black, unallocated space, let's reboot and install Ubuntu. When you get to the Installation Type screen, it's best to select Something Else.
 Ubuntu Installation Type selection
On the next screen you will be prompted with the current disk partition scheme and in that list you should see all the partitions including the unallocated free space. If you only see one line /dev/sda then there is a partition table mismatch. Either Windows partitioned HDD as GPT and you booted Ubuntu in legacy BIOS mode, so it asks for a MBR disk or the other way round. Reboot and configure your BIOS (UEFI). Don't forget that you need the 64 bit version if you boot from UEFI.

Let's start making some partitions. Select the free space and press the plus button.
 Selecting the free space and making new partitions
When prompted with the new partition dialog, there are a few things to consider. Leave everything as default except Size and Mount point.
 Make the root filesystem partition
Every Linux installation needs a mount point for the root filesystem. That's what we are going to do. Now you notice that there are some other mount points available. These can be assigned to different partitions. For example, you can have the root system on a partition and /home on a different one, so you can reinstall Linux without touching your personal user folder with Documents, Pictures etc. But in this dual boot configuration is recommended to put your personal files on a shared (between OSes) Windows partition and avoid standard Documents folder (for example). Because we will not create an extra partition (everything from OS, boot data, temporary files, installed apps and use files will be on the same partition) we can assign the root filesystem (/) the biggest available size. But if you have a PC with lower RAM (less than 4 - 6 GB) it is recommended to make a swap partition (the equivalent of Windows' pagefile.sys), so when your RAM gets full, the system will start moving temporary data to this partition. If the case, leave 1 - 3 GB for this partition.
 Making the swap partition in the remaining free space
Now you're ready to install Linux Ubuntu. Install boot loader to the hard drive (/dev/sda), selected by default. Select the main Linux partition. Click Install Now. That's it. After you restart your PC you will see the GRUB boot screen with Ubuntu as the default operating system and Windows below on that list.
 The disk configuration for installation of Ubuntu
And at last: don't forget to disable fast start up in Windows 8 and newer. It may make Windows partitions inaccessible in Linux and some other hardware may not function properly in Linux.

## 2. Install Windows on Ubuntu PC

In Ubuntu and any other Linux, we'll use the equivalent of Windows Disk Management, called GParted. If it isn't installed you can install it with sudo apt-get install gparted (for Ubuntu-like distros). A default, automatic installation of Ubuntu configures the disk like this.
 Standard Ubuntu installation partition scheme.
Other distros, like OpenSuSE, make also a separate /home partition. Anyway, to install Windows you must make room for it. So you'll shrink the root partition (or home partition if you have one). As you may have guessed it is done from GParted. But not from the running operating system. Instead you must run GParted from a live medium - for example Ubuntu installation disc. Again, boot from the installation DVD and when prompted to try or install, choose Try Ubuntu. After the desktop is loaded, launch GParted and select the hard drive from the top right box. Right click the root partition (sda1) and select Move/Resize. In the dialog that appears we have two approaches. The most reliable is shrinking the partition (as seen in the screenshot), but if you wish you can move it to the right and make space at the beginning of the drive. I do not recommend that because it involves lot of data operations and takes a long time. More than that, you'll see another issue later that is impossible to solve if the partition is moved to the right.
 Shrink partition in GParted - as easy as click and drag.
Be sure to keep it to the left (preceding space should be 0). Now apply the operation clicking the tick-icon button or from Edit menu. After a filesystem check, the unallocated space should appear.
 GParted partition shrinking process.
This is the result. It is enough to let you install Windows without affecting Ubuntu.
 The unallocated space for Windows installation.
Now let's restart and boot from the Windows DVD. It sees the partitions like this.
 Ubuntu partitions as seen by Windows.
We must create some partitions in the unallocated space. So we'll click on New and make the first partition. Actually the second, because Windows will make another one (System Reserved). We got the C: drive. But what if we want D:? If you have a MBR schemed disk, that is impossible.
 Cannot make another partition on MBR disk.
We exceeded the number of 4 primary partitions. How could we have avoided that? When we shrunk the Linux partition, we should have moved the extended partition container (sda2 in my example) a bit left to make room inside it for another partition. In this case this would have been an extended partition that served as D:. As you can see Windows and Linux use the same partition tables so you can mix partitions as you like. Continuing with my setup, I'm forced to remain at a single partition C:. Now you install Windows as usual.

After the installation is done, you get the surprise... No boot menu. Only Windows. That doesn't mean Linux is lost. It is just not bootable. Read section 3.1.

## 3. OS reinstallation

### 1. Windows

For whatever reason you want to do a clean install of Windows. That poses no risk to Ubuntu if you install Windows on the same partition it was before. Feel free to format it or even delete and recreate it from the installer partition tool. Just don't touch Ubuntu partitions. Which are those? The ones with 0.0 MB free space. Windows and Linux use the same partition table, but different filesystems, so Windows can tell where Linux partitions are, but it can't tell their size.

You just reinstalled Windows and got yourself with a non-bootable Ubuntu. You'll need a Linux installation media, preferably but not mandatory of the same Linux distribution you want to restore. Of course you must have a live medium (that doesn't require installation to run). Boot from the installation media and choose Try instead of Install when asked. If not asked and you get to a screen that shows disk configuration and partitions, quit and get a live Linux disc.

Getting Ubuntu bootloader back is simple. The procedure is described in detail at Ubuntu Community Help Wiki. Open a console (Terminal) in the live disc and run sudo parted followed by print all to identify our partitions. You must mount the root partition or the /boot partition if the case. Then you will install GRUB. There are two commands (don't forget to exit parted with quit):
sudo mount /dev/sda1 /mnt
sudo grub-install --boot-directory=/mnt/boot /dev/sda

 Reinstalling GRUB after Windows.
That should be it. If you ran in the opposite problem and can only boot Ubuntu don't worry. Open a terminal in the working Ubuntu and run sudo update-grub. It should detect Windows and add it to the boot menu.

### 2. Linux

There should be no problem when reinstalling Linux over the same partition. Just check the format box for each Linux partition at the installation screen. However, to avoid any boot loader issues, you can erase GRUB with Windows bootloader. See section 4.1 (without the formatting part).

## 4. Removing one operating system

### 1. Removing Linux

Before formatting Linux partitions, you must restore Windows bootloader. If you have a MBR disk, then this can be done by booting from Windows DVD. Choose Repair my computer when prompted with the Install Now dialog. Let it detect your OS and choose other. Open a command prompt and run the following commands:
bootrec /fixmbr
bootrec /fixboot

Now you can boot into Windows and from Disk Management remove all Linux partitions and either make new ones or extend the existing ones in the newly unallocated space.

### 2. Removing Windows

The bootloader is installed by Linux so you can safely delete all Windows partitions from GParted. To get rid of the Windows menu entry at boot, simply run sudo update-grub.