Расширение диска в CentOS

Исходные данные:

  • ОС: CentOS Linux 7.6.1810 (Core).

  • Исходный размер LVM: 4,4 ГБ.

  • Задача: расширить LVM до 10 ГБ, используя весь диск.

Примечание

Перед изменениями обязательно создайте резервную копию данных. Любые операции с разделами могут привести к потере данных.

Шаг 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

Свободного места в PV и VG нет. Для расширения LV создайте новый физический том, добавьте его в группу, затем увеличьте логический том.

Создайте раздел с помощью fdisk:

# fdisk /dev/vda

fdisk /dev/vda
  • n создаёт раздел;

  • p выбирает основной тип.

Укажите номер раздела. Для первого и последнего секторов примите значения по умолчанию. Будет создан раздел Linux размером 5 ГБ. Затем измените его тип на 8e — Linux LVM.

# 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 и содержит корневую файловую систему /.

Теперь rootfs можно увеличить на свободные 5 ГБ. Команда 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.

Расширьте файловую систему на весь доступный объём:

# 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 ГБ.