Hits : 3374

losetup examples


identify partitions


[~]> fdisk -l kobo.img

Disk kobo.img: 1.9 GiB, 1977614336 bytes, 3862528 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
Disklabel type: dos
Disk identifier: 0xcee4c14b

Device     Boot   Start     End Sectors  Size Id Type
kobo.img1         19456  543744  524289  256M 83 Linux
kobo.img2        543745 1068033  524289  256M 83 Linux
kobo.img3       1068034 3862527 2794494  1.3G  b W95 FAT32


top


available losetup virtual device


[~]> losetup -f 
/dev/loop0


top


count bytes


3rd partition starts on 1068034 and one sector has 512 bytes


[~]> echo 1068034*512 | bc
546833408


top


attach the partition


[~]> sudo losetup `losetup -f` -o $( echo 1068034*512 | bc ) kobo.img


top


mount the partition


sudo mount /dev/loop0 /media/


top


how to attach the middle partition ?


The basic idea is:


offset = START * 512
size = ( END – START ) * 512


eg. kobo.img2


SIZE:


[~]> echo "(1068033-543745)*512" | bc
268435456


OFFSET:


[~]> echo 543745*512 | bc
278397440


so ...


# losetup `losetup -f ` --offset 278397440 --sizelimit 268435456 kobo.img

# mount /dev/loop0 /media/

# ls /media/
bin  dev  etc  fs.md5sum  lib  libexec  linuxrc  lost+found  mnt  proc  root  sbin  sys  tmp  upgrade  usr  var


top


de attach virtual disk


sudo umount /media

sudo losetup -d /dev/loop0


top