Installing GRUB on a Hard Disk Image File
来源:互联网 发布:日照市软件研发工程师 编辑:程序博客网 时间:2024/04/29 12:07
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, Bochsand 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 -u -l /dev/loop1
Disk disk.img: 64 MB, 64512000 bytes
16 heads, 63 sectors/track, 125 cylinders, total 126000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xaf9b849f
Device Boot Start End Blocks Id System
disk.img1 * 2048 125999 61976 83 Linux
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 1048576 /dev/loop2 /dev/loop1mkfs /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/loop1mkfs /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/grubcp -r /boot/grub/stage1 /boot/grub/stage2 /boot/grub/menu.lst mnt/hdd/boot/grubcp -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 0timeout 10
title=LonnyOSroot (hd0,0)kernel /boot/linuz
Now install GRUB for real.
grub --device-map=/dev/null
This will open up a GRUB environment. Enter the following:
device (hd0) mnt/hdd.imgroot (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.imgmount /dev/loop0 mnt/hddchown -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.imgbochs -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.
- Installing GRUB on a Hard Disk Image File
- Installing GRUB on a Hard Disk Image File
- Installing GRUB on a Hard Disk Image File
- Installing GRUB on a Hard Disk Image File
- Getting grub into a disk image
- How to Configure Hard Disk Drives on a CEPC
- Accessing the contents of a KVM disk image file
- The APK file does not exist on disk.Error while Installing APK
- The APK file does not exist on disk.Error while Installing APK
- Launching app : The APK file xxxx.apk does not exist on disk Error while Installing APK
- The APK file xxx.apk does not exist on disk. Error while Installing APK
- The APK file xxx.apk does not exist on disk. Error while Installing APK
- the apk file does not exist on disk. error while installing apk
- Speed Up your Hard Disk on Linux
- Android 错误(The APK file *.apk does not exist on disk Error while Installing APK)解决方法
- Debug 切换 Release 编译i The APK file does not exist on disk.Error while Installing APK
- a read-only disk image
- What Is a SATA Hard Disk?
- android 的MIME类型
- Spring3 MVC 深入研究
- eclipse 背景颜色修改插件 color theme
- java.lang.IllegalArgumentException: The char 'xxx ' in 'xxx'
- 失效的程序单元的重新编译
- Installing GRUB on a Hard Disk Image File
- 使用clonezilla做Linux备份及还原出现的问题及解决方法
- 服务器配置apr-apache记事
- 【第七章】 对JDBC的支持 之 7.4 Spring提供的其它帮助 ——跟我学spring3【私塾在线原创】
- Android task和back stack详解五:任务亲和力及其它
- DataGridView保存于修改
- 关于oracle内存参数设置有关的视图
- ANSYS12分布式计算笔记
- 函数中结构体的传递