62

I don't want to burn a CD, DVD or USB memory stick. And I don't care about saving the existing operating system (Linux Mint). I want to install Ubuntu (actually Kubuntu 12.04 LTS) to the computer directly from the ISO image. The image is for the Kubuntu Live DVD (3.5 GB).

The computer is now booted into Linux Mint. A set of command line steps would be satisfactory. In my mind, I simply need to trick the existing system into thinking I inserted a CD/DVD, right?

I know there is a similar question already but it does not have a good answer. The answer basically says, "see this link." I reviewed the link and I decided that StackExchange needs a good answer to this question. Furthermore, most answers at other places on the Internet assume the user is coming to Ubuntu from Windows. So I think this question is one that needs a good answer here. Thanks

Braiam
  • 67,791
  • 32
  • 179
  • 269
MountainX
  • 5,829
  • 19
  • 66
  • 90

2 Answers2

53

I found this looking around on the net.

Grub2 is capable of directly booting ISO images for many Linux distros if the entries have been properly defined in the Grub2 configuration files.

The ISO image must be placed on a separate partition that does not have an operating system installed on it. For the sake of simplicity, we would place it inside a new directory under /boot (assuming it is on a separate partition).

  1. Create the new directory and copy your ISO image:

    sudo mkdir /boot/iso
    sudo cp ~/Desktop/name.iso /boot/iso
    

    Where ~/Desktop/name.iso is the location and name of your ISO image assuming that the image is located at your desktop.

  2. Add the Grub2 entry:

    The entry for the ISO image needs to be added to /etc/grub.d/40_custom file. Edit the file by:

    gksudo gedit /etc/grub.d/40_custom
    

    And replace the text in that file with this one:

    #!/bin/sh
    echo "Adding 40_custom." >&2
    exec tail -n +4 $0
    # This file provides an easy way to add custom menu entries.  Simply type the
    # menu entries you want to add after this comment.  Be careful not to change
    # the 'exec tail' line above.
    
    menuentry "Kubuntu ISO" {
            set isofile="/boot/iso/kubuntu-12.04.iso"
            loopback loop (hd0,8)$isofile
            linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile noprompt noeject
            initrd (loop)/casper/initrd.lz
    }
    

    Where is:

    • Kubuntu ISO = The name you want to display in the Grub menu.
    • boot/iso/kubuntu-12.04.iso = The location and name of your ISO image.
    • (hd0,8) = The partition which contains the ISO image.
    • note: the tail -n +4 means simply "which line grub starts to read the configuration from as is". the 4th line in this example is the first comment line, which is fine.

    Grub reads the partitions in a different way than Ubuntu does. 'hd0' means first HDD which is read as sda by Ubuntu, and 8 is the partition which is the same as for Ubuntu. So in other words, (hd0,8) means 'sda8'.

    To find out your partition, run this command in a Terminal:

    sudo fdisk -l
    

    Suppose your image is contained in the sda1 partition, you'd change (hd0,8) in the above line to (hd0,1) and if the image is in the sdb1 partition, you'd change (hd0,8) to (hd1,1).

  3. Save and close this file and now run this command:

    sudo update-grub
    
  4. Reboot and choose the new ISO entry from Grub menu this time. Hopefully, it will boot successfully to the desktop.

    Now, if you want to perform an installation from the same ISO, you'd need to unmount the ISO image first as it is mounted from a partition on your HDD (probably you've got a single HDD and want to install Ubuntu to the same HDD) and the installer needs to unmount any mounted partitions before it can make any changes.

    So, from the Live CD environment, go to a Terminal and run:

    sudo umount -l /isodevice
    

That's all. Now you can double-click the 'Install' icon at the desktop and proceed with the installation.

More info see Ubuntu Forums 1, and Ubuntu Forums 2

Source:TuxGarage

olfek
  • 1,305
