Daniel, I need to test this code before I include it. Not being a Qemu user, and being lazy, what are the options, I assume, to qemu-img to make partitions on .img file?
The qemu-img app cannot create partitions, only the image file. I typically create the partitions and format them within qemu, but you can use fdisk like so:
# fdisk -C $(( $(stat -c %s tcvd.img) / 255 / 63 / 512 )) tcvd.img
My partition table looks like this:
Disk tcvd.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 114 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
tcvd.img1 1 38 305203+ 83 Linux
tcvd.img2 39 76 305235 83 Linux
tcvd.img3 77 114 305235 83 Linux
The next question is, how do you format a partition? Outside of qemu, you can use losetup. First, display the partition table with sectors:
# fdisk -l -u -C $(( $(stat -c %s tcvd.img) / 512 )) tcvd.img
Disk tcvd.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 1843200 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Device Boot Start End Blocks Id System
tcvd.img1 63 610469 305203+ 83 Linux
tcvd.img2 610470 1220939 305235 83 Linux
tcvd.img3 1220940 1831409 305235 83 Linux
Then use a few calculations to setup the loop device:
# OFFSET=$((512 * $START))
# SIZE=$((512 * $END - $OFFSET))
# losetup --offset $OFFSET --sizelimit $SIZE --find tcvd.img
where $START and $END are the start and end columns for a particular partition, like 610470 and 1220939 for the second partition. Use mke2fs on the loop device like a normal partition.
EDIT: Corrected error