LVM Commands & Scenarios
LVM Scenarios:
Creating a Physical Volume (PV):
- Create a partition using fdisk, and change the hex code of it to 8e.
- Save and exit the fdisk and update the partition table using partx –a command

- Create a PV on newly created partition i.e. /dev/sdb1.
- Verify it by pvs or pvdisplay command
#pvcreate <partition name>
#pvcreate /dev/sdb1

The above command will list all the PVs in the system, if you want to see the details only for a particular PV, then use
#pvdisplay <partition name> i.e. #pvdisplay /dev/sdb1
Creating a Volume Group (VG):
After creating a PV, the next step is to create a Volume Group or VG
To create a VG the syntax is
- #vgcreate <name for the VG> <partition name>
- #vgcreate iwayqvg /dev/sdb1
Verify it by using the following command
#vgs or #vgdisplay <vgname>

To check all the VGs detail you can also use the command
#vgdisplay
- It will list out all the VGs in the system in detail.
Logical Volume Creation:
The syntax for creating an LV is
#lvcreate –L <size of LV> -n <name for LV> <VG name>
#lvcreate –L 512M –n iwayqlv iwayqvg (To create a LV of 512MB)

Verify the LV by using the following commands
#lvs or

Adding File system to the LV and Mount:
- As per now we have our VG created so is our LV. In order make it accessible we need to format it with a file system like ext4 or ext3 or vfat.
- The syntax for formatting an LV is exactly like formatting a normal partition, Instead of /dev/partition name we use the path of LV that will be something like /dev/vg/lv
#mkfs.ext4 /dev/iwayqvg/iwayqlv

Mounting:
- Mounting an LV is exactly same like a normal partition, again the path for mounting will be # /dev/vg/lv
- Create a directory over which the LV should be mounted.
- #mount </dev/vgname/lvname> /directory name
- #mount /dev/iwayqvg/iwayqlv /iwayq
- Verify the mounting with mount command
- Make it a permanent mount by making an entry in /etc/fstab

Extending a Volume Group:
- Extending a volume group is actually adding a new PV to the volume group.
- To extend a volume group we need to create change its hex code to 8e and update the partition table using partx –a command
- Create a PV on the newly created partition using pvcreate command
- Add the partition to the VG using vgextend command, the syntax for it is
#vgextend <VG name> <partition name>
#vgextend iwayqvg /dev/sdb2
Verify with pvs command

Increasing the size of a logical volume:
- Sometimes the file system size may be full, so we need to increase the size of the LV to continue adding the data in it.
- The size of LV can be increased online, no downtime is required.
- Check the current size of the LV by using #df –h command.
- Increase the size of the LV by using lvextend or lvresize command, the syntax for it is
#lvextend –L <+addition size> </dev/vg/lv name> (syntax for lvresize is also same)
#lvextend –L +200M /dev/iwayqvg/iwayqlv
- Update the file system by using resize2fs command
#resize2fs /dev/vg/lv name
#resize2fs /dev/iwayqvg/iwayqlv
- Verify the change by using df –h command

Increasing the size of the LV and updating the file system:

Verify it by df –h

Reducing the size of an LV:
- Reducing the size of an LV is a bit complicated task, there are few things which you need to keep in mind before reducing the size of an LV.
- LV size cannot be reduced online, it requires a down time i.e. unmounting the file system.
- Organized the data before reducing the size of LV.
- Update the file system about the size. I.e. what its size will be after reduction.
- Finally reduce the size.
- If any of the above things are missed then it will be a mess, you may corrupt the file system and LV.
Let’s start the steps carefully
- Check the size of the lv using df –h command
- Unmount the LV using umount command
- Organize the data in LV by using e2fsck command
#e2fsck -f /dev/iwayqvg/iwayqlv
- Update the file system by using resize2fs command
#resize2fs /dev/iwayqvg/iwayqlv 500 (where 300M is the approximate total size of LV after reduction)
- Now reduce the size by using # lvreduce -L -200M /dev/iwayqvg/iwayqlv command
We know the size of LV is around 700MB, from previous picture in case of extending the size of LV.
Or else you can run df –h and verify it again
- Umount the LV by using umount command


Moving or Migrating the LV (data) from one pv to another:
- There might be a situation where the PV might be failing and it is required to be replaced, in such case, we need to migrate or move the data from such PV to the other PV.
The Steps to migrate the PV are:
- Access the mount point of failing PV and check the data in it,
- Verify the size of the PV by pvs command or pvdisplay command.
- Unmount the file system on that PV.
- Add new PV, which should be of the same size or higher than that of the replacing PV to the volume group.
- Migrate the PVs contents to the new PVusing following command
- #pvmove <Old PV> <New PV>
- Mount back the LV, access the mount point and verify the data in it.
- Remove the faulty PV from Volume Group


Deleting/Removing an LV:
To Delete/Remove an LV, first unmount the file system
- Remove the entry from /etc/fstab.
- Use the command lvremove i.e.
#lvremove <LV name>
#lvremove iwayqlv ( it will prompt to you to continue, press y to continue)
- Verify it by using lvdisplay command
Deleting a Volume Group:
- To delete the volume a group, make sure that if there is any LV in it, it should not be mounted. Because while removing a vg it wil no LV in our volume group, so we will not be concerned about it.
- To delete a VG, use the following command.
- #vgremove <vgname>
- #vgremove iwayqvg
Deleting a Physical Volume:
- Deleting a PV is very simple. The only thing we should check that the PV we are going to delete should not belong to any volume group. We can only delete a PV which is free.
- The syntax to delete a PV is
- #pvremove <PV name>
- #pvremove /dev/sdb1
- #pvremove /dev/sdb2
- #pvremove /dev/sdb{1,2} (To remove multiple PVs in one command)
Previous
Next