Start a Fedora 29 installation from the GRUB menu

Many VPS (or cloud) providers offer a Fedora image; if yours doesn’t, ask them to consider doing so. Fedora 29 cloud images are available here https://alt.fedoraproject.org/cloud/. However, in that case this article gives you an alternative to try.

In some cases the provider will allow you to load a custom image (raw or QCOW format). They may even let you mount an ISO and perform an installation from scratch. Another use case for booting into an installation from the GRUB menu is a remote server without physical access (or via some out of band remote management console). In this case you cannot insert an USB key or an installation DVD to boot. What if you want to use your preferred distribution in any case, and not be forced to use what is provided?

The solution

One possible way to solve this is to start the Fedora installer using GRUB2. As a prerequisite to the following steps, you’ll need a VM (or baremetal machine) running CentOS 7, with a working network and internet connection.

Be aware that the following instructions come with caveats such as noted. It depends on:

  • The kind of virtualization used by the provider (KVM or VMWare should be fine), and
  • Whether they allow to start the installed system from the disk volume, or if they use some alternative method to load a different kernel that is not the one shipped by the distribution.

Also, whenever you are modifying the GRUB menu with grub2-mkconfig there is the potential to end up booting into the GRUB shell if the menuentry ends up with an error.

Downloading vmlinuz and initrd

To get the initrd.img and the vmlinuz files issue the following commands one after the other.

curl -L -o /boot/initrd-fedora.img https://download.fedoraproject.org/pub/fedora/linux/releases/29/Server/x86_64/os/isolinux/initrd.img 

curl -L -o /boot/vmlinuz-fedora https://download.fedoraproject.org/pub/fedora/linux/releases/29/Server/x86_64/os/isolinux/vmlinuz

Take some notes

Take note of the current IP address, the MAC address, and the default gateway.

ip a 
ip route

And take note of the UUID of the boot partition using the command

blkid 

In some cases /boot could be a directory inside the root partition, so you need its UUID.

Add a new menu entry to GRUB

Edit this file /etc/grub.d/40_custom and add this entry (similar to other entries you can find in /etc/grub2.cfg):

menuentry 'FedoraNetInstall' {     
load_video
set gfxpayload=keep #Keep gfxmode
insmod gzio #Load the gzio module
insmod part_msdos #Load the MS DOS partition module
insmod ext2 #Load the ext2 partition module
set root='hd0,msdos1' #Set Grub root
if [ x$feature_platform_search_hint = xy ];
then
search --no-floppy --fs-uuid --set=root --hint='hd0,msdos1' <UUID>
else
search --no-floppy --fs-uuid --set=root <UUID>
fi
linux16 /vmlinuz-fedora ip=<IP>::<Gateway>:<Netmask>:eth0:none nameserver=<DNS> ifname=eth0:<MAC address> inst.repo=https://download.fedoraproject.org/pub/fedora/linux/releases/29/Server/x86_64/os inst.lang=en_US inst.keymap=en
initrd16 /initrd-fedora.img
}

Change the values (UUID, IP, gateway and so on) accordingly to reflect your configuration. If you are using DHCP for networking, you don’t need to enter any networking information. If /boot is not a separate partition, you will have to prepend /boot to /vmlinuz-fedora, and also to /initrd-fedora.img in the menuentry.

Generate the new GRUB configuration file

Before generating your grub.cfg file with grub2-mkconfig it is strongly recommended to copy the original grub.cfg file to another name, such as grub.cfg.bak. Also in the /etc/grub.d directory any files not being used to generate the menuentry should be made non-executable with chmod a-x <filename>. CentOS 7 uses 00_header, 00_tuned, 01_users, and 10_linux when generating grub.cfg. Those files and 40_custom need to remain executable for grub2-mkconfig to work as expected.

To make a new grub.cfg file in /boot/grub2 issue the following command as root or preferably with sudo:https://fedoramagazine.org/howto-use-sudo/

grub2-mkconfig -o /boot/grub2/grub.cfg

Then set up the default menu entry that will be selected upon reboot.

grub2-reboot FedoraNetInstall

Reboot into your new GRUB menu

After reboot, you should see the typical web console that cloud providers usually offer in their management dashboard. If the boot is successful, you should find yourself following a regular Fedora installation.

By adding inst.text here:

