Installing GRUB on a Hard Disk Image File
2009-02-15 21:12
417 查看
quoted from
http://www.omninerd.com/articles/Installing_GRUB_on_a_Hard_Disk_Image_File
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.
[/code]
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.
[/code]
Fdisk the image, just like it’s a regular block device. The easiest thing is to just create one Linux primary partition.
[/code]
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.
[/code]
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.
[/code]
If you have multiple partitions, you will need to tell
[/code]
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.
[/code]
[/code]
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.
[/code]
Now install GRUB for real.
[/code]
This will open up a GRUB environment. Enter the following:
[/code]
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).
[/code]
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.
[/code]
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.
[/code]
http://www.omninerd.com/articles/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
[/code]
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
[/code]
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
[/code]
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
[/code]
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
[/code]
If you have multiple partitions, you will need to tell
losetuphow far to go into the target with the
--sizelimitflag. 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
[/code]
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/
[/code]
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/
[/code]
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
[/code]
Now install GRUB for real.
grub --device-map=/dev/null
[/code]
This will open up a GRUB environment. Enter the following:
device (hd0) mnt/hdd.img root (hd0,0) setup (hd0)
[/code]
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 ranlosetup -ato 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
[/code]
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'
[/code]
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
[/code]
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
- The APK file does not exist on disk.Error while Installing APK
- Debug 切换 Release 编译i The APK file 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
- The APK file does not exist on disk.Error while Installing APK
- The APK file xxx.apk does not exist on disk. Error while Installing APK
- Android 错误(The APK file *.apk does not exist on disk Error while Installing APK)解决方法
- 在ghost还原系统的时候,选择了harddisk from image 丢失数据恢复
- Create Linux Loopback File System On Disk File
- Error while Installing APK:apk does not exist on disk.
- Move the datafile to release the diskspace on the drive
- FRM-41839: Disk I/O error on temporary record buffer file /tmp/...TMP
- 解决错误:NAND: could not create temp file for system NAND disk image: No such file or directory
- How to Configure Hard Disk Drives on a CEPC
- How ASM instance reads initiator parameter file on ASM disk when startup?
- Android Studio The APK file does not exist on disk
- The APK file does not exist on disk