When working with virtual machines and embedded devices, you’ll probably end up working with a disk image at some point.
To work with disk images, you can attach the file to the a device. To do so, you use the losetup command (which requires root privileges).
losetup
usage:
losetup loop_device # give info
losetup -d loop_device # delete
losetup -f # find unused
losetup [ options ] {-f|loop_device} file # setup
where options include
--offset , -o
start at offset into file.
–pass-fd , -p
read passphrase from file descriptor
instead of the terminal.
–encryption , -e
encrypt with .
Check /proc/crypto or /proc/crypto/cipher for available ciphers.
–nohashpass, -N
Don’t hash the password given. (previous versions hash, non-debian doesn’t.
–keybits , -k
specify number of bits in the hashed key given
to the cipher. Some ciphers support several key
sizes and might be more efficient with a smaller
key size. Key sizes < 128 are generally not
recommended
The challenge is if the disk image has multiple partitions. Though the loop device will be aware of the different partitions (which you can view with fdisk, and you’ll need to in order to get the offset number required to mount the last partition), you can’t access them all at once.
So far I only know how to mount the last partition on the disk image. You find out the offset of that partition, and attach it to the loopback device:
losetup -o52428800 /dev/loop0 debian-sarge-256-hda.img mount -text3 /dev/loop0 /mnt/debian-sarge-cf/
It works! How to find out the offset? Use fdisk on the loopback device when the entire image is attached to it:
losetup /dev/loop0 debian-sarge-256-hda.img
fdisk /dev/loop0
Command (m for help): p
Disk /dev/loop0: 232 MB, 232783872 bytes
16 heads, 32 sectors/track, 888 cylinders
Units = cylinders of 512 * 512 = 262144 bytes
Device Boot Start End Blocks Id System
/dev/loop0p1 * 1 80 20479+ 83 Linux
/dev/loop0p2 81 200 30720 83 Linux
/dev/loop0p3 201 888 176128 83 Linux
Command (m for help): u
Changing display/entry units to sectors
Command (m for help): p
Disk /dev/loop0: 232 MB, 232783872 bytes
16 heads, 32 sectors/track, 888 cylinders, total 454656 sectors
Units = sectors of 1 * 512 = 512 bytes
Device Boot Start End Blocks Id System
/dev/loop0p1 * 1 40959 20479+ 83 Linux
/dev/loop0p2 40960 102399 30720 83 Linux
/dev/loop0p3 102400 454655 176128 83 Linux
Use the “u” command in fdisk to change the units, then multiply the start number times 512, 102400 * 512 = 52428800.
Actually - turns out this also works on partitions other than the last one. For example, I just mounted the second partition by using the 40960 offset, multiplied by 512 = 20971520:
# losetup -o20971520 /dev/loop0 debian-sarge-256-hda.img # mount -text3 /dev/loop0 /mnt/debian-sarge-cf/
PS - You can use these theories and techniques for mounting ISO images.
Awesome.. just what I was looking for..
.
Hi Varghese, Glad it helped!
All,
You’ll be glad to know that there’s a much more intuitive (and arguably safer) way to do mount partitioned disks. No math minor required.
1) stop any existing loop device usage
2) rmmod loop
3) modprobe loop max_part=63
3) losetup -f /path/to/raw/image.raw
If the loop module’s been setup with max_part=63, it will automatically setup devices for the partitions as /dev/loop0p1 through /dev/loop0p63, just like you’d see for a partitioned hard disk.
There’s one caviat - you need a pretty recent kernel - I’d suggest 2.2.26 or later.
Chris
Thanks for commenting Chris!