2024-10-15 19:11:30 +08:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
expand_img() {
|
|
|
|
|
# 检查是否已安装 parted
|
|
|
|
|
if ! command -v parted &> /dev/null; then
|
|
|
|
|
echo "parted 未安装,正在安装..."
|
|
|
|
|
opkg update
|
|
|
|
|
opkg install parted
|
|
|
|
|
if ! command -v parted &> /dev/null; then
|
|
|
|
|
echo "安装 parted 失败,请检查网络连接或软件源配置。"
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
echo "parted 安装成功。"
|
|
|
|
|
else
|
|
|
|
|
echo ""
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 查找当前目录的 .img.gz 文件
|
|
|
|
|
img_gz_files=($(ls *.img.gz 2>/dev/null))
|
|
|
|
|
|
|
|
|
|
# 检查是否有多个 .img.gz 文件
|
|
|
|
|
if [ ${#img_gz_files[@]} -gt 1 ]; then
|
|
|
|
|
echo "当前目录有多个 .img.gz 文件:"
|
|
|
|
|
for file in "${img_gz_files[@]}"; do
|
|
|
|
|
echo " - $file"
|
|
|
|
|
done
|
|
|
|
|
echo "请删除不需要扩容的img.gz文件,并确保只剩一个 .img.gz 文件。"
|
|
|
|
|
return 1
|
|
|
|
|
elif [ ${#img_gz_files[@]} -eq 0 ]; then
|
|
|
|
|
echo "未找到 .img.gz 文件。"
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
img_gz="${img_gz_files[0]}"
|
|
|
|
|
|
|
|
|
|
# 提示用户输入扩容大小,默认值为 2000MB
|
|
|
|
|
read -p "请输入需要扩容的大小(默认 2000MB, 单位 MB): " size
|
|
|
|
|
size=${size:-2000} # 如果用户没有输入,使用默认值 2000MB
|
|
|
|
|
|
|
|
|
|
# 检查输入的值是否为正整数
|
|
|
|
|
if ! [[ "$size" =~ ^[0-9]+$ ]]; then
|
|
|
|
|
echo "请输入一个有效的数字。"
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "扩展大小设置为 ${size}MB。"
|
|
|
|
|
|
|
|
|
|
# 解压缩镜像文件
|
|
|
|
|
echo "解压缩 $img_gz ..."
|
|
|
|
|
gzip -kd "$img_gz"
|
|
|
|
|
|
|
|
|
|
img_file="${img_gz%.gz}"
|
|
|
|
|
|
|
|
|
|
# 删除原始的 .img.gz 文件
|
|
|
|
|
echo "删除原始的 $img_gz ..."
|
|
|
|
|
rm -f "$img_gz"
|
|
|
|
|
|
|
|
|
|
# 扩展镜像文件的大小
|
|
|
|
|
echo "扩展 $img_file 的大小 ..."
|
|
|
|
|
dd if=/dev/zero bs=1M count="$size" >> "$img_file"
|
|
|
|
|
|
|
|
|
|
# 使用 parted 工具调整分区,并自动输入 Fix 和 OK
|
|
|
|
|
echo "调整分区 ..."
|
|
|
|
|
yes | parted "$img_file" <<EOF
|
|
|
|
|
print
|
|
|
|
|
resizepart 2 100%
|
|
|
|
|
print
|
|
|
|
|
quit
|
|
|
|
|
EOF
|
|
|
|
|
|
2024-10-15 19:24:38 +08:00
|
|
|
|
echo "操作完成!镜像文件已扩展。请输入【ll -h】查看img大小"
|
2024-10-15 19:11:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expand_img
|