磁盤空間不足,擴充根分區,添加新的磁盤


一、擴充根分區

根分區使用率超過預警值,經排查刪除了無用數據,但使用率依然居高不下,於是決定擴充根分區。

 一、查看目前的使用情況

[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       997M  994M  2.9M 100% /
devtmpfs                devtmpfs  915M     0  915M   0% /dev
tmpfs                   tmpfs     921M     0  921M   0% /dev/shm
tmpfs                   tmpfs     921M   17M  905M   2% /run
tmpfs                   tmpfs     921M     0  921M   0% /sys/fs/cgroup
/dev/mapper/centos-usr  xfs       4.9G  1.6G  3.4G  33% /usr
/dev/sda1               xfs        97M   66M   31M  69% /boot
/dev/mapper/centos-var  xfs       2.4G  473M  1.9G  21% /var

 /dev/mapper/centos-root 已經使用了100%

二、添加新的磁盤,分區格式化

 根據公司環境不同,自行添加新的磁盤

 1、添加完后,查看磁盤情況

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

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      206847      102400   83  Linux
/dev/sda2          206848    41943039    20868096   8e  Linux LVM

Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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 /dev/mapper/centos-swap: 2097 MB, 2097152000 bytes, 4096000 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 /dev/mapper/centos-usr: 5242 MB, 5242880000 bytes, 10240000 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 /dev/mapper/centos-root: 1048 MB, 1048576000 bytes, 2048000 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
...
...

可以看到新添加的磁盤/dev/sdb

2、對磁盤進行分區

 fdisk /dev/sdb

 fdisk的交互模式,依次輸入

n  --創建新分區
p  --創建主分區
<回車>  --默認分區編號
<回車>  --默認起始扇區位置。
<回車>  --默認結束扇區位置。
w  --寫入分區表

3、再查看磁盤情況

fdisk -l

多出下面的分區

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     6291455     3144704   83  Linux

三、擴充根分區:將新分區擴展/root

1、查看卷分組

    vgdisplay -v

    找到當前卷分組名為"centos"

Finding all volume groups
    Finding volume group "centos"
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                5
  Open LV               5
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               19.90 GiB
  PE Size               4.00 MiB
  Total PE              5094
  Alloc PE / Size       5093 / 19.89 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               vtJL08-7Jxi-5IqK-3fUg-Pben-682a-wiv2GL
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                ZWTgoT-AMWs-g54v-dZA1-NQUj-mqGa-8tmr4U
  LV Write Access        read/write
  LV Creation host, time localhost, 2016-07-03 21:59:31 -0400
  LV Status              available
  # open                 1
  LV Size                1000.00 MiB
  Current LE             250
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
   currently set to     256
  Block device           253:2
  ...
  ...
  ...
  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               fiVH1e-lwfi-63Lr-oIlK-GDZI-dcuZ-T04VlC
  PV Status             allocatable
  Total PE / Free PE    5094 / 1

2、為新增的分區創建物理卷

  pvcreate /dev/sdb1

  提示是否擦除xfs簽名,可以根據實際情況選擇

WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n] y
  Wiping xfs signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created

3、查看結果

  pvdisplay

  可以看到新創建的3GiB大小的物理卷:

--- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               19.90 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              5094
  Free PE               1
  Allocated PE          5093
  PV UUID               fiVH1e-lwfi-63Lr-oIlK-GDZI-dcuZ-T04VlC
   
  "/dev/sdb1" is a new physical volume of "3.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               VGXSAn-UiZ0-Fy40-eQxb-53xA-5hZM-3eGPg0

4、擴展卷分組,"centos"是vgdisplay命令查到的卷分組名

  vgextend centos /dev/sdb1

  提示擴展成功

5、查看邏輯卷,並擴展邏輯卷

  lvdisplay

  可以看到/dev/centos/root目前的LV Size是1000MiB。

  lvextend -L +3G /dev/centos/root

  注:+3G是我們新增加的分區大小。

6、擴展后執行

  xfs_growfs /dev/centos/root 

  將文件系統擴大,完成。

因為安裝CentOS用的xfs分區,所以使用命令xfs_growfs擴展文件系統大小。

7、查看結果

df -Th

Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       4.0G  995M  3.1G  25% /
devtmpfs                devtmpfs  915M     0  915M   0% /dev
tmpfs                   tmpfs     921M     0  921M   0% /dev/shm
tmpfs                   tmpfs     921M  8.4M  913M   1% /run
tmpfs                   tmpfs     921M     0  921M   0% /sys/fs/cgroup
/dev/mapper/centos-usr  xfs       4.9G  1.6G  3.4G  33% /usr
/dev/mapper/centos-home xfs       9.8G  391M  9.4G   4% /home
/dev/mapper/centos-var  xfs       2.4G  469M  1.9G  20% /var
/dev/sda1               xfs        97M   66M   31M  69% /boot

/root已經擴充到4GiB

二、磁盤空間不足,添加新的磁盤

ECS贈送的系統盤20G,明顯不夠使用。購買一塊500G的數據盤,作為應用服務及數據存儲使用。

一、檢查目前磁盤情況

1、看現在磁盤情況

 

2、查看磁盤個數以及分區情況

 

 新加的500G磁盤為:/dev/xvdb

二、對新磁盤進行分區、格式化、掛載

1、對新磁盤進行分區(根據實際需求)

2、再查看磁盤個數與分區情況

 可以看到新建分區創建完畢

3、通知內存磁盤分區變化

不通知內核,有可能無法格式化操作

partprobe /dev/xvdb

4、格式化分區(根據系統和實際情況格式化成自己想要的文件系統

5、創建掛載目錄

6、掛載分區指定目錄,並創建開機自動掛載

1、掛載分區

mount /dev/xvdb1 /licheng
2、創建開機自動掛載

1.可以通過磁盤分區名掛載

2. 也可以通過uuid進行掛載

查看磁盤的uuid

blkid

掛載

UUID=688f7de2-cdd3-444b-8ffc-3d3239e3e123 /opt ext4 defaults 1 2

 

參考文檔:https://www.cnblogs.com/happy-king/p/9070496.html#_label1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM