Create Loop device volumes on CentOS 7 from files

It has been a while since I wrote my previous article. The substance for this article came from the video on YouTube here:

https://www.youtube.com/watch?v=dd08qoOyTCA

What was I trying to do:

As a part of production docker configuration, I was playing with devicemapper storage drivers for creating a persistent storage area for containers. Docker documentation allows you to configure both block volumes and loop-lvm volumes. loop-lvm volumes are mainly used for testing purposes and hence the research.

How does this work:

A bit of an intro to volumes in CentOS 7 Linux.
One or more physical Volumes are added to Logical Volume Groups and then within the Logical Volume Group, one or more Logical volumes are created.
In our case the situation is a bit different:
Remember to run all the following commands as root user
  • We create a simple file called raid-0 using the touch command like so

touch raid-0

  • Then we use the dd command to copy some portion of the root device to this file. We are creating a file that is 200 MB in size (2M * 100)
dd if=/dev/sda of=raid-0 bs=2M count=100
  • We then copy this raid-0 file 3 times to create 4 files of 200MB each
cp raid-0 raid-1
cp raid-0 raid-2
cp raid-0 raid-3
  • We then create 4 loop devices using the losetup command from these 4 files
losetup /dev/loop0 raid-0
losetup /dev/loop1 raid-1
losetup /dev/loop2 raid-2
losetup /dev/loop3 raid-3
  • Verify that the devices are created correctly
[root@centos75 ~]# losetup -a
/dev/loop0: [64768]:17786836 (/root/raid-0)
/dev/loop1: [64768]:17786805 (/root/raid-1)
/dev/loop2: [64768]:17013266 (/root/raid-2)
/dev/loop3: [64768]:17013268 (/root/raid-3)
  • Create 4 physical volumes based on these loop devices
pvcreate /dev/loop{0,1,2,3}
  • Verify that the physical devices exist. Ignore the original device volume /dev/sda2 below and also a step below that – which adds the 4 physical volumes to the same Logical Volume Group
[root@centos75 ~]# pvs
  PV         VG              Fmt  Attr PSize   PFree
  /dev/loop0 vg00            lvm2 a–  196.00m    0 
  /dev/loop1 vg00            lvm2 a–  196.00m    0 
  /dev/loop2 vg00            lvm2 a–  196.00m    0 
  /dev/loop3 vg00            lvm2 a–  196.00m    0 
  /dev/sda2  centos_centos75 lvm2 a–   18.00g    0 
  • Create a volume group out of these 4 physical devices now. See the above message after the command has been executed
vgcreate vg00 /dev/loop{0,1,2,3}
  • The following command will confirm the created Logical Volume Group. Ignore the root volume group starting with centos..
[root@centos75 ~]# vgs
  VG              #PV #LV #SN Attr   VSize   VFree
  centos_centos75   1   2   0 wz–n-  18.00g    0 
  vg00              4   3   0 wz–n- 784.00m    0 
  • Issuing a pvscan command will also show the full arrangement
[root@centos75 ~]# pvscan
  PV /dev/loop0   VG vg00              lvm2 [196.00 MiB / 0    free]
  PV /dev/loop1   VG vg00              lvm2 [196.00 MiB / 0    free]
  PV /dev/loop2   VG vg00              lvm2 [196.00 MiB / 0    free]
  PV /dev/loop3   VG vg00              lvm2 [196.00 MiB / 0    free]
  PV /dev/sda2    VG centos_centos75   lvm2 [18.00 GiB / 0    free]
  Total: 5 [<18 .77="" 0="" 5="" font="" gib="" in="" nbsp="" no="" use:="" vg:="">
  • Issue the following commands to create 3 different logical volumes within the same volume group to use the entire size of the volume group. Please note: The maximum size that can be used by a volume group will be slightly less due to the amount of space taken for storing metadata about these volumes 
lvcreate -L 300M -n lv00 vg00
lvcreate -L 150M -n lv01 vg00
lvcreate -L 332M -n lv02 vg00
  • Issuing an lsblk command shows the entire arrangement
[root@centos75 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0  1.9G  0 part /boot
└─sda2        8:2    0   18G  0 part 
  ├─centos_centos75-root
            253:0    0   16G  0 lvm  /
  └─centos_centos75-swap
            253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0    8G  0 disk 
sr0          11:0    1 55.3M  0 rom  /run/media/user/VBox_GAs_5.2.
loop0         7:0    0  200M  0 loop 
└─vg00-lv00 253:2    0  300M  0 lvm  
loop1         7:1    0  200M  0 loop 
├─vg00-lv00 253:2    0  300M  0 lvm  
└─vg00-lv02 253:4    0  332M  0 lvm  
loop2         7:2    0  200M  0 loop 
├─vg00-lv01 253:3    0  152M  0 lvm  
└─vg00-lv02 253:4    0  332M  0 lvm  
loop3         7:3    0  200M  0 loop 
└─vg00-lv02 253:4    0  332M  0 lvm  
  • As can be seen above, we have created 4 physical volumes called loop0, 1, 2 and 3. We have added all the 4 physical volumes to a single Logical volume group called vg00. Within the volume group called vg00, we have created 3 logical volumes lv00, lv01 and lv02 with 300M, 150M and 332M sizes respectively. Please note:  Logical volume lv00 spans physical volumes loop0 and loop1, Logical volume lv01 is contained within physical volume loop2 and logical volume lv02 spans physical volumes loop1, loop2 and loop3 – nice!
  • Issue the following command to see the entire arrangement
[root@centos75 ~]# lvmdiskscan
  /dev/loop0                [     200.00 MiB] LVM physical volume
  /dev/centos_centos75/root [      16.00 GiB] 
  /dev/loop1                [     200.00 MiB] LVM physical volume
  /dev/sda1                 [       1.86 GiB] 
  /dev/centos_centos75/swap [       2.00 GiB] 
  /dev/loop2                [     200.00 MiB] LVM physical volume
  /dev/sda2                 [      18.00 GiB] LVM physical volume
  /dev/vg00/lv00            [     300.00 MiB] 
  /dev/loop3                [     200.00 MiB] LVM physical volume
  /dev/vg00/lv01            [     152.00 MiB] 
  /dev/vg00/lv02            [     332.00 MiB] 
  /dev/sdb                  [       8.00 GiB] 
  3 disks
  4 partitions
  0 LVM physical volume whole disks
  5 LVM physical volumes
  • To format them using the xfs filesystem, issue the following commands
[root@centos75 ~]# mkfs.xfs /dev/vg00/lv00
meta-data=/dev/vg00/lv00         isize=512    agcount=4, agsize=19200 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=76800, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
  • Repeat the same command for the remaining 2 logical volumes
mkfs.xfs /dev/vg00/lv01
mkfs.xfs /dev/vg00/lv02
  • If you want to mount these volumes into some branch do the following
mkdir /lvm
mkdir /lvm/disk1
mkdir /lvm/disk2
mkdir /lvm/disk3
  • Mount the volumes now
mount /dev/vg00/lv00 /lvm/disk1
mount /dev/vg00/lv01 /lvm/disk2
mount /dev/vg00/lv02 /lvm/disk3

Now – how I am going to map this to devicemapper will be updated below shortly … Have to save this page before I lose power ..