免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1096 | 回复: 0
打印 上一主题 下一主题

loop 设备 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-29 01:07 |只看该作者 |倒序浏览

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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP