博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

CentOS7 扩容磁盘非根分区

Posted on 2020-12-11 19:14  偷鸟  阅读(316)  评论(0编辑  收藏  举报

CentOS7扩展磁盘分区

​ 因为业务所在分区空间不足,需要扩容。分配磁盘大小为1T但实际使用过程中发现仅使用了500G左右空间,本次计划在原有磁盘上扩容1T,加上之前500G空闲直接扩容1.5TB。以下是操作步骤

1、先查看分区使用情况

#查看分区使用情况
[root@localhost ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root         200G  2.7G  198G   2% /
devtmpfs                         16G     0   16G   0% /dev
tmpfs                            16G   41M   16G   1% /dev/shm
tmpfs                            16G  1.6G   15G  11% /run
tmpfs                            16G     0   16G   0% /sys/fs/cgroup
/dev/vda1                      1014M  189M  826M  19% /boot
/dev/mapper/centos-var_data  299G  285G   15G  96% /var/data
tmpfs                           3.2G     0  3.2G   0% /run/user/0


2、检查可用设备信息

#列出所有可用块设备的信息
[root@localhost ~]# lsblk
NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0                      2:0    1    4K  0 disk 
sr0                     11:0    1 1024M  0 rom  
vda                    252:0    0    2T  0 disk 
├─vda1                 252:1    0    1G  0 part /boot
└─vda2                 252:2    0  499G  0 part #分区500G
  ├─centos-root        253:0    0  200G  0 lvm  /
  └─centos-var_data 253:1    0  299G  0 lvm  /var/data
vdb                    252:16   0 1000G  0 disk #又增加了一块1T的盘,后删除掉了

3、查看磁盘信息

[root@localhost ~]# fdisk -l

Disk /dev/vda: 2147.5 GB, 2147483648000 bytes, 4194304000 sectors#先将磁盘调整为2TB
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: 0x000adc8a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200  1048575999   523238400   8e  Linux LVM #注意观察分区大小只有500G

Disk /dev/mapper/centos-root: 214.7 GB, 214748364800 bytes, 419430400 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-var_data: 321.0 GB, 321044611072 bytes, 627040256 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/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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

4、将剩余空间创建分区

[root@localhost ~]# fdisk /dev/vda  #选择分区设备
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): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): p#打印分区信息

Disk /dev/vda: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x000adc8a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200  1048575999   523238400   8e  Linux LVM

Command (m for help): n#创建分区
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p#创建主分区
Partition number (3,4, default 3): 
First sector (1048576000-4194303999, default 1048576000): 
Using default value 1048576000
Last sector, +sectors or +size{K,M,G} (1048576000-4194303999, default 4194303999): 
Using default value 4194303999
Partition 3 of type Linux and of size 1.5 TiB is set

Command (m for help): p#打印分区信息

Disk /dev/vda: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x000adc8a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200  1048575999   523238400   8e  Linux LVM
/dev/vda3      1048576000  4194303999  1572864000   83  Linux#新增分区

Command (m for help): 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.
#告警信息,重启或者刷新分区表同步磁盘信息
[root@localhost ~]# partprobe




5、调整卷组信息

[root@localhost ~]# vgdisplay
  --- 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                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <499.00 GiB
  PE Size               4.00 MiB
  Total PE              127743
  Alloc PE / Size       127743 / <499.00 GiB
  Free  PE / Size       0 / 0   #没有空余空间显示
  VG UUID               kCpfu1-tscC-HCs2-l6WI-gBA1-zsop-nu06s1
  
  #因为没有同步磁盘信息小插曲~~
  [root@localhost ~]#  pvcreate /dev/vda3
  Device /dev/vda3 not found.
  #[root@localhost ~]# partprobe
#初始化分区新建的分区
[root@localhost ~]# pvcreate /dev/vda3 
  Physical volume "/dev/vda3" successfully created.


6、扩容卷组

 将初始化过的分区加入到虚拟卷组中
#vgextend 虚拟卷组 新增的分区
[root@localhost ~]#vgextend centos /dev/vda3
  Volume group "centos" successfully extended
[root@localhost ~]# 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               1.95 TiB
  PE Size               4.00 MiB
  Total PE              511742
  Alloc PE / Size       127743 / <499.00 GiB
  Free  PE / Size       383999 / 1.46 TiB#空闲卷组容量。
  VG UUID               kCpfu1-tscC-HCs2-l6WI-gBA1-zsop-nu06s1
#查看当前逻辑卷组信息
[root@localhost ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root#逻辑卷名称
  VG Name                centos#卷组
  LV UUID                r2ogDp-MlxN-t0P4-p6Tl-R6Ig-qwSo-BtK9n2
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-01-12 19:40:42 -0500
  LV Status              available
  # open                 1
  LV Size                200.00 GiB
  Current LE             51200
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/centos/var_data#逻辑卷路径记下来要扩容他
  LV Name                var_data#逻辑卷名称
  VG Name                centos#卷组
  LV UUID                se7tQD-APTP-tNxU-2ap8-s2TA-PxnP-P1Zh2G
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-01-12 19:40:43 -0500
  LV Status              available
  # open                 1
  LV Size                <299.00 GiB
  Current LE             76543
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
#lvextend -l +需要扩展的容量,需要扩展的逻辑卷路径
[root@localhost ~]# lvextend -l +100%FREE /dev/centos/var_data  #当前设备可用扩进去
  Size of logical volume centos/var_data changed from <299.00 GiB (76543 extents) to <1.76 TiB (460542 extents).
  Logical volume centos/var_data successfully resized.

7、扩展磁盘空间

[root@localhost ~]#df -Th
Filesystem                     Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root        xfs       200G  2.7G  198G   2% /
devtmpfs                       devtmpfs   16G     0   16G   0% /dev
tmpfs                          tmpfs      16G   41M   16G   1% /dev/shm
tmpfs                          tmpfs      16G  1.6G   15G  11% /run
tmpfs                          tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/vda1                      xfs      1014M  189M  826M  19% /boot
/dev/mapper/centos-var_data xfs       299G  284G   15G  96% /var/data #注意分区格式xfs
tmpfs                          tmpfs     3.2G     0  3.2G   0% /run/user/0
[root@localhost ~] # xfs_growfs /dev/centos/var_data 
meta-data=/dev/mapper/centos-var_data isize=512    agcount=5, agsize=19594752 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=78380032, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=38271, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 78380032 to 471595008

8、检查结果


#检查结果
[root@localhost ~]# df -Th
Filesystem                     Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root        xfs       200G  2.7G  198G   2% /
devtmpfs                       devtmpfs   16G     0   16G   0% /dev
tmpfs                          tmpfs      16G   41M   16G   1% /dev/shm
tmpfs                          tmpfs      16G  1.6G   15G  11% /run
tmpfs                          tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/vda1                      xfs      1014M  189M  826M  19% /boot
/dev/mapper/centos-var_data xfs       1.8T  285G  1.5T  16% /var/data#扩容完成
tmpfs                          tmpfs     3.2G     0  3.2G   0% /run/user/0

注意事项

1、及时同步磁盘分区信息,不同步会造成激活分区失败

2、扩展磁盘空间时注意分区格式 ext4、xfs命令不一样