HOWTO+-+Mount+Raw+Images

The loop module way
First make sure that your loop module shows the loop partitions: Create the file /etc/modprobe.d/loop.conf and reload the loop module by running the following commands as root (or sudo): code format="bash" /dev/loop0 /dev/loop0 /dev/loop0p1  /dev/loop0p2  /dev/loop0p5 code
 * 1) echo "options loop max_part=63 max_loop=8" > /etc/modprobe.d/loop.conf
 * 2) rmmod loop
 * 3) modprobe loop
 * 4) losetup -f
 * 1) losetup /dev/loop0 yourimage.img
 * 2) ls /dev/loop0*
 * 1) mount /dev/loop0p1 /mountpoint
 * 2) umount /mountpoint
 * 3) losetup -d /dev/loop

The built in kernel loop
If the rmmod and modprobe commands are not working, this means that the loop is built into your kernel. To get loop working, edit your grub menu.lst or in Ubuntu edit the file /etc/default/grub and add the "loop.max_part=63" option: code format="bash" ... GRUB_CMDLINE_LINUX="loop.max_part=63" ... code Save the changes, run the update-grub command, reboot your system and use the losetup as previously described. code format="bash" Generating grub.cfg ... Found linux image: /boot/vmlinuz-3.0.0-17-generic Found initrd image: /boot/initrd.img-3.0.0-17-generic Found memtest86+ image: /boot/memtest86+.bin done code
 * 1) sudo update-grub
 * 1) sudo reboot

The kpartx way
code format="bash" loop0p1 : 0 3997953 /dev/loop0 2048 loop0p2 : 0 4388607 /dev/loop0 4000001 loop deleted : /dev/loop0 add map loop0p1 (253:0): 0 3997953 linear /dev/loop0 2048 add map loop0p2 (253:1): 0 4388607 linear /dev/loop0 4000001 /dev/mapper/loop0p1 /dev/mapper/loop0p2 loop0p1 : 0 3997953 /dev/loop0 2048 loop0p2 : 0 4388607 /dev/loop0 4000001 code
 * 1) sudo apt-get install kpartx
 * 2) sudo kpartx -l my-image.img
 * 1) sudo kpartx -a -v my-image.img
 * 1) ls /dev/mapper/loop0p*
 * 1) sudo mount /dev/mapper/loop0p1 /mountpoint
 * 2) sudo umount /mountpoint
 * 3) sudo kpartx -r -v my-image.img

[]

Mount Raw Images

Just recently, I needed to mount a KVM raw image file, because it was depending on a network mount that was no longer accessible, and any attempts to interact with the boot process failed. So, rather than booting off a live CD, or some other medium, I decided to mount the raw image file. After all, it is ext4. However, mounting an image file means knowing where the root filesystem begins, which means knowing how to offset the mount, so you can access your data correctly. I used the following:

First, I setup a loop back device, so I could gather information about its partition setup: code format="bash"
 * 1) losetup /dev/loop0 virt01.img
 * 2) fdisk -l /dev/loop0

Disk /dev/loop0: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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: 0x0009bdb7 Device Boot Start End Blocks Id System

/dev/loop0p1 37943296 41940991 1998848 82 Linux swap / Solaris /dev/loop0p2 * 2048 37943295 18970624 83 Linux

Partition table entries are not in disk order code In this case, the virtual machine filesystem is 21.5 GB in size, in reads and writes in 512 byte blocks. Further, it appears as though swap occupies the second partition, while the ext4 root filesystem occupies the first, and begins at sector 2048, or byte 2048*512=1048576. So, now I just need to tear down the loop back device, and create it again with an offset of 1048576 bytes, at which point, I should be able to mount the device: code format="bash" bin/ home/ lib32/ mnt/ run/ sys/ vmlinuz@ boot/ initrd.img@ lib64/ opt/ sbin/ tmp/ vmlinuz.old@ dev/ initrd.img.old@ lost+found/ proc/ selinux/ usr/ etc/ lib/ media/ root/ srv/ var/ code At this point, I can edit my problematic /mnt/etc/fstab file to fix the troubled boot, and boot it up.
 * 1) losetup -d /dev/loop0
 * 2) losetup /dev/loop0 virt01.img -o 1048576
 * 3) mount /dev/loop0 /mnt
 * 4) ls /mnt