centos7 基于lvm的根目录扩容

2019/3/27 13:02:00

centos7 在安装后,通常我们通过df -h 来查看磁盘空间的时候率。如下:
 
[root@oracle01 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   46G   32G   14G  71% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G   17M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1                497M  125M  372M  26% /boot
tmpfs                    379M     0  379M   0% /run/user/0
 
如上,在根目录 / 下,已经使用了71%,快没有空间了,这个时候,如果需要增加空间,如果你是实体机,则只能 增加磁盘,如果你是虚拟机或者云服务器,那么可以自由扩展,于是我将磁盘空间从50G增加至100G。
直接增加磁盘空间,但是,不管哪种方式,增加后,会发现,并没有生效,df -h 能用的空间并没有增加,而新增的空间使用 fdisk -l 则可以查看到,如下:
可以看到,虽然我的磁盘空间大小为107G ,但是下面使用了的只有50G 左右。 增加的空间并没有被使用。
下面,就需要将没使用到的空间,用来扩容在根目录下,或别的挂载目录下。 在开始之前,我们必须要对linux的磁盘管理有一定的认识:
在centos中,对于储存和磁盘空间的管理是lvm , 我们常用的 fdisk -l 命令,显示的就是物理分区,相当于直接作用于磁盘上的。
 
在物理分区中,我们可以在不同的磁盘中,划分去分区,而这个分区必须要设置为lvm 分区格式,只有设置为lvm分区格式,才可以将这些分区,提取出来创建成物理卷,
这些在不通物理磁盘中的物理卷,就可以组成一个大的卷组,这个卷组就相当于逻辑上的磁盘,然后可以在卷组上进行分区,分为逻辑卷。而这些逻辑卷就是我们常用的 df -h 来查看的磁盘使用锁显示的,例如 根目录  /home目录等就是常用的逻辑卷。
你可能会问为什么要这样做,因为一般的物理磁盘进行的物理分区,一旦确定,是不能修改大小的,也就无法很好的就行扩容。而且当容量大于一般磁盘的需求时,就没有像lvm一样可以通过组合多个物理磁盘来形成一个大的卷组使用。当然,也为了更好的管理。
 
好的。下面就开始对根目录进行扩容。
 
1. 
[root@oracle01 ~]# fdisk /dev/sda  ## /dev/sda为通过fdisk -l 查看到的物理磁盘(第一行)
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  ## n为创建一个新的分区
Partition type:   ## 这里需要选择时创建主分区还是扩展分区,都可以,这里直接选了主分区
   p   primary (2 primary, 0 extended, 2 free)  
   e   extended
Select (default p): p  ## 选择创建一个主分区,主分区只能有4个,编号为1-4,下面的全部直接回车就好了,会自动将剩余所用空间都创建
Partition number (3,4, default 3):  
First sector (104857600-209715199, default 104857600):
Using default value 104857600
Last sector, +sectors or +size{K,M,G} (104857600-209715199, default 209715199):
Using default value 209715199
Partition 3 of type Linux and of size 50 GiB is set
 
Command (m for help): t   ## t为修改分区类型
Partition number (1-3, default 3): 3  ## 刚才创建的分区编号为3
Hex code (type L to list all codes): 8e  ## 8e就是 lvm格式的分区
Changed type of partition 'Linux' to 'Linux LVM'
 
Command (m for help): w  ## 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.
 
这里,在保存后,会发现,可能会出现报错,显示繁忙,无法重新读取分区信息。下面有解决办法。可以用过重启或者执行 partprobe or kpartx。所以,这里直接执行partprobe
 
[root@oracle01 ~]# partprobe
 
2. 创建了物理分区后,就需要将物理分区创建为物理卷:
 
## 将物理分区3 创建物理卷,这里这个/dev/sda3是第一步创建的物理分区,通过fdisk -l 就可以看到
## 是在第一个物理磁盘/dev/sda下面的分区(一个磁盘可以有多个分区),原本已有/dev/sda1,/dev/sda2
## 通过第一步创建生成/dev/sda3这个分区,然后将这个分区 创建成物理卷
[root@oracle01 ~]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.
[root@oracle01 ~]# pvdisplay  ## 查看物理卷
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               49.51 GiB / not usable 3.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              12674
  Free PE               11
  Allocated PE          12663
  PV UUID               8T9dpf-SrDj-maVi-IlAb-tpPb-Lzyt-WLeRHG
  "/dev/sda3" is a new physical volume of "50.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name              
  PV Size               50.00 GiB
  Allocatable           NO
  PE Size               0  
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               uRRJtJ-Ds7J-dvqs-y5Lb-W95H-FXeh-C2QNtl
 
3. 物理卷创建成功后,则需要将物理卷添加到卷组中,这样才能在卷组中进行分配。
[root@oracle01 ~]# vgdisplay  ## 查看卷组
  --- Volume group ---
  VG Name               centos
  System ID            
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <49.51 GiB
  PE Size               4.00 MiB
  Total PE              12674
  Alloc PE / Size       12663 / 49.46 GiB
  Free  PE / Size       11 / 44.00 MiB   ## 可以看到这个卷组只剩下44M的剩余空间了,其他的都划分为逻辑卷了,类似于已分区
  VG UUID               3TMbc1-A7aP-DAMv-QtUU-VTjC-4tOf-iAxDkc
 
[root@oracle01 ~]# vgextend centos /dev/sda3   ## 将物理卷加入到卷组
  Volume group "centos" successfully extended
[root@oracle01 ~]# vgdisplay  ## 加入后,再次查看卷组
  --- Volume group ---
  VG Name               centos
  System ID            
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               99.50 GiB
  PE Size               4.00 MiB
  Total PE              25473
  Alloc PE / Size       12663 / 49.46 GiB
  Free  PE / Size       12810 / <50.04 GiB   ## 可以看到,加入成功后,卷组的剩余空间变大了,这样就有更多的空间可以进行分配,或新创建新的逻辑卷
  VG UUID               3TMbc1-A7aP-DAMv-QtUU-VTjC-4tOf-iAxDkc
 
4. 添加进卷组后,就是将剩余空间对逻辑卷进行扩容。
[root@oracle01 ~]# lvdisplay     ## 查看逻辑卷
  --- Logical volume ---
  LV Path                /dev/centos/root    ## 逻辑卷名称
  LV Name                root
  VG Name                centos
  LV UUID                RaEygR-53tg-UaJQ-5dJs-e8V4-by51-fcp1DQ
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2017-09-30 19:12:05 +0800
  LV Status              available
  # open                 1
  LV Size                45.46 GiB   ## 增加前,逻辑卷大小
  Current LE             11639
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
 
[root@oracle01 ~]# lvextend -l +100%FREE /dev/centos/root   ## 将剩余百分百空间都添加到逻辑卷中
  Size of logical volume centos/root changed from 45.46 GiB (11639 extents) to 95.50 GiB (24449 extents).
  Logical volume centos/root successfully resized.
[root@oracle01 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                RaEygR-53tg-UaJQ-5dJs-e8V4-by51-fcp1DQ
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2017-09-30 19:12:05 +0800
  LV Status              available
  # open                 1
  LV Size                95.50 GiB    ## 增加后,逻辑卷大小
  Current LE             24449
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
 
5. 然后重新识别一下分区大小,就可以通过df -h看到新增的容量了
 
[root@oracle01 ~]# xfs_growfs /dev/centos/root    ## 命令,后面跟的是逻辑卷的path
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=2979584 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=11918336, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=5819, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 11918336 to 25035776
[root@oracle01 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   96G   32G   64G  34% /      ## 这里可以看到,已成功扩容
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G   17M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1                497M  125M  372M  26% /boot
tmpfs                    379M     0  379M   0% /run/user/0