Multi-booting, new installs, updates, removing old installs - all of these events can delete or damage your GRUB and thus your ability to boot. One solution is to separate GRUB completely from an install so it "lives" on it's own so no one installation can remove or damage it.
GRUB exists in two locations - it resides on your boot device in an area unused by your partitions and also has files within the Linux installation that installed it. It's the second part - these files - which are mostly at risk. If you remove the install that "owns" grub, you remove the files that grub needs to boot.
If you are or plan on installing more than one Linux distro, preventing the removal of grub is easy to do and manage. I will not discuss installing GRUB initially. This how-to assumes you have already installed GRUB at least once and are able to boot to your system.
This is a moderately advanced operation. If you are not comfortable recovering an unbootable system and re-installing grub, I do not recommend you attempt this. Sometimes things go wrong. You should be able to fix a non-booting GRUB installation by yourself before trying something this advanced. This how-to is primarily useful for multi-booting Linux installs.
Step 1: prepare a partition for GRUB.
A dedicated GRUB partition only needs a dozen or so megabytes. The minimum partition size if you're using the Partition Manager is 8MB. I recommend at least 24MB (that's MEGAbytes not gigabytes) and using EXT2 as the file system format. EXT3/4 have journaling which is not necessary for this usage. My GRUB partition has 14MB of used space because I also have memtest on the GRUB partition. Without memtest, you'll use about 6MB. If you are worried about GRUB getting larger in the future, you could go larger, to say 64MB. It is just megabytes after all. You'll hardly notice the used bytes.
Create and format this partition to EXT2 using your preferred tools.
For the example commands in this how-to, I will use sda1 as the GRUB partition and sda as the boot drive. Obviously, use the device names of your boot drive and new GRUB partition as you follow these steps.
Step 2: Mount the GRUB partition.
Make a mount point for the GRUB partition and mount it:
sudo mkdir /mnt/grub
sudo mount /dev/sda1 /mnt/grub
Step 3: Copy GRUB to the new partition.
From the install you are currently booting from, install GRUB to the new partition.
sudo grub-install --root-directory=/mnt/grub /dev/sda
This command does two things:
1) It modifies the boot record of sda to look in /dev/sda1 for grub's files.
2) It copies the files grub needs to boot to /dev/sda1 (/mnt/grub) in the proper directory structure.
If you do a file listing of /mnt/grub right now, you will see the GRUB files are there with one very large exception: There is no grub.cfg file. If you rebooted right now, your boot would fail. This is OK because we are going to create a custom grug.cfg manually. Other files you may have added to customize your GRUB look - background images,fonts, or themes are also not copied be the above command. You'll have to copy those manually if you want to continue using those.
Step 4: Make a custom grub.cfg
One thing that grub does for you is update its grub.cfg each time you update your kernel. We don't want to lose this functionality. Also, some distros use different setting for the construction of grub.cfg. We don't want to lose that either. We preserve these things by allowing each install to retain it's own grub.cfg and then set up our dedicated grub to link to those files. This results a nested grub menu. When you first boot you will see the menu from your manually created grub.cfg. When you select on of the entries, you will now see the grub.cfg from the selected installation. Another real advantage to this method is if you initially select the wrong menu item from your custom grub.cfg menu, hitting the "ESC" key before the boot process begins will return you to back to your custom menu.
To create your first custom grub.cfg we will use your current grub.cfg and modify it. Use your favorite editor if you don't like Kate.
kate /boot/grub/grub.cfg
Now delete everything after the line:
### BEGIN /etc/grub.d/10_linux ###
and add this menu stanza:
Let me break down each line because there are some important things to note in the above;
This first line is title or name that you will see in your initial grub menu. Use whatever you would like to see between the quotes.
This indicates that I'm using a GPT partition table. If you're using MBR on this drive, use "part_msdos" instead of "part_gpt
This indicates that i'm using the BTRFS file system with this particular install. If you're using something else you'll need to change it. For example EXT4 would need "ext4" is place of "btrfs".
This is the UUID of the file system I want to boot to. Change this to reflect yours.
Finally, this is the location of the installation grub.cfg that I'm going to link to. I use BTRFS so the subvolume name is in there as well. This should reflect your installation's grub.cfg full path from the root of the installation. If you are using EXT4 it should look like this:
Don't forget the bracket - it signals the end of the stanza (the opening bracket in at the end of the menuentry line.
You will need to repeat the above stanza for each installation you want to boot this way. I have six distros installed and bootable through this custom grub.cfg. As you add or delete Linux installs, you will need to return to this menu and manually edit the stanzas to reflect the changes you made.
When you're all done, save the file as /mnt/grub/boot/grub/grub.cfg.
On reboot, you should see your custom grub.cfg. When you select an entry, you should then see the grub entries from the selected distro. ESC will bring you back to your custom menu.
Here's what mine looks like:
Optional features:
Memtest:
I also copied the three memtest files:
from one of my installs to my grub partition under /boot. This allows me to run memtest from the first grub menu and also allows me to purge memtest from all my subsequent installs. I didn't see a need for six copies of memtest to be installed. Look at the bottom of your current grub.cfg or my custom one above to see how to add memtest to your custom grub.
Booting an ISO from grub:
Also note in my custom grub.cfg the last entry:
With this I can boot directly to an ISO. Not all ISOs are bootable this way, but it's a neat feature if you want to try out a new distro on your hardware before you install it and don't want to make a liveUSB. The entry may vary for the ISO of your choice, but that for another thread.
Default timeout and menu selection:
To set the default distro to boot to, look for this near the top of your custom grub:
set default="0"
which will always boot the first menuentry in the list and you may not want that. By using the menuentry title in the quotes instead of a number, you will always boot to your choice regardless of the order of the stanzas in grub.cfg, so change it to
set default="YOUR MENU ENTRY HERE"
You obviously also need to see this first menu or you won't be able to select a non-default choice so make sure your custom grub.cfg has a suitable timeout setting.
An important final note: As you install each new Linux distro, DON'T INSTALL GRUB TO SDA! This will remove Step 3 and prevent you from using the custom menu. Most installers will allow you to install grub to a partition instead of the drive. This is one way around this. If you have a second drive, just install grub to sdb instead of sda. This will allow you to continue to use your custom grub. If you forget or the installer won't allow you to avoid installing it's grub to sda, just redo Step 3 and you should be back in business.
One interesting other thing to note; If you use only EXT4 and aren't in the habit of redoing your partitions, you could have a custom grub that never needed editing. Just use device names or labels in place of UUIDs to identify the partitions and use generic menu titles like 'Partition 4' as your menuentry.
GRUB exists in two locations - it resides on your boot device in an area unused by your partitions and also has files within the Linux installation that installed it. It's the second part - these files - which are mostly at risk. If you remove the install that "owns" grub, you remove the files that grub needs to boot.
If you are or plan on installing more than one Linux distro, preventing the removal of grub is easy to do and manage. I will not discuss installing GRUB initially. This how-to assumes you have already installed GRUB at least once and are able to boot to your system.
This is a moderately advanced operation. If you are not comfortable recovering an unbootable system and re-installing grub, I do not recommend you attempt this. Sometimes things go wrong. You should be able to fix a non-booting GRUB installation by yourself before trying something this advanced. This how-to is primarily useful for multi-booting Linux installs.
Step 1: prepare a partition for GRUB.
A dedicated GRUB partition only needs a dozen or so megabytes. The minimum partition size if you're using the Partition Manager is 8MB. I recommend at least 24MB (that's MEGAbytes not gigabytes) and using EXT2 as the file system format. EXT3/4 have journaling which is not necessary for this usage. My GRUB partition has 14MB of used space because I also have memtest on the GRUB partition. Without memtest, you'll use about 6MB. If you are worried about GRUB getting larger in the future, you could go larger, to say 64MB. It is just megabytes after all. You'll hardly notice the used bytes.
Create and format this partition to EXT2 using your preferred tools.
For the example commands in this how-to, I will use sda1 as the GRUB partition and sda as the boot drive. Obviously, use the device names of your boot drive and new GRUB partition as you follow these steps.
Step 2: Mount the GRUB partition.
Make a mount point for the GRUB partition and mount it:
sudo mkdir /mnt/grub
sudo mount /dev/sda1 /mnt/grub
Step 3: Copy GRUB to the new partition.
From the install you are currently booting from, install GRUB to the new partition.
sudo grub-install --root-directory=/mnt/grub /dev/sda
This command does two things:
1) It modifies the boot record of sda to look in /dev/sda1 for grub's files.
2) It copies the files grub needs to boot to /dev/sda1 (/mnt/grub) in the proper directory structure.
If you do a file listing of /mnt/grub right now, you will see the GRUB files are there with one very large exception: There is no grub.cfg file. If you rebooted right now, your boot would fail. This is OK because we are going to create a custom grug.cfg manually. Other files you may have added to customize your GRUB look - background images,fonts, or themes are also not copied be the above command. You'll have to copy those manually if you want to continue using those.
Step 4: Make a custom grub.cfg
One thing that grub does for you is update its grub.cfg each time you update your kernel. We don't want to lose this functionality. Also, some distros use different setting for the construction of grub.cfg. We don't want to lose that either. We preserve these things by allowing each install to retain it's own grub.cfg and then set up our dedicated grub to link to those files. This results a nested grub menu. When you first boot you will see the menu from your manually created grub.cfg. When you select on of the entries, you will now see the grub.cfg from the selected installation. Another real advantage to this method is if you initially select the wrong menu item from your custom grub.cfg menu, hitting the "ESC" key before the boot process begins will return you to back to your custom menu.
To create your first custom grub.cfg we will use your current grub.cfg and modify it. Use your favorite editor if you don't like Kate.
kate /boot/grub/grub.cfg
Now delete everything after the line:
### BEGIN /etc/grub.d/10_linux ###
and add this menu stanza:
Code:
menuentry 'Kubuntu 16.04' { insmod part_gpt insmod btrfs search --no-floppy --fs-uuid --set=root 8f0c1661-4e84-4512-b875-23bcfd5be1d8 configfile /@Kubuntu_16_04/boot/grub/grub.cfg }
Code:
menuentry 'Kubuntu 16.04' {
Code:
insmod part_gpt
Code:
insmod btrfs
Code:
search --no-floppy --fs-uuid --set=root 8f0c1661-4e84-4512-b875-23bcfd5be1d8
Code:
configfile /@Kubuntu_16_04/boot/grub/grub.cfg }
Code:
configfile /boot/grub/grub.cfg.
You will need to repeat the above stanza for each installation you want to boot this way. I have six distros installed and bootable through this custom grub.cfg. As you add or delete Linux installs, you will need to return to this menu and manually edit the stanzas to reflect the changes you made.
When you're all done, save the file as /mnt/grub/boot/grub/grub.cfg.
On reboot, you should see your custom grub.cfg. When you select an entry, you should then see the grub entries from the selected distro. ESC will bring you back to your custom menu.
Here's what mine looks like:
Code:
menuentry 'KDE Neon' { insmod part_gpt insmod btrfs search --no-floppy --fs-uuid --set=root 8f0c1661-4e84-4512-b875-23bcfd5be1d8 configfile /@KDEneon/boot/grub/grub.cfg } menuentry 'KDE Neon - new' { insmod part_gpt insmod btrfs search --no-floppy --fs-uuid --set=root 8f0c1661-4e84-4512-b875-23bcfd5be1d8 configfile /@KDEneon_new/boot/grub/grub.cfg } menuentry 'Kubuntu 16.04' { insmod part_gpt insmod btrfs search --no-floppy --fs-uuid --set=root 8f0c1661-4e84-4512-b875-23bcfd5be1d8 configfile /@Kubuntu_16_04/boot/grub/grub.cfg } menuentry 'Ubuntu 16.04' { insmod part_gpt insmod btrfs search --no-floppy --fs-uuid --set=root 8f0c1661-4e84-4512-b875-23bcfd5be1d8 configfile /@Ubuntu_16_04/boot/grub/grub.cfg } menuentry 'Manjaro free' { insmod part_gpt insmod btrfs search --no-floppy --fs-uuid --set=root 8f0c1661-4e84-4512-b875-23bcfd5be1d8 configfile /@Manjaro_free/boot/grub/grub.cfg } menuentry 'Manjaro nonfree' { insmod part_gpt insmod btrfs search --no-floppy --fs-uuid --set=root 8f0c1661-4e84-4512-b875-23bcfd5be1d8 configfile /@Manjaro_nonfree/boot/grub/grub.cfg } menuentry 'Memory test (memtest86+)' { insmod part_gpt insmod ext2 search --no-floppy --fs-uuid --set=root 4bd4447b-48df-43d8-9781-d444d68ce462 knetbsd /boot/memtest86+.elf } menuentry 'Memory test (memtest86+, serial console 115200)' { insmod part_gpt insmod ext2 search --no-floppy --fs-uuid --set=root 4bd4447b-48df-43d8-9781-d444d68ce462 linux16 /boot/memtest86+.bin console=ttyS0,115200n8 } menuentry 'KDEneon ISO' { set isofile="/neon-useredition-20170608-2351-amd64.iso" loopback loop (hd0,2)$isofile linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile noprompt noeject initrd (loop)/casper/initrd.lz } ### END Custom menu###
Optional features:
Memtest:
I also copied the three memtest files:
Code:
memtest86+.bin memtest86+.elf memtest86+_multiboot.bin
Booting an ISO from grub:
Also note in my custom grub.cfg the last entry:
Code:
menuentry 'KDEneon ISO' { set isofile="/neon-useredition-20170608-2351-amd64.iso" loopback loop (hd0,2)$isofile linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile noprompt noeject initrd (loop)/casper/initrd.lz }
Default timeout and menu selection:
To set the default distro to boot to, look for this near the top of your custom grub:
set default="0"
which will always boot the first menuentry in the list and you may not want that. By using the menuentry title in the quotes instead of a number, you will always boot to your choice regardless of the order of the stanzas in grub.cfg, so change it to
set default="YOUR MENU ENTRY HERE"
You obviously also need to see this first menu or you won't be able to select a non-default choice so make sure your custom grub.cfg has a suitable timeout setting.
An important final note: As you install each new Linux distro, DON'T INSTALL GRUB TO SDA! This will remove Step 3 and prevent you from using the custom menu. Most installers will allow you to install grub to a partition instead of the drive. This is one way around this. If you have a second drive, just install grub to sdb instead of sda. This will allow you to continue to use your custom grub. If you forget or the installer won't allow you to avoid installing it's grub to sda, just redo Step 3 and you should be back in business.
One interesting other thing to note; If you use only EXT4 and aren't in the habit of redoing your partitions, you could have a custom grub that never needed editing. Just use device names or labels in place of UUIDs to identify the partitions and use generic menu titles like 'Partition 4' as your menuentry.
Comment