From 4061c5c6b6d64247b1a514acedb5c7b648f010d4 Mon Sep 17 00:00:00 2001 From: bin456789 Date: Thu, 17 Apr 2025 21:10:24 +0800 Subject: [PATCH] =?UTF-8?q?dd:=20`State.ini`=20=E4=B8=8D=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E6=97=B6=E4=BB=8E=E6=B3=A8=E5=86=8C=E8=A1=A8=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trans.sh | 52 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/trans.sh b/trans.sh index 50d62c3..b1c16b6 100644 --- a/trans.sh +++ b/trans.sh @@ -2775,6 +2775,28 @@ EOF create_cloud_init_network_config "$ci_file" "$recognize_static6" "$recognize_ipv6_types" } +get_image_state() { + local os_dir=$1 + local image_state= + + # 如果 dd 镜像精简了 State.ini,则从注册表获取 + if state_ini=$(find_file_ignore_case $os_dir/Windows/Setup/State/State.ini); then + image_state=$(grep -i '^ImageState=' $state_ini | cut -d= -f2 | tr -d '\r') + fi + if [ -z "$image_state" ]; then + apk add hivex + hive=$(find_file_ignore_case $os_dir/Windows/System32/config/SOFTWARE) + image_state=$(hivexget $hive '\Microsoft\Windows\CurrentVersion\Setup\State' ImageState) + apk del hivex + fi + + if [ -n "$image_state" ]; then + echo "$image_state" + else + error_and_exit "Cannot get ImageState." + fi +} + modify_windows() { os_dir=$1 info "Modify Windows" @@ -2784,15 +2806,8 @@ modify_windows() { # https://learn.microsoft.com/windows-hardware/manufacture/desktop/add-a-custom-script-to-windows-setup # 判断用 SetupComplete 还是组策略 - # 默认视为 IMAGE_STATE_COMPLETE,除非有 State.ini,因为 dd 镜像可能精简了 State.ini - # 但最好还是从注册表获取 - image_state=IMAGE_STATE_COMPLETE - if state_ini=$(find_file_ignore_case $os_dir/Windows/Setup/State/State.ini); then - cat -n $state_ini - if tmp=$(grep -i '^ImageState=' $state_ini | cut -d= -f2 | tr -d '\r' | grep .); then - image_state=$tmp - fi - fi + image_state=$(get_image_state "$os_dir") + echo "ImageState: $image_state" if [ "$image_state" = IMAGE_STATE_COMPLETE ]; then use_gpo=true @@ -5231,15 +5246,18 @@ is_list_has() { echo "$list" | grep -qFx "$item" } +# hivexget 是 shell 脚本,开头是 #!/bin/bash +# 但 alpine 没安装 bash,直接运行 hivexget 会报错 +hivexget() { + ash "$(which hivexget)" "$@" +} + get_installation_type_from_windows_drive() { - os_dir=$1 + local os_dir=$1 apk add hivex - software_hive=$(find_file_ignore_case $os_dir/Windows/System32/config/SOFTWARE) - hivexsh "$software_hive" <