Loopback Device: Difference between revisions
[unchecked revision] | [unchecked revision] |
Content deleted Content added
m changed category |
|||
(28 intermediate revisions by 12 users not shown) | |||
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.
Note: This article only covers UNIX environments (including [[Cygwin]]). For information on how to use loopback devices on Windows, see [[Windows_Tools#diskpart | diskpart]].
== Loopback Device under Linux==
The linux loopback device can be used by root only, and needs to be enabled in the kernel before use.
=== Floppy Disk Images With GRUB and EXT2 ===
First, lets create an empty image.
dd if=/dev/zero of=floppy.img bs=512 count=2880
Line 14 ⟶ 16:
losetup /dev/loop0 floppy.img
Now lets make it
mkfs -t ext2 /dev/loop0
Mount!
mount -t ext2 /dev/loop0 /mnt/myfloppy
Create GRUB directory.
cd /mnt/myfloppy
mkdir grub
Copy GRUB's second stage files. (GRUB stage[12] could also be located in /usr/lib/grub/)
cp /lib/grub/i386-pc/stage[12] /mnt/myfloppy/grub
Create a device mapping for the GRUB installation. ''You need quotations around the first part.''
echo "(fd0) /dev/loop0" > /mnt/myfloppy/grub/device.map
Start GRUB console for installation into the boot record.
grub --device-map=/mnt/myfloppy/grub/device.map /dev/loop0
In the GRUB console:
root (fd0)
setup (fd0)
NOTE: You must unmount /mnt/myfloppy before using a emulator to directly read /dev/loop0, such as:
qemu -fda /dev/loop0
NOTE: When deleting the loop device, the original floppy.img file will be saved with the modified contents.
=== Floppy Disk Images With FAT16 ===
Create an empty image.
dd if=/dev/zero of=floppy.img bs=512 count=2880
Set it up for mounting.
losetup /dev/loop0 floppy.img
Make it MSDOS formatted.
mkdosfs /dev/loop0
Line 21 ⟶ 69:
mount -t msdos /dev/loop0 /mnt/myfloppy
=== Floppy Disk Images With FAT12 ===
The steps mentioned below will be useful for the BrokenThorn Entertainment tutorials
Create an empty image.
dd if=/dev/zero of=floppy.img bs=512 count=2880
Set it up for mounting.
losetup /dev/loop0 floppy.img
Format it to FAT12.
mkdosfs -F 12 /dev/loop0
Mount!
mount /dev/loop0 /mnt -t msdos -o "fat=12"
Unmount
umount /mnt
Destroy loopback device
losetup -d /dev/loop0
=== Hard Disk Images ===
Line 55 ⟶ 130:
|}
That leaves us with a nice sized file full of zeros that we'll use for our disk image.
==== Partitioning ====
Line 83 ⟶ 135:
Now to create the MBR and partition table on the disk image (Usually you need to be root).
fdisk -u -C#cylinders -S63 -H16 /
Explanation:
Line 103 ⟶ 155:
| Set the heads/track to 16.
|-
| /
| fdisk is capable of partitioning image files directly.
|}
Line 116 ⟶ 168:
You should end up with a screen that looks something like this:
Disk /
16 heads, 63 sectors/track, 1000 cylinders, total 1008000 sectors
Units = sectors of 1 * 512 = 512 bytes
Device Boot Start End Blocks Id System
/
Obviously the cylinder count, partition end and blocks will be different depending on the size of your image.
Line 137 ⟶ 189:
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.
==== Mounting ====
Ok, now we attach the file to the loopback device
losetup -o32256 /dev/loop0 /path/to/c.img
Line 164 ⟶ 202:
|}
The reason we move 32256 bytes into the file is this is where the partition starts. Remember I said to note the start sector of the partition (63 is usual)? Well, since each sector is 512 bytes long we therefore know the starting byte of the partition is 32256 (63*512) bytes into the file. The reason behind this gap is that most (
''Note: If you aren't using the suggested geometry then you'll have to calculate this for yourself.''
Line 174 ⟶ 212:
For ext2fs, use:
mke2fs -b1024 /dev/loop0 #blocks
Explanation:
Line 180 ⟶ 218:
|-
| mke2fs
| Create an
|-
| -b1024
Line 192 ⟶ 230:
|}
This gives us a clean
Note: mke2fs is smart enough to figure out block size and #blocks for itself, but if you ever want to use multiple partitions you'll need to know how to use those values.
Line 198 ⟶ 236:
For FAT32, use:
mkdosfs -F32 /dev/loop0 #blocks
Explanation:
Line 228 ⟶ 266:
mount -text2 /dev/loop0 /mnt/wherever
or:
mount -tvfat /dev/loop0 /mnt/wherever
Explanation:
{| {{Wikitable}}
|-
| mount
|-
| -text2 / -tvfat
| Filesystem being used, Linux can usually figure this out on its own.
|-
| /dev/loop0
| The device representing our partition
|-
| /mnt/wherever
| A directory to mount the partition on.
|}
This should leave you with a nicely mounted partition. If you run df -Th you should end up with a line similar to:
Line 243 ⟶ 291:
/dev/loop0 vfat 492M 4.0K 492M 1% /mnt/wherever
...or for ext2fs
Filesystem Type Size Used Avail Use% Mounted on
Line 260 ⟶ 308:
Explanation:
{| {{Wikitable}}
|-
| umount
| Linux command to unmount a filesystem.
|-
| /dev/loop0
| The device that was mounted
|}
==== Making it Easier ====
Line 267 ⟶ 321:
One final thing to do, which is to simplify mounting and unmounting that partition.
Mounting:
mount -text2 -oloop=/dev/loop0,offset=32256 /path/to/c.img /mnt/wherever
Unmounting:
umount /path/to/c.img
This is essentially a combination of the losetup and mount commands we used previously when formatting the partition. If used it also means we lose access to the raw 'disk' or 'partition' through /dev/loop0.
Line 282 ⟶ 333:
See also http://www.pixelbeat.org/scripts/lomount.sh
Finally, if you have to mount and umount that image very frequently and you're too lazy to type the sudo password each time, just add to /etc/fstab:
/path/to/c.img /mnt/wherever ext2 user,loop 0 0
now you can just call:
mount /mnt/wherever
umount /mnt/wherever
==== 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).
Things to remember:
* 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)
Don't forget to flush the filesystem buffers when manipulating with files on mounted disk image. On a Unix-like system, this can be simply done by executing the <tt>sync</tt> program in your shell.
== Loopback Device under FreeBSD ==
FreeBSD uses mdconfig. First, use DD to create an empty floppy image (1.44 MB in size). Memdisks are allocated dynamically, and the name is displayed after the mdconfig command. This assumes that "md0" is printed.
To mount:
Line 318 ⟶ 364:
To unmount:
umount /
mdconfig -d -u md0
== Loopback Device under OpenBSD ==
OpenBSD has used [http://www.openbsd.org/cgi-bin/man.cgi?query=vnconfig&apropos=0&sektion=0&manpath=OpenBSD+Current&arch=i386&format=html vnconfig(8)] since version 2.2 (perhaps earlier..).
As root or using su/sudo, Here is an example scenario for configuring a vnode pseudo disk device.
Creating the floppy.img file using dd:
dd if=/dev/zero of=/path/to/floppy.img bs=512 count=2880
Configuring the vnd0 device:
vnconfig vnd0 /path/to/floppy.img
Listing configured devices:
vnconfig -l
Output:
vnd0: covering floppy.img on wd0a, inode 270473
vnd1: not in use
vnd2: not in use
vnd3: not in use
Creating a FAT12 file system and then mounting the device:
newfs_msdos -F 12 -f 1440 /dev/rvnd0c
mount -t msdos /dev/vnd0i /mnt/floppy
Removing the device mount and uninstalling the vnd0 device:
umount /mnt/floppy
vnconfig -u vnd0
More Information: [http://www.openbsd.org/cgi-bin/man.cgi?query=vnd&apropos=0&sektion=4&manpath=OpenBSD+Current&arch=i386&format=html vnd(4)] / [http://www.openbsd.org/cgi-bin/man.cgi?query=vnconfig&apropos=0&sektion=0&manpath=OpenBSD+Current&arch=i386&format=html vnconfig(8)]
[[Category:Disk Image Utilities]]
|