From 1b8fe2637f57ecc9bb357520d49d7e8566b0f266 Mon Sep 17 00:00:00 2001
From: bin456789 <bin456789@gmail.com>
Date: Sun, 4 Jun 2023 19:12:57 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0dd=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 README.md    |  6 +++++-
 reinstall.sh | 20 +++++++++++++++++---
 trans.sh     | 17 +++++++++++++++++
 3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md
index f4f5308..515f102 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@
 
 #### 亮点:
 ```
-使用官方安装方式,非第三方 dd 镜像,更安全
+使用官方安装方式,非第三方 dd 镜像,更安全(也提供 dd 功能)
 支持 BIOS/EFI 机器,支持 ARM 机器
 可能是第一个支持在 1g 内存上安装 红帽 7/8/9 系列的脚本
 可能是第一个支持重装到 ubuntu 22.04 的脚本
@@ -21,6 +21,9 @@ bash reinstall.sh centos-7 (或其他系统)
 安装 Windows:
 bash reinstall.sh windows --iso=https://example.com/zh-cn_windows_10_enterprise_ltsc_2021_x64_dvd_033b7312.iso --image-name='Windows 10 Enterprise LTSC 2021'
 
+dd:
+bash reinstall.sh dd --ddimg=https://example.com/xxx.gz
+
 重启:
 reboot
 ```
@@ -34,6 +37,7 @@ ubuntu-20.04/22.04
 alpine-3.16/3.17/3.18
 debian-10/11
 windows (见下方注意事项)
+dd
 ```
 #### Windows 注意事项:
 ```
diff --git a/reinstall.sh b/reinstall.sh
index ea9ae26..e7cc954 100644
--- a/reinstall.sh
+++ b/reinstall.sh
@@ -3,7 +3,7 @@ confhome=https://raw.githubusercontent.com/bin456789/reinstall/main
 localtest_confhome=http://192.168.253.1
 
 usage_and_exit() {
-    echo "Usage: reinstall.sh centos-7/8/9 alma-8/9 rocky-8/9 fedora-37/38 ubuntu-20.04/22.04 alpine-3.16/3.17/3.18 debian-10/11 windows"
+    echo "Usage: reinstall.sh centos-7/8/9 alma-8/9 rocky-8/9 fedora-37/38 ubuntu-20.04/22.04 alpine-3.16/3.17/3.18 debian-10/11 windows dd"
     exit 1
 }
 
@@ -96,6 +96,14 @@ setos() {
         eval "${step}_image_name='$image_name'"
     }
 
+    setos_dd() {
+        if [ -z "$ddimg" ]; then
+            echo "dd need --ddimg"
+            exit 1
+        fi
+        eval "${step}_ddimg='$ddimg'"
+    }
+
     setos_redhat() {
         if [ "$localtest" = 1 ]; then
             mirror=$confhome/$releasever/
@@ -132,13 +140,14 @@ setos() {
     alpine) setos_alpine ;;
     debian) setos_debian ;;
     windows) setos_windows ;;
+    dd) setos_dd ;;
     *) setos_redhat ;;
     esac
 }
 
 # 检查是否为正确的系统名
 verify_os_string() {
-    for os in 'centos-7|8|9' 'alma|rocky-8|9' 'fedora-37|38' 'ubuntu-20.04|22.04' 'alpine-3.16|3.17|3.18' 'debian-10|11|12' 'windows-'; do
+    for os in 'centos-7|8|9' 'alma|rocky-8|9' 'fedora-37|38' 'ubuntu-20.04|22.04' 'alpine-3.16|3.17|3.18' 'debian-10|11|12' 'windows-' 'dd-'; do
         ds=$(echo $os | cut -d- -f1)
         vers=$(echo $os | cut -d- -f2 | sed 's \. \\\. g')
         finalos=$(echo "$@" | tr '[:upper:]' '[:lower:]' | sed -n -E "s,^($ds)[ :-]?($vers)$,\1:\2,p")
@@ -208,7 +217,7 @@ if [ "$EUID" -ne 0 ]; then
     exit 1
 fi
 
-if ! opts=$(getopt -a -n $0 --options l --long localtest,iso:,image-name: -- "$@"); then
+if ! opts=$(getopt -a -n $0 --options l --long localtest,iso:,image-name:,ddimg: -- "$@"); then
     usage_and_exit
 fi
 
@@ -220,6 +229,10 @@ while true; do
         confhome=$localtest_confhome
         shift
         ;;
+    --ddimg)
+        ddimg=$2
+        shift 2
+        ;;
     --iso)
         iso=$2
         shift 2
@@ -262,6 +275,7 @@ esac
 if [ "$distro" = "ubuntu" ] ||
     [ "$distro" = "alpine" ] ||
     [ "$distro" = "windows" ] ||
+    [ "$distro" = "dd" ] ||
     { [ "$distro_like" = "redhat" ] && [ $releasever -ge 8 ] && [ $ram_size -lt 2048 ]; } ||
     { [ "$distro_like" = "redhat" ] && [ $releasever -eq 7 ] && [ $ram_size -lt 1536 ] && [ $basearch = "aarch64" ]; }; then
     # 安装alpine时,使用指定的版本。 alpine作为中间系统时,使用 3.18
diff --git a/trans.sh b/trans.sh
index f732770..ef5af74 100644
--- a/trans.sh
+++ b/trans.sh
@@ -89,6 +89,23 @@ if [ "$distro" = "alpine" ]; then
     export BOOTLOADER="grub"
     printf 'y' | setup-disk -m sys $kernel_opt -s 0 /dev/$xda
     exec reboot
+
+elif [ "$distro" = "dd" ]; then
+    filetype=$(echo $ddimg | awk -F. '{print $NF}')
+    case "$filetype" in
+    gz) prog=gzip ;;
+    xz) prog=xz ;;
+    esac
+
+    if [ -n "$prog" ]; then
+        # alpine busybox 自带 gzip xz,但官方版也许性能更好
+        apk add curl $prog
+        curl -L $ddimg | $prog -dc >/dev/$xda
+    else
+        echo 'Not supported'
+        sleep 1m
+    fi
+    exec reboot
 fi
 
 download() {