Booting an EXT4 image file from GRUB2

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