Friday, August 3, 2018

Logical Volume Manager (LVM)


Concepts
--------

PHYSICAL VOLUME (PV): physical devices, local disk or luns
|
|
VOLUME GROUP (VG): groups of pvs
|
|
LOGICAL VOLUME (LV): partitions inside vg
|
|
FILESYSTEM (FS): ext2/3/4,DOS,swap,etc..

Paritions
---------

/proc/partitions  ##contains list of known partitions to kernel

Listing/Viewing
fdisk   ##adds partition to a device
fdisk -l  ##lists available devices together with partition tables
fdisk -l | grep sd  ##filters scsi devices
cfdisk   ##similar to fdisk but with less features
 
## to lists partition codes
sfdisk -T
Modifying
## rescans the whole disk (useful in resizing VMDK disks)
echo 1 > /sys/block/sd/device/rescan
pvresize /dev/sdc
 
partprobe   ##force update the /proc/partition file without rebooting (no longer works for rhel6)
partx -a   ##force update the /proc/partition file without rebooting (works for rhel6)
Debugging
dumpe2fs   ##checks deleted partitions
e2fsck   ##checks if partition is readable

Physical Volumes
----------------

examples of PVs:

/dev/sdb
/dev/sdb[12]

Commands:

Displayng
## displays info reg physical volumes
pvdisplay  --> more detailed output
pvs  --> quick view
pvscan
pvdisplay --ignorelockingfailure  --> use incase of r/o filesystem

note: you need a physical device or a partition from a physical device to create a physical volume

here are the rest of pv commands:
[mvineza@secutil07 ~]$ ls /sbin/pv*
/sbin/pvchange  /sbin/pvcreate   /sbin/pvmove    /sbin/pvresize  /sbin/pvscan
/sbin/pvck      /sbin/pvdisplay  /sbin/pvremove  /sbin/pvs
[mvineza@secutil07 ~]$
Creating
## creates and display physical volume/s
pvcreate

## forcely initialize/reinitialize a physical device (for previously used devices)
pvcreate -ff
Removing
## forcely removes a physical volume from a volume group
pvremove
 
Volume Groups
-------------

commands:

Displaying
## displays vg info
vgdisplay
vgdisplay --> for a specific vg
vgdisplay -v   --> list also pvs and lvs associated with it
vgscan --> scan all disks for volume groups and rebuild caches
vgs --> quick view
 
here are list of vg commands:
$ ls /sbin/vg*
/sbin/vgcfgbackup   /sbin/vgck       /sbin/vgdisplay  /sbin/vgimport       /sbin/vgmknodes  /sbin/vgrename  /sbin/vgsplit
/sbin/vgcfgrestore  /sbin/vgconvert  /sbin/vgexport   /sbin/vgimportclone  /sbin/vgreduce   /sbin/vgs
/sbin/vgchange      /sbin/vgcreate   /sbin/vgextend   /sbin/vgmerge        /sbin/vgremove   /sbin/vgscan
$
Creating/Modifying
## creates vg
vgcreate   --> whole pv will be used for vg
vgcreate -c n   --> clustered environment, -c option signifies that vg is accessible by other nodes
vgcreate -s [MGK]   --> indicate the physical extent size
vgcreate -L size   --> if you don’t want to use the entire pv
 
## increase vg
vgextend
 
## activates a volume group
vgchange -a y
Deleting
## decrease vg
vgreduce
vgreduce --removemissing  --> for error similar to these: Couldn't find device with uuid F27gpk-Gpte-11yf-WTvH-94ez-QpnF-fkMmu3

Logical Volumes
---------------

Displaying
## displays info on logical volumes
lvdisplay /dev//
lvscan
lvs
 
here is a list of lv commands:

$ ls /sbin/lv*
/sbin/lvchange   /sbin/lvdisplay  /sbin/lvmchange    /sbin/lvmdump  /sbin/lvmsar    /sbin/lvrename  /sbin/lvscan
/sbin/lvconvert  /sbin/lvextend   /sbin/lvmconf      /sbin/lvmetad  /sbin/lvreduce  /sbin/lvresize
/sbin/lvcreate   /sbin/lvm        /sbin/lvmdiskscan  /sbin/lvmsadc  /sbin/lvremove  /sbin/lvs
$
Creating
## basic way of creating an LV
lvcreate [options]

## if you want to explicitly define its characteristics, use this
lvcreate -n -L [M|G]

## if you want to consume all the space in the volume group
lvcreate -n -l 100%FREE

Resizing
## increases size of a logical volume
lvextend -L +[MKG] /dev//
lvextend -L +[MKG] /dev/mapper/-
lvextend -L [M|G] /full/path/to/lv  --> sets the size of lv instead of adding space (example is with + sign above)
lvextend -L +50g /dev/VG1/LV1 --> for redhat 5.1
lvextend -l +100%FREE /dev/mapper/- --> extends LV to 100% of the free space in the VG

## increasing by pv (uses whole pv)
lvextend /dev/mapper/vg_root-lv_root /dev/sda4

## decreases size of a logical volume
lvreduce -L -[MKG] /dev//
lvreduce -L -[MKG] /dev/mapper/-

Filesystem
----------

commands:

Creating
## creates a filesystem on top of lv
mkfs -t /dev/mapper/-

## other way
mkfs. [options]         
-c     --> checks for bad blocks before the file system is created
-v    --> verbose output
-l      --> obtains a list of currently known bad blocks from the specified file
-t      --> enables you to specify the file system type of the file system to create on the partition
-m   -->sets a percentage space to be reserved for the root user

to formats a device into a specific filesystem:

## creates a swap filesystem
mkswap

## creates an ext2 filesystem
mke2fs -j
Resizing
## resizes a file system
e2fsck -f /dev//  --> checks a linux ext2/3/4 filesystem
resize2fs /dev//  --> resizes an ext2/3/4 filesystem
resize2fs -f /dev//  --> forcefully resizes an ext2/3/4 filesystem (online resizing)
resize_reiserfs /dev//  --> online resizing for suse linux
Modifying
## adds label to a storage device
e2label
tune2fs -L

## prints the label of a storage device
e2label


Troubleshooting
---------------

Missing PV/VG/LV
One possible reason if your LVM device doesn't show up in lvm commands like pvs, lvs, or vgs is that the underlying LUN/storage device got vanished, corrupted, or its LUN assignment has changed.

Example of the error in /var/log/messages is:
Dec 24 19:56:42 orcl01 kernel: sd 2:0:8:0: Warning! Received an indication that the LUN assignments on this target have changed. The Linux SCSI layer does not automatically remap LUN assignments.

On possible resolution is to scan the LVM device again
# Take backup of your data.
# umount /u01/LocalBackupIBM
# pvscan
# vgscan
# lvscan
# Run vgs & lvs ( If you can't see the volume group then you can proceed with the next step)
# vgcfgrestore -f /etc/lvm/archive/orcl01_backupsIBM_00001-493843090.vg orcl01_backupsIBM
# lvs
Unable to remove LV
Issue:

You see this error when remove LV via `lvremove`
  Logical volume iscsi_vg/iscsi_lv is used by another device.

Resolution:

1. Find out what is holding the LV and take note of the major and minor numbers.
[root@node1 ~]# dmsetup info -c
Name                Maj Min Stat Open Targ Event  UUID
ha_vg-ha_lv         253   3 L--w    0    1      0 LVM-1C5GryiDYDe0lllqq1O8f9HsJyTpoUbe8OgaMOFiV5HEG4bi5fzzXLNHubN6A7pw
VolGroup00-LogVol01 253   1 L--w    2    1      0 LVM-SA8LTUF2yzFEV1RdgThw0ZiRxhyHFKuUIAjIC6ScnMtvH67BTyTN31hd82bgDSzd
VolGroup00-LogVol00 253   0 L--w    1    1      0 LVM-SA8LTUF2yzFEV1RdgThw0ZiRxhyHFKuUj6b8IVKEw37bTwOqy81Ud3juFCSJBg12
iscsi_vg-iscsi_lv   253   2 L--w    1    1      0 LVM-9ZTlpYoUyX9xLbFn2NoTfp3313n89MMh81CFS2y999Cvv9DqQbxdZ9XUnI3xx0QG
[root@node1 ~]#

2. Delete it manually.
[root@node1 ~]# ls -l /sys/dev/block/253\:2
lrwxrwxrwx. 1 root root 0 Jun 15 06:37 /sys/dev/block/253:2 -> ../../devices/virtual/block/dm-2
[root@node1 ~]# dmsetup remove /dev/dm-3                                                                                                                                                                                                     
[root@node1 ~]#

3. You can now remove the LV successfully.
[root@node1 ~]# lvremove /dev/mapper/iscsi_vg-iscsi_lv -f
  Logical volume "iscsi_lv" successfully removed
[root@node1 ~]#
removing a stale device
similar errors you may encounter:
/dev/test_vg/test_lv: read failed after 0 of 4096 at 5368643584: Input/output error
/dev/sdb: read failed after 0 of 4096 at 0: Input/output error

cause:
physical device was remove before deleting from OS

solution:
dmsetup remove /dev/test_vg/*
echo 1 > /sys/block/sdb/device/delete
or
reboot

Tutorials
---------

technique in extending lvs by using physical extents
--> this will prevent you from wasting space in your disk


sample extension using "-L +50G" traditonal human readable size (you can see 1.34G wasted or not used during lv extension)
[root@rt01 /]# pvs
  PV         VG         Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup00 lvm2 a-   50.38G     0
  /dev/sdb   VolGroup00 lvm2 a-   51.97G  1.34G
[root@rt01 /]#

extending using physical extents:
1. determine first if how many remaining free extents you have by extending the lv above the maximum size
[root@rt01 /]# lvextend -L +100g /dev/VolGroup00/lv_root --> here we extend it 100GB which is above 50GB
  Extending logical volume lv_root to 201.00 GB
  Insufficient free space: 3200 extents needed, but only 1642 available --> here you will see 1642 extents available
[root@rt01 /]#

2. use the value in #1 to extend your lv by using "-l" flag instead of "-L"
[root@rt01 /]# lvextend -l +1642 /dev/VolGroup00/lv_root
  Extending logical volume lv_root to 152.31 GB
  Logical volume lv_root successfully resized
[root@rt01 /]#

3. check now your remaining space (you can see no space is wasted)
[root@rt01 /]# pvs
  PV         VG         Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup00 lvm2 a-   50.38G    0
  /dev/sdb   VolGroup00 lvm2 a-   51.97G    0
[root@rt01 /]#
resizing a a single vmdk disk
1. edit virtual disk in vmware settings
2. reboot host
3. create new partition
  # fdisk /dev/sda
  # n > primary > follow defaults
  # t > > 8e
  # w
4. reboot (at this point I don't know if it is possible not to reboot)
5. pvcreate /dev/sda4
6. vgextend vg_root /dev/sda4
7. lvextend /dev/mapper/vg_ftp01-lv_root /dev/sda4
8. resize2fs /dev/mapper/vg_ftp01-lv_root
expanding a non-LVM partition
1. shutdown server
2. add storage
3. insert gparted cd into cdrom
4. boot server from cd
5. use gparted to expand partition

## reminders in designing your partitions

when layouting partitions, make sure to make / the last partition

BAD layout:
/dev/sda1 = /
/dev/sda2 = /boot
/dev/sda3 = swap

--> in this scenario, / is the first partition
--> there is another partition right next to it
--> extending / will be difficult because you must consider the starting and end boundaries of the cylinder

GOOD layout:
/dev/sda1 = /boot
/dev/sda2 = swap
/dev/sda3 = /

--> there must be no partitions after /
--> in that case, you can extend it without worrying about the neighboring cylinder
Destroying an LVM filesystem
1. Remove LV
lvremove

2. Deactivate and remove VG
vgchange -a n
vgremove

3. Remove PV
pvremove
Resizing tmpfs
tmpfs is a RAM based temporary file system which is generally mounted on /dev/shm. To to extend the tmfs file system use below steps:

1. Check initial size: # df -h /dev/shm
2. Edit fstab and specify tmpfs size
  # vi /etc/fstab
  tmpfs                   /dev/shm                tmpfs   size=4g        0 0
3. Remount: # mount -o remount /dev/shm
4. Check final size: # df -h /dev/shm
Moving data to another disk without any downtime
If you want to migrate your data to a new disc - let's say you decided to
purchase another storage appliance and you want your MYSQL datadir to move to
that appliance without any downtime, LVM mirroring is the way to go.

1. Prepare your LUN and assign/zone it to your server

2. Once the LUN is available, add it to the current VG that holds the MYSQL
datadir
# vgextend myql_vg /dev/sdc

3. Create a mirror of the MYSQL logical volume pointing to the newly added LUN
(this is the RIGHT side of the mirror)
# lvconvert -m1 /dev/mapper/mysql_vg-mysql_lv /dev/sdc

4. Wait until the sync is complete (must be 100% under Cpy%Sync field)
# lvs

5. Once the sync is complete, remove/detach the LEFT side of the mirror (that is
the old LUN) from the MYSQL logical volume
# lvconvert -m0 /dev/mapper/mysql_vg-mysql_lv /dev/sdb

6. Remove the old LUN from the MYSQL VG
# vgreduce mysql_vg /dev/sdb

7. Remove PV label
# pvremove /dev/sdb

8. Detach it from your server

9. Hola! You were able to move your MYSQL database to a new and faster disk
without a  downtime!

Renaming a volume group in RHEL/CENTOS 6
1. Verify old name
# vgs

2. Rename VG
# vgrename old_vg_name new_vg_name

3. Backup and replace references in fstab
# cp -p /etc/fstab /etc/fstab.$(date +%F)
# grep old_vg_name /etc/fstab
/dev/mapper/old_vg_name-lv_root    /                       ext4    defaults        1 1
/dev/mapper/old_vg_name-lv_swap    swap                    swap    defaults        0 0

4. Backup and replace referencs in grub.conf
# cp -p /boot/grub/grub.conf /boot/grub/grub.conf.$(date +%F)
# grep old_vg_name /boot/grub/grub.conf
kernel /vmlinuz-2.6.32-642.1.1.el6.x86_64 ro root=/dev/mapper/old_vg_name-lv_root ... rd_LVM_LV=old_vg_name/lv_root ... rd_LVM_LV=old_vg_name/lv_swap ...

5. Backup and rebuild initramfs
# cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bak
# dracut -f

6. Restart server
# reboot


No comments:

Post a Comment