Loading 9 Votes - +

Installing GRUB on a Hard Disk Image File

Introduction

GRUB is the GRand Unified Bootloader. For those unfamiliar, a bootloader is a critical piece of software used when a computer turns on. Its job is to load an operating system. The bootloader resides on a disk of some sort (floppy, hard) and is called by the BIOS, which is the real low-level program that runs on startup. GRUB is installed at a specific location on these devices.

Image files are byte-for-byte representations of block devices, such as a hard disk or floppy disk. They can be used for a variety of things, such as backup, offline investigation, or virtual machines. I was using the latter (specifically, Bochs and KVM). These programs act like a computer within a computer, and take an image file to be the disk for the virtual machine.

How do these come together? When building an image file from scratch, I wanted to install GRUB on it. There are a lot of resources about how to install GRUB on a floppy image or on a real HDD, but not on a HDD image. Perhaps this info is already out there, but since I didn’t find it, I figured I’d tell you all what klueska and I came up with.

Details

The following was done on a regular Linux box. Use root/sudo when you need to. Normally, I’ll explain what I’ll do, and then show the commands I used to do it.

Creating the Image

This will create an 8MB image. Use whatever size you want. These values will result in one cylinder (based on the old sector/cylinder/head sizing). Fdisk complained on smaller images, so YMMV. I have a folder called mnt/ in my current directory, which is where I store both the images and the image mount point.


dd if=/dev/zero of=mnt/hdd.img bs=512 count=16065

This creates a loopback device that connects to the image file. This helps some utilities that are expecting to work with a device instead of a file. For instance, fdisk was a little happier with this.


losetup /dev/loop1 mnt/hdd.img

Fdisk the image, just like it’s a regular block device. The easiest thing is to just create one Linux primary partition.


fdisk /dev/loop1

Okay, enough with the slow stuff; it’s time to put your “daddy pants” on. Here’s the problem. You need to make a file system on that partition, but you do not have a device that points to just that partition, like you would for a normal hard drive. You only have the loopback device, which points to the whole image. It’s like you only have /dev/hda, and not /dev/hda1. What to do? Create another loopback device, and have the device start from a certain offset within the image.

This determines the offset, in sectors, for the beginning of the partition. Then multiply the sector offset by 512, since losetup offsets by bytes.


fdisk -ul /dev/loop1

This will have us point loop2 to the partition on the disk (if there is only one partition), as explained in more detail here. Then we simply make a filesystem on it.


losetup -o 32256 /dev/loop2 /dev/loop1
mkfs /dev/loop2

If you have multiple partitions, you will need to tell losetup how far to go into the target with the --sizelimit flag. By default, it will map all the way to the end of the target, which will clobber any following partitions. To figure out the size, look at the Blocks column output of fdisk -ul, and multiply that by 1024. Thanks to Anonymous for pointing this out in the comments below. Here’s an example (for about 400 cylinders):


losetup -o 32256 --sizelimit 3290079232 /dev/loop2 /dev/loop1
mkfs /dev/loop2

This will mount it so you can examine it and so you’re ready to install grub. Make sure you have mkdir’d mnt/hdd, yada yada yada.


mount /dev/loop2 mnt/hdd/

Installing GRUB

This creates the file structure on the image for GRUB and copies local copies of critical GRUB files to the appropriate folder. It also copies whatever kernel you want to load into the root of the image’s file system. Adjust files and paths to your liking.


mkdir -p mnt/hdd/boot/grub
cp -r /boot/grub/stage1 /boot/grub/stage2 /boot/grub/menu.lst mnt/hdd/boot/grub
cp -r the_kernel mnt/hdd/

Don’t forget to edit the menu.lst file (a grub.conf) to suit your kernel. Mine looked something like this. Look elsewhere for more guidance.


default 0
timeout 10

title=LonnyOS
root (hd0,0)
kernel /the_kernel

Now install GRUB for real.


grub --device-map=/dev/null

This will open up a GRUB environment. Enter the following:


device (hd0) mnt/hdd.img
root (hd0,0)
setup (hd0)

Note that we set the device to the actual image file, and not the loopback device. GRUB can work with either. Normally, utilities work better with the device, but due to a bug in GRUB, it was flipping out (Error 22) when given the loopback device. It worked fine when installing on the hdd.img. For more details, look elsewhere (my references and google are decent starting points).

