When working with disk images the admin is often faced with the problem of mounting the contained partitions inside of these images.
Such full-disk images can be the containers of virtual machines for xen as fount in /var/lib/xen/images or disk-images ripped from dying drives with the help of recovery tools such as dd_rescue.
Regardless of the origin, these images do contain everything needed to run a machine, a partition table, a boot sector, at least one, but often several partitions inside of one container.
Mounting these images through a normal loop mount does only result in the first image to be mounted, often not what is wanted.
In the past, suggestions ranged from imaging single partitions to using dd to "cut" the single partitions out of the full-disk image. Crude workarounds as we've had a perfect tool called device mapper for some time now.
Executing fdisk on the my test disk-image shows several partitions, among some ingorable warnings:
[root@workstation ~]# fdisk -l disk.img
You must set cylinders.
You can do this from the extra functions menu.
Disk disk.img: 0 MB, 0 bytes
4 heads, 32 sectors/track, 0 cylinders
Units = cylinders of 128 ∗ 512 = 65536 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
disk.img1 ∗ 1 32 2032 83 Linux
disk.img2 33 216 11776 83 Linux
disk.img3 217 705 31296 83 Linux
disk.img4 706 978 17472 5 Extended
disk.img5 706 859 9840 83 Linux
disk.img6 860 978 7600 83 Linux
[root@workstation ~]#
Thus, we know we do have an image file containing several partitions.
Now, how to handle this?
Mounting this test-image gives an error in my case. Often though the first partition gets sucessfully mounted.
[root@workstation ~]# mount disk.img /mnt/ -t ext2 -o loop,ro
mount: wrong fs type, bad option, bad superblock on /dev/loop1,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so
[root@workstation ~]#
The solution to this is called kpartx, a tool to map the partitions contained inside such an image into device-mapper block devices. This tools is contained in the kpartx RPM and can be easily installed by executing yum install kpartx if it is not already installed on your system.
Execution is quite simple. To list the contained partitions and see onto which loop-devices they would be mapped, the following command is needed:
[root@workstation ~]# kpartx -l disk.img
loop0p1 : 0 4064 /dev/loop0 32
loop0p2 : 0 23552 /dev/loop0 4096
loop0p3 : 0 62592 /dev/loop0 27648
loop0p5 : 0 19680 /dev/loop0 90272
loop0p6 : 0 15200 /dev/loop0 109984
[root@workstation ~]#
To actually map these partitions onto the loop-devices, call kpartx with the
-a instead of the
-l parameter:
[root@workstation ~]# kpartx -a -v disk.img
add map loop0p1 : 0 4064 linear /dev/loop0 32
add map loop0p2 : 0 23552 linear /dev/loop0 4096
add map loop0p3 : 0 62592 linear /dev/loop0 27648
add map loop0p5 : 0 19680 linear /dev/loop0 90272
add map loop0p6 : 0 15200 linear /dev/loop0 109984
[root@workstation ~]#
The partitions are then available in
/dev/mapper/ to be mounted accordingly and the containing files to be accessed:
[root@workstation ~]# ls -all /dev/mapper/
total 0
drwxr-xr-x 2 root root 160 2008-06-11 15:06 .
drwxr-xr-x 13 root root 4180 2008-06-11 15:06 ..
crw-rw---- 1 root root 10, 60 2008-06-11 13:28 control
brw-rw---- 1 root disk 253, 0 2008-06-11 15:06 loop0p1
brw-rw---- 1 root disk 253, 1 2008-06-11 15:06 loop0p2
brw-rw---- 1 root disk 253, 2 2008-06-11 15:06 loop0p3
brw-rw---- 1 root disk 253, 3 2008-06-11 15:06 loop0p5
brw-rw---- 1 root disk 253, 4 2008-06-11 15:06 loop0p6
[root@workstation ~]#
[root@workstation ~]# mount /dev/mapper/loop0p5 /mnt/ -o loop,ro
[root@workstation ~]# mount | grep '/mnt'
/dev/mapper/loop0p5 on /mnt type ext2 (ro,loop=/dev/loop1)
[root@workstation ~]# ls /mnt/
backup conf.tar.gz etc manifest.txt tool
bin default.list factory_default.md5 rc.reboot upgrade.list
cfg default.tar.gz lost+found test
[root@workstation ~]#
After the partitions have been unmounted, the device-mapper mappings can be removed by calling kpartx with the
-d parameter. The image file can then be used again as it is not in use anymore by the device mapper.
[root@workstation ~]# umount /mnt/
[root@workstation ~]# kpartx -d -v disk.img
del devmap : loop0p1
del devmap : loop0p2
del devmap : loop0p3
del devmap : loop0p5
del devmap : loop0p6
loop deleted : /dev/loop0
[root@workstation ~]# ls -al /dev/mapper/
total 0
drwxr-xr-x 2 root root 60 2008-06-11 15:16 .
drwxr-xr-x 13 root root 4080 2008-06-11 15:16 ..
crw-rw---- 1 root root 10, 60 2008-06-11 13:28 control
[root@workstation ~]#