Loopback Device: Difference between revisions

Merged BSD's loopback device with the linux topic
[unchecked revision][unchecked revision]
(Split off loopback device from linux disk images page)
 
(Merged BSD's loopback device with the linux topic)
Line 1:
A Loopback Device is a mechanism used to interpret files as real devices. The main advantage of this method is that all tools used on real disks can be used with a loopback device.
Linux uses loopback devices for images. This allows any file to be used as image for any device. Drawbacks are that the loopback device needs to be enabled in the kernel, and these operation can only be performed as root. Advantages are that this method supports anything supported by real disks.
 
== FloppyLinux DiskLoopback ImagesDevice ==
The linux loopback device can be used by root only, and needs to be enabled in the kernel before use.
 
=== Floppy Disk Images ===
 
First, lets create an empty image.
Line 19 ⟶ 22:
mount -t msdos /dev/loop0 /mnt/myfloppy
 
=== Hard Disk Images ===
 
A hard disk image contains an MBR, then a number of partitions, but the 'mount' instruction in Linux works with disk partitions, not full disks. To mount a partition contained in our disk image, we need to make sure the 'mount' command only sees our partition, not the whole disk.
 
==== Creating an image ====
 
First create the empty file that we will use for our disk image. We will assume a disk geometry of #cylinders, 16 heads, 63 sectors/track, 512 bytes/sector, which means that each cylinder contains 516096 bytes (16*63*512). Decide how large you want your disk image to be, and choose an appropriate number of cylinders (I’ll be using #cylinders throughout).
Line 53 ⟶ 56:
That leaves us with a nice sized file full of zeros that we'll use for our disk image.
 
==== Mounting ====
 
Now we attach the file to a loopback device. This lets us treat the file as though it were a physical disk.
Line 76 ⟶ 79:
If you run ps ax, you should now see a [loop0] process.
 
==== Partitioning ====
 
Now to create the MBR and partition table on the disk image (Usually you need to be root).
Line 134 ⟶ 137:
Unfortunately this also means that from here on out we have to account for the fact that our partition does not start at byte 0 of the image.
 
==== Detach our disk image from the loopback device. ====
 
Command:
Line 167 ⟶ 170:
We now have a device (/dev/loop0) which we can use in a similar fashion to a normal one for a partition (eg /dev/hda1).
 
==== Formatting the partition ====
 
For ext2fs, use:
Line 217 ⟶ 220:
''Note: The reason for #blocks is the same as for ext2fs, ie possible multiple partitions.''
 
==== Mount Partition ====
 
You should now be able to mount the partition (Because it is still setup on the loopback device).
Line 247 ⟶ 250:
(Yup, these are for the same disk image. By default ext2fs reserves/uses quite a bit of space even empty.)
 
==== Unmount, Detach ====
 
Ok, unmount the partition and detach the loopback device.
Line 260 ⟶ 263:
/dev/loop0 The device that was mounted
 
==== Making it Easier ====
 
One final thing to do, which is to simplify mounting and unmounting that partition.
Line 279 ⟶ 282:
See also http://www.pixelbeat.org/scripts/lomount.sh
 
==== The End ====
 
That's it, you now know how to handle hard disk images under Linux. Whilst mounted you can use it in exactly the same way you use a normal disk partition. Multiple partitions are an extension of this, just change the offset of the losetup command according to the partition you want to use (And format using the correct number of blocks).
Line 286 ⟶ 289:
losetup type command will give you the equivalent of a raw disk device (Eg /dev/hda)
losetup -o type command will give you the equivalent of a raw partition device (Eg /dev/hda1)
 
== Loopback Device under FreeBSD ==
 
FreeBSD 4.x uses vnconfig FreeBSD 5.x uses mdconfig
 
First, use DD to create an empty floppy image (1.44mb in size)
 
=== FreeBSD 4.x ===
 
dd if=/dev/zero of=floppy.img bs=512 count=2880
vnconfig vn0 floppy.img
newfs_msdos -f 1440 /dev/vn0
mount -t msdosfs /dev/vn0 /mnt/myfloppy
 
To shut and image down, unmount and unconfigure it.
 
umount /tmp/myfloppy
vnconfig -c /dev/vn0
=== FreeBSD 5.x ===
Memdisks are allocated dynamically, and the name is displayed after the mdconfig command. This assumes that "md0" is printed.)
 
To mount:
dd if=/dev/zero of=floppy.img bs=512 count=2880
mdconfig -a -t vnode -f floppy.img
newfs_msdos -f 1440 /dev/md0
mount -t msdosfs /dev/md0 /mnt/myfloppy
 
To unmount:
umount /tmp/myfloppy
mdconfig -d -u md0
 
 
[[Category:Disk Utilities]]
1,490

edits