Ceci est une ancienne révision du document !
In previous parts, I explained how GRUB (version 2) works and how you can tune its behaviour, either instantly through the GRUB terminal or permanently by editing a couple of files and running update-grub. Now we arrive at the final stage of this exciting adventure! We will customize GRUB so that it offers a choice between multiple installed operating systems (OSs) to choose from. We will do it in such a way that this GRUB menu no longer depends on one of the installed distros, but is at your command only…
Before starting, we need to know some basics about partitioning. One physical hard drive can be ‘divided’ into separate partitions, which are formatted according to a specific filesystem (like FAT, NTFS, ext3, etc. – refer to Wikipedia and other sources if you are unfamiliar with this). When installing multiple (Linux) operating systems, you will need a number of partitions, plus one for swap space if it is needed on your machine (swap serves as extended, but much slower, memory on the hard drive; see online resources). Some may want to take a separate /home partition into account. However, better avoid having several Linux installations use the same /home partition, as that may lead to inconsistencies.
Is there a (relevant) limit to the number of partitions on one hard drive? Well, it depends on the type of partition:
• one physical hard drive can hold four primary partitions at most. This is the basic (and default) type of partition; • if you need more partitions (like us), you have to do this with an extended partition. One hard drive can hold one extended partition at most, and it counts as one of the maximum four primary partitions on the hard drive. So if you already had four primary partitions, you have to delete a primary partition first and create an extended partition instead; • one extended partition can hold as many logical partitions as you like (including swap). They can be formatted as any filesystem (while an extended partition cannot be formatted: its only purpose is to hold the logical partitions).
In Linux, the partitions are denoted as /dev/sda1, /dev/sda2, /dev/sda3, etc. (considering partitions on your first hard drive, i.e. /dev/sda). To access their contents, you may have to mount each one as follows (for the first partition):
mkdir /mnt/partition1 && sudo mount /dev/sda1 /mnt/partition1
That is, if they are not already mounted automatically by the distribution you are working in.
An excellent way to create-extend-move-shrink-delete partitions, is to use GParted in a live session from USB-stick/DVD (that will allow you to modify everything on the hard disk). The program is quite self-explaining, but clear guidance can be found on the internet. If GParted is not yet installed in your downloaded iso-file, it is only an ‘apt-get install gparted’ away. After having made the desired partitioning layout on the hard disk, it is always a good idea to write down the partition numbers (know what /dev/sda1, /dev/sda2 etc. correspond to), as well as the unique identifiers (UUID), which you will find in the properties of each partition. Also, Disk Utility (palimpsest) from the gnome-disk-utility package provides a nice graphical overview of your partitions. Finally, commands like 'sudo fdisk -l', 'sudo blkid' and 'sudo parted -l' will tell you similar things.
The rest of my story is to give you one (of several possible) ways to achieve a multiboot system with an independent GRUB on the MBR of your hard drive. The key will be to provide one extra partition on which /boot/grub - and nothing else - resides with all its usual contents (including grub.cfg). Let’s call this partition GRUBpart. ONLY the GRUB installed on the Master Boot Record (which I will call MasterGRUB) is allowed to refer to this partition! The grub.cfg file on GRUBpart should only contain menu entries that chainload either Windows or GRUB installations on the distribution-specific partitions. The GRUBs on the Linux partitions use their ‘own’ /boot/grub directory within the distro’s partition. Therefore the /boot/grub folder on GRUBpart will NOT be used or updated by any of the installed distros! Consequently, when you replace one distro with another, everything still works: the MasterGRUB just continues to chainload the partition-specific bootloaders. Of course, during installation of a new Linux distro (replacing a previous one), you will have to specify that GRUB must be installed on the specific partition (/dev/sda4 for example), and NOT on /dev/sda (which refers to the entire disk, and therefore to its Master Boot Record).
Let’s make such a system in 10 steps. But be prepared first! Especially by learning GRUB terminal commands and knowing how GRUB works; see previous sessions for more information. I have tested the below myself, but of course I am not responsible for your own GRUB- and PC-adventures…
I will start from a blank hard disk, so if you already have something installed, first read everything in order to decide what you will need to do. And in that case: first backup all your precious stuff please… To be broadly applicable, I presume that you will install Windows as well – but of course that is up to you.
• Use GParted from a live session to create two primary partitions – Windows and GRUBpart – and one extended partition with the number of logical partitions therein corresponding to swap (option) plus the (maximum) number of Linux distros you may want to multiboot. For Windows, use the NTFS file system, and for Linux and GRUBpart use ext2 or higher. Of course you have to decide on partition sizes yourself; I consider 15 GB for the OS partitions as a convenient lower limit. Decide on whether you need a partition for swap (format it as such), and on its size. If you only have 1 GB RAM, it is wise to add another GB as swap. Use at least 50 MB (yes, just mega-) for the GRUBpart partition.
• Install Windows onto its primary partition. Beware that creating an NTFS partition beforehand (step 1) was necessary to prevent Windows 7 (and alike) to create an extra primary partition for specific system files. It would do so when you installed Windows on an unpartitioned hard disk. And in that case, live with it and shrink partitions (see link at the end).
Keep in mind which Linux distro you (eventually) want to let install its GRUB on the MBR. My choice would be a distro that holds the latest version of the grub-pc package (see distrowatch.com for an easy overview). Let’s call that distro ‘distro Z’. Install each Linux distro, except Z, on its own partition. For all distros except Z, install their GRUB (during installation) on their OWN partition; this differs from the suggested default during installation. This means that you will usually have to change /dev/sda as GRUB’s target into - for example - /dev/sda5 (correctly choosing the partition number of course).
• Install distro Z as the last one. Here we will follow the default /dev/sda to install GRUB to. Now we get GRUB in the MBR pointing at /boot/grub on the Z partition. This gives us a bootable machine, which is the reason to take this way (a more advanced alternative is that you handle Z like in step 3, and that you accomplish everything else from a live Z session).
• Boot from your hard disk, and check that all OSs are able to boot from the GRUB menu that Z installed. If not, figure out what went wrong and get it working.
• Boot into distro Z and install Z’s GRUB onto its own partition (supposing partition 8):
sudo grub-install /dev/sda8
You may get some warnings, but the job should get done (otherwise, add –force). At this point, both MasterGRUB and Z’s GRUB use the same grub.cfg.
• In distro Z, add chainload entries to /etc/grub.d/40_custom (see previous time) that chainload the respective OSs (including GRUB on partition Z itself – indeed, you will be able to loop, for now). Tune /etc/default/grub according to your taste; you may want to add a background image in /boot/grub (check our previous session!). Now run: sudo update-grub
Inspect the updated grub.cfg for abnormal stuff, and reboot if OK. Try out all your chainloaded GRUBs. We are nearly there. Remember that it is still distro Z that is governing the MasterGRUB.
• Boot into distro Z. We will now overwrite GRUB in the MBR with one that points at the GRUBpart partition. We will also make two different grub.cfg files, one for MasterGRUB (containing only the chainload entries), and one for distro Z. Let’s do this in a root terminal from distro Z: sudo -s
cd /boot/grub
cp grub.cfg grubZ.cfg
here we save Z’s current grub.cfg as a backup
cd /etc/grub.d
chmod –x *linux* *lupin* *memtest* *prober*
don't mind if one of the patterns is not available
update-grub
this creates a grub.cfg which we can use for our MasterGRUB, with the chainloaded entries only
chmod +x *linux* *lupin* *memtest*
to have future grub-updates in Z making a “normal” grub.cfg for Z again –- # don't mind if one of the patterns is not available
mkdir /mnt/GRUBpart && mount /dev/sda2 /mnt/GRUBpart
of course replace sda2 with the appropriate number of the GRUBpart partition. You can skip this step if GRUBpart was already mounted.
mkdir /mnt/GRUBpart/boot
grub-install /dev/sda –boot-directory=/mnt/GRUBpart/boot/ you may get some warnings, but the job should get done (otherwise, add –force).
Yes, we just installed our final MasterGRUB, which is told to look into the GRUBpart partition!
For version 1.98 and earlier, set –-root-directory=/mnt/GRUBpart/ instead of the –-boot-directory option.
cd /boot/grub
cp grub.cfg /mnt/GRUBpart/boot/grub/
and do the same for a possible background image if grub.cfg pointed at one
mv grubZ.cfg grub.cfg
i.e. we change its name back to restore Z’s grub.cfg
exit
To finish grub.cfg in the GRUBpart partition, you need to look for all lines in grub.cfg above your custom entries and replace pointers to the Z's partition with pointers to GRUBpart. In our example this means replacing hd0,8 by hd0,2 and also the corresponding listed UUID's, which you get with 'sudo blkid'. This is necessary because grub.cfg points to some other files and modules in /boot/grub (such as the background image), and we want to specify that is to be found on GRUBpart.
• Check out that everything works fine now. We accomplished the essentials of our mission, except for one thing: the grub-pc package of distro Z is still configured to install future updates of GRUB onto the MBR (that is, when new versions of the grub-pc package arrive). To change its default target, run the following in distro Z: sudo dpkg-reconfigure grub-pc After some questions, for which you can safely accept the default, you will be asked to set the default target device to install updates of Z’s GRUB, which must be its own partition ONLY (/dev/sda8 in our example). Select it with an asterisk by hitting the spacebar. Also grub.cfg (in /boot/grub on /dev/sda8) will be overwritten in this process.
• Tune each distro’s own grub configuration file according to your taste (edit /etc/default/grub plus contents of /etc/grub.d/, and run 'sudo update-grub'). Among other things, you will want to get rid of the chainload entries of 40_custom in distro Z. A tweak that I like to add in 40_custom of each distro is a chainload entry ‘Go Back’ that will redirect the user to the MasterGRUB menu (use set root='(hd0)'). Just in case you had picked the wrong distro – or changed your mind!
Well that’s it! It may have taken some time to learn things, but after all it was a relatively easy job. You are now the proud user of a multi-OS machine with a boot loader that is no longer controlled by any of the operating systems. This also means that you need to manually edit grub.cfg on the GRUBpart partition when necessary, i.e. typically by changing a menu title or adding a menu entry – but that is easy now. For future Linux installs, just keep in mind to install each distro’s GRUB on its own partition. Your MasterGRUB chainloads them. Happy booting!
Floris is very grateful to David H. and Bart A. for their useful comments on an earlier version of the manuscript.
Interesting links
Ubuntu Community ‘swap FAQ’: https://help.ubuntu.com/community/SwapFaq
Ubuntu Community ‘How to partition’: https://help.ubuntu.com/community/HowtoPartition
Resize Windows partitions with Linux: https://help.ubuntu.com/community/HowtoResizeWindowsPartitions
Dedoimedo GRUB2 full tutorial, on GRUB configuration and multibooting: http://www.dedoimedo.com/computers/grub-2.html
Sanket Totewar’s article on GRUB2 multibooting: to download from http://www.linuxidentity.com/us/index.php?name=News&file=article&sid=5047