linux16 /vmlinuz-fedora inst.text ip=<IP>::<Gateway>:<Netmask>:eth0:none [...] 

You could perform the installation in text mode instead of graphical mode.

If GRUB2 is not installed

If the machine is running grub legacy, edit /etc/grub.conf and add these lines:

title Fedora Inst
root (hd0,0)
kernel /boot/vmlinuz-fedora
ip=<IP>::<Gateway>:<Netmask>::eth0:none nameserver=<DNS> ifname=eth0:<MAC address>
inst.repo=https://download.fedoraproject.org/pub/fedora/linux/releases/29/Server/x86_64/os inst.lang=en_US inst.keymap=en
initrd /boot/initrd-fedora.img

Then issue the following command, where vdX is the VM primary disk.

grub-install /dev/vdX

If you get an error stating No suitable drive was found in the generated device map, try to install GRUB manually. Issue the grub command, then run these commands:

grub> find /boot/grub/stage1 find /boot/grub/stage1  (hd0,0) 
grub> root (hd0,0)
root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
setup (hd0)
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/e2fs_stage1_5" exists... yes
Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... 27 sectors are embedded. succeeded
Running "install /boot/grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/boot/grub/stage2 /boot/grub/grub.conf"... succeeded
Done.
grub> quit

Final notes and troubleshooting

This installation method works best with the VM (or baremetal machine) equipped with at least 2GB of RAM. The CentOS installation should be a default install to begin with, and is expected to use the ext2 filesystem as per the examples.

Pay special attention when partitioning the disk, leave the partition table as is, and format the existing partitions.

For more information on grub2 and how to use the tools provided to better customize your GRUB boot experience, check out this Fedora documentation on the GRUB boot loader.

If you do find yourself in the GRUB shell, you can boot from the backup you made earlier (grub.cfg.bak), by doing something like the following:

grub> ls (hd0), (hd0,msdos1), (hd0,gpt2) 
grub> ls (hd0,msdos1)/ efi/ grub2/ initramfs.img vmlinuz
grub> ls (hd0,msdos1)/grub2 device.map grub.cfg grub.cfg.bak
grub> configfile (hd0,msdos1)/grub2/grub.cfg.bak
grub> boot

Fedora Project community

7 Comments

  1. Edgar Hoch

    There is no need for CentOS 7. Any recent Fedora installation is also sufficient.

    • Sure. But what about if the already installed machine is CentOS? And if you can’t install Fedora starting from a CD/USB or from a raw/qcow image? Using grub could be handy.

      • Edgar Hoch

        I wanted to state that the method you have described can be used with any system that has grub2 installed, not only CentOS 7. This restriction is not necessary.

        Another note: You mention ext2 – who ever uses ext2 nowadays? Usually we should use ext4 (and ext4 is an extension of ext2, I know, but there is a difference if you format a file system with ext2 or with ext4).

        • Steven

          There is also ms-dos as a filesystem used and who uses ms-dos nowadays anyway? But that isn’t the point, the commands are the same for ext2 vs ext4.

          insmod ext4

          Or

          insmod xfs

          is not too much of a stretch from

          insmod ext2

          in any case. The example is just that, an example to be modified to suit your particular requirements if you choose to try it out.
          You are correct that any system using Grub2 can employ this method of install. There is also the BFO project which is an alternative method of getting the kernel booted to start an install, and it too can be done from Grub2.

        • Tim K.

          There is no need for journaling on /boot and some folks feel that ext2 is still a good choice. I’ve used both ext2 and ext4 on /boot without any noticeable difference in performance or reliability, but I haven’t truly studied the difference.

  2. Etienne Champetier

    Hi Alessio,

    Safer (you don’t modify your grub) and simpler in my opinion, you can do the same thing using kexec \o/
    https://github.com/champtar/notes/tree/master/kexec

    You can append kickstart file to the initrd and do in place unattended reinstall of your VPS
    I used that to reinstall my VPS with full disk encryption, and to type the decryption password I use https://copr.fedorainfracloud.org/coprs/gsauthof/dracut-sshd/

    You also have ‘inst.vnc’ option that can be interesting

  3. FP

    It’s going tto bе еnd of mine ⅾay, exceрt Ьefore еnd І am
    reading tһis impressive post to increase mmy experience.

Comments are Closed

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions