CentOS新增磁盘扩容根分区及/home分区操作方法

CentOS7.6新增一块100G磁盘扩容给根分区及home分区,新增/data分区

**

需求:现有CentOS根分区及/home分区空间不够,需要新增一块100G的磁盘将20G空间扩容给根分区,60G空间扩容给/home分区,剩下的20G空间划给新建/data分区

查看原有磁盘分区状态

[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G   16K  1.9G   1% /dev/shm
tmpfs                   tmpfs     1.9G  185M  1.7G  10% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        50G  3.6G   47G   8% /
/dev/vda1               xfs      1014M  189M  826M  19% /boot
/dev/mapper/centos-home xfs        26G  377M   25G   2% /home
tmpfs                   tmpfs     379M     0  379M   0% /run/user/0

新增100G磁盘,查询磁盘是否识别到

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0               2:0    1    4K  0 disk 
sr0              11:0    1  4.3G  0 rom  
vda             252:0    0   80G  0 disk 
├─vda1          252:1    0    1G  0 part /boot
└─vda2          252:2    0   79G  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 25.1G  0 lvm  /home
vdb             252:16   0  100G  0 disk
[root@localhost ~]# fdisk -l

Disk /dev/vda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x000bd0ad

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

Disk /dev/mapper/centos-root: 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 /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 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-home: 27.0 GB, 26969374720 bytes, 52674560 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: 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

可见新增磁盘已经识别到了,为/dev/vdb

1、下面开始给/data分配空间

1.1 创建/data目录

[root@localhost ~]# mkdir /data

1.2 查看vg列表,收集相关的vg信息

[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                3
  Open LV               3
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               178.99 GiB
  PE Size               4.00 MiB
  Total PE              45822
  Alloc PE / Size       20222 / 78.99 GiB
  Free  PE / Size       25600 / 100.00 GiB
  VG UUID               uBTksn-aQAX-Qj8E-Ezw9-7EH6-nCQm-d48dha

1.3 查看lv列表,收集lv相关信息

[root@localhost ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                wYvp54-IM0a-Ur8O-6glF-PksC-gRFi-MFhpcI
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-06-30 23:24:34 -0400
  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                aIpK61-I41C-6oKX-5Gcz-LRnj-LNCV-7Q8IAT
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-06-30 23:24:34 -0400
  LV Status              available
  # open                 1
  LV Size                <85.12 GiB
  Current LE             21790
  Segments               2
  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                LSxMUH-zNHQ-JKp3-DSbU-GunX-8cJ3-I7lj3v
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-06-30 23:24:34 -0400
  LV Status              available
  # open                 1
  LV Size                70.00 GiB
  Current LE             17920
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   

已经收集好了相关信息,下面开始操作

1.4 将新增的磁盘添加到原有的centosvg里面去

[root@localhost ~]# vgextend centos /dev/vdb 
WARNING: xfs signature detected on /dev/vdb at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/vdb.
  Physical volume "/dev/vdb" successfully created.
  Volume group "centos" successfully extended

1.5 创建LVM并分配20G的空间给/data

[root@localhost ~]# lvcreate -L 20G -n data centos
  Logical volume "data" created.
#-L:指定大小  -n:定义LVM卷名(lv0)  centos(指定vg)

1.6 分配好了对该lv执行格式化,格式化为.xfs的磁盘格式

root@localhost ~]# mkfs.xfs /dev/mapper/centos-data 
meta-data=/dev/mapper/centos-data isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242880, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

1.7 挂载该lv到/data分区上

[root@localhost ~]# mount /dev/mapper/centos-data /data/

1.8 查询该分区的UUID并写入到/etc/fstab文件中以实现服务器开机自动挂载

[root@localhost ~]# blkid /dev/mapper/centos-data
/dev/mapper/centos-data: UUID="4a88369f-3749-49e5-9f1b-15389b8482e5" TYPE="xfs"
vim /etc/fstab

在这里插入图片描述

1.9 最后输入mount -a名字没有报错确保挂载无问题

2、接下来该给根分区及/home扩容了

2.1 给/分区扩容20G

[root@localhost ~]# lvextend -L +20G /dev/mapper/centos-root 
  Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 70.00 GiB (17920 extents).
  Logical volume centos/root successfully resized.

2.2 给/home分区扩容60G

[root@localhost ~]# lvextend -L +60G /dev/mapper/centos-home
  Size of logical volume centos/home changed from <25.12 GiB (6430 extents) to <85.12 GiB (21790 extents).
  Logical volume centos/home successfully resized.

附:lvextend是可以动态调整分区的,-l是按百分比分配,+表示增加;-L是安装指定容量分配,+表示增加,-表示减少,不带符号表示将分区调整至指定大小;

lvextend –L +20G /dev/mapper/centos-root #root分区增加20G
lvreduce -L -20G /dev/mapper/centos-home #root分区减少2G
lvextend –L 20G /dev/mapper/centos-root #root分区设置为20G
lvextend -l +10%FREE /dev/mapper/vg_master-lv_root  #root分区增加空闲空间的10%

2.3 执行分配命令,使得改变马上生效

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

2.4 查看对应的分区是否扩容到位

[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G   16K  1.9G   1% /dev/shm
tmpfs                   tmpfs     1.9G  185M  1.7G  10% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        70G  3.6G   67G   6% /
/dev/vda1               xfs      1014M  189M  826M  19% /boot
/dev/mapper/centos-home xfs        86G  378M   85G   1% /home
tmpfs                   tmpfs     379M     0  379M   0% /run/user/0

2.5 /分区,/home分区扩容已到位,/data分区已完成,至此,所有操作已完成。

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值