Working with LVM by examples

Some of the important commands to verify (check) existing block devices, partitions and LVM details:

  • To list current block device details including partitions
  • to view details of existing volume group:
  • alternatively (more details) use vgs command:

Problem 1: Creating Logical Volume

You are required to create a logical volume of 300m, where volume group name should be vg1 and logical volume name should be vg1

  1. Allocating partition for physical volume
    fdisk /dev/sdb #assuming that /dev/sdb don’t have partition yet, in my case i have verified it with lsblk command.

    • to print existing partition. In my case no partitions exist.
      choose p
    • To create new partition
      choose n
    • choose partition type (primary or logical):
      choose p
    • choose first secton
      press enter to choose default
    • choose last sector of partition. you can use +size{k,m,g}
      type +300m
    • you need to change file system type to Linux lvm
      press t and enter 8e
    • you need to save and exit
      press w
    • Now you are out of fdisk command, to you need to update kernel for changes in partition with partprobe command.
  2. Creating physical volume (you can avoid this steps, creating volume group automatically creates physical volume if not already created):
    pvcreate /dev/sdb1
  3. Creating volume group
    vgcreate vg1 /dev/sdb1
  4. Creating logical volume
    lvcreate -n mylv1 -L 300m vg1
  5. Creating file system
    mkfs.xfs /dev/vg1/mylv1
  6. Mounting to /data
    echo “/dev/vg1/mylv1 /data xfs defaults 0 0″ >> /etc/fstab
    mount -a
    Note: using UUID will be the safer approach.

Problem 2: Extending a Logical Volume

You are required to extend mylv1 to 500mg total size.
You are required to extend mylv1 with additional 200MB size.
You are required to extend mylv1 with 50 additional extents (assuming default 4MB PE size)
You are required to extend mylv1 to 125 total extents.

  1. use pvs command to view details of pvs. If you don’t have additional space add another partition. In my case I have /dev/sdc1 (new disk)
  2. Create partition following step 1 in problem1.
    Note: As I have already mentioned that pvcreate is not requried, as vgcreate automatically calls it. I am skipping it.
  3. Extending Volume group vg1
    vgextend vg1 /dev/sdc1
  4. Extending logical volume
    lvextend -r -L +200m /dev/vg1/mylv1

    xfs_grow /dataalternatively,  you can use following command to extend the desire paritition to 500MB
    lvextend -r -L 500m /dev/vg1/mylv1
    or using -l (125 extent x 4MB = 500MB)
    lvextend -r -l 125 /dev/vg1/mylv1
    or, (adding 50×4=200m with existing size)
    lvextend -r -l +50 /dev/vg1/mylv1

Some related topics which are less important

  • Changing vg details

    • To deactivate vgs
      vgchange -a n vg1
    • to activate
      vgchange –available y vg1
  • Taking a backup using snapshots
    • to take snapshon use lvcreate with  -s option. I am using 50 extent which will be 200, if PE size is 4MB.
      lvcreate -s -l 50  -n backup_of_mylv1 /dev/vg1/mylv1
Recent Posts
Recent Comments
    Updates on Recent activieies