DEV Community

Or Yaacov
Or Yaacov

Posted on

How to boot from iso file using GRUB & internal HDD (without external USB/CD)

I just received my new System76's Lemp11 pro, and even though I love Pop!_OS I prefer to use Arch (btw), but… I didn't have an external USB drive that will use me as installation media, so…

Creating Partition

I have two internal hard drives and I'm going to shrink my secondary one to allocate more space for a new partition, you can skip this step in case you have some available memory.

Open Disks, and resize your partition
Image description

I Choose to leave 32GB my internal partition

Image description

in the free space create a new ext4 and name it as you want :)

Copying the ISO files

step one, copy the iso files to the new disk that you just created. 
step two, None.

Preparing the data

in the next and last step, we will need to configure our grub, and for this, we will need to collect a few pieces of data:

  1. your .iso file hdd number: you can run lsblk | grep disk to list your hard drives:

/dev/hda refers to the first IDE hard drive
/dev/sda refers to the first SCSI or SATA hard drive.
 If you use a NMVe SSD, it might be named as /dev/nvme0n1, /dev/nvme1n1 and so on.
But in GRUB, the first hard drive is always referred to as hd0, no matter what the interface type is

  1. your .iso file partition number:
    you can run the following command sudo parted -l and locate your iso's files partition number

  2. your setup's .efi files
    browse trough your .iso files (you can do it by mount your iso or open it an archive manager), and locate your desired linuz .efi file and image file
    for example:
    Ubuntu setup: 
     /casper/vmlinuz and /casper/initrd
    Arch setup
    /arch/boot/x86_64/vmlinuz-linux and /arch/boot/x86_64/initramfs-linux

Configuring GRUB

use your favorite text editor to add the following line to

your custom configuration file:
sudo vi /etc/grub.d/40_custom 
(if the file doesn't exists /etc/grub.d/40_custom and
update-grub executable install, please install grub-disk)

and add the following lines:

menuentry "{{menu entry name}}" {
  insmod ext2
  set isopartition=hd{{hdd number}},{{partition number}}
  set isofile="{{iso file path (relative to hdd)}}"
  loopback loop (hd{{hdd number}},{{partition number}})$isofile
  linux (loop){{iso's vm linuz efi file path}} img_dev=/dev/disk/by-uuid/$isouuid img_loop=$isofile
  initrd (loop){{iso's ram memory image file path}}
}
Enter fullscreen mode Exit fullscreen mode

working example (Arch linux)

menuentry "archlinux-2022.07.01-x86_64.iso" {
  insmod ext2
  set isopartition=hd1,2
  set isofile="/iso/archlinux-2022.07.01-x86_64.iso"
  loopback loop (hd1,2)$isofile
  linux (loop)/arch/boot/x86_64/vmlinuz-linux img_dev=/dev/disk/by-uuid/$isouuid img_loop=$isofile
  initrd (loop)/arch/boot/x86_64/initramfs-linux.img
}
Enter fullscreen mode Exit fullscreen mode

Ubuntu:

menuentry "ubuntu-20.04.2.0-desktop-amd64.iso" {
  insmod ext2
  set isofile="/iso/ubuntu-20.04.2.0-desktop-amd64.iso"
  loopback loop (hd1,2)$isofile
  linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile quiet noeject noprompt splash
  initrd (loop)/casper/initrd
}
Enter fullscreen mode Exit fullscreen mode

menuentry: your GRUB2 menu entry, name it as you like.
insmod: inserts a module. Since the ISO file is stored in ext4 partition he ext2 module is needed.
set isofile: Specify that path of your ISO image file
loopback: Mount the ISO file. hd0 means the first hard drive in the computer and 5 means the ISO file is stored on the 5th disk partition.
The linux command loads a Linux kernel from the specified path. casper/vmlinuz.efi is the linux kernel inside the Ubuntu ISO image.
initrd: initrd command loads an initial ramdisk from the specified path. It can only be used after the linux command has been run. The initial ramdisk is a minimal root file system mounted to the RAM. casper/initrd.lz is the initrd file inside the Ubuntu ISO image.

next all you need to do is to run the following command sudo update-grub and reboot. 

All done :)

Top comments (0)