centos7根分区硬盘扩容

运用虚拟机常常会出现硬盘容量不足的状况,在不改变配置的要求下,直接对根目录增长容量。centos

centos7的默认安装已经把磁盘管理成LVM的卷形式,能够比较容易扩展磁盘容量。可是centos7在拓展文件分区的时候,不少文章用resize2fs命令,就会报错,LVM使用了XFS的分区文件格式,就不能用该命令了。app


目前还有不少使用的硬盘是MBR模式,主分区只能有4个。安装的时候用了2个,一个是swap一个是root,分别使用了两个分区。这样就只能分2个,若是对将来的使用不肯定,那么扩展的时候,就不要分主分区,而是扩展分区,为了之后能够屡次扩展硬盘容量。ide


首先是在虚拟机的管理界面,扩大硬盘容量,此处省略,启动机器。centos7

而后先看看本身的容量spa

df -horm

/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% /bootip


fdisk -lci

/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    33554431    16264192   8e  Linux LVM虚拟机

而后对硬盘分区it

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就能够看见容量变大了。

相关文章
相关标签/搜索