Mitch
  • 107,631
  • 8
    Using this to load Ubuntu 14.04 (64-bit), I had to change the kernel on line 4 of the menuentry to vmlinuz.efi, hence it became:linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=$isofile noprompt noeject – nanyaks May 09 '14 at 05:48
  • 1
    I submitted a fix to above. tail -n +3 causes error. It's what used in normal 40_custom file, but this example has an additional echo line. It should be +4 in this case. – Timo Jyrinki Sep 23 '14 at 16:32
  • 2
    @nanyaks Thank you, I also had to add .efi to the command (Ubuntu 14.10, 64-bit). Somehow grep referred to the partition I needed as (hd0,msdos7), while within the old Ubuntu install it was called sdb7. – Dennis van der Schagt Mar 03 '15 at 13:24
  • Is it necessary to put iso under /boot or can we point the path of iso from other drive e.g. if iso file is at /dev/sda4/xyz.iso then can I use set isofile="/xzy.iso" and (hd0,4) or I've to put under /boot ? – Pandya Dec 21 '15 at 13:36
  • I haven't tried that, but I think it might work, as long as the path is correct. – Mitch Dec 21 '15 at 13:43
  • How can I boot Debian 8? I see the error message that can not find /casper/vmlinuz or /casper/vmlinuz.efi – Vitaly Zdanevich Jan 04 '16 at 20:01
  • Hmm, I tried Ubuntu 15.10 x64 but I see the same error that /casper/vmlinuz or /casper/vmlinuz.efi can not be found.. – Vitaly Zdanevich Jan 04 '16 at 20:46
  • @VitalyZdanevich Make sure that the file exist under the folder /casper – Mitch Jan 04 '16 at 21:25
  • 1
    @Mitch Thank you, I found grub-imageboot - the simplest solution to boot from iso – Vitaly Zdanevich Jan 04 '16 at 21:29
  • Hello, to see in which partition the image is contained you need the command sudo mount and see where is mounted the root/ or sudo mount | grep ' / ' (with spaces before and after the dash) – Iacchus Jan 28 '16 at 03:13
  • This method works, but I have one question. – olfek Mar 26 '16 at 12:39
  • @sudoman What is your question? – Mitch Mar 26 '16 at 12:41
  • If an OS is being installed to the same drive that the ISO file is on, how can installation successfully complete because during the installation process the drive (which has the installation media on it) is formatted? – olfek Mar 26 '16 at 12:50
  • It has to be a separate partition. – Mitch Mar 26 '16 at 12:55
  • @Mitch I actually used a separate partition when using this method, but paragraph 2 on step 4 is a little unclear so I was just curious. – olfek Mar 26 '16 at 13:00
  • @Mitch "It has to be a separate partition." Paragraph 3 also contradicts this. – olfek Mar 28 '16 at 14:34
  • I'm a bit late to the party but this answer led me to the right path. You CAN use a partition with an O/S already installed on it as long as the ISO you are booting contains a module for the file system the partion is using. As example, to boot an ISO located at the root of a Windows partition, you need to add "insmod part_msdos" and "insmod ntfs" before the "set isofile" line. Similarly you can use an existing ext2 partition with "insmod ext2". Beware that you CANNOT resize a partition containing the live ISO (which is why I needed to do this). – Xenhat Nov 15 '16 at 17:19
  • I'm getting error invalid sector size 65535 when trying to boot for the iso file. – chfast Apr 11 '17 at 12:34
  • This worked for me, i've installed ubuntu 16.04.3 from debian 9 in a lenovo laptop: https://rizaumami.github.io/2017/04/27/boot-iso-menggunakan-grub/ – luistm Aug 04 '17 at 10:40
  • casper/vmlinuz can not be found. I check file file exists under iso. But iso is not mounted when I rebbot. For me /dev/sda6 mounts as /mnt/hitachi/data. But it should not matter if mount path is different in fstab for existing system The iso file is under Downloads folder. So all I did was isofile="/Downloads/cosmic.iso". But it did not work? – Khurshid Alam Sep 24 '18 at 17:13
  • It worked after I change it to initrd from initrd.lz. They changed the naming again. :( – Khurshid Alam Sep 24 '18 at 17:56
6

I couldn't use Mitch's answer because I wanted to install Ubuntu minimal and the ISO image didn't work (there's no vmlinuz). So instead I adapted it to use NetbootInstall

I put the downloaded files into /boot/netboot and added the following entry to the grub config:

menuentry "NetInstall" {
    linux (hd0,0)/boot/netboot/ubuntu-installer/amd64/linux
    initrd (hd0,0)/boot/netboot/ubuntu-installer/amd64/initrd.gz
}

I then forgot to run

sudo update-grub

so I had to boot into the existing linux once again, but having finally updated grub the actual installation went very smoothly.

z7sg
  • 593
  • I tried your method for netboot, but it didn't work. Where is the iso? All you are doing is adding kernel and initrd. Can you explain a bit more ? – Khurshid Alam Jan 06 '19 at 07:18