Description: Candidates should be able to create and remove logical volumes, volume groups, and physical volumes. This objective includes snapshots and resizing logical volumes.
Key Knowledge Areas:
Tools in the LVM suite
Resizing, renaming, creating, and removing logical volumes, volume groups, and physical volumes
Creating and maintaining snapshots
Activating volume groups
Terms and Utilities:
/sbin/pv*
/sbin/lv*
/sbin/vg*
mount
/dev/mapper/
lvm.conf
LVM
In traditional storage management, Operating system searchs for Disk Drives like /dev/sda, /dev/sdb and then looks for what partitions are available on the disks like /dev/sda1, /dev/sdb1 .Partitions are limited to the disks and they are not so flexible. Logical Volume Manager (LVM) bring us flexibility by creating an abstraction layer between Operating System and Disk Devices.
LVM functions by layering abstractions on top of physical storage devices, The basic layers that LVM use are:
physical volumes(pv):A physical volume is typically a hard disk, though it may be a device that looks like a hard disk (ex:software raid device).It can be a partition or entire of a disk.
volume groups(vg): The Volume Group is central level and heart of the LVM. It gathers together a collection of Physical Volumes and create a pool of different storage resources.
logical volumes(lv):The equivalent of a disk partition in a non-LVM system. logical volume takes disk space from disk space which is available on volume group. On top of logical volumes we create File Systems( xfs, ext4, ...)
LVM is capable of doing operations such as increasing, decreasing the size of a logical volume(which we will be discussing later) because the physical volume is made up of small chunks which are always of fixed size. Each physical disk that combinely makes up a volume group will have a number of small chunks of equal size, where data will reside.
This small chunks of equal size, which makes up the physical volumes are called as Physical Extents. Creating a volume group simply combines all the physical extents of all the physical volumes to form one large logical volume group.
Redhat base Systems use LVM by default, they setup LVM during installation, so here we use ubuntu to create lvm :
root@server2:~# apt-get install lvm2
Working with physical volumes(pv)
Here we use sdb, sdc to create pv. In order to prepare a partition to be a physical volume in LVM, it is recommended to format it with LVM tag:
root@server2:~# ls -l /dev/sd*
brw-rw---- 1 root disk 8, 0 Jan 13 21:55 /dev/sda
brw-rw---- 1 root disk 8, 1 Jan 13 21:55 /dev/sda1
brw-rw---- 1 root disk 8, 2 Jan 13 21:55 /dev/sda2
brw-rw---- 1 root disk 8, 5 Jan 13 21:55 /dev/sda5
brw-rw---- 1 root disk 8, 16 Jan 13 22:02 /dev/sdb
brw-rw---- 1 root disk 8, 32 Jan 13 22:02 /dev/sdc
root@server2:~# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-2097151, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-2097151, default 2097151):
Created a new partition 1 of type 'Linux' and of size 1023 MiB.
Command (m for help): t
Selected partition 1
Partition type (type L to list all types): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Rufus alignment
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
root@server2:~# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-4194303, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303):
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Command (m for help): t
Selected partition 1
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Now lets chek weather any LVM has been setup or not and start:
root@server2:~# pvdisplay
root@server2:~# pvcreate /dev/sdb1 /dev/sdc1
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdc1" successfully created
root@server2:~# pvdisplay
"/dev/sdc1" is a new physical volume of "2.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 2.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 5XaGvK-vjWs-lzNk-IPMA-grXi-yFZ8-AeLUBk
"/dev/sdb1" is a new physical volume of "1023.00 MiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 1023.00 MiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID JZ3DkI-goy7-tNIW-MpgH-NC3T-02dT-YB2WTL
Working with volume group(vg)
Now we create a volume group consist of two pv, /dev/sdb1 and /dev/sdc1:
root@server2:~# vgdisplay
root@server2:~# vgcreate myfirstvg /dev/sdb1 /dev/sdc1
Volume group "myfirstvg" successfully created
root@server2:~# vgdisplay
--- Volume group ---
VG Name myfirstvg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 2.99 GiB
PE Size 4.00 MiB
Total PE 766
Alloc PE / Size 0 / 0
Free PE / Size 766 / 2.99 GiB
VG UUID pBA82t-uB9F-VGpT-PKjn-HL9K-1KyN-Yo1VLg
Working with logical volume(lv)
root@server2:~# lvdisplay
root@server2:~# lvcreate --name myfirstlv --size 1G myfirstvg
Logical volume "myfirstlv" created.
root@server2:~# lvdisplay
--- Logical volume ---
LV Path /dev/myfirstvg/myfirstlv
LV Name myfirstlv
VG Name myfirstvg
LV UUID g75etD-o9YW-iAeM-sU2d-nKat-K84V-yle2qv
LV Write Access read/write
LV Creation host, time server2, 2018-01-13 22:47:49 -0800
LV Status available
# open 0
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
And lets format it and mount it:
root@server2:~# mkfs.ext4 /dev/myfirstvg/myfirstlv
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: fe539cb4-596c-456d-b337-9e90b6e47b28
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
root@server2:~# mount /dev/myfirstvg/myfirstlv /mnt/
root@server2:~# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,nosuid,relatime,size=474520k,nr_inodes=118630,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=99492k,mode=755)
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=25,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=15263)
mqueue on /dev/mqueue type mqueue (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime)
sunrpc on /run/rpc_pipefs type rpc_pipefs (rw,relatime)
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
vmware-vmblock on /run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,relatime,user_id=0,group_id=0,default_permissions,allow_other)
tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=99492k,mode=700,uid=1000,gid=1000)
gvfsd-fuse on /run/user/1000/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000)
/dev/mapper/myfirstvg-myfirstlv on /mnt type ext4 (rw,relatime,data=ordered)
We can now easily resize volume group with lvresize:
if we havn't used -rswitch then we had to use resizefs /dev/myfirstvg/myfirstlvcommand inorder to get File System .informed about size changes.
LVM snapshots
snapshots lets us to freeze the current state of logical volume.Every thing which is added or removed doesn't make any changes in our snapshot and we can easily reverse to previous stat of volume. snapshots can help us to create a short term backup when no files are open. For creating snap shots we should define the size of snap shot, so we should consider amount of future changes:
lvm.conf is loaded during the initialisation phase of lvm. This file can in turn lead to other files being loaded - settings read in later override earlier settings. File timestamps are checked between commands and if any have changed, all the files are reloaded.
Metadata backups and archives are automatically created on every volume group and logical volume configuration change unless disabled in the lvm.conf file. By default, the metadata backup is stored in the /etc/lvm/backup file and the metadata archives are stored in the /etc/lvm/archive file. How long and how many meta data archive are kept? it is determined by parameters we can set in the lvm.conf file.
Logical Volume Backup
A daily system backup should include the contents of the /etc/lvm directory in the backup. Note that a metadata backup does not back up the user and system data contained in the logical volumes.We can manually back up the metadata to the /etc/lvm/backup file with the vgcfgbackup command.And we can restore metadata with the vgcfgrestore command.
/dev/mapper
Lets Take a look:
root@server2:~# ls -l /dev/myfirstvg/myfirstlv
lrwxrwxrwx 1 root root 7 Jan 14 00:22 /dev/myfirstvg/myfirstlv -> ../dm-0
root@server2:~# ls -l /dev/dm-0
brw-rw---- 1 root disk 253, 0 Jan 14 00:22 /dev/dm-0
By using LVM or RAID or LUKS more md-X devices are created and used. Nice interface to that is:
root@server2:~# ls -l /dev/mapper/
total 0
crw------- 1 root root 10, 236 Jan 13 21:55 control
lrwxrwxrwx 1 root root 7 Jan 13 22:55 myfirstvg-myfirstlv -> ../dm-0
Some Device Mapper Devices might be created just for the system to do it things. We can work with Device Mapper Directly with dmsetup command but thats not straightforward and it is beyond the scope of LPIC exam.
root@server2:/dev# dmsetup ls
myfirstvg-myfirstlv (253:0)
root@server2:/dev# dmsetup info myfirstvg-myfirstlv
Name: myfirstvg-myfirstlv
State: ACTIVE
Read Ahead: 256
Tables present: LIVE
Open count: 1
Event number: 0
Major, minor: 253, 0
Number of targets: 1
UUID: LVM-pBA82tuB9FVGpTPKjnHL9K1KyNYo1VLgg75etDo9YWiAeMsU2dnKatK84Vyle2qv
so we have seen while using RAID can give us performance and reliability, LVM cause flexibility.