2017年1月4日 星期三

[Linux 文章收集] Managing loop devices on CentOS and Fedora Linux hosts

Source From Here
Preface
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.

How-To
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/loop2

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/loop2 /root/CentOS-7-x86_64-DVD-1611.iso

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: [2051]:204233251 (/var/lib/docker/devicemapper/devicemapper/data)
/dev/loop1: [2051]:204233252 (/var/lib/docker/devicemapper/devicemapper/metadata)
/dev/loop2: [2051]:136421085 (/root/CentOS-7-x86_64-DVD-1611.iso)


# losetup -j /root/CentOS-7-x86_64-DVD-1611.iso
/dev/loop2: [2051]:136421085 (/root/CentOS-7-x86_64-DVD-1611.iso)

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:
# mkdir /mnt/cent_disk
# mount /dev/loop2 /mnt/cent_disk/
mount: /dev/loop2 is write-protected, mounting read-only
# ls /mnt/cent_disk/ // Check the content of mount point
CentOS_BuildTag EULA ...

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/cent_disk/
# losetup -d /dev/loop2
# losetup -a // Make sure the loop2 had been detached


沒有留言:

張貼留言

[ FP with Java ] Ch1 - What is functional programming

Preface   This chapter covers: ( Functional Programming in Java )  *  The benefits of functional programming *  Problems with side ef...