Linux 存储扩容,分给扩容


CentOS为例扩容分区

MBR分区扩容

[root@localhost ~]# df -hT
文件系统    类型  容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs  200G 2.2G 198G 2% /
devtmpfs    devtmpfs 32G  0 32G 0% /dev
tmpfs     tmpfs  32G  0 32G 0% /dev/shm
tmpfs     tmpfs  32G 49M 32G 1% /run
tmpfs     tmpfs  32G  0 32G 0% /sys/fs/cgroup
/dev/sda1    xfs  197M 139M 59M 71% /boot
tmpfs     tmpfs  6.3G  0 6.3G 0% /run/user/0
/dev/mapper/centos-home xfs  628G 33M 718G 1% /home

查看新增加的磁盘信息

[root@DEV-CMDB-DB02 ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000e0945

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   104857599    51915776   8e  Linux LVM

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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_mb--centos7-root: 47.7 GB, 47747956736 bytes, 93257728 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_mb--centos7-swap: 5368 MB, 5368709120 bytes, 10485760 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

根据以上信息,对新增加的磁盘进行分区

[root@DEV-CMDB-DB02 ~]# fdisk /dev/sdb
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x232defba.

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x232defba

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): 
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x232defba

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209715199   104856576   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

创建物理卷

[root@DEV-CMDB-DB02 ~]# pvcreate /dev/sdb1
v  Physical volume "/dev/sdb1" successfully created

将新增加的分区/dev/sdb1加入到根目录分区centos_mb-centos7中

[root@DEV-CMDB-DB02 ~]# vgextend centos_mb-centos7 /dev/sdb1
  Volume group "centos_mb-centos7" successfully extended

使用vgdisplay命令查看系统上的空闲空间

root@localhost ~]# vgdisplay
 --- 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    3
 Open LV    3
 Max PV    0
 Cur PV    1
 Act PV    1
 VG Size    930.80 GiB
 PE Size    4.00 MiB
 Total PE    238285
 Alloc PE / Size  212736 / 831.00 GiB
 Free PE / Size  25549 / 99.80 GiB  //这一项表示目前该系统上还存在99.80G的空闲空间(25549)
 VG UUID    a5hiAh-LB8M-9lRv-Ps1a-z35L-J4fk-sP3KrF

将上面查到的空闲空间中的90G增减到/home分区上

[root@localhost ~]# lvextend -l +100%FREE /dev/mapper/centos-home  //或者使用-l参数(跟PE数量),即lvextend -l +25500 /dev/mapper/centos-home
 Size of logical volume centos/home changed from 628.00 GiB (160768 extents) to 718.00 GiB (183808 extents).
 Logical volume centos/home successfully resized.

[root@localhost ~]# xfs_growfs /dev/mapper/centos-home
meta-data=/dev/mapper/centos-home isize=512 agcount=4, agsize=41156608 blks
   =      sectsz=512 attr=2, projid32bit=1
   =      crc=1  finobt=0 spinodes=0
data  =      bsize=4096 blocks=164626432, imaxpct=25
   =      sunit=0  swidth=0 blks
naming =version 2    bsize=4096 ascii-ci=0 ftype=1
log  =internal    bsize=4096 blocks=80384, version=2
   =      sectsz=512 sunit=0 blks, lazy-count=1
realtime =none     extsz=4096 blocks=0, rtextents=0
data blocks changed from 164626432 to 188219392

再次看着系统分区,发现home分区已经增加了90G(这种方式增加后,home分区之前的数据还不会丢失)

[root@localhost ~]# df -h
文件系统     容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 200G 2.2G 198G 2% /
devtmpfs     32G  0 32G 0% /dev
tmpfs      32G  0 32G 0% /dev/shm
tmpfs      32G 49M 32G 1% /run
tmpfs      32G  0 32G 0% /sys/fs/cgroup
/dev/sda1    197M 139M 59M 71% /boot
tmpfs     6.3G  0 6.3G 0% /run/user/0
/dev/mapper/centos-home 718G 33M 718G 1% /home

虽然xfs文件系统只支持增加,不支持减少。但并不是说在xfs系统文件下不能减小,只是减小后,需要重新格式化才能挂载上。这样原来的数据就丢失了!

GPT分区扩容

GPT分区与MBR分区有一定区别,不能通过 growpart 进行分区扩容,所以还是采用新建LVM物理卷后合并到逻辑分区中的办法。

# 磁盘分区工具
parted
# 显示分区信息,如果将虚拟机的磁盘进行扩容后提示分区错误调整信息,直接选择两次Fix即可
print
# 切换到扇区数显示
unit s
# 显示分区信息(最后一个分区的结束扇区显示:4106239s)
print
# 获取print中最后一个分区的end扇区,然后+1,作为新分区的开始扇区,创建主分区,将剩余空间全部创建到新分区中
mkpart primary  4106240s 100%
# 再次查看分区信息,可以发现新建的分区
print
# 激活新分区为LVM,4为新分区的分区号
set 4 lvm on
# 退出 parted 
quit
# 更新系统分区信息
partprobe
# 创建LVM物理卷
pvcreate /dev/sda4
# 将物理卷加入到卷组
vgextend centos /dev/sda4
# 将剩余百分百空间都添加到逻辑卷中
lvextend -l +100%FREE /dev/centos/root
# xfs 在线扩容分区
xfs_growfs /dev/centos/root
# 查看扩容后分区状态信息
df -h

文章作者: Jeremy Peng
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Jeremy Peng !
 上一篇
Centos7 搭建开源个人网盘Nextcloud与常用插件 Centos7 搭建开源个人网盘Nextcloud与常用插件
简介文件服务器,是一个公司最常用的服务应用,每个公司企业基本都有自己的文件服务器实,现储存分享,上传下载文件文档等功能,常见的文件服务器就是ftp服务器,但是ftp服务器的功能实在有限,且对于普通用户使用入手难度较大,又缺乏界面,对于公司的
2020-06-20
下一篇 
Linux 查看服务器远程管理IP(DELL iDrac和HP iLO) Linux 查看服务器远程管理IP(DELL iDrac和HP iLO)
在我们运维工作中经常会有使用ilo口管理服务器情况,这时如果忘记啦或都不知道默认IP,这时我们就可以在系统中查看。
2020-05-20
  目录