PR小鸡Debian调整swap

先通过https://developer.aliyun.com/ask/213532

If the case is remote server without console, you can shrink filesystem using initramfs while rebooting. You need to add resize2fs to initramfs and run it before mounting root.

Debian/Ubuntu example:

/etc/initramfs-tools/hooks/resizefs (executable):

#!/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

/etc/initramfs-tools/scripts/local-premount/resizefs (executable)

#!/bin/sh

set -e

PREREQS=""

prereqs() { echo "$PREREQS"; }

case "$1" in
prereqs)
prereqs
exit 0
;;
esac

/sbin/e2fsck -yf /dev/sda1
/sbin/resize2fs /dev/sda1 5G
/sbin/e2fsck -yf /dev/sda1

Now run update-initramfs, reboot, check size, remove these scripts and update-intiramfs again.

For dracut (Fedora,CentOS) use the same logic (/usr/lib/dracut/modules.d).

This is very usefull for crypting or moving rootfs, with no free space available for the new partition.

(注意代码需要unix格式)将/dev/host-01-vg/root 调整为7GB

然后再
lvresize -r -L-2G /dev/host-01-vg/root
swapoff /dev/host-01-vg/swap_1
lvresize -r -L+2G /dev/host-01-vg/swap_1
mkswap /dev/host-01-vg/swap_1
swapon /dev/host-01-vg/swap_1

resize2fs /dev/host-01-vg/root

参考:https://github.com/Yanhao/articles/blob/8a4c4ae5e4c1e71e48692c3ee4e9339c86492442/articles/debian%20initramfs%E8%B0%83%E8%AF%95%E6%96%B9%E6%B3%95.md

https://serverfault.com/questions/528075/is-it-possible-to-on-line-shrink-a-ext4-volume-with-lvm?spm=a2c6h.13066369.0.0.4d9a6ec7HU8ebd