Davide | 6 May 11:26 2011

qemu raw image partition offset calculation

It's often handy to know the physical offset of the first partition of a qemu raw disk image.
If you do not use raw images you can convert it to raw ... do what you need and convert it back.
I've not yet found a reliable way of mounting non raw images from ordinary linux OS.
You can use this trick to populate a qemu disk image with an armedslack miniroot sistem image directky from
your linux x86 box.

Unfortunatelly the offset depends on the geometry of the disk image.
Here's how I guess the correct offset:

the firs block of the firs partition is located in:
the first sector of the second track
it will be located in <sectors per track> * 512

fdisk -l qemu_hdu.raw
Disk qemu_hdu.raw: 0 MB, 0 bytes
5 heads, 54 sectors/track, 0 cylinders
Units = cylinders of 270 * 512 = 138240 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

       Device Boot      Start         End      Blocks   Id  System
qemu_hdu.raw1               1        1016      137133   83  Linux

the firs partition of this image (qemu_hdu.raw1) will begin:
(54 * 512) = 27648

Ho do you go about creating the filesystem from the x86 linux box:

What folows creates an ext2 filesystem on the first partition of the 
qemu raw disk image and subsequently mount is so that you can polulate it.

# losetup -o <previously calculated offset> /dev/loop0  qemu_hdu.raw
# mke2fs -b 4096 -i 16384 -m 1 -L surap_root /dev/loop0
# mount /dev/loop0 /mnt/tmp/

Hope this helps