磁盘空间不够用?centos7如何给分区扩容

1,952 阅读3分钟

基操:

du -sh *也会列出当前文件夹下所有文件对应的大小

一、前言

今天发现,服务器的/home 目录的硬盘空间被挤满了,原因是mysql的大量数据占据了大多的内存,因此,需要对内存进行重新分区,命令如下:

[superadmin@localhost ~]# lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   600G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0   199G  0 part 
  ├─centos-root 253:0    0    50G  0 lvm  /
  ├─centos-swap 253:1    0   3.9G  0 lvm  [SWAP]
  └─centos-home 253:2    0 145.1G  0 lvm  /home
sr0              11:0    1   4.5G  0 rom  

[superadmin@localhost home]# du -h --max-depth=1 |grep 'G' |sort
118G    ./mysql8
145G    .
25G     ./nacos

在磁盘中,我们看到sda分配了600G的内存,但只分了两个sda1 和sda2,还剩余400G的内存未分配,造成了极大的浪费,因此需要先新建分区。

二、新建分区

新建后,需要刷新分区后才能看到新的分区sda3,如果还是不行就重启试试。

给新的分区300G 内存,剩余100G备用。

[superadmin@localhost home]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 
Using default response p
分区号 (3,4,默认 3):3
起始 扇区 (419430400-1258291199,默认为 419430400):
将使用默认值 419430400
Last 扇区, +扇区 or +size{K,M,G} (419430400-1258291199,默认为 1258291199):1048575999
分区 3 已设置为 Linux 类型,大小设为 300 GiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
[superadmin@localhost home]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   600G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0   199G  0 part 
  ├─centos-root 253:0    0    50G  0 lvm  /
  ├─centos-swap 253:1    0   3.9G  0 lvm  [SWAP]
  └─centos-home 253:2    0 145.1G  0 lvm  /home
sr0              11:0    1   4.5G  0 rom  
[superadmin@localhost home]# partprobe /dev/sda
[superadmin@localhost home]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   600G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
├─sda2            8:2    0   199G  0 part 
│ ├─centos-root 253:0    0    50G  0 lvm  /
│ ├─centos-swap 253:1    0   3.9G  0 lvm  [SWAP]
│ └─centos-home 253:2    0 145.1G  0 lvm  /home
└─sda3            8:3    0   300G  0 part 
sr0              11:0    1   4.5G  0 rom  

三、分配内存

1、将新分区的sda3 分区创建为物理卷

[superadmin@localhost home]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.

2、将创建好的物理卷扩展到/home所在卷组

[superadmin@localhost home]# vgdisplay -v
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <199.00 GiB
  PE Size               4.00 MiB
  Total PE              50943
  Alloc PE / Size       50942 / 198.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               ditVGC-NFOL-cOTM-fW4Z-Btv4-yKLW-T1riU6
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                DHEcu3-r7yQ-dcKB-ZvBV-1aZl-gbPT-CFGmf0
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-06-15 23:32:37 +0800
  LV Status              available
  # open                 2
  LV Size                <3.88 GiB
  Current LE             992
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/home # 这个是我们目的扩容的目录
  LV Name                home
  VG Name                centos		# 这个是分区所在卷组名字
  LV UUID                kv17CJ-xC2V-olZV-5tL0-W1QM-ud5Y-c8PBwO
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-06-15 23:32:37 +0800
  LV Status              available
  # open                 1
  LV Size                <145.12 GiB
  Current LE             37150
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/root  # 这个是根分区所挂载的逻辑卷路径
  LV Name                root
  VG Name                centos		# 这个是根分区所在卷组名字
  LV UUID                bA2a4N-7Alz-xNyU-FcWX-t8eX-Q4E8-ELqf8o
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-06-15 23:32:37 +0800
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               hpbzq1-xMEL-PeVv-veFD-hrB2-13be-hjzcZr
  PV Status             allocatable
  Total PE / Free PE    50943 / 1

扩展卷组容量:

[superadmin@localhost home]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended

3、扩展/home 分区所挂载的逻辑卷路径并使扩容生效

# 将剩余可用容量全部扩展到 /dev/centos/home 
[superadmin@localhost home]# lvextend -l +100%FREE /dev/centos/home 
  Size of logical volume centos/home changed from <145.12 GiB (37150 extents) to <445.12 GiB (113950 extents).
  Logical volume centos/home successfully resized.
[superadmin@localhost home]# xfs_growfs /dev/centos/home
meta-data=/dev/mapper/centos-home isize=512    agcount=4, agsize=9510400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=38041600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=18575, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 38041600 to 116684800

4、查看扩展后的/home 分区容量

[superadmin@localhost home]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   600G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
├─sda2            8:2    0   199G  0 part 
│ ├─centos-root 253:0    0    50G  0 lvm  /
│ ├─centos-swap 253:1    0   3.9G  0 lvm  [SWAP]
│ └─centos-home 253:2    0 445.1G  0 lvm  /home
└─sda3            8:3    0   300G  0 part 
  └─centos-home 253:2    0 445.1G  0 lvm  /home
sr0              11:0    1   4.5G  0 rom  

最后,大功告成!

扩大文件系统 xfs_growfs /dev/mapper/centos-root