2023-05-03 22:22:21 +08:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
PATH="/usr/sbin:/usr/bin"
|
|
|
|
|
|
|
|
|
|
update_part() {
|
2023-12-05 22:09:34 +08:00
|
|
|
|
partx -u "$1"
|
|
|
|
|
udevadm trigger
|
|
|
|
|
udevadm settle
|
|
|
|
|
}
|
2023-05-03 22:22:21 +08:00
|
|
|
|
|
2023-05-04 20:57:49 +08:00
|
|
|
|
# el 自带 fdisk parted (el7的part不支持在线扩容)
|
2023-05-03 22:22:21 +08:00
|
|
|
|
# ubuntu 自带 fdisk growpart
|
|
|
|
|
|
2023-05-04 20:57:49 +08:00
|
|
|
|
# 删除分区用
|
|
|
|
|
# el/ubuntu fdisk
|
|
|
|
|
|
|
|
|
|
# 扩容分区用
|
|
|
|
|
# el7 grownparted 额外安装
|
|
|
|
|
# el8/9/fedora parted
|
|
|
|
|
# ubuntu grownpart
|
|
|
|
|
|
2023-05-03 22:22:21 +08:00
|
|
|
|
# 找出主硬盘
|
2023-10-22 00:42:06 +08:00
|
|
|
|
root_drive=$(mount | awk '$3=="/" {print $1}')
|
|
|
|
|
xda=$(lsblk -r --inverse "$root_drive" | grep -w disk | awk '{print $1}')
|
2023-05-03 22:22:21 +08:00
|
|
|
|
|
|
|
|
|
# 删除 installer 分区
|
|
|
|
|
installer_num=$(readlink -f /dev/disk/by-label/installer | grep -o '[0-9]*$')
|
|
|
|
|
if [ -n "$installer_num" ]; then
|
2023-12-05 22:32:54 +08:00
|
|
|
|
# 要添加 LC_NUMERIC 或者将%转义成\%才能在cron里正确运行
|
|
|
|
|
# locale -a 不一定有"en_US.UTF-8",但肯定有"C.UTF-8"
|
|
|
|
|
LC_NUMERIC="C.UTF-8"
|
|
|
|
|
printf "d\n%s\nw" "$installer_num" | fdisk "/dev/$xda"
|
|
|
|
|
update_part "/dev/$xda"
|
2023-05-03 22:22:21 +08:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 找出现在的最后一个分区,也就是系统分区
|
|
|
|
|
# el7 的 lsblk 没有 --sort,所以用其他方法
|
|
|
|
|
# shellcheck disable=2012
|
2023-12-05 22:32:54 +08:00
|
|
|
|
part_num=$(ls -1v "/dev/$xda"* | tail -1 | grep -o '[0-9]*$')
|
|
|
|
|
part_fstype=$(lsblk -no FSTYPE "/dev/$xda"*"$part_num")
|
2023-05-03 22:22:21 +08:00
|
|
|
|
|
|
|
|
|
# 扩容分区
|
2023-05-04 20:57:49 +08:00
|
|
|
|
# ubuntu 和 el7 用 growpart,其他用 parted
|
|
|
|
|
# el7 不能用parted在线扩容,而fdisk扩容会改变 PARTUUID,所以用 growpart
|
|
|
|
|
if grep -E -i 'centos:7|ubuntu' /etc/os-release; then
|
2023-12-05 22:32:54 +08:00
|
|
|
|
growpart "/dev/$xda" "$part_num"
|
2023-05-04 20:57:49 +08:00
|
|
|
|
else
|
2023-12-05 22:32:54 +08:00
|
|
|
|
printf 'yes\n100%%' | parted "/dev/$xda" resizepart "$part_num" ---pretend-input-tty
|
2023-05-04 20:57:49 +08:00
|
|
|
|
fi
|
2023-12-05 22:32:54 +08:00
|
|
|
|
update_part "/dev/$xda"
|
2023-05-03 22:22:21 +08:00
|
|
|
|
|
|
|
|
|
# 扩容最后一个分区的文件系统
|
|
|
|
|
case $part_fstype in
|
|
|
|
|
xfs) xfs_growfs / ;;
|
2023-12-05 22:32:54 +08:00
|
|
|
|
ext*) resize2fs "/dev/$xda"*"$part_num" ;;
|
2023-08-07 23:30:14 +08:00
|
|
|
|
btrfs) btrfs filesystem resize max / ;;
|
2023-05-03 22:22:21 +08:00
|
|
|
|
esac
|
2023-12-05 22:32:54 +08:00
|
|
|
|
update_part "/dev/$xda"
|
2023-05-03 22:22:21 +08:00
|
|
|
|
|
|
|
|
|
# 删除脚本自身
|
|
|
|
|
rm -f /resize.sh /etc/cron.d/resize
|