原文链接:点击打开链接
环境:Debian 9,例子中,将阿里云ECS云服务器默认的系统盘,40GB的/dev/vda1,划成30GB的/dev/vda1和10GB的/dev/vda2。
过程:
1. df -h # 查看当前的磁盘使用情况,/dev/vda1 是40GB
2. on-line shrinking not supported # 尝试直接运行resize2fs来压缩,提示错误,不允许对正在使用的分区进行压缩操作
3. vi /etc/initramfs-tools/hooks/resizefs # 创建好脚本,复制好resize2fs
- #!/bin/sh
- set -e
- PREREQS=""
- prereqs() { echo "$PREREQS"; }
- case $1 in
- prereqs)
- prereqs
- exit 0
- ;;
- esac
- . /usr/share/initramfs-tools/hook-functions
- copy_exec /sbin/e2fsck
- copy_exec /sbin/resize2fs
- exit 0
4. chmod +x /etc/initramfs-tools/hooks/resizefs # 添加执行权限
5. vi /etc/initramfs-tools/scripts/local-premount/resizefs # 创建好脚本,在挂载/前进行resize2fs操作
- #!/bin/sh
- set -e
- PREREQS=""
- prereqs() { echo "$PREREQS"; }
- case "$1" in
- prereqs)
- prereqs
- exit 0
- ;;
- esac
- /sbin/e2fsck -yf /dev/vda1
- /sbin/resize2fs /dev/vda1 30G
- /sbin/e2fsck -yf /dev/vda1
![](http://bbs.aliyun.com/attachment/Fid_239/239_1477160476054779_7b6bc5d01d46ff6.png?6)
7. update-initramfs -u -v # 更新系统引导
![](http://bbs.aliyun.com/attachment/Fid_239/239_1477160476054779_1fdb89071ac708b.png?19)
8. 重启系统后用 df 查看磁盘使用情况
![](http://bbs.aliyun.com/attachment/Fid_239/239_1477160476054779_065af35382c77f2.png?9)
9. fdisk /dev/vda # 用fdisk删除vda1分区,再新建30GB的vda1分区和10GB容量的vda2分区
![](http://bbs.aliyun.com/attachment/Fid_239/239_1477160476054779_ade3b096e729741.png?34)
10. rm /etc/initramfs-tools/hooks/resizefs /etc/initramfs-tools/scripts/local-premount/resizefs # 删除之前的启动脚本
11. update-initramfs -u -v # 更新系统引导
10. 重启系统后,用 fdisk -l 查看,可以看到相应的分区和容量 信息
![](http://bbs.aliyun.com/attachment/Fid_239/239_1477160476054779_742024ccde9f3b7.png?20)
参考: https://serverfault.com/questions/528075/is-it-possible-to-on-line-shrink-a-ext4-volume-with-lvm