23.4.2014

foto Petr Bravenec

Petr Bravenec
Twitter: @BravenecPetr
+420 777 566 384
petr.bravenec@hobrasoft.cz

I use the loop device in Linux for a long time. The loop device is handy when I need to mount CD-ROM ISO image - but this is a thing I made last time few years ago.

Today I found other use of loop device. For some customer I make some installation USB flash disk. The system is booted from the flash and then the installation is started. I make whole system directly on the USB flash and then it was passed to customer. I left only the disk image created with dd command:

dd if=/dev/sde of=instalacni-flash.img

When I needed to change anything, I mounted the file automatically using the loop device:

losetup /dev/loop0 instalacni-flash.img

and I found that I do not know how to mount the first partition of the disk. Fdisk told mi some information:

# fdisk -l /dev/loop0 

Disk /dev/loop1: 2002 MB, 2002780160 bytes, 3911680 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: 0xe5837737

      Device Boot      Start         End      Blocks   Id  System
/dev/loop1p1   *        2048     3911679     1954816   83  Linux

But device /dev/loop0p1 does not exist. Little research on internet show me solution. The mount command can use the device with some shift from the beginning - in this case I have to shift the mounted part 2048 units forward. The mount command needs the shift in bytes. The unit size can be found on line

Units = sectors of 1 * 512 = 512 bytes

The size is 512 bytes.

The needed shift can be found on line

/dev/loop0p1   *        2048     3911679     1954816   83  Linux

The shift needed is 2048 units multiplied by unit size = 1048576 bytes. Now I can mount the partition:

mount -t ext4 -o offset=1048576 /dev/loop0 /mnt

When the work is finished I have to unmount the filesystem and disconnect the loop device:

umount /mnt
losetup -d /dev/loop0
Hobrasoft s.r.o. | Contact