DEV Community

Cover image for LVM (Quick Snippet)
Ashish R Bhandari
Ashish R Bhandari

Posted on • Updated on

LVM (Quick Snippet)

  1. Physical Volume (PV)
  2. Volume Group (VG)
  3. Logical Volume (LV)

All PV COMMANDS

pvcreate /dev/sdb2
Enter fullscreen mode Exit fullscreen mode

All VG COMMANDS

Syntax: vgcreate NEW_VG_NAME PV_NAME

vgcreate ubuntu-vg /dev/sdb2 
Enter fullscreen mode Exit fullscreen mode

Syntax: vgextend OLD_VG_NAME PV_NAME

vgextend ubuntu-vg /dev/sdb3 
Enter fullscreen mode Exit fullscreen mode

ALL LV COMMANDS

Syntax: lvcreate -n NEW_LV_NAME -L SIZE(G,M) EXISITING_VG_NAME

lvcreate -n app-log-lv -L 12G ubuntu-vg 
Enter fullscreen mode Exit fullscreen mode

Syntax: lvextend [LEFT TO ADD]

[LEFT TO ADD]
Enter fullscreen mode Exit fullscreen mode

lvreduce

# Check First
lvs

LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
app-log-lv ubuntu-vg -wi-ao---- <12.00g


# Perform Reduce
lvreduce --resizefs -L -1G ubuntu-vg/app-log-lv


# Check Again
lvs

LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
app-log-lv ubuntu-vg -wi-ao---- <11.00g



Enter fullscreen mode Exit fullscreen mode

A Quick Setup

Step1: Make Sure a Disk is Present or Create it using pvcreate

pvcreate /dev/sdb2
Enter fullscreen mode Exit fullscreen mode

Note: Check all PVs
commands: pvs , pvscan , pvdisplay

Step2: Make Sure a Volume Group(VG) is Present or Create it using vgcreate

vgcreate ubuntu-vg /dev/sdb2 
Enter fullscreen mode Exit fullscreen mode

Note: Check all VGs
commands: vgs , vgscan , vgdisplay

Step3: Make Sure a Logical Volume(LV) is Present or Create it using lvcreate

lvcreate -n app-log-lv -L 12G ubuntu-vg 
Enter fullscreen mode Exit fullscreen mode

Note: Check all LVs
commands: lvs , lvscan , lvdisplay

Step4: Make Sure it has a File System(fs) else provide one using mkfs.ext4 LV_PATH

#Get the LV_PATH
lvdisplay
#OutPut [It will show the LV Path]

  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/app-log-lv
  LV Name                app-log-lv
  VG Name                ubuntu-vg
  LV UUID                bGNxCm-Beki-UX3D-HSXm-NU3k-YWs0-1ru2Tp
  LV Write Access        read/write
  LV Creation host, time tester, 2020-11-04 10:52:02 +0530
  LV Status              available
  # open                 1
  LV Size                12 GiB
  Current LE             2092
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:8

Enter fullscreen mode Exit fullscreen mode

Give File System (fs)

mkfs.ext4 /dev/ubuntu-vg/app-log-lv
Enter fullscreen mode Exit fullscreen mode

Note: Check all mkfs
commands: mkfs.ext4, mkfs -t ext4

Step5: Make Sure it is MOUNTED(mount) if NOT mount it to a DIRECTORY

so that any changes made in directory is reflected in the Disk

Syntax: mount LV_Path DIR_PATH

# Create Dir If You don't have: 
mkdir /var/log/app-logs
mount /dev/ubuntu-vg/app-log-lv /var/log/app-logs

Enter fullscreen mode Exit fullscreen mode

Step6: Make Sure it is PERMANENTLY MOUNTED(fstab) if NOT mount it PERMANENTLY

Quickest: Copy New Data from mtab to fstab

cat /etc/mtab

/dev/mapper/ubuntu-vg-app-log-lv /var/log/app-logs ext4 rw,relatime,data=ordered 0 0

Enter fullscreen mode Exit fullscreen mode
# Add some Here
vim fstab

/dev/mapper/ubuntu-vg-app-log-lv /var/log/app-logs ext4 rw,relatime,data=ordered 0 0

Enter fullscreen mode Exit fullscreen mode

Restart & Check

Top comments (0)