Розширення диска в CentOS
Вихідні дані:
ОС: CentOS Linux release 7.6.1810 (Core).
Початковий розмір LVM: 4,4 GB.
Завдання: розширити LVM до 10 GB, використавши весь доступний простір диска.
Примітка
Перед внесенням змін обов’язково створіть резервну копію всіх даних сервера. Операції з розділами пов’язані з ризиком втрати даних.
Крок 1
На початку є один фізичний том (PV), одна група томів (VG) і один логічний том (LV).
fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000a25ba
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 1026047 512000 83 Linux
/dev/vda2 1026048 10485759 4729856 8e Linux LVM
Disk /dev/mapper/vg0-rootfs: 4840 MB, 4840226816 bytes, 9453568 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/vdb: 214.7 GB, 214748364800 bytes, 419430400 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: 0x66b4980d
Device Boot Start End Blocks Id System
/dev/vdb1 2048 419430399 209714176 83 Linux
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-rootfs 4,4G 2,0G 2,2G 48% /
devtmpfs 2,0G 0 2,0G 0% /dev
tmpfs 2,0G 0 2,0G 0% /dev/shm
tmpfs 2,0G 8,4M 2,0G 1% /run
tmpfs 2,0G 0 2,0G 0% /sys/fs/cgroup
/dev/vda1 477M 121M 327M 28% /boot
tmpfs 396M 0 396M 0% /run/user/0
/dev/vdb1 197G 61M 187G 1% /data
# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 vg0 lvm2 a-- <4,51g 0
# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 1 1 0 wz--n- <4,51g 0
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
rootfs vg0 -wi-ao---- <4,51g
У фізичному томі та групі томів немає вільного місця для розширення логічного тому. Тому потрібно створити новий фізичний том, додати його до групи, а потім розширити логічний том.
Створіть новий розділ диска за допомогою fdisk:
# fdisk /dev/vda
fdisk /dev/vda
n— створити новий розділ;p— зробити новий розділ основним.
Укажіть номер розділу. Для першого й останнього секторів залиште стандартні значення. Після цього буде створено розділ Linux розміром 5 GB.
# fdisk /dev/vda
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.
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):
First sector (10485760-20971519, default 10485760):
Using default value 10485760
Last sector, +sectors or +size{K,M,G} (10485760-20971519, default 20971519):
Using default value 20971519
Partition 3 of type Linux and of size 5 GiB is set
Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000a25ba
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 1026047 512000 83 Linux
/dev/vda2 1026048 10485759 4729856 8e Linux LVM
/dev/vda3 10485760 20971519 5242880 8e Linux LVM
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.
Після створення розділу створіть на його основі новий фізичний том:
# pvcreate /dev/vda3
Physical volume "/dev/vda3" successfully created.
Команда pvs підтверджує, що фізичний том створено:
# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 vg0 lvm2 a-- <4,51g 0
/dev/vda3 lvm2 --- 5,00g 5,00g
Крок 2
Додайте створений фізичний том до групи томів, щоб збільшити її розмір:
# vgextend vg0 /dev/vda3
Volume group "vg0" successfully extended
Команда lvdisplay показує список доступних логічних томів:
# lvdisplay
--- Logical volume ---
LV Path /dev/vg0/rootfs
LV Name rootfs
VG Name vg0
LV UUID 5qbxoS-AZZj-vG93-9q7d-XhzN-X0Nd-LJ3PEK
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2017-09-14 14:20:30 +0200
LV Status available
# open 1
LV Size <4,51 GiB
Current LE 1154
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
Доступний логічний том має ім’я rootfs. На ньому розміщено кореневий розділ /.
Тепер логічний том lvm-rootfs можна збільшити на доступні 5 GB. Команда
vgdisplay показує кількість вільних фізичних екстентів (PE):
vgdisplay
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 9,50 GiB
PE Size 4,00 MiB
Total PE 2433
Alloc PE / Size 1154 / <4,51 GiB
Free PE / Size 1279 / <5,00 GiB
VG UUID 4YhkXZ-oYz5-yeMo-N9O7-owQM-MveR-VlY1wX
Розширте LV rootfs на цю кількість PE:
# lvextend -l +1279 /dev/vg0/rootfs
Size of logical volume vg0/rootfs changed from <4,51 GiB (1154 extents) to 9,50 GiB (2433 extents).
Logical volume vg0/rootfs successfully resized.
Після розширення LV збільште файлову систему до всього доступного обсягу:
# resize2fs /dev/vg0/rootfs
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/rootfs is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/vg0/rootfs is now 2491392 blocks long.
Перевірте новий розмір LV:
# lvdisplay
--- Logical volume ---
LV Path /dev/vg0/rootfs
LV Name rootfs
VG Name vg0
LV UUID 5qbxoS-AZZj-vG93-9q7d-XhzN-X0Nd-LJ3PEK
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2017-09-14 14:20:30 +0200
LV Status available
# open 1
LV Size 9,50 GiB
Current LE 2433
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-rootfs 9,3G 1,9G 7,0G 21% /
devtmpfs 2,0G 0 2,0G 0% /dev
tmpfs 2,0G 0 2,0G 0% /dev/shm
tmpfs 2,0G 8,5M 2,0G 1% /run
tmpfs 2,0G 0 2,0G 0% /sys/fs/cgroup
/dev/vdb1 197G 61M 187G 1% /data
/dev/vda1 477M 121M 327M 28% /boot
tmpfs 396M 0 396M 0% /run/user/0
Розмір збільшився з 4,4 до 9,3 GB.