Installing GRUB on a Hard Disk Image File

Citation: brho, Installing GRUB on a Hard Disk Image File, OmniNerd.com, 24 January 2009, accessed on 01 September 2014 from http://www.omninerd.com/articles/Installing_GRUB_on_a_Hard_Disk_Image_File
Tags: grub, virtualization, computers, and disk images

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

Information This article was edited after publication by the author on 27 Jul 2012. View changes.