概述:本文将介绍在VMware ESXi6.7中CentOS7虚拟机如何缩小根分区大小,以及如何将缩小后的根分区迁移到其他盘。实例中根分区容量为20G,数据实际占用空间为6.4G,因此以下步骤将根分区无损缩减至7G,然后将缩小后的根分区由/dev/sdb迁移到/dev/sda2物理分区。
注意:1、xfs文件系统不支持直接缩小,以下操作根分区为ext4文件系统;2、以下操作有风险,不熟悉的情况下,请勿轻易在生产环境操作!!!
1 缩小根分区大小
1.1 系统资源检查
1.1.1 检查可用块设备和系统空间使用
可用块设备情况
[ root@centos7 ~]
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 ~]
文件系统 容量 已用 可用 已用% 挂载点
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 ~]
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 ~]
VG
centos 2 3 0 wz--n- 43 .99g 17 .49g
[ root@centos7 ~]
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 ~]
--- 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 ~] — 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 ~] — 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 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 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 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
1.1.3 检查根分区文件系统
根分区逻辑卷文件系统为ext4
[ root@centos7 ~]
/dev/mapper/centos-root: UUID = "e85374cc-7d9f-4ba2-bd30-41fe2ae08f09" TYPE = "ext4"
1.1.4 系统关机
以上信息检查完成以后执行shutdown -h now
进行系统关机
[ root@centos7 ~]
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
sh-4.2
sh-4.2
sh-4.2
sh-4.2
1.3.3 缩小根文件系统大小
sh-4.2
sh-4.2
sh-4.2
1.3.4 缩小lv大小
sh-4.2
sh-4.2
sh-4.2
sh-4.2
2 移动根分区
2.1 重新检查根分区情况
根分区已由原来的20G无损变为7G
[ root@centos7 ~]
文件系统 容量 已用 可用 已用% 挂载点
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 ~]
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 ~]
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 ~]
--- 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
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 ~]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
2.2 移动根分区到sda2上
将根分区lv由/dev/sdb
移动到/dev/sda2
[ root@centos7 ~]
/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 ~]
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 ~]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2.3 删除虚拟硬盘
注意:虚拟磁盘需要未使用才能删除,以上/dev/sdb上还有lv,因此不能删除,以下仅为删除方法示例
[ root@centos7 ~]
[ root@centos7 ~]
[ root@centos7 ~]
[ root@centos7 ~]
[ root@centos7 ~]
[ root@centos7 ~]
[ root@centos7 ~]
问题背景:有的用户在使用服务器 过程中,没有规划好服务器的磁盘使用,到后期,想从已有的磁盘中压缩出一块空间创建新的分区,挂载到其他业务使用,本文针对这种场景,输出下具体的操作方案,仅供参考;
一、缩容系统盘分区
分区缩容必须在磁盘非挂载状态下操作,所以对于腾讯云服务器 系统盘,需要进入到救援模式下操作。
1、查看系统盘vda1分区已使用大小,为2.3G,整个分区大小为50G;
2、卸载:umount /dev/vda1
3、检查分区:e2fsck -f /dev/vda1
4、设置文件系统大小为10G:resize2fs /dev/vda1 10G
5、fdisk命令进行分区:
1)fdisk /dev/vda
2)输入p 查看分区表;
3)输入d,删除指定的某个分区表
4)输入n,创建新分区
5)输入p,创建主分区
6)回车,默认为1
7)回车,默认为分区起始大小
8)输入+10G,设置分区为10G;
9)输入w,保存退出
6、挂载到/mnt目录,查看分区大小
7、卸载/vda1:umount /dev/vda1
8、创建新分区
9、给新分区创建文件系统:mkfs.ext4 /dev/vda2
10、查看分区情况:fdisk -l /dev/vda
11、退出救援模式 ,登录服务器查看,vda1分区已缩小至10G,创建的其他分区2、3可挂载到其他的目录使用;
二、缩容数据盘分区
缩容数据盘分区和缩容系统盘分区其实操作上没有差异,相对而言更简单,不需要进入救援模式,在服务器中直接操作;
1、查看磁盘分区情况:fdisk -l
2、查看文件系统大小:df -Th
3、卸载分区:umount /dev/vdb1
4、检查分区:e2fsck -f /dev/vdb1
5、设置文件系统大小:resize2fs /dev/vdb1 10G
6、fdisk命令进行分区:
1)fdisk /dev/vdb
2)输入p 查看分区表;
3)输入d,删除指定的某个分区表
4)输入n,创建新分区
5)输入p,创建主分区
6)回车,默认为1
7)回车,默认为分区起始大小
8)输入+10G,设置分区为10G;
9)输入w,保存退出
7、挂载:mount /dev/vdb1 /data 开始使用即可。
以上即为分区缩容操作,有问题欢迎评论留言。
本次是虚拟机装的centos7 的内核不知原因以外丢失造成无法开机,开机显示找不到内核!
恢复方法:
挂载新的ISO文件,然后进入bios 选择dvd启动。
启动后进入Troublesshooting,然后选择救援模式Rescue a centos system.
一路continue,ok进入救援模式页面。
进入后是sh-4.2#
执行命令:chroot /mnt/sysimage/
进入bash-4.2#
创建目录:mkdir /verbOS/
挂载目录: mount /dev/sr0 /verbOS
进入内核软件包目录: cd /verbOS/Package/
找到对应的内核:kernel-3.10.0-XXXXX.rpm
复制到mnt目录:cp kernel-3.10.0-XXXXX.rpm /mnt/
卸载之前内核:
rpm -qa|grep kernel
rpm -e kernel-3.10.0-XXXXX
去/boot/目录查看文件,只剩grub和grub2目录就可以了。
执行grub2-install /dev/sda安装grub2
执行grub2-mkconfig /boot/grub2/grub.cfg写入配置文件
复制 /boot/grub2/grub.cfg到/boot/grub/grub.cfg就可以了。
然后exit,exit
重启后选择本地启动就可以了
转载于:https://www.cnblogs.com/xiongyoutom/p/10410509.html
肥肠粉打野
CentOS7缩小根分区大小并移动根分区
开发者社区
专栏首页
故障排查
腾讯云服务器CentOS7.8系统分区缩容
weixin_30861459
记一次centos7内核可能意外丢失(测试直接干掉)恢复方法