How to Make a Bochs Disk Image

來源:互聯網
上載者:User

 

http://www.julienlecomte.net/blog/2007/07/3/

Published by Julien Lecomte at 8:57 pm under System Programmin

 

In this article, I try to synthesize several (incomplete or inaccurate) articles I’ve found on the Internet to guide you through the process of creating a disk image you may use with the Bochs emulator. The steps described below are intended for a GNU/Linux system and some of them require super user privileges. Also, you need to have GRUB installed on your system.

1. Create a disk image. Here, I create a file named disk.img, containing 10080 blocks (each block being 512 bytes, this will create a file that’s about 5MB)

$ dd if=/dev/zero of=disk.img count=10080

2. Use FDISK to create a partition table on the image file:

$ fdisk disk.img   x     -> Extra functionality   c 10  -> 10 cylinders   h 16  -> 16 heads   s 63  -> 63 sectors per track   r     -> Return to main menu   n     -> Create a new partition   p     -> Primary   1     -> Partition #1   1     -> First cylinder   10    -> Last cylinder   a     -> Set bootable flag   1     -> Partition number   w     -> Write partition to disk

Note that you have to tell fdisk about the geometry of your disk, and that geometry has to match the size of the disk you created in step 1 (10 cylinders * 16 heads * 63 sectors per track = 10080 blocks). For an introduction on disk geometry, cylinders, heads and sectors, read this Wikipedia article.

Let’s take a quick look at the Partition Table in the Master Boot Record (See this Wikipedia article for a description of the structure of the partition table)

$ hexdump disk.img

The 16 bytes at offset 446 (0×1BE) are:

0180 0001 0f83 093f 003f 0000 2721 0000

Keep in mind that these values are stored using little-endian convention (see this Wikipedia article to find out more about the meaning of endianness in computer science). Also see this article to find out how the CHS values are computed.

Offset Description
0×00 0×80 means that this partition is bootable
0×01 0×000101 is the CHS address of the first sector in the partition:
S = 0×01 = 1
H = 0×01 = 1
C = 0×00 = 0
0×04 0×83 is the type of the partition (Linux native here)
0×05 0×093f0f is the CHS address of the last sector in the partition:
S = 0×3F = 63
H = 0×0F = 15
C = 0×09 = 9
0×08 0×0000003f (63) is the logical block address of the first sector in the partition
0×0C 0×00002721 (10017 = 10080 – 63) is the size of the partition, in number of 512 byte blocks

3. Setup the loopback device. In order to do this, you need to calculate the offset (in bytes) of the first sector of your single partition. Use the following command:

$ fdisk -l -u disk.img  Device Boot      Start         End      Blocks   Id  Systemdisk.img1   *          63       10079        5008+  83  Linux

This tells us that our single partition starts at the 63rd block. Hence our offset is 63 * 512 = 32256.

Finally, type:

$ losetup -o 32256 /dev/loop0 disk.img

4. Format the disk (EXT2FS)

$ mkfs.ext2 /dev/loop0

5. Mount the disk:

$ mount -o loop /dev/loop0 /mnt

6. Now, let’s install GRUB. Start by copying the necessary GRUB files:

$ mkdir -p /mnt/boot/grub$ cp /boot/grub/stage1 /boot/grub/stage2 /mnt/boot/grub/$ vi /mnt/boot/grub/grub.conf   title=MyKernel   root (hd0,0)   kernel /mykernel

7. Unmount the device:

$ umount /mnt

8. Detach the loopback device:

$ losetup -d /dev/loop0

9. Finish up the GRUB installation:

$ grub --device-map=/dev/null   device (hd0) disk.img   geometry (hd0) 10 16 63   root (hd0,0)   setup (hd0)   quit

10. Setup your .bochsrc file in the same directory as your disk image:

megs: 32romimage: file=/usr/local/share/bochs/BIOS-bochs-latest, address=0xf0000vgaromimage: file=/usr/local/share/bochs/VGABIOS-elpin-2.40ata0-master: type=disk, path="disk.img", mode=flat, cylinders=10, heads=16, spt=63cpu: count=1, ips=15000000mouse: enabled=0log: out.bochsboot: disk

That’s it. Start Bochs and the GRUB interface should appear

 

 

 

------------------------------------------------------------------------------------------------------------------------------------------

Bloger's Comment:

 

Sometimes, it will fail to setup grub at setup(hd0). For example, I make a 128M disk image, and I use 256*16*63=258048 to calculate the blocks, and apparently the Cylinder, heads and sector should be 256, 16 and 63 respectively, if so, the error will happen that grub complain /boot/grub or /grub not exists.

I don't the reason, but I found a way to resolve this problem by using bximge (disk image create tool along with Bochs) to create disk image 128M and then fdisk it, and you will see the cylinder is 260!!! Why? I don't know, but it does work!

 

Create VMware disk

qemu-img convert -f raw xxx.img -O vmdk xxx.vmdk

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.