From 7edc57c5a3ac3fe61f0964d5fc6789b170760b3d Mon Sep 17 00:00:00 2001 From: bin456789 Date: Fri, 23 Aug 2024 00:33:59 +0800 Subject: [PATCH] =?UTF-8?q?dd:=20=E6=94=AF=E6=8C=81=20raw=20vhd=20?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.en.md | 2 +- README.md | 2 +- reinstall.sh | 33 +++++++++++++++++++++++++++------ trans.sh | 33 +++++++++++++++++++++------------ 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/README.en.md b/README.en.md index 821459a..63b97c4 100644 --- a/README.en.md +++ b/README.en.md @@ -151,7 +151,7 @@ bash reinstall.sh centos 9 ### Feature 2: DD -- Supports `gzip` and `xz` formatted images +- Supports `raw` `vhd` `gzip` and `xz` formatted images - When using DD with a Windows image, the script will automatically expand the system partition. For static IP machines, the IP will be configured automatically, and it may take a few minutes to take effect on first boot ```bash diff --git a/README.md b/README.md index 0947097..a3a8fd8 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ bash reinstall.sh centos 9 ### 功能 2: DD -- 支持 `gzip`、`xz` 格式的镜像 +- 支持 `raw` `vhd` `gzip`、`xz` 格式的镜像 - DD Windows 镜像时,会自动扩展系统盘。静态的机器会自动配置好 IP,可能首次开机几分钟后才生效 ```bash diff --git a/reinstall.sh b/reinstall.sh index 1ca30af..a54ac4d 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -52,8 +52,7 @@ Usage: $reinstall____ centos 9 kali arch gentoo - dd --img='http://xxx.com/xxx.xz' - dd --img='http://xxx.com/xxx.gzip' + dd --img='http://xxx.com/xxx.raw' (supports raw vhd gzip xz) windows --image-name='windows xxx yyy' --lang=xx-yy windows --image-name='windows xxx yyy' --iso='http://xxx.com/xxx.iso' netboot.xyz @@ -1063,10 +1062,32 @@ setos() { # shellcheck disable=SC2154 setos_dd() { - test_url $img 'xz|gzip' img_type + # 下面两种都是 raw + # DOS/MBR boot sector + # x86 boot sector; partition 1: ... + test_url $img 'xz|gzip|dos/mbr|x86' img_type + + # 修正 raw 的 img_type + if [ "$img_type" = dos/mbr ] || [ "$img_type" = x86 ]; then + img_type=raw + fi if is_efi; then - install_pkg hexdump $img_type + install_pkg hexdump + + if ! [ "$img_type" = raw ]; then + install_pkg $img_type + fi + + extract() { + if [ "$img_type" = raw ]; then + cat "$1" + else + # xz/gzip -d 文件必须有正确的扩展名,否则报扩展名错误 + # 因此用 stdin + $img_type -dc <"$1" + fi + } # openwrt 镜像 efi part type 不是 esp # 因此改成检测 fat? @@ -1079,7 +1100,7 @@ setos() { # 仅打印前34个扇区 * 4096字节(按最大的算) # 每行128字节 - "$img_type" -dc <"$tmp/img-test" | hexdump -n $((34 * 4096)) -e '128/1 "%02x" "\n"' -v >$tmp/img-test-hex + extract "$tmp/img-test" | hexdump -n $((34 * 4096)) -e '128/1 "%02x" "\n"' -v >$tmp/img-test-hex if grep -q '^28732ac11ff8d211ba4b00a0c93ec93b' $tmp/img-test-hex; then echo 'DD: Image is EFI.' else @@ -1087,7 +1108,7 @@ setos() { warn ' The current machine uses EFI boot, but the DD image is not an EFI image. Continue with DD? -当前机器使用 EFI 引导,但 DD 镜像不是 EFI 镜像。 +当前机器使用 EFI 引导,但 DD 镜像可能不是 EFI 镜像。 继续 DD?' read -r -p '[y/N]: ' if [[ "$REPLY" = [Yy] ]]; then diff --git a/trans.sh b/trans.sh index 7e86b7c..f2417b1 100644 --- a/trans.sh +++ b/trans.sh @@ -1650,18 +1650,27 @@ get_http_file_size_to() { fi } -# shellcheck disable=SC2154 -dd_gzip_xz() { +dd_gzip_xz_raw() { + # 用官方 wget,一来带进度条,二来自带重试 + # alpine busybox 自带 gzip xz,但官方版也许性能更好 + # raw 包括 vhd + # shellcheck disable=SC2154 case "$img_type" in - gzip) prog=gzip ;; - xz) prog=xz ;; + xz | gzip) apk add wget $img_type ;; + raw) apk add wget ;; *) error_and_exit 'Not supported' ;; esac - # alpine busybox 自带 gzip xz,但官方版也许性能更好 - # 用官方 wget,一来带进度条,二来自带重试 - apk add wget $prog - if ! command wget $img -O- --tries=5 --progress=bar:force | $prog -dc >/dev/$xda 2>/tmp/dd_stderr; then + pipe_extract() { + if [ "$img_type" = raw ]; then + cat + else + $img_type -dc + fi + } + + if ! command wget $img -O- --tries=5 --progress=bar:force | + pipe_extract >/dev/$xda 2>/tmp/dd_stderr; then # vhd 文件结尾有 512 字节额外信息,可以忽略 if grep -iq 'No space' /tmp/dd_stderr; then apk add parted @@ -4084,17 +4093,17 @@ trans() { ;; esac ;; - gzip | xz) + gzip | xz | raw) # 暂时没用到 gzip xz 格式的云镜像 - dd_gzip_xz + dd_gzip_xz_raw resize_after_install_cloud_image modify_os_on_disk linux ;; esac elif [ "$distro" = "dd" ]; then case "$img_type" in - gzip | xz) - dd_gzip_xz + gzip | xz | raw) + dd_gzip_xz_raw modify_os_on_disk windows ;; qemu) # dd qemu 不可能到这里,因为上面已处理