mount losetup 查看带文件系统的文件

来源:互联网 发布:走心机编程软件 编辑:程序博客网 时间:2024/05/13 00:52

搜索到如下的文章,关于losetup的使用;

losetup 命令可以把普通文件当作块设备挂载;当你的一个镜像存在分区,而你想查看后一个分区里的文件,那么lsosetup就派上用场了;
如VR10.img文件存在两个分区,现在需要查看后一个分区,需要先把后一个分区拷贝出来,然后挂载;
qemu-img convert -O raw VR10.img  VR10.raw        //首先把Img文件 转换为 raw格式
losetup  /dev/loop5  ./VR10.raw                                    //挂载该镜像;
fdisk -lu /dev/loop5                                                    //查看第一、二分区的间隔,即拷贝第二个分区需要跳过多少block szie,如下图,/dev/loop0p2的start列值


dd if=/dev/loop5 of=loop5p2 bs=512 count=1993728 skip=206848                                   //拷贝第二个分区出来
file  ****                           //查看拷贝出的文件成功?


 mount -t ext4 -o loop ./loop5p2 mount_VR10/                               //挂载拷贝出的镜像,然后就可以查看里面的文件        -o loop

More about loop device

A loop device is a pseudo-device that makes a file accessible as a block device. Loop devices are often used for CD ISO images and floppy disc images. 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 using above commands.


Managing loop devices on CentOS and Fedora Linux hosts

Linux loop devices provide a way to mount ordinary files as block devices. This capability allows you to easily access an ISO image that resides on disk, mount and unmount encrypted devices (the dm-crypt and fuse encryption module may be a better solution for this), or test out new file systems using plain old files.

Linux loop devices are managed through the losetup utility, which has options to add, list, remove and locate unused loop devices. To associate a loop device with a file, you will first need to locate an unused loop device in /dev. This can be accomplished by running losetup with the “-f” (find an unused loop device) option:

losetup -f
/dev/loop0

Once you identify an available loop device, you can associate the loop device with a file by running losetup with the name of the loop device, and the file to associate with the loop device:

losetup /dev/loop0 /root/foo

To verify the device is attached, you can run losetup with the “-a” (show all loop devices) or “-j” (show loop devices associate with the corresponding file) option:

losetup -a
/dev/loop0: [0801]:12779871 (/root/foo)

losetup -j /root/foo
/dev/loop0: [0801]:12779871 (/root/foo)

To access the contents of a loop device, you can use the mount utility to mount the loop device to a directory that resides in an existing file system:

mount /dev/loop0 /mnt

This of course assumes that the underlying file contains a valid label and file system (you can run fdisk or parted to create a label, and then use your favorite mkfs variation to create a file system). Once you finish using a loop device, you can remove it by running losetup with the “-d” (remove loop device) option:

umount /mnt

losetup -d /dev/loop0

losetup -a

I have come to rely on loop devices for all sorts of purposes, and their simplicity makes them so so useful!

0 0
原创粉丝点击