windows: 在 pe 下设置主硬盘 id

This commit is contained in:
bin456789 2023-11-15 20:51:44 +08:00
parent 56f5c90a26
commit 56a8d9b421
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B
2 changed files with 61 additions and 10 deletions

View File

@ -1793,6 +1793,8 @@ install_windows() {
locale=$(wiminfo $install_wim | grep 'Default Language' | head -1 | awk '{print $NF}') locale=$(wiminfo $install_wim | grep 'Default Language' | head -1 | awk '{print $NF}')
sed -i "s|%arch%|$arch|; s|%image_name%|$image_name|; s|%locale%|$locale|" /tmp/Autounattend.xml sed -i "s|%arch%|$arch|; s|%image_name%|$image_name|; s|%locale%|$locale|" /tmp/Autounattend.xml
# 修改 disk_id
if false; then
# sda 只读,放的是 cloud-init 配置,通常 win 有驱动,能识别 # sda 只读,放的是 cloud-init 配置,通常 win 有驱动,能识别
# 而 vda/nvme/xen 加载驱动后才能识别,所以这时 disk_id 应该为 1 # 而 vda/nvme/xen 加载驱动后才能识别,所以这时 disk_id 应该为 1
if [ -e "/sys/class/block/sda/ro" ] && if [ -e "/sys/class/block/sda/ro" ] &&
@ -1802,6 +1804,7 @@ install_windows() {
disk_id=0 disk_id=0
fi fi
sed -i "s|%disk_id%|$disk_id|" /tmp/Autounattend.xml sed -i "s|%disk_id%|$disk_id|" /tmp/Autounattend.xml
fi
# 修改应答文件,分区配置 # 修改应答文件,分区配置
if is_efi; then if is_efi; then
@ -1832,7 +1835,6 @@ EOF
</ModifyPartition> </ModifyPartition>
EOF EOF
fi fi
unix2dos /tmp/Autounattend.xml
# # ei.cfg # # ei.cfg
# cat <<EOF >/os/installer/sources/ei.cfg # cat <<EOF >/os/installer/sources/ei.cfg
@ -1879,7 +1881,15 @@ EOF
fi fi
# 复制应答文件 # 复制应答文件
cp /tmp/Autounattend.xml /wim/ # 移除注释,否则 windows-setup.bat 重新生成的 Autounattend.xml 有问题
apk add xmlstarlet
xmlstarlet ed -d '//comment()' /tmp/Autounattend.xml >/wim/Autounattend.xml
unix2dos /wim/Autounattend.xml
# 复制安装脚本
# https://slightlyovercomplicated.com/2016/11/07/windows-pe-startup-sequence-explained/
mv /wim/setup.exe /wim/setup.exe.disabled
download $confhome/windows-setup.bat /wim/Windows/System32/startnet.cmd
# 提交修改 boot.wim # 提交修改 boot.wim
wimunmount --commit /wim/ wimunmount --commit /wim/

41
windows-setup.bat Normal file
View File

@ -0,0 +1,41 @@
@echo off
:: 安装 SCSI 驱动
for %%F in ("X:\drivers\*.inf") do (
find "Class=SCSIAdapter" "%%~F" >nul
if not errorlevel 1 (
drvload "%%~F"
)
)
:: 等待加载分区
:: 没有 timeout 命令
:: 没有加载网卡驱动,无法用 ping 来等待
echo wscript.sleep(5000) > sleep.vbs
cscript //nologo sleep.vbs
:: 获取主硬盘 id
for /F "tokens=2 delims==" %%A in ('wmic logicaldisk where "VolumeName='installer'" assoc:value /resultclass:Win32_DiskPartition ^| find "DiskIndex"') do (
set "DiskIndex=%%A"
)
:: 设置 Autounattend.xml 的主硬盘 id
set "file=X:\Autounattend.xml"
set "tempFile=X:\tmp.xml"
set "search=%%disk_id%%"
set "replace=%DiskIndex%"
(for /f "delims=" %%i in (%file%) do (
set "line=%%i"
setlocal EnableDelayedExpansion
echo !line:%search%=%replace%!
endlocal
)) > %tempFile%
move /y %tempFile% %file%
:: 执行 setup.exe
ren X:\setup.exe.disabled setup.exe
X:\setup.exe