CentOS 7.x Create LVM partition on new disk

From Notes_Wiki

Home > CentOS > CentOS 7.x > System Administration > File system management > LVM > CentOS 7.x Create LVM partition on new disk

The below steps assume device name to be /dev/nda. In reality it might be /dev/sda or /dev/sdb for serial disks and /dev/xvda or /dev/xvdb for virtual disks, etc.

  1. yum -y install lvm2
  2. (Optional) Create partition to be used as physical volume. We can use entire disk for physical volume also. But creating partition seems like a good idea.
    fdisk /dev/nda
    #below on fdisk prompt
    n #for new partition
    p #for primary partition
    1 #for first primary partition
    #leave blank for default for first sector typically 2048
    #leave blank for default for last sector
    p #print partition table
    t #for changing partition type
    #automatically partition 1 will get selected, if that is the only partition
    8e #for Linux LVM
    w #for save and quit
  3. Rescan disk for kernel to learn the new partition
    partprobe /dev/nda
  4. Create LVM physical volume on new partition
    pvcreate /dev/nda1
    On iSCSI volumes try /dev/disk/by-path/ based device names.
  5. Create volume group using available physical partitions
    vgcreate vgtest /dev/nda1
    More than one physical volume can be part of same volume group. Thus to increase space we can add new disks, create physical volume on new disks and add new physical volume to existing vg using:
    vgextend vgtest /dev/ndb
  6. Create logical volume as specific size or as percentage of VG size using:
    lvcreate -L 900M -n lvtest1 vgtest
    or
    lvcreate -l '100%VG' -n lvtest1 vgtest
    Later on if volume group is extended using 'vgextend' the lvm partition can be extended using 'lvextend' with similar -l or -L options. Example lvextend command is:
    lvextend -l '100%VG' --resizefs vgtest/lvtest1
    Here -l is for extents and -L is for size (eg 30GB)
    We can increase/decrease size by specific value by prefixing +/-. Eg to increase by 30GB we can use lvextend -L +30G <device>
    --resizefs is useful in avoiding separate step for resizing filesystem. We can increase both LVM and filesystem with a single step.
  7. Display logical volume availability using:
    vgdisplay -v | grep LV | grep -i status
    If the status is *NOT available* then use following to activate vg
    vgchange -a y vgtest
    and validate that status changes to *available*
  8. Create partition on newly created logical volume:
    mkfs.ext4 /dev/vgtest/lvtest1
    Use filesystems such as ext3 or ext4 or xfs that can be resized online, if kernel supports it. ext3 or ext4 can also be shrinked if not mounted. For resizing use:
    resize2fs /dev/vgtest/lvtest1
    For brtfs refer https://www.suse.com/support/kb/doc/?id=000018798 and for xfs use xfs_grow
  9. For persistent mounting use /etc/fstab with UUID. For that list UUID with
    blkid
    and note UUID against /dev/mapper/vgtest-lvtest1.
  10. Then add following to /etc/fstab
    UUID="XXX" /mnt/backups2 ext4 defaults 0 0
    assuming above UUID, file-system and desired mount location of /mnt/backups2.
  11. Finally test by mounting using:
    mount -a
    df -h | grep backup
    to validate whether /etc/fstab entry is correct or not.


For reverse shriking or reducing existing partition on top of LVM refer https://www.systutorials.com/124416/shrinking-a-ext4-file-system-on-lvm-in-linux/

Also see:


Home > CentOS > CentOS 7.x > System Administration > File system management > LVM > CentOS 7.x Create LVM partition on new disk