Instead of figuring out the offset and mounting that way, why not use kpartx to figure out where all of the partitions are.
You would run:
losetup –f to determine an available loop device
losetup /dev/loopX <path to image> to do the loop setup where X is the loop device number
kpartx –a /dev/loopX to read the partition table and create entries in /dev/mapper
mount /dev/mapper/loopXpY where Y is the partition number
If you really want to get the proper offset and are running in Python do
Import parted
pdev = parted.device.Device(<path to image>)
pdisk = parted.disk.Disk(pdev)
offset = pdisk.partitions[<partition num> - 1].geometry.start * pdisk.device.sectorSize # Need to multiply by the sectorSize as parted gives the offset in sectors not bytes.
If you are doing this from bash run:
parted <path to image> -s unit b print | grep “^\s*<partition number>” | awk ‘{print $2 }’ | tr –d ‘B’
This prints out the partition table with all the units in bytes, then selects the line with the proper partition number, selects the starting offset entry and removes the trailing B from the number. This will leave you with just the byte offset which you can then give to the mount command to mount the partition.
Matt Keeler
From: xen-users-bounces@xxxxxxxxxxxxx [mailto:xen-users-bounces@xxxxxxxxxxxxx] On Behalf Of Scott Meyers
Sent: Saturday, April 14, 2012 2:15 PM
To: Xen Users List
Subject: [Xen-users] Mounting *.img file
I am trying to figure out the offest sector for vm01.img2 partition. I tried many possible ways, for example (1020 *512) , but in vain. I just can't hit the right offest sector to mount vm01.img2 partition. Any help is greatly appreciated.
BTW, I am using xen-3.0.3-135.el5_8.2. The vm01.img file was created using virt-install command. The command I am using to mount the partition in that img file is: mount -o loop,offset=130048 -t ext3 /vm/vm01.img /mnt/xenimg
#fdisk -l /vm/vm01.img
vice Boot Start End Blocks Id System
/vm/vm01.img1 * 1 64 514048+ 83 Linux
/vm/vm01.img2 65 1020 7679070 83 Linux
/vm/vm01.img3 1021 1657 5116702+ 83 Linux
Partition 3 has different physical/logical endings:
phys=(1023, 254, 63) logical=(1656, 254, 63)
/vm/vm01.img4 1658 3916 18145417+ 5 Extended
Partition 4 has different physical/logical beginnings (non-Linux?):
phys=(1023, 254, 63) logical=(1657, 0, 1)
Partition 4 has different physical/logical endings:
phys=(1023, 254, 63) logical=(3915, 254, 63)
/vm/vm01.img5 1658 1784 1020096 83 Linux
/vm/vm01.img6 1785 1848 514048+ 82 Linux swap / Solaris
/vm/vm01.img7 1849 3916 16611178+ 83 Linux