Linux LVM Command Examples

Logical Volume Management Example 02

In this example we are going to create a loopback device to act as our disk. To do this we will use the "losetup" command. Losetup is used to associate loop devices with regular files or block devices. We are using this approach so that if you are not using a virtual environment where you can add disk, you can still try out the LVM commands.

Creating image files

The first step is to create an image file that will represent our disk:

Check what space you have available to your system with the "df -h" command:


# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos01-lv_root
                      6.0G  633M  5.1G  11% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot

From the above output we can see that we have 5.1GB of space available.

Now lets create our disk:


# fallocate -l 3G disk1.img

The above will create a file approximately 3GB in size called "disk1.img". This file will be created in the "/tmp" area as this is our current directory. We can use the "ls -lh" list command to display our new files attributes.


# ls -lh disk1.img
-rw-r--r--. 1 root root 3.0G Apr 11 08:54 disk1.img

Configure loopback device

To associate our image file with a loopback device we use the command "losetup"


# losetup /dev/loop0 /tmp/disk1.img

# losetup --show /dev/loop0
/dev/loop0: [fd00]:266586 (/tmp/disk1.img)

From the above output you can see the device "dev/loop0" is now associated to the file /tmp/disk1.img

Create a Physical Volume (PV)

Next we will use the "pvcreate" command to create our Physical Volume:


# pvcreate /dev/loop0
  Writing physical volume data to disk "/dev/loop0"
  Physical volume "/dev/loop0" successfully created

We can use the "pvs" command to view our results from the above:


# pvs
  PV         VG          Fmt  Attr PSize PFree
  /dev/loop0             lvm2 a--  3.00g 3.00g
  /dev/sda2  vg_centos01 lvm2 a--  7.51g    0

From the above "PV" indicates the path, "VG" indicates the Volume Group our PV belongs to. PSize and PFree indicate the amount of space available for this PV.

We can also use the "pvdisplay" command for further information:


# pvdisplay

 "/dev/loop0" is a new physical volume of "3.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/loop0
  VG Name
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               QAQyHk-nXFh-QV1k-TAV0-WoW7-6kiS-OFxwac

Note, the first three lines after the "--- NEW Physical volume ---" relate to the name of your PV, the VG (Volume Group) the PV is associated with and the size of the PV. In this case our PV is not currently associated with a Volume Group. This is also confirmed by the "NO" in the allocatable row.

Create a Volume Group

Next we need to add our PV to a Volume Group. To do this we use the "vgcreate" command:


# vgcreate vg01 /dev/loop0
  Volume group "vg01" successfully created

We have now associated the volume group "vg01" with our device "/dev/loop0". We can use the "VGS" command to check our results:


# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          1   0   0 wz--n- 3.00g 3.00g
  vg_centos01   1   2   0 wz--n- 7.51g    0

From the above we can see that Volume Group "vg01" has one PV associated. We can also see that there are no Logical Volumes (LV) located within the VG.

As a check we can run the "pvs" command again. This time the output shows that our PV is now associated with Volume Group "vg01".


# pvs
  PV         VG          Fmt  Attr PSize PFree
  /dev/loop0 vg01        lvm2 a--  3.00g 3.00g
  /dev/sda2  vg_centos01 lvm2 a--  7.51g    0

Create a Logical Volume

Next we will define our Logical Volume within our Volume Group. This is where we will place our filesystem. To create our Logical Volume we will use the command "lvcreate". We are initially going to create this with a size of "2GB":


# lvcreate -n lvdata01 -L 2GB vg01
  Logical volume "lvdata01" created

Now we can issue the command "lvs" to display our new Logical Volume:


# lvs
  LV       VG          Attr     LSize Pool Origin Data%  Move Log Copy%  Convert
  lvdata01 vg01        -wi-a--- 2.00g

Now if we issue the "vgs" command again, we can see that we have a Logical Volume "vg01" with one associated PV and LV. We can also see that there is around "1Gb" of free space still available:


# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          1   1   0 wz--n- 3.00g 1020.00m

For a more detailed overview of our layout we can issue the "vgdisplay" command:


