LVM Deep Dive

LVM Deep Dive

Logical Volume Management

A Brief  on LVM

  1. LVM is a virtual layer that sits sandwiched between the operating system and storage devices i.e., hard disks.
  2. It creates groups of hard disks called volume groups (VGs). With these volume groups , you can create partitions called Logical Volumes (LVs)
  3. The operating system will never know the difference , the behavior is same as accessing any regular disk/partions.
    The benefits of LVM over regular file systems include : dynamic resizing of VGs and live snapshots of partitions without unmounting them.
  4. LVM is built into the Linux Kernel, and it uses the same mechanism to manage disk access that file system drivers use.
  5. LVM has two types of volumes ( physical and logical volumes ). Physical volumes are the actual disks that store your LVM set.
  6. Logical Volumes are the volumes that you actually mount and store your data in.
  7. In between the physical and logical volumes lies the volume group. You create volume group on one or more physical volumes, and then you create logical volumes inside volume group.
  8. Logical volumes must reside inside their volume group. So volume group serves as an elastic container for your volumes.

Setting up LVM

To begin using LVM, you need some physical volumes first.
We are having following structure

Node size code Name
Sda1 512M EF EFI system
Sda2 127G 07 Microsoft basic data
Sda3 337G 8E Linux LVM

1. Create Physical Volumes

#pvcreate /dev/sda3

If SSD in use, want to make sure blocks are aligned to 1MB , you might want to run the following command instead.

#pvcreate --dataalignment 1m /dev/sda3
#pvdisplay

2. Create Volume group as follows

#vgcreate volgroup /dev/sda3

3. Create standard volume  as follows

#lvcreate –L 64G volgroup –n logicalvolume

Logical volume is available in “/dev/volgroup/logicalvolume or /dev/mapper/volgroup-logicalvolume”

4. Now create a filesystem on it and mount it as follows

# mkfs.ext4 /dev/volgroup/logicalvolume
# mount /dev/volgroup/logicalvolume /mnt

 Creating swap partitions inside LVM volume group

This takes away a lot of flexibility offered by LVM, but to minimize disk access time, this is good option.

#lvcreate –C y –L 4G volgroup –n swap

Extending LVM sets

Suppose if we want additional 20 gigabytes on the logical volume we created. The command is as follows

#lvextend –L +20G volgroup/logicalvolume

Now logicalvolume has just grown, in the mean while we need to grow the filesystem inside it to make use of additional disk space.

#resize2fs /dev/volgroup/logicalvolume

You can resize a volume once they are created. You can do both shrink and expand. The following command will shrink a 20Gb volume to 19GB.

#resize2fs /dev/volgroup/logicalvolume 19GB

Now we reduce logical volume as follows

#lvreduce –L 20GB volgroup/logicalvolume

As a short cut if you want to grow your volume to maximum size , taking all space in the volume group , run the command as follows:

#lvextend –l +100%FREE volgroup/logicalvolume

If you want to shrink 2G , you can run as follows

#lvresize –L +20G volgroup/logicalvolume

Adding additional  storage to LVM

If your disk is full and you want to add new disk and want to dedicate complete disk to LVM.
Let’s say the drive shows up as /dev/sdb .

Enable id 8e00 using t option using fdisk command

Create physical volume on /dev/sdb1

 #pvcreate /dev/sdb1

Here we are not creating new volume group , instead we will tell the old volume group to use the space available in the new physical volume like shown below

 #vgextend volgroup /dev/sdb1

It’s that simple .You can go ahead and grow your volumes as you need

LVM and Migration

LVM has even more magic .It allows migration with zero down time. For example consider a cases. If your hard disk has started making clicking noises and
retrieve it before it crashes for good, since you are using LVM ,you don’t have to worry

Let’s say /dev/sdb1 is failing .If you have enough space on the rest of physical volumes in the volume group. Now just you need to do is use the following commands

#pvmove /dev/sdb1

All the data from /dev/sdb1 will be redistributed amongst the other physical volumes that constitute your volume group . Now just kick the physical volume out of your volume group , as follows

#vgreduce volgroup /dev/sdb1

If you didn’t have luck to have free space in your volume group? One way to addressing this would be to buy a new disk, add it your volume group then run pvmove and rest of the commands.

You could also move content from one physical volume to another physical volume in the same volume group.
Let’s say the new device node is /dev/sdc1

#vgextend volgroup /dev/sdc1

#pvmove /dev/sdb1 /dev/sdc1

You can remove /dev/sdb1 from your volume group. Remember that pvmove moves data, block by block, extent by extent. Pvmove does take a lot of time and uses lot of I/O disk rate ,so disk access is going to be very slow while pvmove does its thing.

LVM snapshots

LVM snapshots are ‘copy-on- write’, which means that when you create a snapshot, it hardly takes up any space because the files in the snapshots are just pointers to the existing data. As you modify data in the snapshot ,the pointers for the changed data begin to the new files, While the pointers to the unchanged data continue to point to the old files. To create new snapshot , you need to decide how much write-space you want to dedicate to it.

If you give 2GB of write space to a snapshot, you will be able to accumulate 2 GB of changes to your snapshots.

This is how you create snapshot

#lvcreate –L 2G –n logicalvolumesnapshot –snapshot /dev/volumegroup/logicalvolume

Snapshot is available at /dev/volumegroup/logicalvolumesnapshot for you to mount and use.

Encryption Logical Volume

If we want to encrypt logicalvolume , start up by setting up LUKS as shown below

#cryptsetup luksFormat /dev/volgroup/logicalvolume

Once you are done , you can unlock the volume with the following command

#cryptsetup open –type luks /dev/volumegroup/logicalvolume cryptlogicalvolume

This makes encrypted volume available as /dev/mapper/cryptlogicalvolume. Create filesystem on it, as follows

#mkfs.ext4 /dev/mapper/cryptlogicalvolume

Now mount the volume with following command shown below

#mount /dev/mapper/cryptlogicalvolume /mnt

If you want to grow logical volume , begin by un mounting the filesystem and locking the volume with the following command

#umount /dev/mapper/cryptlogicalvolume
#cryptsetup luksclose /dev/mapper/logicalvolume

Then resize the actual logical volume ,with the following command

#lvresize +10G volgroup/logicalvolume

Then unlock the encrypted volume again

#cryptsetup open –type luks /dev/volgroup/logicalvolume cryptlogicalvolume
#umount /dev/mapper/cryptlogicalvolume

Leave a Reply

Your email address will not be published. Required fields are marked *