Loop Devices in Linux

Mount partition inside image file

Find the offset of the partition inside the file:

tyr ~ # fdisk -l soekris_5501_140112.dd 

Disk soekris_5501_140112.dd: 8019 MB, 8019099648 bytes
16 heads, 63 sectors/track, 15538 cylinders, total 15662304 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: 0x00000000

                 Device Boot      Start         End      Blocks   Id  System
soekris_5501_140112.dd1              63      250991      125464+  82  Linux swap / Solaris
soekris_5501_140112.dd2   *      250992    15662303     7705656   83  Linux

The root partition has offset 240992 (in sectors). Notice in top that fdisk says that each sector is 512 bytes.

Now use mount the file as loop device with the offset parameter set to the proper value (sectors * sector size):

# mount -o loop,offset=$((250992*512)) soekris_5501_140112.dd /mnt/soekris/

Create file system on partition inside file

We cant use mount for this. We need to create a device node that points to the particular point in the image file. We skip mounts and use losetup directly:

# losetup -o$((250992*512)) /dev/loop0 soekris_5501_140112.dd 

We can now create a file system as usual on /dev/loop0:

# mkfs.ext3 /dev/loop0

Test by mounting /dev/loop0:

# mount /dev/loop0 /mnt/soekris
# ls /mnt/soekris
loopdevices.txt · Last modified: 2012/01/14 14:44 by jsc
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki