支持 BIOS 机器重装到 Windows

This commit is contained in:
bin456789 2023-05-28 00:52:48 +08:00
parent 2eb137ec0b
commit 33264f8cad
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B
3 changed files with 129 additions and 65 deletions

View File

@ -15,20 +15,6 @@
<DiskID>0</DiskID>
<WillWipeDisk>false</WillWipeDisk>
<ModifyPartitions>
<ModifyPartition wcm:action="add">
<Order>1</Order>
<PartitionID>1</PartitionID>
<Format>FAT32</Format>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>2</Order>
<PartitionID>2</PartitionID>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>3</Order>
<PartitionID>3</PartitionID>
<Format>NTFS</Format>
</ModifyPartition>
</ModifyPartitions>
</Disk>
</DiskConfiguration>
@ -46,7 +32,7 @@
<WillShowUI>OnError</WillShowUI>
<InstallTo>
<DiskID>0</DiskID>
<PartitionID>3</PartitionID>
<PartitionID>%installto_partitionid%</PartitionID>
</InstallTo>
</OSImage>
</ImageInstall>
@ -113,11 +99,11 @@
</RunSynchronousCommand>
<RunSynchronousCommand wcm:action="add">
<Order>2</Order>
<Path>xcopy D:\resize.bat C:\Windows\Temp\</Path>
<Path>C:\resize.bat</Path>
</RunSynchronousCommand>
<RunSynchronousCommand wcm:action="add">
<Order>3</Order>
<Path>C:\Windows\Temp\resize.bat</Path>
<Path>cmd /c "del C:\resize.bat"</Path>
</RunSynchronousCommand>
</RunSynchronous>
</component>

View File

@ -19,14 +19,14 @@ curl -O https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.sh
bash reinstall.sh centos-7 (或其他系统)
安装 Windows:
bash reinstall.sh windows --iso=https://archive.org/download/xxx/zh-cn_windows_10_enterprise_ltsc_2021_x64_dvd_033b7312.iso --image-name='Windows 10 Enterprise LTSC 2021' (或其他系统)
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'
重启:
reboot
```
#### 支持重装到:
```
centos-7/8/9 # centos 8/9 为 stream 版本
centos-7/8/9 (centos 8/9 为 stream 版本)
alma-8/9
rocky-8/9
fedora-36/37/38
@ -37,13 +37,16 @@ windows (见下方注意事项)
```
#### Windows 注意事项:
```
只支持 UEFI 引导的机器,已测试成功的系统有 7 10 11其他系统不保证成功
支持 x64 BIOS, x64 UEFI测试成功的系统有 7 10 11 2022
经测试不支持甲骨文云的 ARM
安装 Windows 需要以下参数
--iso iso 链接,不需要提前添加 virtio 驱动
--image-name 系统全名,两边要有引号,例如:
--image-name 系统全名,不区分大小写,两边要有引号,例如:
'Windows 7 Ultimate'
'Windows 10 Enterprise LTSC 2021'
'Windows 11 Pro'
'Windows Server 2022 SERVERDATACENTER'
```
#### 内存要求:
```
@ -61,4 +64,11 @@ windows 1G
```
root 123@@@
administrator 123@@@
````
#### todo:
```
32位 Windows
测试 Xen
添加 Xen Windows 驱动
使用 Cloud Images
````

156
trans.sh
View File

