Manage loop device on CentOS7
Linux loop device provide a way to mount ordinary or sparse as block device. This feature also provide us easy way to mount iso images present on local disk. In this post we will see HowTo manage loop device to mount sparse file as block device.
We can add, list, delete and locate sparse file as block device and manage them in proper way. We an also use these block device for test purpose, in case we don’t have disk to work on.
We are using CentOS7 for this post.
# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) # uname -r 3.10.0-693.2.2.el7.x86_64 # which losetup /sbin/losetup # rpm -qf /sbin/losetup util-linux-2.23.2-43.el7.x86_64
First we need to create Sparse file that we manage as block device with help of loop.
# dd if=/dev/zero of=/data/Disk_1 bs=1M seek=10240 count=0 0+0 records in 0+0 records out 0 bytes (0 B) copied, 0.000590667 s, 0.0 kB/s # dd if=/dev/zero of=/data/Disk_2 bs=1M seek=10240 count=0 0+0 records in 0+0 records out 0 bytes (0 B) copied, 0.000776487 s, 0.0 kB/s # ls -lh total 0 -rw-r--r--. 1 root root 10G Sep 30 18:19 Disk_1 -rw-r--r--. 1 root root 10G Sep 30 18:19 Disk_2
In above commands, we have created two sparse files, 10G both. Later we would use these files to create disk that could work on them.
Let’s see last available loop device for mount.
# losetup -f /dev/loop0
Let’s start work to mount loop device for sparse files.
# losetup /dev/loop0 /data/Disk_1 # losetup -l NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE /dev/loop0 0 0 0 0 /data/Disk_1 # losetup -f /dev/loop1
Now we can see /dev/loop0 is mount on /data/Disk_1 and next available loop device is /dev/loop1, So we have mounted both devices on sparse files.
# losetup -l NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE /dev/loop0 0 0 0 0 /data/Disk_1 /dev/loop1 0 0 0 0 /data/Disk_2
Now we need to see, how we could work on these devices. we can work them as LVM disk mount for rlocal storage or iscsi disk use to share remote disk.
We can detach loop device like below. With -d options we can detach specific device.
# losetup -d /dev/loop0 # losetup -a /dev/loop1: [64769]:67731536 (/data/Disk_2)
With -D options we can detach all loop device.
# losetup -D
Leave a Reply