CentOS7缩小根分区大小并移动根分区

概述:本文将介绍在VMware ESXi6.7中CentOS7虚拟机如何缩小根分区大小,以及如何将缩小后的根分区迁移到其他盘。实例中根分区容量为20G,数据实际占用空间为6.4G,因此以下步骤将根分区无损缩减至7G,然后将缩小后的根分区由/dev/sdb迁移到/dev/sda2物理分区。

注意:1、xfs文件系统不支持直接缩小,以下操作根分区为ext4文件系统;2、以下操作有风险,不熟悉的情况下,请勿轻易在生产环境操作!!!

1 缩小根分区大小

1.1 系统资源检查

1.1.1 检查可用块设备和系统空间使用

可用块设备情况

[root@centos7 ~]# lsblk 
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda             8:0    0   15G  0 disk 
├─sda1          8:1    0    1G  0 part /boot
└─sda2          8:2    0   14G  0 part 
sdb             8:16   0   30G  0 disk 
├─centos-root 253:0    0   20G  0 lvm  /
├─centos-swap 253:1    0  1.5G  0 lvm  [SWAP]
└─centos-home 253:2    0    5G  0 lvm  
sr0            11:0    1  4.5G  0 rom 

系统空间使用情况

[root@centos7 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 984M     0  984M    0% /dev
tmpfs                   1000M     0 1000M    0% /dev/shm
tmpfs                   1000M  9.7M  990M    1% /run
tmpfs                   1000M     0 1000M    0% /sys/fs/cgroup
/dev/mapper/centos-root   20G  6.4G   13G   35% /
/dev/sda1                976M  142M  768M   16% /boot
tmpfs                    200M   20K  200M    1% /run/user/0

1.1.2 检查pv、vg、lv

[root@centos7 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  centos lvm2 a--  <14.00g <14.00g
  /dev/sdb   centos lvm2 a--  <30.00g  <3.50g
[root@centos7 ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  centos   2   3   0 wz--n- 43.99g 17.49g
[root@centos7 ~]# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-a-----  5.00g                                                    
  root centos -wi-ao---- 20.00g                                                    
  swap centos -wi-ao----  1.50g                                                    
[root@centos7 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <14.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              3583
  Free PE               3583
  Allocated PE          0
  PV UUID               y1Zcnr-QR4j-d7ZV-PCT0-0PkU-U3lq-WrdJlQ
   
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               centos
  PV Size               30.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              7679
  Free PE               895
  Allocated PE          6784
  PV UUID               HGq346-LRER-oe3g-RfkB-85kP-ZW63-VZ1KnC
   
[root@centos7 ~]# vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  52
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               43.99 GiB
  PE Size               4.00 MiB
  Total PE              11262
  Alloc PE / Size       6784 / 26.50 GiB
  Free  PE / Size       4478 / 17.49 GiB
  VG UUID               C54xlW-5xo1-You2-HduJ-PIgl-D81L-ZdCvVe
   
[root@centos7 ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                gUjv12-A1UL-ZpPq-o8PT-87vZ-HveZ-Ai8qcW
  LV Write Access        read/write
  LV Creation host, time MiWiFi-R4CM-srv, 2022-01-25 02:17:11 +0800
  LV Status              available
  # open                 1
  LV Size                20.00 GiB
  Current LE             5120
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                k3a07P-JKtA-LmYO-f5ck-VNFb-bDjE-Pwd4Le
  LV Write Access        read/write
  LV Creation host, time MiWiFi-R4CM-srv, 2022-01-25 02:17:12 +0800
  LV Status              available
  # open                 2
  LV Size                1.50 GiB
  Current LE             384
  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                0bOQMg-IYhx-Dv3q-EliN-tFxn-RFDI-sX0F4t
  LV Write Access        read/write
  LV Creation host, time MiWiFi-R4CM-srv, 2022-01-26 23:38:34 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
[root@centos7 ~]# 

1.1.3 检查根分区文件系统

根分区逻辑卷文件系统为ext4

[root@centos7 ~]# blkid /dev/mapper/centos-root
/dev/mapper/centos-root: UUID="e85374cc-7d9f-4ba2-bd30-41fe2ae08f09" TYPE="ext4" 

1.1.4 系统关机

以上信息检查完成以后执行shutdown -h now进行系统关机

[root@centos7 ~]# shutdown -h now

1.2 CenOS7系统镜像上传到VMware ESXi6.7

1.2.1 上传本地iso镜像到VMware ESXi6.7

在这里插入图片描述

1.2.2 新增虚拟机启动项配置

添加iso镜像到虚拟光驱
在这里插入图片描述
设置引导延时3s,否则开机很容易错过从光驱启动,直接进入系统
在这里插入图片描述

1.3 进入rescue模式缩小根分区

1.3.1 进入rescue模式

在这里插入图片描述

1.3.2 扫描pv、vg和lv,并激活vg

#扫描pv
sh-4.2# pvscan
#扫描vg
sh-4.2# vgscan
#扫描lv
sh-4.2# lvscan
#激活根分区所在的vg
sh-4.2# vgchange -ay /dev/

在这里插入图片描述

1.3.3 缩小根文件系统大小

#检查根分区文件系统是否正常
sh-4.2# e2fsck -f /dev/centos/root
#缩小根文件系统大小为7G
sh-4.2# resize2fs /dev/centos/root 7G
#根文件系统检查
sh-4.2# fsck /dev/centos/root

在这里插入图片描述

1.3.4 缩小lv大小

#缩小根分区lv大小为7G
sh-4.2# lvreduce -L 7G /dev/centos/root
sh-4.2# resize2fs /dev/centos/root
#根文件系统检查
sh-4.2# fsck /dev/centos/root
#退出rescue模式,重启系统
sh-4.2# exit

在这里插入图片描述

2 移动根分区

2.1 重新检查根分区情况

根分区已由原来的20G无损变为7G

[root@centos7 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 984M     0  984M    0% /dev
tmpfs                   1000M     0 1000M    0% /dev/shm
tmpfs                   1000M  9.6M  991M    1% /run
tmpfs                   1000M     0 1000M    0% /sys/fs/cgroup
/dev/mapper/centos-root  6.8G  6.4G   55M  100% /
/dev/sda1                976M  142M  768M   16% /boot
tmpfs                    200M  8.0K  200M    1% /run/user/42
tmpfs                    200M     0  200M    0% /run/user/0
[root@centos7 ~]# lsblk 
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda             8:0    0   15G  0 disk 
├─sda1          8:1    0    1G  0 part /boot
└─sda2          8:2    0   14G  0 part 
sdb             8:16   0   30G  0 disk 
├─centos-root 253:0    0    7G  0 lvm  /
├─centos-swap 253:1    0  1.5G  0 lvm  [SWAP]
└─centos-home 253:2    0    5G  0 lvm  
sr0            11:0    1  4.5G  0 rom  
[root@centos7 ~]# lvs
  LV   VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-a----- 5.00g                                                    
  root centos -wi-ao---- 7.00g                                                    
  swap centos -wi-ao---- 1.50g                                                    
[root@centos7 ~]# lvdisplay /dev/centos/root 
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                gUjv12-A1UL-ZpPq-o8PT-87vZ-HveZ-Ai8qcW
  LV Write Access        read/write
  LV Creation host, time MiWiFi-R4CM-srv, 2022-01-25 02:17:11 +0800
  LV Status              available
  # open                 1
  LV Size                7.00 GiB
  Current LE             1792
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
[root@centos7 ~]# 

2.2 移动根分区到sda2上

将根分区lv由/dev/sdb移动到/dev/sda2

#pvmove命令将lv(/dev/centos/root)由源pv(/dev/sdb)移动到目的pv(/dev/sda2),其中参数[-i number]为进度显示间隔时间,单位为s,参数[-n /dev/vg/lvname]指定需要迁移的lv。
[root@centos7 ~]# pvmove -i 10 -n /dev/centos/root /dev/sdb /dev/sda2 
  /dev/sdb: Moved: 0.06%
  /dev/sdb: Moved: 25.11%
  /dev/sdb: Moved: 44.25%
  /dev/sdb: Moved: 59.93%
  /dev/sdb: Moved: 80.02%
  /dev/sdb: Moved: 100.00%
[root@centos7 ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   15G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   14G  0 part 
  └─centos-root 253:0    0    7G  0 lvm  /
sdb               8:16   0   30G  0 disk 
├─centos-swap   253:1    0  1.5G  0 lvm  [SWAP]
└─centos-home   253:2    0    5G  0 lvm  
sr0              11:0    1  4.5G  0 rom  
[root@centos7 ~]# 

2.3 删除虚拟硬盘

注意:虚拟磁盘需要未使用才能删除,以上/dev/sdb上还有lv,因此不能删除,以下仅为删除方法示例

[root@centos7 ~]# vgreduce centos /dev/sdb 
[root@centos7 ~]# pvremove /dev/sdb
[root@centos7 ~]# vgs
[root@centos7 ~]# vgdisplay
[root@centos7 ~]# pvs
[root@centos7 ~]# pvdisplay
[root@centos7 ~]# lsblk
  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肥肠粉打野

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值