After using Linux for a long time, you will surely realize that there is not enough capacity for a partition. What can you do if you want to expand it? This involves the management of LVM logical volumes, which can dynamically adjust the Linux partition capacity.
Overview of LVM
Logical Volume Manager can dynamically adjust disk capacity and improve disk management flexibility.
When automatic partitioning is selected during the installation of CentOS 7, the default is to install the system in the LVM scheme.
However, the / boot partition must be independent and cannot be created based on LVM.
PV Physical Volume
Physical Volume, the basic storage device of LVM mechanism, usually corresponds to a common partition or the entire hard disk.
When creating a physical volume, a reserved block for recording LVM attributes is created in the partition or disk header, and the storage space is divided into four MB basic units (Physical Extend, PE) by default to form a physical volume.
Common partition first converts the partition type to 8e; the whole hard disk can divide all the space into one main partition and then adjust it.
VG Volume Group
Volume group, Volume Group, is a whole composed of one or more physical volumes. Physical volumes can be added and removed dynamically, and PE size can be specified when created.
LV Logic Volume
Logical Volume, built on volume groups, is not directly related to physical volumes. Once formatted, it can be mounted for use.
Three Relations
From the above explanations, we can see the process of establishing LVM. First, a physical volume is created from a common partition or the entire hard disk; then, one or more physical volumes are created as volume groups; finally, different data storage spaces are divided on volume groups to form logical volumes. With logical volumes, you can format, mount and use them.
LVM Management
Common LVM commands
function | PV Management Command | VG Management Command | LV Management Command |
---|---|---|---|
Scan (Scan) | pvscan | vgscan | lvscan |
Create | pvcreate | vgcreate | lvcreate |
Display (display) | pvdisplay | vgdisplay | lvdisplay |
Remove (Remove) | pvremove | vgremove | lvremove |
Extend (Extension) | / | vgextend | lvextend |
Reduce (decrease) | / | vgreduce | lvreduce |
PV Management
pvsacn
- Scanning lists all physical volumes in the system
- e: Display only physical volumes belonging to the output volume group
- n: Show only physical volumes that do not belong to any volume group
[root@localhost ~]# pvscan PV /dev/sda2 VG centos lvm2 [<19.00 GiB / 0 free] PV /dev/sdb1 lvm2 [<20.00 GiB] PV /dev/sdc1 lvm2 [<20.00 GiB] Total: 3 [58.99 GiB] / in use: 1 [<19.00 GiB] / in no VG: 2 [<40.00 GiB]
pvcreate
- Converting partitions or entire hard disks into physical volumes
[root@localhost ~]# pvcreate /dev/sd{b,c}1 Physical volume "/dev/sdb1" successfully created. Physical volume "/dev/sdc1" successfully created.
pvdisplay
- Display physical volume details
[root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name centos PV Size <19.00 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 4863 Free PE 0 Allocated PE 4863 PV UUID E4eovQ-CgPo-OSDK-75MO-BfD8-pGsP-BUlqip "/dev/sdb1" is a new physical volume of "<20.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size <20.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID gR5mm3-f50o-XLTq-34GY-n6Iz-WXkQ-uMZAkJ "/dev/sdc1" is a new physical volume of "<20.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc1 VG Name PV Size <20.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID O2gKxa-ONts-kyXK-zZrO-3dDg-FhtI-V6GPy9
pvremove
- Delete the specified physical volume and restore it to a normal partition or disk
TIPS: Only physical volumes that do not belong to volume groups can be deleted and restored.
[root@localhost ~]# pvremove /dev/sdd1 Labels on physical volume "/dev/sdd1" successfully wiped.
VG management
vgscan
- LVM Volume Group Established in Scanning System
[root@localhost ~]# vgscan Reading volume groups from cache. Found volume group "class" using metadata type lvm2 Found volume group "centos" using metadata type lvm2
vgcreate
- Create one or more physical volumes as a volume group
- l: Maximum number of logical volumes allowed to be created on volume groups
- p: Maximum number of physical volumes allowed to be added in volume groups
- s: PE size of physical volumes on volume groups
[root@localhost ~]# vgcreate class /dev/sd{b,c}1 Volume group "class" successfully created
vgdisplay
- Display detailed information of each volume group in the system
[root@localhost ~]# vgdisplay --- Volume group --- VG Name class 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 39.99 GiB PE Size 4.00 MiB Total PE 10238 Alloc PE / Size 0 / 0 Free PE / Size 10238 / 39.99 GiB VG UUID ZtQvGX-YOdy-U8wj-mGQs-TT1e-1PBR-dBNKAG --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size <19.00 GiB PE Size 4.00 MiB Total PE 4863 Alloc PE / Size 4863 / <19.00 GiB Free PE / Size 0 / 0 VG UUID bLPJGb-SQQ3-KcUb-OSdo-qaMR-9hJE-MTkc7a
vgremove
- Delete the specified volume group
TIPS: The logical volumes below the volume group are not mounted and used. When the volume group is deleted, the logical volumes below are deleted at the same time.
[root@localhost ~]# vgremove class Volume group "class" successfully removed
vgextend
- Adding new physical volumes to dynamically expand the disk space of volume groups
[root@localhost ~]# vgextend class /dev/sdd1 Volume group "class" successfully extended
vgreduce
- Remove physical volumes from volume groups to reduce volume group capacity.
- a: If no physical volumes are specified on the command line to be deleted, all empty physical volumes are deleted. (all)
TIPS: Before deleting, make sure that the corresponding physical volume is not in use, that is, there is no logical volume below, otherwise it can not be deleted.
[root@localhost ~]# vgreduce class /dev/sdd1 Removed "/dev/sdd1" from volume group "class"
[root@localhost ~]# vgreduce -a class Physical volume "/dev/sdb1" still in use Physical volume "/dev/sdc1" still in use Removed "/dev/sdd1" from volume group "class"
LV Management
lvscan
- LVM Logic Volume Established in Scanning System
[root@localhost ~]# lvscan ACTIVE '/dev/class/stu01' [25.00 GiB] inherit ACTIVE '/dev/class/stu02' [14.99 GiB] inherit ACTIVE '/dev/centos/swap' [2.00 GiB] inherit ACTIVE '/dev/centos/root' [<17.00 GiB] inherit
lvcreate
- Create a new logical volume by separating space from a specified volume group
- L: Specifies the size of the logical volume in KMGT
- l: Specify percentage creation, such as 50%vg volume group half space, 100%free all remaining.
- n: Specify the name of the logical volume
[root@localhost ~]# lvcreate -L 25G -n stu01 class Logical volume "stu01" created.
[root@localhost ~]# lvcreate -l 100%free -n stu02 class Logical volume "stu02" created.
lvdisplay
- Display detailed information about logical volumes
[root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/class/stu01 LV Name stu01 VG Name class LV UUID XUP7dq-IM9f-PdKk-XpDH-J1hx-kkuB-cOu3Qx LV Write Access read/write LV Creation host, time localhost.localdomain, 2019-08-24 09:42:35 +0800 LV Status available # open 0 LV Size 25.00 GiB Current LE 6400 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 --- Logical volume --- LV Path /dev/class/stu02 LV Name stu02 VG Name class LV UUID P7iMC1-X0oT-6TCH-vLYj-HW2j-6QMu-rS7Jyp LV Write Access read/write LV Creation host, time localhost.localdomain, 2019-08-24 09:59:30 +0800 LV Status available # open 0 LV Size 14.99 GiB Current LE 3838 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:3 --- Logical volume --- LV Path /dev/centos/swap LV Name swap VG Name centos LV UUID NXiaXT-T6yv-ktM2-plyS-j6Hg-hkvu-1JQ2eL LV Write Access read/write LV Creation host, time localhost, 2019-08-16 17:12:19 +0800 LV Status available # open 2 LV Size 2.00 GiB Current LE 512 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1 --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID 7FnsfT-B8xi-3vgu-8N0k-nzNn-8Rra-eoE4iC LV Write Access read/write LV Creation host, time localhost, 2019-08-16 17:12:19 +0800 LV Status available # open 1 LV Size <17.00 GiB Current LE 4351 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
lvremove
- Delete the specified logical volume
TIPS: Back up data, unmount, delete.
[root@localhost ~]# umount /mnt/stu02/ [root@localhost ~]# lvremove /dev/class/stu02
lvextend
- Dynamic Extension of Space Size of LVM Logical Volumes
- L: Specifies the size of the extended logical volume in KMGT
- l: Specify percentage expansion, such as 100%free all remaining.
[root@localhost ~]# lvextend -L +5G /dev/class/stu02 Size of logical volume class/stu02 changed from 14.99 GiB (3838 extents) to 19.99 GiB (5118 extents). Logical volume class/stu02 successfully resized.
TIPS: On-line resize, XFS uses xfs_growfs, ext3, ext4 resize2fs.
[root@localhost ~]# xfs_growfs /dev/class/stu02 meta-data=/dev/mapper/class-stu02 isize=512 agcount=4, agsize=982528 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=3930112, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 3930112 to 5240832
lvreduce
- Reducing the space size of LVM logical volumes
TIPS: Back up data, unmount, reduce space, force formatting, re-mount.
- L: Specifies the size of the logical volume in KMGT
[root@localhost ~]# umount /mnt/stu02/ [root@localhost ~]# lvreduce -L -10G /dev/class/stu02 [root@localhost ~]# mkfs.xfs -f /dev/class/stu02 [root@localhost ~]# mount -a [root@localhost ~]# df -hT
LVM experiment
Creating LVM logical volumes
1. Add two 20G disks, restart the machine and partition it. The whole space is divided into one main partition, the type is changed to Linux LVM, and the ID is 8e. Partition process omitted, unclear for reference Last blog.
[root@localhost ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0009e645 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 41943039 19921920 8e Linux LVM Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x5e081577 Device Boot Start End Blocks Id System /dev/sdb1 2048 41943039 20970496 8e Linux LVM Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x25c06655 Device Boot Start End Blocks Id System /dev/sdc1 2048 41943039 20970496 8e Linux LVM Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
2. Convert / dev/sdb1, / dev/sdc1 to physical volume.
[root@localhost ~]# pvcreate /dev/sd{b,c}1 Physical volume "/dev/sdb1" successfully created. Physical volume "/dev/sdc1" successfully created.
3. Create a volume group named class and add sdb1 and sdc1 to the volume group.
[root@localhost ~]# vgcreate class /dev/sd{b,c}1 Volume group "class" successfully created
4. Create logical volumes with the name stu01 and size 25G; create logical volumes with the name stu02 in the remaining space.
[root@localhost ~]# lvcreate -L 25G -n stu01 class Logical volume "stu01" created.
[root@localhost ~]# lvcreate -l 100%free -n stu02 class Logical volume "stu02" created.
5. Format logical volume, set up automatic mount, and use it after mounting.
[root@localhost ~]# mkfs.xfs /dev/class/stu01 meta-data=/dev/class/stu01 isize=512 agcount=4, agsize=1638400 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=6553600, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=3200, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@localhost ~]# mkfs.xfs /dev/class/stu02 meta-data=/dev/class/stu02 isize=512 agcount=4, agsize=982528 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=3930112, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mkdir /mnt/stu0{1,2} [root@localhost ~]# vim /etc/fstab //Finally, add the following two lines /dev/class/stu01 /mnt/stu01 xfs defaults 0 0 /dev/class/stu02 /mnt/stu02 xfs defaults 0 0
[root@localhost ~]# mount -a [root@localhost ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 17G 1.1G 16G 7% / devtmpfs devtmpfs 901M 0 901M 0% /dev tmpfs tmpfs 912M 0 912M 0% /dev/shm tmpfs tmpfs 912M 8.7M 904M 1% /run tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 143M 872M 15% /boot tmpfs tmpfs 183M 0 183M 0% /run/user/0 /dev/mapper/class-stu01 xfs 25G 33M 25G 1% /mnt/stu01 /dev/mapper/class-stu02 xfs 15G 33M 15G 1% /mnt/stu02
Expansion of LVM Logic Volume
1. Add another 20G disk, restart the machine and partition it. The whole space is divided into one main partition, the type is changed to Linux LVM, and the ID is 8e.
[root@localhost ~]# fdisk /dev/sdd Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0xfaadbaba. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p Partition number (1-4, default 1): First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): Using default value 41943039 Partition 1 of type Linux and of size 20 GiB is set Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): p Disk /dev/sdd: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xfaadbaba Device Boot Start End Blocks Id System /dev/sdd1 2048 41943039 20970496 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
2. Convert / dev/sdd1 to physical volume.
[root@localhost ~]# pvcreate /dev/sdd1 Physical volume "/dev/sdd1" successfully created.
3. If the remaining space of volume group is insufficient, it is necessary to expand the class volume group first.
[root@localhost ~]# vgdisplay class --- Volume group --- VG Name class System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 6 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 2 Act PV 2 VG Size 39.99 GiB PE Size 4.00 MiB Total PE 10238 Alloc PE / Size 10238 / 39.99 GiB Free PE / Size 0 / 0 VG UUID iB1h9h-2uQa-rD7o-GBVK-pOCA-ek2c-TXieLr
[root@localhost ~]# vgextend class /dev/sdd1 Volume group "class" successfully extended
[root@localhost ~]# vgdisplay class --- Volume group --- VG Name class System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 7 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 3 Act PV 3 VG Size <59.99 GiB PE Size 4.00 MiB Total PE 15357 Alloc PE / Size 10238 / 39.99 GiB Free PE / Size 5119 / <20.00 GiB VG UUID iB1h9h-2uQa-rD7o-GBVK-pOCA-ek2c-TXieLr
4. To expand the capacity of stu02 5G, use xfs_growfs to adjust the size online.
[root@localhost ~]# lvextend -L +5G /dev/class/stu02 Size of logical volume class/stu02 changed from 14.99 GiB (3838 extents) to 19.99 GiB (5118 extents). Logical volume class/stu02 successfully resized.
[root@localhost ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 17G 1.1G 16G 7% / devtmpfs devtmpfs 901M 0 901M 0% /dev tmpfs tmpfs 912M 0 912M 0% /dev/shm tmpfs tmpfs 912M 8.7M 903M 1% /run tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 143M 872M 15% /boot /dev/mapper/class-stu01 xfs 25G 33M 25G 1% /mnt/stu01 /dev/mapper/class-stu02 xfs 15G 33M 15G 1% /mnt/stu02 tmpfs tmpfs 183M 0 183M 0% /run/user/0
[root@localhost ~]# xfs_growfs /dev/class/stu02 meta-data=/dev/mapper/class-stu02 isize=512 agcount=4, agsize=982528 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=3930112, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 3930112 to 5240832
[root@localhost ~]# df -hT | grep stu02 /dev/mapper/class-stu02 xfs 20G 33M 20G 1% /mnt/stu02
summary
The content is not much, but it needs practical operation to understand. It's three steps: creating physical volumes, creating volume groups, creating logical volumes, and then formatting and mounting them for use. If the expansion is not mounted, it will expand normally, then format the mount, which has been mounted and used. After the expansion, the use of xfs_growfs online adjustment will take effect. There's another article about disk quotas.