Thursday, June 22, 2006

Create your own Loopback Device

A loopback device enables you to simulate a block device using an ordinary disk file.Data can be written or read from a file name disk-image rather than to and from the tracks and sector of an actual physical disk drive or disk partition.(ofcourcd disk-image must reside on an actual disk,which must be larger than the simulated disk). Loopback devices are named like /dev/loop0, /dev/loop1, and so on.Each can be used as a single block device at one time. Be sure when you are stting up loopback device you should be logged as a super user.

This loopback device can be used as any other block device(like your hard disk). You can create file system and mount this files system as you would mount the file system on a normal disk or partition.Such type of file system is basically called virtual file system.
Construct the virtual File System and Mount it with loopback device(Follow this step)

1.Create any 10MB file using dd command to hold the virtual file system. You can create this file anywhere as you like but i like to create in /tmp.
[suman@linux tmp]#dd if=/dev/zero of=/tmp/disk-image count=20480
your output should be like

20480+0 records in
20480+0 records out
10485760 bytes (10 MB) copied, 0.218746 seconds, 47.9 MB/s
2.cheack with ls -l command
[suman@linux tmp]# ls -l /tmp/disk-image
-rw-rw-r-- 1 suman suman 10485760 Jun 22 15:43 /tmp/disk-image
3.Now create a file System (i prefered ext2 File System..U can choose as you like) by this command
suman@ajit program]$ mke2fs -q /tmp/disk-image
It will ask for proceed just put y
/tmp/disk-image is not a block special device.
Proceed anyway? (y,n) y
4.Mount the file system using loopback device. Before that you need to create one mount point for the virtual file system.
[suman@linux tmp]# mkdir /tmp/virtual_fs
[suman@linux tmp]# mount -o loop=/dev/loop0 /tmp/disk-image /tmp/virtual_fs

5.If you don't see any error, you have successfully mounted the file System . Now cheack it through this command
[suman@linux tmp]#df -h /tmp/virtual_fs
you would see
Filesystem Size Used Avail Use% Mounted on
/tmp/disk-image 9.7M 92K 9.1M 1% /tmp/virtual_fs
6. Create one test file to test whether it is working correctly or not
[suman@linux tmp]# cd /tmp/virtual_fs
[suman@linux tmp]#echo 'I have done' >test.txt
[suman@linux tmp]#ls -l
lost+found test.txt
[suman@linux tmp]#cat test.txt
I have done
......lost+found is a direcxtory that was automatically added by mke2fs.

If want to unmount the file system just put this command
[suman@linux tmp]# umount virtual_fs

Why should i need loopback device? This question may arriase..let me tell one situation and explain me how we can use loopback device..

You have one CDROM image and you are using frequently. You need to just copy the whole CDROM image into new loopback can do like that
[suman@linux]$ cp /dev/cdrom /tmp/disk-image
[suman@linux]$dd if=/dev/cdrom of=/tmp/disk-image

This may take several time depends on the size of your CD ROM. The resulting image file could be as large as the contents of the CDROM.
Now you can mount the CDROM image with out having the original CDROM in your CDROM.For example
[suman@linux]# mount -o loop=/dev/loop0 /tmp/disk-image /mnt/cdrom

You can access much faster than CDROM beacause whole CDROM content is in Hard Disk. Note most CDROM use the file System type iso9660.

You are playing a game which always needs your CDROM. You could not play without CDROM and sometimes you are seeing that your game is going to slow as you expected. Well create a loopback device and copy the CROM contents and mount it......and Enjoy...You are not accessing CDROM phisically which could save your CDROM life.
If you have any comments any question and also if you found any error about this mini tutorial please mail me and please leave your suggestion in comment section that i can improve later. I always apologies my poor english expression. forgive me..


divya said...

good one

Add to this, To accomodate the enhanced loopback device, some new entries need to be created in /dev. A script named createdev is available to handle that task for you, and it can be run at start up if you're running devfs to recreate the entries for you at boot. You may need to comment out the sourcing of the RedHat functions within the script if you aren't on a RedHat based distribution, like Debian. By default the script will create enough entries in /dev for a fifteen disks with up to fifteen partitions. You can adjust that to your requirements within the script.

Invest on this technology...very interesting
divya dutta

suman said...

Yes i could use this script if i don't want to pass offset during the partition mounting in a disk image.For normal, I could pass the sector number in each tract multiply by the bytes per sector. A fdisk command will help to get this things.I appriciate this great script but i don't like to eat with help of spoon!!!
Happy Hacking!!

jack said...

This is really usefull

Enthu Guy said...

This post is very useful. Thank you very much.