Installing GRUB2 in an image file

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
gamiel
Posts: 2
Joined: Sat Sep 05, 2009 3:27 pm

Installing GRUB2 in an image file

Post 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
User avatar
f2
Member
Member
Posts: 311
Joined: Mon Jun 15, 2009 10:01 am
Location: France

Re: Installing GRUB2 in an image file

Post 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...
"Open source seems to embrace the dark side of human nature." - Ville Turjanmaa
torshie
Member
Member
Posts: 89
Joined: Sun Jan 11, 2009 7:41 pm

Re: Installing GRUB2 in an image file

Post 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
gamiel
Posts: 2
Joined: Sat Sep 05, 2009 3:27 pm

Re: Installing GRUB2 in an image file

Post 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
Post Reply