# vgdisplay
--- Volume group ---
  VG Name               vg01
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               3.00 GiB
  PE Size               4.00 MiB
  Total PE              767
  Alloc PE / Size       512 / 2.00 GiB
  Free  PE / Size       255 / 1020.00 MiB
  VG UUID               V8Hfck-fl1W-m8ax-vLBp-gPSV-RR2O-LvRPyb

Create a filesystem

Our next step is to create a filesystem on our Logical Volume "/dev/vg01/lvdata01". The command used for creating filesystems is "mkfs". From my test CentOS system I have the following filesystem options available: (These may vary depending on your installation and distribution)


# mkfs
mkfs          mkfs.ext2     mkfs.ext4     mkfs.xfs
mkfs.cramfs   mkfs.ext3     mkfs.ext4dev

We now issue the command: mkfs.ext3 /dev/vg01/lvdata01


# mkfs.ext3 /dev/vg01/lvdata01
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Create a Mount Point

To use our newly created filesystem we need to create a mount point for our Logical Volume. To create our mount point we use the command "mkdir":


# mkdir /var/myspace

Once this directory has been created, we next need to mount our Logical Volume to this point.

To do this we issue the "mount" command: "mount /dev/vg01/lvdata01 /var/myspace"


# mount /dev/vg01/lvdata01 /var/myspace

Now we can check that our filesystem is mounted and available for use with the "df -h" command:


# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos01-lv_root
                      6.0G  3.7G  2.1G  65% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvdata01
                      2.0G   68M  1.9G   4% /var/myspace

From the above we can see that we have 1.9G of available space within our storage area "/var/myspace"

Mount Table

Next we will need to add our details of our device into the mount table. This will ensure that our device is automatically mounted again at boot time. The location of the mount table is: "/etc/fstab"

Add the following line into "/etc/fstab":


/dev/vg01/lvdata01                    /var/myspace                   ext3    defaults        0 0

As this is only a mock-up of a real configuration, I am going to skip this section on my test system. On a real system you would add your details.

Increasing Space in a Logical Volume

LVM is designed to make the process of adding space to a filesystem relatively easy. In our initial allocation, we created a disk with 3GB of space, however we only allocated 2GB of the available space. If we wanted to allocate more space, we would use the "lvextend" command. Although many filesystems support online resizing, for our example I am going to unmount our device first:


# umount /var/myspace

If we use the "lvextend" command, it doesn't automatically resize the filesystem unless we supply the "-r" option. Lets check our available space again:


# lvs
  LV       VG          Attr     LSize Pool Origin Data%  Move Log Copy%  Convert
  lvdata01 vg01        -wi-a--- 2.00g
  lv_root  vg_centos01 -wi-ao-- 6.04g
  lv_swap  vg_centos01 -wi-ao-- 1.47g

# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          1   1   0 wz--n- 3.00g 1020.00m
  vg_centos01   1   2   0 wz--n- 7.51g       0

# lvextend /dev/vg01/lvdata01 -L2.5G
  Extending logical volume lvdata01 to 2.50 GiB
  Logical volume lvdata01 successfully resized

We can now see that our Logical Volume has now changed to 2.5GB and our free space within the Volume Group "vg01" has now changed to reflect this change:


# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          1   1   0 wz--n- 3.00g 508.00m
  vg_centos01   1   2   0 wz--n- 7.51g      0

As we mentioned earlier, extending the Logical Volume does not automatically extend the filesystem unless the "-r" option is applied. If you do not use the "-r" option, then you will need to use the "resize2fs" command. However, we will need to run the "e2fsck" command first as a check:


# e2fsck -f /dev/vg01/lvdata01
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg01/lvdata01: 11/131072 files (0.0% non-contiguous), 25405/524288 blocks

Now we can run the "resize2fs" command:


# resize2fs /dev/vg01/lvdata01
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg01/lvdata01 to 655360 (4k) blocks.
The filesystem on /dev/vg01/lvdata01 is now 655360 blocks long.

Next we need to mount our device again:


[root@centos var]# mount /dev/vg01/lvdata01 /var/myspace
[root@centos var]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos01-lv_root
                      6.0G  3.7G  2.1G  65% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvdata01
                      2.5G   68M  2.3G   3% /var/myspace

Reducing amount of Space

To reduce the amount of space available, we need to use the "lvreduce" command. As a precaution, we will unmount our device first:


# umount /var/myspace

# lvreduce /dev/vg01/lvdata01 -r -L 2G
fsck from util-linux-ng 2.17.2
/dev/mapper/vg01-lvdata01: 11/163840 files (0.0% non-contiguous), 27461/655360 blocks
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg01-lvdata01 to 524288 (4k) blocks.
The filesystem on /dev/mapper/vg01-lvdata01 is now 524288 blocks long.

  Reducing logical volume lvdata01 to 2.00 GiB
  Logical volume lvdata01 successfully resized

As you can see from the above output, we have used the "-r" option with the command:

lvreduce /dev/vg01/lvdata01 -r -L 2G

This takes care of our resizing for us by reducing the filesystem and then the Logical Volume.

Now if we check using the "lvs" and "vgs" commands we can see our changes.


# lvs
vgs  LV       VG          Attr     LSize Pool Origin Data%  Move Log Copy%  Convert
  lvdata01 vg01        -wi-a--- 2.00g
  lv_root  vg_centos01 -wi-ao-- 6.04g
  lv_swap  vg_centos01 -wi-ao-- 1.47g

# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          1   1   0 wz--n- 3.00g 1020.00m
  vg_centos01   1   2   0 wz--n- 7.51g       0

Adding Another Disk

One of the most frequent tasks an administrator will be asked is to add extra space. To continue with our example, I will add an extra 1GB of disk to a loopback device "/dev/loop1".


# fallocate -l 1G /tmp/disk2.img

Now we will associate this image file with "/dev/loop1":


# losetup /dev/loop1 /tmp/disk2.img

# losetup --show /dev/loop1
/dev/loop1: [fd00]:266761 (/tmp/disk2.img)

Create a PV for the new loopback device:


# pvcreate /dev/loop1
  Writing physical volume data to disk "/dev/loop1"
  Physical volume "/dev/loop1" successfully created

We can issue "pvs" to display our new device:


# pvs
  PV         VG          Fmt  Attr PSize PFree
  /dev/loop0 vg01        lvm2 a--  3.00g 1020.00m
  /dev/loop1             lvm2 a--  1.00g    1.00g
  /dev/sda2  vg_centos01 lvm2 a--  7.51g       0

Now we need to add this device to our existing Volume Group "vg01":


# vgextend vg01 /dev/loop1
  Volume group "vg01" successfully extended

# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          2   1   0 wz--n- 3.99g 1.99g
  vg_centos01   1   2   0 wz--n- 7.51g    0

We can now see that we have two Physical Volumes associated now with the Volume Group "vg01".

Now lets extend our Logical Volume to use some of this space. Again we use the "lvextend" command with the "-r" option:


# lvextend /dev/vg01/lvdata01 -r -L 3.5G
fsck from util-linux-ng 2.17.2
/dev/mapper/vg01-lvdata01: clean, 11/131072 files, 25405/524288 blocks
  Extending logical volume lvdata01 to 3.50 GiB
  Logical volume lvdata01 successfully resized
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg01-lvdata01 to 917504 (4k) blocks.
The filesystem on /dev/mapper/vg01-lvdata01 is now 917504 blocks long.

Now we mount our device again:


# mount /dev/vg01/lvdata01 /var/myspace

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos01-lv_root
                      6.0G  4.7G  1.1G  82% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   38M  422M   9% /boot
/dev/mapper/vg01-lvdata01
                      3.5G   69M  3.3G   3% /var/myspace

We can now see the extra space is available and ready for use. Now if we issue the "pvs" command and "vgs" command we can see our changes are now reflected:


# pvs
  PV         VG          Fmt  Attr PSize    PFree
  /dev/loop0 vg01        lvm2 a--     3.00g      0
  /dev/loop1 vg01        lvm2 a--  1020.00m 504.00m
  /dev/sda2  vg_centos01 lvm2 a--     7.51g      0

# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg01          2   1   0 wz--n- 3.99g 504.00m
  vg_centos01   1   2   0 wz--n- 7.51g      0