Linux: Multiple ways to mount a partition in a disk image file

Hi,

there are several ways to mount a partition which is within a diskimage file created for example with dd.

The first and hard way  is to determine the offset of the partition start sector and mounting the disk by specifing the offset as parameter for the loop device.

You can use the  file or fdisk command to examine the file.
root@ubdev:/mnt/RaspberryPI # file 2012-07-15-wheezy-raspbian.img
2012-07-15-wheezy-raspbian.img: x86 boot sector; partition 1: ID=0xc, starthead 130, startsector 8192, 114688 sectors; partition 2: ID=0x83, starthead 165, startsector 122880, 3665920 sectors, code offset 0xb8

or

root@ubdev:/mnt/RaspberryPI # fdisk 2012-07-15-wheezy-raspbian.img

Command (m for help): p

Disk 2012-07-15-wheezy-raspbian.img: 1939 MB, 1939865600 bytes
255 heads, 63 sectors/track, 235 cylinders, total 3788800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000714e9

Device Boot                               Start         End      Blocks   Id  System
2012-07-15-wheezy-raspbian.img1            8192      122879       57344    c  W95 FAT32 (LBA)
2012-07-15-wheezy-raspbian.img2          122880     3788799     1832960   83  Linux

For example the startsector of partition 2 is 122880,  this multiplied with the 512 bytes per sector is the byte offset of the partition. 122880*512=62914560.

mount -o loop,offset=62914560 2012-07-15-wheezy-raspbian.img /root/mnt/loop/

ls -l /root/mnt/loop/

total 92
drwxr-xr-x  2 root root  4096 Jul 15  2012 bin
drwxr-xr-x  2 root root  4096 Jul 15  2012 boot
drwxr-xr-x  5 root root  4096 Jul 15  2012 dev
drwxr-xr-x 87 root root  4096 Jul 16  2012 etc
drwxr-xr-x  3 root root  4096 Jul 15  2012 home
drwxr-xr-x 14 root root  4096 Jul 15  2012 lib
drwx------  2 root root 16384 Jul 15  2012 lost+found
drwxr-xr-x  2 root root  4096 Jul 15  2012 media
drwxr-xr-x  2 root root  4096 Jun  2  2012 mnt
drwxr-xr-x  3 root root  4096 Jul 15  2012 opt
drwxr-xr-x  2 root root  4096 Jun  2  2012 proc
drwx------  2 root root  4096 Jul 16  2012 root
drwxr-xr-x  6 root root  4096 Jul 15  2012 run
drwxr-xr-x  2 root root  4096 Jul 15  2012 sbin
drwxr-xr-x  2 root root  4096 Jun 20  2012 selinux
drwxr-xr-x  2 root root  4096 Jul 15  2012 srv
drwxr-xr-x  2 root root  4096 Apr 14  2012 sys
drwxrwxrwt  2 root root  4096 Jun  2  2012 tmp
drwxr-xr-x 10 root root  4096 Jul 15  2012 usr
drwxr-xr-x 11 root root  4096 Jul 15  2012 var



The 2nd way, if your kernel and the util-linux package supports it,  is to let the loop driver recognize the offset.

First step unload the loop modul
modprobe -r loop
and load it again with the parameter max_part=15

modprobe loop max_part=15

Note: If your kernel has a builtin loop module add the parameter loop.max_part=15 to your kernel command line(/boot/grub/grub.cfg):

linux   /boot/vmlinuz-3.8.0-31-generic root=UUID=b91540e1-fd13-466f-a16a-a0fea82595ea ro   splash quiet $vt_handoff loop.max_part=15

mount the whole disk

root@ubdev:/mnt/RaspberryPI # losetup --find --partscan --show 2012-07-15-wheezy-raspbian.img
and it will create a loop device for each partition like /dev/loop0p1, which you can mount.

The 3rd way is to let the RAID admin utility mdadm let do this for you. Mount the whole diskfile

root@ubdev:/mnt/RaspberryPI # losetup -f 2012-07-15-wheezy-raspbian.img

root@ubdev:/mnt/RaspberryPI # losetup -a
/dev/loop0: [0015]:281474976991968 (/mnt/VMs/RaspberryPI/qemu/2012-07-15-wheezy-raspbian.img)

and scan the loop device with mdadm
root@ubdev:/mnt/RaspberryPI # mdadm --build --force testMount --level linear --raid-devices 1 /dev/loop0
mdadm: array /dev/md/testMount built and started.

mdadm creates a device for each partition

root@ubdev:/mnt/VMs/RaspberryPI/qemu# ls -l /dev/md/*
lrwxrwxrwx 1 root root  8 Okt 16 14:16 /dev/md/testMount -> ../md127
lrwxrwxrwx 1 root root 10 Okt 16 14:16 /dev/md/testMount1 -> ../md127p1
lrwxrwxrwx 1 root root 10 Okt 16 14:16 /dev/md/testMount2 -> ../md127p2

And you could mount these devices:

root@ubdev:/mnt/VMs/RaspberryPI/qemu# mount /dev/md/testMount2 /root/mnt/p2/
root@ubdev:/mnt/VMs/RaspberryPI/qemu# ls -l /root/mnt/p2/
total 92
drwxr-xr-x  2 root root  4096 Jul 15  2012 bin
drwxr-xr-x  2 root root  4096 Jul 15  2012 boot
drwxr-xr-x  5 root root  4096 Jul 15  2012 dev
drwxr-xr-x 87 root root  4096 Jul 16  2012 etc
drwxr-xr-x  3 root root  4096 Jul 15  2012 home
drwxr-xr-x 14 root root  4096 Jul 15  2012 lib
drwx------  2 root root 16384 Jul 15  2012 lost+found
drwxr-xr-x  2 root root  4096 Jul 15  2012 media
drwxr-xr-x  2 root root  4096 Jun  2  2012 mnt
drwxr-xr-x  3 root root  4096 Jul 15  2012 opt
drwxr-xr-x  2 root root  4096 Jun  2  2012 proc
drwx------  2 root root  4096 Jul 16  2012 root
drwxr-xr-x  6 root root  4096 Jul 15  2012 run
drwxr-xr-x  2 root root  4096 Jul 15  2012 sbin
drwxr-xr-x  2 root root  4096 Jun 20  2012 selinux
drwxr-xr-x  2 root root  4096 Jul 15  2012 srv
drwxr-xr-x  2 root root  4096 Apr 14  2012 sys
drwxrwxrwt  2 root root  4096 Jun  2  2012 tmp
drwxr-xr-x 10 root root  4096 Jul 15  2012 usr
drwxr-xr-x 11 root root  4096 Jul 15  2012 var

The 4th way is kpartx from , but I havn’t tested it.

Enjoy
Michael

Leave a Reply