Using the Image

You can easily mount the image and use it, even while using the image directly for a virtual machine. I ran losetup -a to see which loopback devices I had, then the -d flag to delete whichever I don’t need. Now I’ll loopback directly into the image, and mount it. And then make sure my regular user account has all the access necessary. Also, be careful of any necessary sizelimits to losetup, as mentioned above.


losetup -o 32256 /dev/loop0 mnt/hdd.img
mount /dev/loop0 mnt/hdd
chown -R brho:brho mnt/hdd

As I muck around with the_kernel, I can just cp it into mnt/hdd/, and quickly run KVM or Bochs to test the new kernel.


kvm mnt/hdd.img
bochs -q 'ata0-master: type=disk, mode=flat, path="./mnt/hdd.img", cylinders=1, heads=255, spt=63'

One thing to note: after you copy the_kernel to mnt/hdd, the hdd.img is not actually updated instantly. This is because filesystems do not immediately flush their changes to disk, and the write() syscall will return early. If you want to immediately run your VM with your new image, simply sync your disks to make sure all writes are flushed.


sync

Conclusion

Hopefully this has helped you. If you hose your system, you’re on your own / standard disclaimers apply. But if not, perhaps you are the happy owner of a fresh GRUB hdd image. More importantly, you should know how to make one and (roughly) how the process worked.

References

http://www.linuxjournal.com/article/4622
http://sig9.com/bochs-grub
http://web2.clarkson.edu/projects/itl/honeypot/ddtutorial.txt
http://www.mail-archive.com/bug-grub@gnu.org/msg09648.html

Similarly tagged OmniNerd content:

Information This article was edited after publication by the author on 27 Jul 2012. View changes.
Thread parent sort order:
Thread verbosity:
0 Votes  - +
Of course ... by VnutZ

… if you were a true sadist programmer, you would write your own bootloader – PC Bootstrap Loader Programming Tutorial in ASM

Thx for great article.
I had one problem when I tried install Gentoo to this image.
I mouted partitions this way.

losetup -o 32256 /dev/loop2 /dev/loop1
mkfs /dev/loop2

and I got wrong partition size (size was the same like whole image file) when I mount it.

I used this to fix wrong partition size:
losetup -o 32256 —sizelimit 3232502272 /dev/loop2 /dev/loop1.
mkfs /dev/loop2

After that everything seems to be ok.

0 Votes  - +
nice article by Anonymous

a simpler way to create the device nodes for the created partitions is:

kpartx -av /dev/loop1

best regards
jochen

after searching the interweb i finally found a grub on an img file tutorial,
great job of explaining it, now i think i have a good starting point for putting grub on my evo t20 .bin file

-justin
www.justinstech.org

Great tutorial. I decided o use most of the information here to write the following tutorial:
http://augustopedroza.wordpress.com/2010/02/22/straight-to-the-operating-system-main-using-grub-and-bochskvm/

I just took one step further and described how someone can use your tutorial to boot a kernel with a simple main.

Augusto Pedroza

No need for losetup offset gymnastics. Linux supports partitions for loop devices, but it is disabled by default. To turn it on, do the following:

modprobe loop max_part=10

Or if your loop device is compiled into the kernel, add the kernel parameter “loop.max_part=10”

Once you partition loop1, you will see the following devices:

/dev/loop1
/dev/loop1p1
/dev/loop1p2
..etc

Also, when creating an empty file you can do it much more quickly, and with almost nil disk space like this:

dd if=/dev/zero of=myimage bs=512 count=1 seek=16064

The image will behave similarly to a dynamic disk that you see in VM products; space is only taken up on disk when you write to it.

Cheers,
Dan.

0 Votes  - +
nice write up by Anonymous

However, you should emphasize that the originating system should be the same OS as what you are operating on inside the image file. For example if you have CentOS6 running natively and you are trying to repair a CentOS5 system, you shouldn’t mix up GRUB versions.

0 Votes  - +
Great Article by Anonymous

I’ve been stuck for ages trying to install grub on a image file.
chroot and losetup has failed everytime.
However, this little approach worked a treat!

Well done!

Share & Socialize

What is OmniNerd?

Omninerd_icon Welcome! OmniNerd's content is generated by nerds like you. Learn more.

Voting Booth

Dzhokar Tsarnaev deserves due process?

33 votes, 4 comments