您的位置:首页 > 运维架构

Loop设备和使用loop设备打开ISO文件

2016-01-28 17:32 316 查看

1 Loop设备

    在类 UNIX 系统里,loop 设备是一种伪设备(pseudo-device),或者也可以说是仿真设备。它能使我们像块设备一样访问一个文件。

    在使用之前,一个 loop 设备必须要和一个文件进行连接。这种结合方式给用户提供了一个替代块特殊文件的接口。

    因此,如果这个文件包含有一个完整的文件系统,那么这个文件就可以像一个磁盘设备一样被 mount 起来。上面说的文件格式,我们经常见到的是 CD 或 DVD 的 ISO 光盘镜像文件或者是软盘(硬盘)的 *.img 镜像文件。通过这种 loop mount (回环mount)的方式,这些镜像文件就可以被 mount 到当前文件系统的一个目录下。

    

    Loop设备提供了一种创建一个存在于其他文件中的虚拟文件系统的机制.

1.1 Pseudo-devices

    Device nodes on Unix-like systems do not necessarily have to correspond to physical devices. Nodes that lack this correspondence form the group of pseudo-devices.

    

    Some of the most commonly used (character-based) pseudo-devices include:

    /dev/null – accepts and discards all input; produces no output (always returns an end-of-file indication on a read)

    /dev/zero – accepts and discards all input; produces a continuous stream of NUL (zero value) bytes

    /dev/full – produces a continuous stream of NUL (zero value) bytes when read, and returns a "disk full" message when written to

    /dev/random and
/dev/urandom – they produce a variable-length stream of pseudo-random numbers.

1.2 Loop设备

    In Unix-like operating systems, a loop device, vnd (vnode disk), or lofi (loop file interface) is a pseudo-device that makes a file accessible as a block device.

    

1.3 Loop设备使用

    Before use, a loop device must be connected to an existing file in the filesystem. 

    If the file contains an entire file system, the file may then be mounted as if it were a disk device. Files of this kind are often used for CD ISO images and floppy disc images.

    

1.4 linux下的loop设备

    Sometimes, the loop device is erroneously referred to as loopback device。

    In Linux, device names are encoded in the symbol table entries of their corresponding device drivers. The device is called "loop" device and device nodes are usually named /dev/loop0, /dev/loop1, etc. They can be created with makedev for the static device
directory, dynamically by the facilities of the device filesystem (udev), or directly with mknod. The management user interface for the loop device is losetup。

    

    默认情况下系统支持的loop device是8个。
root@localhost:~# ls -l /dev/loop*
brw-rw---- 1 root disk  7,   0 Jan 22 16:00 /dev/loop0
brw-rw---- 1 root disk  7,   1 Jan 22 16:00 /dev/loop1
brw-rw---- 1 root disk  7,   2 Jan 22 16:00 /dev/loop2
brw-rw---- 1 root disk  7,   3 Jan 22 16:00 /dev/loop3
brw-rw---- 1 root disk  7,   4 Jan 22 16:00 /dev/loop4
brw-rw---- 1 root disk  7,   5 Jan 22 16:00 /dev/loop5
brw-rw---- 1 root disk  7,   6 Jan 22 16:00 /dev/loop6
brw-rw---- 1 root disk  7,   7 Jan 22 16:00 /dev/loop7
crw------- 1 root root 10, 237 Jan 22 16:00 /dev/loop-control

1.5 losetup命令

此命令用来设置循环设备。循环设备可把文件虚拟成块设备,籍此来模拟整个文件系统,让用户得以将其视为硬盘驱动器,光驱或软驱等设备,并挂入当作目录来使用。

    losetup  is used to associate loop devices with regular files or block devices, to detach loop devices and to query the status of a loop device. If only the loopdev argument is given, the status of the corresponding loop device is shown.
Setup loop device:
losetup [-o offset] [--sizelimit size] [-p pfd] [-rP] {-f[--show]|loopdev} file
Delete loop:
losetup -d loopdev...


loop_device 循环设备名,在 linux 下如 /dev/loop0 , /dev/loop1 等。

file 要与循环设备相关联的文件名,这个往往是一个磁盘镜象文件,如 *.img

    

2 使用loop设备打开ISO文件

    Mounting a file containing a filesystem via such a loop mount makes the files within that filesystem accessible. They appear in the mount point directory.

    

2.1 使用loop设备打开ISO文件的方法

Mounting a file containing a disk image on a directory requires two steps:
step 1: association of the file with a loop device node,

step 2 mounting of the loop device at a mount point directory

These two operations can be performed either using two separate commands, or through special flags to the mount command. 

2.1 mount方法

      For example, the command
mount /tmp/disk.img /mnt -t vfat -o loop=/dev/loop
       will set up the loop device /dev/loop3 to correspond to the file /tmp/disk.img, and then mount this device on /mnt.

       If  no  explicit  loop  device  is  mentioned (but just an option `-o loop' is given), then mount will try to find some

       unused loop device and use that, for example
mount /tmp/disk.img /mnt -o loop


       The mount command automatically creates a loop device from a regular file if a filesystem type is not specified or  the

       filesystem is known for libblkid, for example:
mount /tmp/disk.img /mnt
mount -t ext3 /tmp/disk.img /mnt

2.2 losetup配合mount方法

使用 losetup将磁盘镜像文件虚拟成快设备
losetup /dev/loop0 example.img
mount /dev/loop0 /home/you/dir


To identify an available loop device for use:
losetup -f

2.3 umount卸载loop设备

umount /home/you/dir
# or, after finding the associated loop number by e.g. mount | grep "/home/you/dir"
# or losetup -a | grep example.img
umount /dev/loop<N>

2.4 实例

2.4.1 挂载ISO文件

root@www ~]# ll -h /root/centos5.2_x86_64.iso
-rw-r--r-- 1 root root 4.3G Oct 27 17:34 /root/centos5.2_x86_64.iso
[root@www ~]# mkdir /mnt/centos_dvd
[root@www ~]# mount -o loop /root/centos5.2_x86_64.iso
/mnt/centos_dvd
[root@www ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/root/centos5.2_x86_64.iso
4493152 4493152 0 100% /mnt/centos_dvd
# iso文件内所有数据可以在 /mnt/centos_dvd 看到!
[root@www ~]# ll /mnt/centos_dvd
total 584
drwxr-xr-x 2 root root 522240 Jun 24 00:57 CentOS
-rw-r--r-- 8 root root 212 Nov 21 2007 EULA
-rw-r--r-- 8 root root 18009 Nov 21 2007 GPL
drwxr-xr-x 4 root root 2048 Jun 24 00:57 images
...
[root@www ~]# umount /mnt/centos_dvd/


2.4.2 制作loop设备文件
(1)创建空的磁盘镜像文件,这里创建一个1.44M的软盘
$ dd if=/dev/zero of=floppy.img bs=512 count=2880
(2)使用 losetup将磁盘镜像文件虚拟成快设备
$ losetup /dev/loop1 floppy.img
(3)挂载块设备
$ mount /dev/loop0 /tmp
经过上面的三步之后,我们就可以通过/tmp目录,像访问真实快设备一样来访问磁盘镜像文件floppy.img。
(4) 卸载loop设备
$ umount /tmp
$ losetup -d /dev/loop1

参考:

Device file:https://en.wikipedia.org/wiki/Device_file#devfs

Loop device:https://en.wikipedia.org/wiki/Loop_device

loop设备及losetup命令介绍: http://blog.csdn.net/ustc_dylan/article/details/6878252
modprobe修改设备数:http://blog.sina.com.cn/s/blog_759dc36b0100q3yx.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: