Home tmade.de

Home Wiki

LVM

LVM Layout

Basically LVM looks like this:

You have one or more physical volumes (/dev/sdb1 - /dev/sde1 in our example), and on these physical volumes you create one or more volume groups (e.g. fileserver), and in each volume group you can create one or more logical volumes. If you use multiple physical volumes, each logical volume can be bigger than one of the underlying physical volumes (but of course the sum of the logical volumes cannot exceed the total space offered by the physical volumes).

It is a good practice to not allocate the full space to logical volumes, but leave some space unused. That way you can enlarge one or more logical volumes later on if you feel the need for it.

In this example we will create a volume group called fileserver, and we will also create the logical volumes /dev/fileserver/share, /dev/fileserver/backup, and /dev/fileserver/media (which will use only half of the space offered by our physical volumes for now - that way we can switch to RAID1 later on (also described in this tutorial)).

To create a LVM on commandline you have to:

1. Create a Partition (e.g. fdisk /dev/sdb - Hex code (type L to list codes): <-- 8e = Linux LVM)
2. Execute "pvscan"
3. Execute "pvcreate /dev/sdb1"
4. Execute "vgcreate data /dev/sdb1"
5. Execute "lvcreate -l 100%FREE -n lv-data data"                # lv-data = name logical volume, data = name volume group

or

lvcreate -L 5G -n lv-data data
lvcreate --name lv-data --size 30G data                          #data = name vg, lv-data = name lv
lvcreate --name home --size 5G system                            #system = name vg, home = name lv
6. Format disk (e.g. mkfs.ext3 -Ldata /dev/data/lv-data)

Note: TAB (automatic completion) does NOT work here and you have to enter device info/path manually!!

To extend an existing LV (logical Volume)

1.   Create a Partition (e.g. fdisk /dev/sdb)
1.1  partx -u /dev/sdb (reread kernel partition table to omit reboot)
2.   Execute "pvscan"
3.   Execute "pvcreate /dev/sdb1"
4.   vgextend data /dev/sda4 /dev/sdn1           #Tries to extend the existing volume group "data" by the new physical volumes (see pvcreate) "/dev/sdn1" and /dev/sda4"
5.   lvextend -L+100G /dev/data/lv-data          #Extends "/dev/data/lv-data" 100GB - e. g. from 100 to 200 GB
5.1. lvextend -L 10G /dev/data/lv-data           #Extends "/dev/data/lv-data" to 10GB
5.2. lvextend -l +100%FREE /dev/data/lv-data     #Extends to max. available space
6.   resize2fs /dev/data/lv-data                 #Resize FS (ext3/4)

Note: To expand a xfs filesystem run

xfs_growfs /mountpoint
xfs_info /mountpoint

Resize LVM-Disk

http://www.techrepublic.com/blog/smb-technologist/extending-partitions-on-linux-vmware-virtual-machines/

or (if no data on Disk!!):

1.  Enlarge vdisk (on VMware/ HyperV or whatever)
1.1 Check on system, if enlarged disk has been recognised/ updated (cat /proc/partitions) - if not, see next chapter "LUN" to get it updated
2.  fdisk /dev/device , delete Partition, create Partition with new blocksize
3.  restart system (depends on OS)
4.  pvresize -v -d /dev/device
5.  lvextend -l +100%FREE /dev/vgname/lvname
6.  resize2fs /dev/vgname/lvname

Note: To execute 5 and 6 at once (add “-r”):

lvextend -r -l +100%FREE /dev/vgname/lvname
lvextend -r -L 10G /dev/vgname/lvname

Backup & Recovery Partition Table

sfdisk -d /dev/sda > partinfo_sda
sfdisk -d /dev/sdb > partinfo_sdb                        #backup partition table   
sfdisk -d /dev/sdc > partinfo_sdc
sfdisk --force /dev/sdb < partinfo_sdb                   #restore partition table 

LUN

A „LUN“ (or virtual Disk) can be recognised in a VMware Virtual Maschine (Linux) with the following command:

echo "- - -" > /sys/class/scsi_host/host0/scan
echo "- - -" > /sys/class/scsi_host/host1/scan
echo "- - -" > /sys/class/scsi_host/host2/scan  

“host1” represents the controller ID.

Another solution:

rescan-scsi-bus

Note: The package “scsitools” is required (apt-get install scsitools)!

Last solution (this works always even on older linux-derivates):

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

Update Partition:

partprobe
cat /proc/partitions
kpartx -a /dev/sdc
cat /proc/partitions
partx -a /dev/sdc

What the kernel actualy see´s:

cat /sys/block/sdc/sdc1/start
cat /sys/block/sdb/sdb1/size

Usefull Commands

pvscan
vgscan
lvscan

pvdisplay
vgdisplay 
lvdisplay

pvs
vgs
lvs 

vgcfgrestore vgname
vgchange -ay vgname
pvcreate --uuid "LxgpTz-iIqL-wpz5-sbuN-9Dn8-VOWF-cNZgSM" --restorefile /etc/lvm/archive/x /dev/sdb2

/etc/lvm/archive/ 

lvextend -L+1G /dev/myvg/homevol
lvm lvremove /dev/system/lv-system                #Delete LV "lvm lvremove /dev/system/lv-system"
lvcreate -n swaplv -L 500M volg1                  #Create LVM-Swapfile "swaplv" on volumegroup "volg1" with size 500M
vgremove volume_group_name                        #Delete volumegroup (execute "vgscan" to get an overview of groups and replace the name)
vgreduce --removemissing --test volume_group      #Removes missing disks (uuid) on volumegroup "volume_group", but just as a test
vgreduce --removemissing volume_group             #Removes missing disks (uuid) on volumegroup "volume_group"
lvchange -an /dev/system/lv-system                #Disable LV "/dev/system/lv-system"
lvchange -ay /dev/system/lv-system                #Enable LV "/dev/system/lv-system"
vgchange -an system                               #Disable VG "system"
vgchange -ay system                               #Enable VG "system"
pvcreate /dev/sdb1
pvresize -v -d /dev/sdb1
vgextend system /dev/sdc1                         #add "/dev/sdc1" to vg "system"
vgreduce system /dev/sdb1                         #remove "/dev/sdb1" from vg "system"
lvcreate --name backups --size 30G server1
mkfs.ext3 /dev/mapper/server1-backups

pvmove

/dev/server1/root = /dev/mapper/server1-root

Snapshot

lvcreate -L10G -s -n rootsnapshot /dev/vg_name/lv_name
mkdir -p /mnt/backup
mkdir -p /backup
mount /dev/vg_name/lv_name /mnt/backup
dd if=/dev/vg_name/lv_name of=/mnt/backup/root.dd
tar -pczf /backup/root.tar.gz /mnt/backup
umount /mnt/backup
lvremove /dev/vg_name/lv_name

Restore Snapshot

dd if=/mnt/backup/root.dd of=/dev/vg_name/lv_name

MySQL Backup

To backup MySQL via LVM-sanpshot:

#!/bin/bash

snapshot_create() {
	exec 3> >(/usr/bin/mysql) && \
	echo "flush tables with read lock;" >&3 && \
	LVM_SUPPRESS_FD_WARNINGS='y' /sbin/lvcreate -L100%FREE -s -n mysql-backup /dev/vg00/root >/dev/null && \
	echo "unlock tables;" >&3 && \
	exec 3>&- && \
	mount /dev/vg00/mysql-backup /mnt && \
	mount --bind /mnt/srv/mysql /srv/mysql-backup
}

snapshot_cleanup() {
	umount /srv/mysql-backup
	umount /mnt
	lvremove -f /dev/vg00/mysql-backup >/dev/null
}

snapshot_create || { snapshot_cleanup; exit 1; }
res=$(/usr/bin/rdiff-backup --preserve-numerical-ids --exclude-sockets --exclude-globbing-filelist /etc/rdiff-backup-exclude / /backup/bastet/ 2>&1)
ret=$?
snapshot_cleanup

if [ $ret -ne 0 ]; then
	[ "$res" ] && echo "$res"
	exit $ret
fi

[ "$res" ] && echo "$res" | grep -Ev 'UpdateError var/log/(auth.log|btmp) Updated mirror temp file .* does not match source'
exit 0
linux/lvm.txt · Last modified: 2019/12/30 14:41 by tmade
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki