Page 1 of 1

Installing GRUB2 in an image file

Posted: Sat Sep 05, 2009 3:45 pm
by gamiel
I have an image file, hdd.img, created as follows:

Code: Select all

$ dd if=/dev/zero of=hdd.img bs=512 count=16065
$ losetup /dev/loop/0 hdd.img
# create a single pirmary parition with fdisk
$ fdisk /dev/loop/0
$ losetup -o <offset in file to the partition> /dev/loop/1 /dev/loop/0
$ mke2fs /dev/loop/1
How do I install GRUB2 on hdd.img. In the past, I have install GRUB Legacy on a similar file, simply by running:

Code: Select all

$ grub --device-map=/dev/null
and then entering

Code: Select all

device (hd0) hdd.img
root (hd0,0)
setup (hd0)
I have tried playing around with grub-install without any success. Can anyone please give me some pointers on how to install GRUB2.

Thanks,
gamiel

Re: Installing GRUB2 in an image file

Posted: Sat Sep 05, 2009 4:21 pm
by f2
Look at GRUB2 Manual on GRUB Wiki.
On the future, please search before posting... and remember, Google is your friend!

EDIT: How to install Grub 2 on Ubuntu 9.04 : may be useful...

Re: Installing GRUB2 in an image file

Posted: Sun Sep 06, 2009 8:10 am
by torshie
In my experience, grub2 isn't as easy as grub-legacy to install. It can be only installed on a physical drive not a disk image, the installer is still buggy, sometimes you even cannot install it on a secondary hard drive.
You can try qemu and mount the disk image as a secondary hard drive.
losetup -o <offset in file to the partition> /dev/loop/1 /dev/loop/0
If you disk image contains more than one partition the offset is not so easy to find out. You should use kpartx instead

Re: Installing GRUB2 in an image file

Posted: Sun Sep 06, 2009 8:25 am
by gamiel
torshie, thanks for the reply.

As you said, I guess I can boot a linux live CD in a virtual machine and attache the image file as the primary hard drive and install GRUB2 that way. But still this is not the solution I am looking for. I want to install GRUB2 in a similar fashion as i install GRUB Legacy (see OP), if possible.
If you disk image contains more than one partition the offset is not so easy to find out. You should use kpartx instead
You can find the offsets with $ fdisk -lu /dev/loop/0, I was just to lacy to write it in the OP.

Thanks,
gamiel