@ -136,14 +136,14 @@ partx -d /dev/$xda
disk_size=$(blockdev --getsize64 /dev/$xda)
disk_2t=$((2 * 1024 * 1024 * 1024 * 1024))
# 对于红帽系是临时分区表,安装时除了 installer 分区,其他分区会重建为默认的大小
# 对于ubuntu是最终分区表因为 ubuntu 的安装器不能调整个别分区,只能重建整个分区表
# {xda}*1 星号用于 nvme0n1p1 的字母 p
# xda*1 星号用于 nvme0n1p1 的字母 p
if [ "$distro" = windows ]; then
add_community_repo
apk add ntfs-3g ntfs-3g-progs fuse virt-what wimlib rsync dos2unix
modprobe fuse
if [ -d /sys/firmware/efi ]; then
add_community_repo
apk add dosfstools ntfs-3g ntfs-3g-progs fuse virt-what wimlib rsync efibootmgr
modprobe fuse
# efi
apk add dosfstools
parted /dev/$xda -s -- \
mklabel gpt \
mkpart '" "' fat32 1MiB 1025MiB \
@ -154,16 +154,24 @@ if [ "$distro" = windows ]; then
set 2 msftres on \
set 3 msftdata on
update_part /dev/$xda
mkfs.fat -F 32 -n efi /dev/${xda}*1 #1 efi
echo #2 msr
mkfs.ext4 -F -L os /dev/${xda}*3 #3 os
mkfs.ntfs -f -F -L installer /dev/${xda}*4 #4 installer
mkfs.fat -F 32 -n efi /dev/$xda*1 #1 efi
echo #2 msr
mkfs.ext4 -F -L os /dev/$xda*3 #3 os
mkfs.ntfs -f -F -L installer /dev/$xda*4 #4 installer
else
echo "This script not support install windows on none-efi system"
sleep 1m
exec reboot
# bios
parted /dev/$xda -s -- \
mklabel msdos \
mkpart primary ntfs 1MiB -6GiB \
mkpart primary ntfs -6GiB 100% \
set 1 boot on
update_part /dev/$xda
mkfs.ext4 -F -L os /dev/$xda*1 #1 os
mkfs.ntfs -f -F -L installer /dev/$xda*2 #2 installer
fi
else
# 对于红帽系是临时分区表,安装时除了 installer 分区,其他分区会重建为默认的大小
# 对于ubuntu是最终分区表因为 ubuntu 的安装器不能调整个别分区,只能重建整个分区表
if [ -d /sys/firmware/efi ]; then
# efi
apk add dosfstools
@ -174,9 +182,9 @@ else
mkpart '" "' ext4 -2GiB 100% \
set 1 boot on
update_part /dev/$xda
mkfs.fat -F 32 -n efi /dev/${xda}*1 #1 efi
mkfs.ext4 -F -L os /dev/${xda}*2 #2 os
mkfs.ext4 -F -L installer /dev/${xda}*3 #3 installer
mkfs.fat -F 32 -n efi /dev/$xda*1 #1 efi
mkfs.ext4 -F -L os /dev/$xda*2 #2 os
mkfs.ext4 -F -L installer /dev/$xda*3 #3 installer
elif [ "$disk_size" -ge "$disk_2t" ]; then
# bios 2t
parted /dev/$xda -s -- \
@ -186,9 +194,9 @@ else
mkpart '" "' ext4 -2GiB 100% \
set 1 bios_grub on
update_part /dev/$xda
echo #1 bios_boot
mkfs.ext4 -F -L os /dev/${xda}*2 #2 os
mkfs.ext4 -F -L installer /dev/${xda}*3 #3 installer
echo #1 bios_boot
mkfs.ext4 -F -L os /dev/$xda*2 #2 os
mkfs.ext4 -F -L installer /dev/$xda*3 #3 installer
else
# bios
parted /dev/$xda -s -- \
@ -197,8 +205,8 @@ else
mkpart primary ext4 -2GiB 100% \
set 1 boot on
update_part /dev/$xda
mkfs.ext4 -F -L os /dev/${xda}*1 #1 os
mkfs.ext4 -F -L installer /dev/${xda}*2 #2 installer
mkfs.ext4 -F -L os /dev/$xda*1 #1 os
mkfs.ext4 -F -L installer /dev/$xda*2 #2 installer
fi
fi
@ -210,7 +218,9 @@ mount /dev/disk/by-label/os /os
# 挂载其他分区
mkdir -p /os/boot/efi
mount /dev/disk/by-label/efi /os/boot/efi
if [ -d /sys/firmware/efi/ ]; then
mount /dev/disk/by-label/efi /os/boot/efi
fi
mkdir -p /os/installer
mount /dev/disk/by-label/installer /os/installer
@ -220,9 +230,8 @@ case "$basearch" in
"aarch64") basearch_alt=arm64 ;;
esac
# 目前只支持 efi
# shellcheck disable=SC2154
if [ "$distro" = "windows" ] && [ -d /sys/firmware/efi/ ]; then
if [ "$distro" = "windows" ]; then
download $iso /os/windows.iso
mkdir /iso
mount /os/windows.iso /iso
@ -246,8 +255,7 @@ if [ "$distro" = "windows" ] && [ -d /sys/firmware/efi/ ]; then
if [ $sys = w7 ]; then
# https://github.com/virtio-win/virtio-win-pkg-scripts/issues/40
# https://tcler.github.io/2022/01/24/virtio-win-for-windows-7/
dir=archive-virtio/virtio-win-0.1.189-1
dir=archive-virtio/virtio-win-0.1.173-9
else
dir=stable-virtio
fi
@ -256,25 +264,60 @@ if [ "$distro" = "windows" ] && [ -d /sys/firmware/efi/ ]; then
mount /os/virtio-win.iso /virtio
fi
# 复制启动文件到efi分区
mkdir -p /os/boot/efi/sources/
/bin/cp -rv /iso/boot* /iso/efi/ /os/boot/efi/
/bin/cp -rv /iso/sources/boot.wim /os/boot/efi/sources/
# 复制全部文件到installer分区除了 boot.wim
rsync -rv --exclude=/sources/boot.wim /iso/* /os/installer/
# 合并分区脚本
download $confhome/resize.bat /os/installer/resize.bat
# efi: 复制boot开头的文件+efi目录到efi分区复制iso全部文件(除了boot.wim)到installer分区
# bios: 复制iso全部文件到installer分区
if [ -d /sys/firmware/efi/ ]; then
mkdir -p /os/boot/efi/sources/
/bin/cp -rv /iso/boot* /iso/efi/ /os/boot/efi/
/bin/cp -rv /iso/sources/boot.wim /os/boot/efi/sources/
rsync -rv --exclude=/sources/boot.wim /iso/* /os/installer/
boot_wim=/os/boot/efi/sources/boot.wim
else
rsync -rv /iso/* /os/installer/
boot_wim=/os/installer/sources/boot.wim
fi
install_wim=/os/installer/sources/install.wim
# 修改应答文件
download $confhome/Autounattend.xml /tmp/Autounattend.xml
locale=$(wiminfo /os/boot/efi/sources/boot.wim | grep 'Default Language' | head -1 | awk '{print $NF}')
sed -i "s|%arch%|$basearch_alt|; s|%image_name%|$image_name|; s|%locale%|$locale|; s|%confhome%|$confhome|" /tmp/Autounattend.xml
locale=$(wiminfo $install_wim | grep 'Default Language' | head -1 | awk '{print $NF}')
sed -i "s|%arch%|$basearch_alt|; s|%image_name%|$image_name|; s|%locale%|$locale|" /tmp/Autounattend.xml
# 修改应答文件,分区配置
line_num=$(grep -E -n '<ModifyPartitions>' /tmp/Autounattend.xml | cut -d: -f1)
if [ -d /sys/firmware/efi/ ]; then
sed -i "s|%installto_partitionid%|3|" /tmp/Autounattend.xml
cat <<EOF | sed -i "${line_num}r /dev/stdin" /tmp/Autounattend.xml
<ModifyPartition wcm:action="add">
<Order>1</Order>
<PartitionID>1</PartitionID>
<Format>FAT32</Format>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>2</Order>
<PartitionID>2</PartitionID>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>3</Order>
<PartitionID>3</PartitionID>
<Format>NTFS</Format>
</ModifyPartition>
EOF
else
sed -i "s|%installto_partitionid%|1|" /tmp/Autounattend.xml
cat <<EOF | sed -i "${line_num}r /dev/stdin" /tmp/Autounattend.xml
<ModifyPartition wcm:action="add">
<Order>1</Order>
<PartitionID>1</PartitionID>
<Format>NTFS</Format>
</ModifyPartition>
EOF
fi
unix2dos /tmp/Autounattend.xml
# 挂载 boot.wim
mkdir /wim
wimmountrw /os/boot/efi/sources/boot.wim 2 /wim/
wimmountrw $boot_wim 2 /wim/
# virtio 驱动
if [ -d /virtio ]; then
@ -283,21 +326,46 @@ if [ "$distro" = "windows" ] && [ -d /sys/firmware/efi/ ]; then
-ipath "*/$sys/$basearch_alt/*" \
-not -iname '*.pdb' \
-not -iname '*.doc' \
-exec /bin/cp -r {} /wim/virtio/ \;
-exec /bin/cp -rf {} /wim/virtio/ \;
fi
# win7 要添加 bootx64.efi 到 efi 目录
[ $basearch = x86_64 ] && boot_efi=bootx64.efi || boot_efi=bootaa64.efi
if [ ! -e /os/boot/efi/efi/boot/$boot_efi ]; then
if [ -d /sys/firmware/efi/ ] && [ ! -e /os/boot/efi/efi/boot/$boot_efi ]; then
mkdir -p /os/boot/efi/efi/boot/
cp /wim/Windows/Boot/EFI/bootmgfw.efi /os/boot/efi/efi/boot/$boot_efi
fi
# 应答文件
# 复制应答文件
cp /tmp/Autounattend.xml /wim/
# 提交修改 boot.wim
wimunmount --commit /wim/
efibootmgr -c -L "Windows Installer" -d /dev/$xda -p1 -l "\\EFI\\boot\\$boot_efi"
# windows 7 没有 invoke-webrequest
# installer分区盘符不一定是D盘
# 所以复制 resize.bat 到 install.wim
image_name=$(wiminfo $install_wim | grep -ix "Name:[[:blank:]]*$image_name" | cut -d: -f2 | xargs)
wimmountrw $install_wim "$image_name" /wim/
download $confhome/resize.bat /wim/resize.bat
wimunmount --commit /wim/
# 添加引导
if [ -d /sys/firmware/efi/ ]; then
apk add efibootmgr
efibootmgr -c -L "Windows Installer" -d /dev/$xda -p1 -l "\\EFI\\boot\\$boot_efi"
else
# 或者用 ms-sys
apk add grub-bios
grub-install --boot-directory=/os/boot /dev/$xda
cat <<EOF >/os/boot/grub/grub.cfg
set timeout=5
menuentry "reinstall" {
search --no-floppy --label --set=root installer
ntldr /bootmgr
}
EOF
fi
exec reboot
fi
@ -341,7 +409,7 @@ if [ "$distro" = "ubuntu" ]; then
set timeout=5
menuentry "reinstall" {
# https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1851311
rmmod tpm
# rmmod tpm
search --no-floppy --label --set=root installer
loopback loop $iso_file
linux (loop)/casper/vmlinuz iso-scan/filename=$iso_file autoinstall noprompt noeject cloud-config-url=$ks $extra_cmdline ---