在 Debian Linux 里“压缩卷” - shrink partion
环境:Debian 9,例子中,将阿里云ECS云服务器默认的系统盘,40GB的/dev/vda1,划成30GB的/dev/vda1和10GB的/dev/vda2。
过程:
- df -h # 查看当前的磁盘使用情况,/dev/vda1 是40GB
- on-line shrinking not supported # 尝试直接运行resize2fs来压缩,提示错误,不允许对正在使用的分区进行压缩操作
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
- chmod +x /etc/initramfs-tools/hooks/resizefs # 添加执行权限
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
- chmod +x /etc/initramfs-tools/scripts/local-premount/resizefs # 添加执行权限
- update-initramfs -u -v # 更新系统引导
- 重启系统后用 df 查看磁盘使用情况
- fdisk /dev/vda # 用fdisk删除vda1分区,再新建30GB的vda1分区和10GB容量的vda2分区
- rm /etc/initramfs-tools/hooks/resizefs /etc/initramfs-tools/scripts/local-premount/resizefs # 删除之前的启动脚本
- update-initramfs -u -v # 更新系统引导
- 重启系统后,用 fdisk -l 查看,可以看到相应的分区和容量信息
参考:https://serverfault.com/questions/528075/is-it-possible-to-on-line-shrink-a-ext4-volume-with-lvm