linux logical volume - ghdrako/doc_snipets GitHub Wiki

A physical volume is a partition or disk that’s managed by a logical volume. The physical volume looks exactly like a disk partition. For example, the partition /dev/sdd1 is also the physical volume /dev/sdd1.

A volume group contains physical volumes. A logical volume is equivalent to a partition on a disk but you create logical volume partitions from volume groups. Logical volumes contain filesystems that are named and those names can be descriptive.

Another way of thinking about logical block devices is that volume groups are analogous to disks and logical volumes are analogous to disk partitions.

The advantages of abstracting physical volumes into logical ones are that you have the flexibility of spanning multiple disks to create very large volume groups and that you can dynamically resize (Shrink and grow) logical volumes without taking the system offline to do so.

Implementing Logical Volumes

set up a logical volume from a raw disk ( convert a currently used disk to a logical volume cause lose all data)

lsblk # show avaliable disks
sudo pvcreate /dev/sdb # create the physical volume (PV), which is the basic block device onto which you’ll build logical volumes
sudo pvs # PV Show
sudo pvdisplay /dev/sdb #  see details about your physical volume. You’ll need to unmount /dev/sdb before proceeding else error
sudo vgcreate vgsw /dev/sdb #  create a Volume Group (VG) from the physical volume /dev/sdb
sudo vgs
sudo vgdisplay vgsw

The general syntax of the lvcreate command is as follows:

$ sudo lvcreate -L size -n lvname vg
  • size parameter in G for gigabytes or M for megabytes
  • lvname is the name you want to use for this logical volume (software-lv)
  • you must supply the volume group from which you want to create the logical volume (vgsw)
sudo lvcreate -L 1G -n software-lv vgsw
WARNING: xfs signature detected on /dev/vgsw/software-lv at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/vgsw/software-lv.
Logical volume "software-lv" created.

using a previously used disk cause that all the information on it will be overwritten and unrecoverable in this process.

sudo lvs
sudo lvdisplay /dev/vgsw/software-lv
sudo lvs /dev/vgsw/software-lv

The fourth step in this process is to create a filesystem on your logical volume.

sudo mkfs.ext3 /dev/mapper/vg00
sudo mkfs.xfs /dev/vgsw/software-lv

Create a mount point for your filesystem.

$ sudo mkdir /sw

Mount the filesystem onto the mount point.

$ lvdisplay                   # To lookup partition name
$ sudo mount /dev/vgsw/software-lv /sw
$ mount |grep software
/dev/mapper/vgsw-software--lv on /sw type xfs \
(rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)

Check available space on the device.

$ df -h /sw
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vgsw-software--lv 1014M 40M 975M 4% /sw

The final step is to add the filesystem and its mount point to /etc/fstab to make the mount persistent. My entry looks like the following:

/dev/vgsw/software-lv /sw xfs defaults 0 0

NOTE You don’t use the UUID for defining logical volumes in /etc/fstab. Instead, you use the device name.

Extending a Logical Volume

To extend volume, you can use one the following general commands:

$ sudo lvextend -L +size(M or G) lvname
$ sudo lvextend -l +100%FREE lvname

use the -l (extents) option rather than a specific size so consume the rest of the free space on the device.

$ df -h /sw
sudo lvextend -l +100%FREE /dev/vgsw/software-lv
sudo lvextend -l +100%FREE /dev/vgsw/software-lv

The lvextend command extends the logical volume to its maximum capacity but a df shows the same amount of available space.

$ df -h /sw

You have extended the logical volume but not the filesystem. Now resize the filesystem using the xfs_growfs command with no size parameter. Not specifying a size parameter with -D size, xfs_growfs will extend the filesystem to its maximum value.

$ sudo xfs_growfs /dev/vgsw/software-lv

** NOTE ** You cannot shrink an XFS volume.

resize2fs /dev/vda

Adding Disks to Existing Volume

Via: LVM - Add another disk

# Setup partition with (use parted for >2TB)
(parted) mklabel gpt       # only when >2TB
(parted) mkpart primary lvm 0 4T    # setup disk full size (e.g. 4TB)

pvcreate /dev/sdb1       # Create physical LVM disk
vgextend vg01 /dev/sdb1      # Add to volume group
vgextend -L +4t /dev/mapper/vg01-lvdata  # Extend your volume 
resize2fs /dev/mapper/vg01-lvdata   # Auto-resize file system