CentOS 7.x Create LVM partition on new disk

From Notes_Wiki
Revision as of 11:49, 17 March 2019 by Saurabh (talk | contribs)

<yambe:breadcrumb self="Create LVM partition on new disk">CentOS_7.x_LVM|LVM</yambe:breadcrumb>

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' vgtest/lvtest1
  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
  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.


<yambe:breadcrumb self="Create LVM partition on new disk">CentOS_7.x_LVM|LVM</yambe:breadcrumb>