Расширение системного LVM-раздела CentOS

Имеется виртуальная машина на ESXi с CentOS 7 в качестве операционной системы. Задача – расширить системный раздел.

Смотрим, что там вообще с дисковым пространством

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 14G 13G 621M 96% /
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 2.0G 8.9M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda1 1014M 163M 852M 17% /boot
tmpfs 396M 0 396M 0% /run/user/0

Раздел /dev/mapper/centos-root занят на 96%. Выключаем виртуальную машину, расширяем диск на гипервизоре. Включаем обратно. Приступаем…

Смотрим информацию о дисках утилитой fdisk

# fdisk -l

Disk /dev/sda: 26.8 GB, 26843545600 bytes, 52428800 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: 0x00039a63

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 33554431 15727616 8e Linux LVM

Disk /dev/mapper/centos-root: 14.4 GB, 14382268416 bytes, 28090368 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: 1719 MB, 1719664640 bytes, 3358720 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

Создаём новый раздел на нужном диске

# fdisk /dev/sda2
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 0xe7ca7d8c.

Command (m for help): p

Disk /dev/sda: 26.8 GB, 26843545600 bytes, 52428800 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: 0x00039a63

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 33554431 15727616 8e Linux LVM

Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (33554432-52428799, default 33554432): 
Using default value 33554432
Last sector, +sectors or +size{K,M,G} (33554432-52428799, default 52428799): 
Using default value 52428799
Partition 3 of type Linux and of size 9 GiB is set

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Для принятия изменений перезагружаем операционную систему

# reboot

Проверяем изменения

# fdisk -l

Disk /dev/sda: 26.8 GB, 26843545600 bytes, 52428800 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: 0x00039a63

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 33554431 15727616 8e Linux LVM
/dev/sda3 33554432 52428799 9437184 83 Linux

Disk /dev/mapper/centos-root: 14.4 GB, 14382268416 bytes, 28090368 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: 1719 MB, 1719664640 bytes, 3358720 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

Создаём новый том

# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.

Расширяем пространство диска LVM

 # vgdisplay
--- 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 <15.00 GiB
PE Size 4.00 MiB
Total PE 3839
Alloc PE / Size 3839 / <15.00 GiB
Free PE / Size 0 / 0
VG UUID mXeD4D-SuSA-6WW8-Y8x2-7ToD-1tv1-vbj8vx

# vgextend centos /dev/sda3
Volume group "centos" successfully extended

# pvscan
PV /dev/sda2 VG centos lvm2 [<15.00 GiB / 0 free]
PV /dev/sda3 VG centos lvm2 [<9.00 GiB / <9.00 GiB free]
Total: 2 [23.99 GiB] / in use: 2 [23.99 GiB] / in no VG: 0 [0 ]
# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID RF70xz-7MKS-OLvc-JGNL-4sIu-1bv9-BwhcBc
LV Write Access read/write
LV Creation host, time zabbix.fnox.local, 2019-01-14 17:26:55 +0300
LV Status available
# open 2
LV Size 1.60 GiB
Current LE 410
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 JUDKWu-1mvq-8ldB-rutm-pHLX-FEe1-HW3Hsw
LV Write Access read/write
LV Creation host, time zabbix.fnox.local, 2019-01-14 17:26:55 +0300
LV Status available
# open 1
LV Size 13.39 GiB
Current LE 3429
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

# lvextend -l+100%FREE /dev/centos/root
Size of logical volume centos/root changed from 13.39 GiB (3429 extents) to 22.39 GiB (5732 extents).
Logical volume centos/root successfully resized.

# vgchange -ay
2 logical volume(s) in volume group "centos" now active
# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=877824 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=3511296, 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 3511296 to 5869568

Финальная проверка дискового пространства системы

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 23G 13G 9.7G 58%
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 2.0G 8.9M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda1 1014M 163M 852M 17% /boot
tmpfs 396M 0 396M 0% /run/user/0

В завершение, есть замечательная утилита, чтобы узнать размеры всех папок на диске – ncdu

ncdu 1.15.1 ~ Use the arrow keys to navigate, press ? for help 
--- / ----------------------------------------------------------------------------------------------------------------------
10.8 GiB [##########] /var 
1.9 GiB [# ] /usr
130.1 MiB [ ] /boot
35.4 MiB [ ] /etc
13.7 MiB [ ] /opt
8.8 MiB [ ] /run
56.0 KiB [ ] /root
20.0 KiB [ ] /tmp
. 0.0 B [ ] /proc
0.0 B [ ] /sys
0.0 B [ ] /dev
@ 0.0 B [ ] lib64
@ 0.0 B [ ] sbin
@ 0.0 B [ ] lib
@ 0.0 B [ ] bin
e 0.0 B [ ] /srv
e 0.0 B [ ] /mnt
e 0.0 B [ ] /media
e 0.0 B [ ] /home