您的位置:首页 > 运维架构 > Linux

centos7根分区硬盘扩容

2016-11-08 10:51 465 查看
运用虚拟机经常会出现硬盘容量不足的情况,在不改变配置的要求下,直接对根目录增加容量。
centos7的默认安装已经把磁盘管理成LVM的卷形式,可以比较容易扩展磁盘容量。但是centos7在拓展文件分区的时候,很多文章用resize2fs命令,就会报错,LVM使用了XFS的分区文件格式,就不能用该命令了。

目前还有很多使用的硬盘是MBR模式,主分区只能有4个。安装的时候用了2个,一个是swap一个是root,分别使用了两个分区。这样就只能分2个,如果对未来的使用不确定,那么扩展的时候,就不要分主分区,而是扩展分区,为了以后可以多次扩展硬盘容量。

首先是在虚拟机的管理界面,扩大硬盘容量,此处省略,启动机器。
然后先看看自己的容量
df -h
/dev/mapper/centos-root 16G 5.8G 13G 33% /
devtmpfs 912M 0 912M 0% /dev
tmpfs 921M 84K 921M 1% /dev/shm
tmpfs 921M 8.9M 912M 1% /run
tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot

fdisk -l
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 33554431 16264192 8e Linux LVM
然后对硬盘分区
fdisk /dev/sda
使用n命令
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 1 free)
e extended
会让选第几分区,一般顺序排,就是3,用p选项,建立了一个主分区,然后可以用p命令查看结果
/dev/sda3 37748736 46137343 4194304 83 Linux
可以看见Id是83,是Linux,这样不能扩展到券,用t命令改变分区格式为8e,就是Linux LVM逻辑卷格式。
然后用w命令保存。要重启动一下。

建立物理卷
pvcreate /dev/sdb3
显示一下看看
pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size 15.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 3970
Free PE 0
Allocated PE 3970
PV UUID Dxuc0B-LZe1-X8k8-jzmI-sEQE-fX1j-cXkIHS
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 2.00 GiB / not usable 4.00 MiB
Allocatable No
PE Size 0
Total PE 0
Free PE 0
Allocated PE 511
PV UUID nxf54x-ZdcS-b7GI-lhFo-1YPQ-DWW5-kvSipf

下面就扩容
看看现在的卷组
vgdisplay
--- Volume group ---
VG Name centos
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 19.50 GiB
PE Size 4.00 MiB
Total PE 4992
Alloc PE / Size 4992 / 19.50 GiB
Free PE / Size 0 / 0
VG UUID lwO318-dF9J-SqwF-k7Sk-aNVD-BXtB-zNFhBH
把新做的sda3分区加入卷,从上面显示 VG Name centos,卷名是centos
vgextend centos /dev/sda3
在查看一下
vgdisplay
Free PE / Size 511 / 2 Gib
已经变了

查看一下逻辑区的情况
lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID i7xZpm-66bQ-NNy4-waBk-TCRl-EwLD-T8dhy7
LV Write Access read/write
LV Creation host, time localhost, 2016-11-07 15:27:13 +0900
LV Status available
# open 2
LV Size 1.60 GiB
Current LE 410
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID Kn1mUQ-T30g-x4B3-noRh-TxGu-r1Ql-vL5w6P
LV Write Access read/write
LV Creation host, time localhost, 2016-11-07 15:27:14 +0900
LV Status available
# open 1
LV Size 15.90 GiB
Current LE 4582
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
扩展卷组的大小,路径就是上面LV Path /dev/centos/root
lvextend -L +1.966G /dev/centos/root
有些文章里用的是/dev/mapper/centos-root
其实都是指向../dm-1,都可以。
另外一点,这里用的+1.996G,不是2G,因为2GB会失败,Free PE / Size 511 / 2 Gib显示511个区块,511*4/1024=1.996
再查看一下逻辑区的情况
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID i7xZpm-66bQ-NNy4-waBk-TCRl-EwLD-T8dhy7
LV Write Access read/write
LV Creation host, time localhost, 2016-11-07 15:27:13 +0900
LV Status available
# open 2
LV Size 1.60 GiB
Current LE 410
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID Kn1mUQ-T30g-x4B3-noRh-TxGu-r1Ql-vL5w6P
LV Write Access read/write
LV Creation host, time localhost, 2016-11-07 15:27:14 +0900
LV Status available
# open 1
LV Size 17.90 GiB
Current LE 4582
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
尺寸 LV Size 从15.90 GiB变成17.90 GiB了

下面就是扩展文件系统的尺寸,因为这时候用df -h是看不到变大的。
网上文章大多用了resize2fs,很遗憾,resize2fs: Bad magic number in super-block
这个命令已经不适应新的系统要求了
xfs_growfs /dev/centos/root
显示的最后一行
data blocks changed from xxxxxx to xxxxxx
表明已经扩展了
这时候再用df -h就可以看见容量变大了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息