- 论坛徽章:
- 0
|
loop device
The loop device is a device driver that allows an image file to be mounted as though it were a normal block device. The question that immediately jumps to mind is, "So, how do I use this beast?". As an example, let's look at mounting, and examining the contents of, the rescue floppy image file.
First the CD-ROM, or other device containing this image file needs to be mounted like:
mount -r -t iso9660 /dev/scd0 /cdrom
Note: The -r option declares the device to be read only. Also /dev/scd0 assumes the drive is the first SCSI device. If the machine uses a SoundBlaster Pro/Creative Labs combo for the cdrom this device would be /dev/sbpcd0 instead. The other possibilities depend on the particular interface being used.
If this is an "Official CD" that was mounted, the file of interest is found as:
/cdrom/debian/hamm/main/disks-i386/current/resc1440.bin
So the mount command using the loop device will look like:
mount -t msdos -o loop /../../resc1440.bin /mnt
After this an ls of /mnt will show all the files that will appear on the floppy disk when this image file is transferred to it. This is a "live" file system. That is, it can be modified just like any other read/write file system mounted as a block device. The changes made to the file system while mounted become part of the image file and will be reflected on the floppy constructed from that image. Of course, in our example the file image resides on a read-only medium so changes aren't possible, but when the image file resides on a writable medium, like the hard disk, then those kinds of changes become possible.
OK. We can mount such file systems, but how are they constructed in the first place? This, of course requires a *nix environment. The tools are dd and losetup, and the process goes something like this:
First it is necessary to create an empty file of the desired size. This is done with dd and the zero device in a command like:
dd if=/dev/zero of=/../image.file bs=1k count=100000
This will create a file with 100 MB of space. Note that the file size is equivalent to the partition size when creating partitions on a block device like the hard disk. This file will thus hold a file system that can reach 100 MB in size before "device full" errors occur.
Before a file system can be created on this image file and mounted, it needs to be made to look like a block device. This is done with the losetup program in the following fashion:
losetup /dev/loop0 /../image.file
There are 8 loop devices to choose from, so you may need to check to see if a particular device is already in use. losetup /dev/loopN, where N ranges between 0 and 7, will return the status of the Nth loop device if it is mounted and will return an error message if it is not.
Now that /dev/loop0 is a legitimate(合法的) block device, mke2fs (or any of the other file system creation utilities) can be used to create a file system on the looped image file with a command like:
mke2fs -c /dev/loop() 100000
The -c option checks the device for bad blocks and the value on the end specifies how many blocks are on the file system. These two options can be left off the command line and results will be seen faster, but there is no guarantee that the resultant file system will be useful. Take the time to check for bad blocks. It is time you will not spend looking for the cause of problems later. Once the file system has been created, and a loop device associated with file, it can be mounted using the following command line:
mount -t ext2 /dev/loop0 /mnt
When this is later unmounted make sure to release the device with the command losetup -d /dev/loop1. Another alternative is to release the loop device as soon as the file system creation is complete, and mount the file as in the previous example with a command like:
mount -t ext2 -o loop=/../image.file /mnt
In either case this produces a "file system" mounted on /mnt that has 100 MB of storage space. Whatever this file system is intended to contain can now be copied, or untarred, or otherwise created on the new file system.
It should be evident that loop devices are very useful critters. The installation software makes use of one to perform several sets in the installation by mounting image files from the CD, instead of requiring a floppy disk to mount. This is not only faster, but physically easier than the floppy installation method.
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/59740/showart_472790.html |
|