DISKIMAGE for qemu HOWTO
Shuwei Bai <[email protected]>
The document showes how to build a disk image for qemu. I don't know whether it will work for all the platform, but I hope so :). I will be glad if it can help you. The document doesn't only show the instructions, but also display the output result in author's host. Before you build the disk image, you should build a file system, kernel image, and install dd, fdisk, mount, losetup, mkfs.ext2 tools.
The process build the disk image is divided into six steps. 1) create the disk; 2) partition the disk; 3) format the partition; 4) copy the file system; 5) build the boot loader by grub; 6) boot the system with qemu. The instructions used in each step are showed bellow.
baishuwei:xtratum# dd if=/dev/zero of=disk.img bs=4096 count=16384
// 64M = bs*count
baishuwei:xtratum# fdisk -C 16065 -H 255 -S 63 disk.img
// see man fdisk for -C, -H, -S
baishuwei:xtratum# losetup -o 32256 /dev/loop0 disk.img
// 32256 = 63*512. The precious blocks are used for grub.
baishuwei:xtratum# mkfs.ext2 -m 0 /dev/loop0
baishuwei:xtratum# mount /dev/loop0 fs/
baishuwei:xtratum# cp -a ../root/* fs/
baishuwei:xtratum# umount fs/
baishuwei:xtratum# grub --device-map=/dev/null
baishuwei:xtratum#qemu -hda disk.img
The detail results are showed bellow.
(1) Create the RAM disk whose size is 64M.
baishuwei:xtratum# dd if=/dev/zero of=disk.img bs=4096 count=16384
16384+0 records in
16384+0 records out
67108864 bytes (67 MB) copied, 0.175206 s, 383 MB/s
(2) Part the disk.
baishuwei:xtratum# fdisk -C 16065 -H 255 -S 63 disk.img
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x275374b9.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
The number of cylinders for this disk is set to 16065.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): p
Disk disk.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 16065 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x275374b9
Device Boot Start End Blocks Id System
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-16065, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-16065, default 16065):
Using default value 16065
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 25: Inappropriate ioctl for device.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
baishuwei:xtratum# fdisk -ul disk.img
You must set cylinders.
You can do this from the extra functions menu.
Disk disk.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x275374b9
Device Boot Start End Blocks Id System
disk.img1 63 258084224 129042081 83 Linux
Partition 1 has different physical/logical endings:
phys=(1023, 254, 63) logical=(16064, 254, 63)
(3) Format the filesystem.
baishuwei:xtratum# losetup -o 32256 /dev/loop0 disk.img
baishuwei:xtratum# mkfs.ext2 -m 0 /dev/loop0
mke2fs 1.41.3 (12-Oct-2008)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
16384 inodes, 65504 blocks
0 blocks (0.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67108864
8 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
(4) Copy file system.
baishuwei:xtratum# mount /dev/loop0 fs/
baishuwei:xtratum# ls fs/
lost+found
baishuwei:xtratum# cp -a ../root/* fs/
baishuwei:xtratum# ls fs/
bin boot dev etc home init lib linuxrc lost+found mnt opt proc root sbin sys tmp usr var
(5) Build the bootloader by grub
baishuwei:xtratum# cat fs/boot/grub/menu.lst
title LInux
root (hd0,0)
kernel /boot/bzImage root=/dev/hda1
baishuwei:xtratum# umount fs/
baishuwei:xtratum# grub --device-map=/dev/null
GNU GRUB version 0.97 (640K lower / 3072K upper memory)
[ Minimal BASH-like line editing is supported. For
the first word, TAB lists possible command
completions. Anywhere else TAB lists the possible
completions of a device/filename. ]
grub> device (hd0) disk.img
device (hd0) disk.img
grub> root (hd0,0)
root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
setup (hd0)
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/e2fs_stage1_5" exists... yes
Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... 15 sectors are embedded.
succeeded
Running "install /boot/grub/stage1 (hd0) (hd0)1+15 p (hd0,0)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded
Done.
grub> quit
quit
baishuwei:xtratum# file disk.img
disk.img: x86 boot sector; GRand Unified Bootloader, stage1 version 0x3, stage2 address 0x2000, stage2 segment 0x200; partition 1: ID=0x83, starthead 1, startsector 63, 258084162 sectors
(6) Boot system with your new disk.
baishuwei:xtratum#qemu -hda disk.img