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

Появилась необходимость увеличения размера виртуальных дисков на виртуальной машине с CentOS 7. Задача простая, но все мануалы которые находятся поиском в google почему-то не работают. На деле всё достаточно просто, делается буквально несколькими командами.

Итак, имеется два виртуальных диска, системный – 16Gb, смонтированный – 5Gb. Смотрим список всех разделов, их размеры и процент использования

[root@centos7 ~]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda3 14G 9.9G 3.4G 75% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 912M 84K 912M 1% /dev/shm
tmpfs 912M 8.9M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sdb1 4.8G 3.9G 666M 86% /mnt/mailstore
/dev/sda1 297M 152M 146M 51% /boot
tmpfs 183M 12K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0

Средствами гипервизора увеличиваем размеры дисков, системный – 20Gb, смонтированный – 15Gb.

Смотрим таблицы разделов

[root@centos7 ~]# 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: 0x000c45a0

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 3762175 1572864 82 Linux swap / Solaris
/dev/sda3 3762176 31457279 13847552 83 Linux

Disk /dev/sdb: 16.1 GB, 16106127360 bytes, 31457280 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: 0x5d48b270

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10485759 5241856 83 Linux

Размеры дисков увеличились, осталось увеличить размеры разделов. Сначала изменим смонтированный диск – в конкретном случае sdb1. Первым делом размонтируем его, потом удалим имеющийся раздел (данная операция не затрагивает хранящиеся данные на диске) и создадим его заново большего размера

[root@centos7 ~]# umount /mnt/mailstore/
[root@centos7 ~]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda3 14G 9.9G 3.4G 75% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 912M 84K 912M 1% /dev/shm
tmpfs 912M 9.0M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 297M 152M 146M 51% /boot
tmpfs 183M 16K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0

[root@centos7 ~]# fdisk /dev/sdb

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): d
Selected partition 1
Partition 1 is deleted

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

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@centos7 ~]# resize2fs /dev/sdb1

resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/sdb1 is mounted on /mnt/mailstore; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/sdb1 is now 3931904 blocks long.

Монтируем диск обратно, перечитаем таблицу разделов  и проверяем размер интересующего раздела

[root@centos7 ~]# mount -t ext4 /dev/sdb1 /mnt/mailstore/
[root@centos7 ~]# /sbin/partprobe
[root@centos7 ~]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda3 14G 9.9G 3.4G 75% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 912M 84K 912M 1% /dev/shm
tmpfs 912M 9.0M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 297M 152M 146M 51% /boot
tmpfs 183M 16K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0
/dev/sdb1 15G 3.9G 11G 28% /mnt/mailstore

Теперь вторая половина задания, увеличим системный раздел – в текущей конфигурации sda3. Смотрим таблицу разделов, удаляем нужный, создаём его заново – большего размера

[root@centos7 ~]# fdisk /dev/sda

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): p

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: 0x000c45a0

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 3762175 1572864 82 Linux swap / Solaris
/dev/sda3 3762176 31457279 13847552 83 Linux

Command (m for help): d
Partition number (1-3, default 3): 3
Partition 3 is deleted

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 (3762176-41943039, default 3762176): 
Using default value 3762176
Last sector, +sectors or +size{K,M,G} (3762176-41943039, default 41943039): 
Using default value 41943039
Partition 3 of type Linux and of size 18.2 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.

[root@centos7 ~]# /sbin/partprobe

Error: Partition(s) 3 on /dev/sda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use. As a result, the old partition(s) will remain in use. You should reboot now before making further changes.

Как видно – перечитать таблицу разделов не удалось, т.к. раздел используется системой. Для внесения изменений необходима перезагрузка операционной системы. После перезагрузки осталось расширить раздел

[root@centos7 ~]# resize2fs /dev/sda3

resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/sda3
Couldn't find valid filesystem superblock.

Проблема в том, что resize2fs умеет работать только с ext2-4 файловыми системами, а CentOS7 по умолчанию создаёт разделы в файловой системе xfs. Решением является использование команды xfs_growfs

[root@centos7 ~]# xfs_growfs /dev/sda3

meta-data=/dev/sda3 isize=512 agcount=4, agsize=865472 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=3461888, 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 3461888 to 4772608

[root@centos7 ~]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 9.9G 8.4G 55% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 912M 84K 912M 1% /dev/shm
tmpfs 912M 8.9M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sdb1 15G 3.9G 11G 28% /mnt/mailstore
/dev/sda1 297M 152M 146M 51% /boot
tmpfs 183M 12K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0

В результате получилось увеличить раздел sda3 с 14Gb до 19Gb, а монтируемый раздел sdb1 с 4,8Gb до 15Gb.

Яндекс.Метрика