Site Tools


linux:general:lvm

This is an old revision of the document!


Logical Volume Manager (LVM)

What is LVM?

Create new Physical Volume (PV), Volume Group (VG) and Logical Volume (LV)

First you need to create a partition on a disk with type LVM
List the partitions

cat /proc/partitons

major minor  #blocks  name

  11        0    1048575 sr0
   8        0   31457280 sda
   8        1     194560 sda1
   8        2   31260672 sda2
   8       16  209715200 sdb
In this example the right one is “sdb”
fdisk /dev/sdb
n
p
enter
enter
t
1
8e
w
n = new partition, p = primary partition, enter to select default or select own range, t = change partition system id, 1 = id 1 but it might be another on your system, 8e = type LVM, w = write changes to disk
pvcreate /dev/sdb1
vgcreate data /dev/sdb1
lvcreate -l 100%FREE -n backup data
mkfs.ext3 -Ldata /dev/data/backup 
#mkfs.xfs -Ldata /dev/data/backup
vi /etc/fstab
/dev/mapper/data-backup /data                    ext3     defaults        0 0

Extend Physical Volume (PV)

Get the “PV Name”/ Partition via

pvdisplay

  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               datavg
  PV Size               <600.00 GiB / not usable 2.00 MiB
...

(Resize VMDK to 1,3TB) Check if your disk resize has already been recognized via

cat /proc/partitions

major minor  #blocks  name
...
   8       32  629145600 sdc
   8       33  629144576 sdc1
...

sdc is not 1,3TB, so get the scsi target via

lsscsi -v

...
[2:0:0:0]    disk    VMware   Virtual disk     1.0   /dev/sdc
  dir: /sys/bus/scsi/devices/2:0:0:0  [/sys/devices/pci0000:00/0000:00:17.0/0000:13:00.0/host2/target2:0:0/2:0:0:0]
...

Then rescan the adapter

echo 1 > /sys/class/scsi_disk/2\:0\:0\:0/device/rescan

The OS now knows that the disk has 1,3TB

cat /proc/partitions

major minor  #blocks  name
...
   8       32 1395864371 sdc
   8       33  629144576 sdc1
...

Resize the partition (see Create new Physical Volume (PV))
The partitiontable might have an old state, to refresh it try the following commands

partprobe

kpartx -a /dev/sdc

partx -a /dev/sdc
Looking at /proc/partitions should now list sdc and sdc1 with 1,3TB, if not you should restart your VM to refresh the table.

If both sdc and sdc1 are up to date, resize the PV

pvresize -v -d /dev/sdc1

    Archiving volume group "datavg" metadata (seqno 7).
    Resizing volume "/dev/sdc1" to 2791726694 sectors.
    Resizing physical volume /dev/sdc1 from 153599 to 340786 extents.
    Updating physical volume "/dev/sdc1"
    Creating volume group backup "/etc/lvm/backup/datavg" (seqno 8).
  Physical volume "/dev/sdc1" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

pvdisplay and vgdisplay are now resized

pvdisplay

 --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               datavg
  PV Size               <1.30 TiB / not usable <2.80 MiB


vgdisplay

  --- Volume group ---
  VG Name               datavg
  ...
  VG Size               <1.30 TiB

Extend Logical Volume (LV)

Create a new partition for free space (example above)
Then extend the LV

pvcreate /dev/sdb1
vgextend data /dev/sdb1
lvextend -l +100%FREE /dev/data/backup     
resize2fs /dev/data/backup

Remove Physical Volume (PV), Volume Group (VG) and Logical Volume (LV)

lvremove /dev/data/backup
vgremove data
pvremove /dev/sdb1

Other commands

Command Function
pvscan
vgscan
lvscan
pvdisplay show all PVs
vgdisplay show all VGs
lvdisplay show all LVs
vgchange -ay <VGNAME> activate LV
lvextend -L+5G /dev/data/backup extend by 5GB

Troubleshooting

Cant mount a logical volume

Description

Mounting the device leads to the following error

mount: special device /dev/mapper/VG01-LVbackup does not exist

Checking the LV via “lvdisplay” shows that its unavailable

LV Status              NOT available

Reason

Its deactivated.

Maybe you are missing the services in autostart? check /etc/boot.d/ for the following services and add them if needed:

chkconfig boot.lvm on
chkconfig boot.lvm_monitor on

Fix

Run the following command to activate it

vgchange -ay <VGNAME>

and remount

mount <PATH>


linux/general/lvm.1552311088.txt.gz · Last modified: 2019/03/11 14:31 by lunetikk