6

My friend needed a fast HDD so I gave her my small 64GB SDD. This SSD had my Linux install on it. I used dd to make an image of the partition (boot, root and home on one partition).

This partition is now sitting on a traditional 500GB EXT4 formatted drive.

Is there any way I can get GRUB to just boot using this .img file I have? I'm not getting my SSD back and I can't be bothered to go through the hassle of setting up my Linux install from scratch. I have come across loopback support in GRUB for ISO images. Does this support EXT4 also? I don't seem to be able to find anything specific and don't want to trash anything.

Cheers.

2
  • 1
    I highly doubt you have an *ISO*(9660) image. Also note that even if you can boot the kernel with grub, the initrd has no idea about your new (and nested) layout. The shortest solution is to replay the disk image file back onto a (raw) disk, i.e. resolving the loop indirection. Commented Apr 13, 2012 at 14:01
  • Yeah I thought as much. I expect booting from the image would cause all kinds of problems with GRUB and kernel updates anyway. Thanks for replying. Commented Apr 14, 2012 at 12:01

1 Answer 1

3

even if file contains partition table grub2 can meanwhile boot from, where (hd0,1) is the location of file and (loop,1) is partition within file. however, this will only boot the initramfs, the file is not really mounted.

/etc/grub.d/40_custom

menuentry "My bootable disk image" { set isofile="hdd_ext4.img" loopback loop (hd0,1)/${isofile} linux (loop,1)/boot/vmlinuz-3.16.0-4-amd64 root=/dev/sda1 loop=/${isofile} ro initrd (loop,1)/boot/initrd.img-3.16.0-4-amd64 } 

write your own mount script, chmod a+x and copy into local-premount folder. use initramfs-tools to create your own "initrd.img-3.16.0-4-amd64" and copy it inside the image file. no need to hard code just use the vars from grub entry ${ROOT} and ${loop} inside the script.

/etc/initramfs-tools/scripts/local-premount

#!/bin/sh modprobe loop modprobe ext4 # mount /dev/sda1 (file location) mkdir /host mount -n -t ext4 -o rw,data=ordered ${ROOT} /host # kpartx add partition devmappings for hdd_ext4.img loop_pt=$(kpartx -av /host${loop} 2>&1 | grep loop | cut -f3 -d" ") # mount hdd_ext4.img (image file) mount -n -t ext4 -o loop,rw,data=ordered /dev/mapper/${loop_pt} ${rootmnt} 

Note: this will only work if kpartx is installed in initramfs

1
  • grep loop0p1 from output is not always working, this is more exactly... replace: grep loop | cut -f3 -d" " with: grep -owE '(loop[0-9]+p[0-9]+)' | head -n1 Commented Mar 8, 2018 at 19:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.