2

Ext4分区的缩小

 7 months ago
source link: https://bajie.dev/posts/20240124-ext4_resize/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Ext4分区的缩小

2024-01-24 2 分钟阅读

缩小/ext4分区步骤如下:

首先查看分区是什么文件类型

file -sL /dev/sd*  
/dev/sda:  x86 boot sector; GRand Unified Bootloader, stage1 version 0x3, boot drive 0x80, 1st sector stage2 0x2044148, GRUB version 0.94; partition 1: ID=0x82, starthead 32, startsector 2048, 8388608 sectors; partition 2: ID=0x83, active, starthead 75, startsector 8390656, 411039744 sectors, code offset 0x48
/dev/sda1: Linux/i386 swap file (new style) 1 (4K pages) size 1048575 pages
/dev/sda2: Linux rev 1.0 ext4 filesystem data (needs journal recovery) (extents) (large files) (huge files)

能看出来sda1是交换分区,那么要缩的分区是/dev/sda2,文件类型是ext4

ok,先确保虚机内/分区只占到5G,然后最好重新拷贝一下qcow2文件,把文件到弄到前面5G中。

然后修改一下kvm虚机配置,先加个iso进入rescue模式

virsh edit xxx  
把boot顺序从hd改成cdrom

...
    <boot dev='hd'/>
    <boot dev='cdrom'/>
...
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/export/kvm/iso/CentOS-7-x86_64-NetInstall-1708.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
...

系统启动,进入cdrom安装,选择Troubleshooting

image-20240124092137952

选择Rescue

image-20240124092158574

因为要对磁盘操作,所以不能选1把硬盘mount到/mnt/sysimage,选择3,不mount硬盘,直接进入一个shell

image-20240124092219572

检查磁盘,缩减/dev/sda2 到5G空间

e2fsck -f /dev/sda2  
resize2fs /dev/sda2 5G  
image-20240124092255483

注意,现在只做了一半,文件是被集中到/dev/sda2的前5G空间里面去了。但是,硬盘分区还没有更改。

接着来,用parted来修改分区

parted /dev/sda  
print  
查看后发现前2096是swap分区,2是/dev/sda2
删除/dev/sda2
rm 2  
重建
mkpart  
... primary
... 2096
... 7096

注意,新建的/dev/sda2起点是2096,分区终点是2096+5000=7096

最后再运行一下磁盘检查和重建

e2fsck -f /dev/sda2  
resize2fs /dev/sda2 5G  

重启搞定!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK