支持 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> <DiskID>0</DiskID>
<WillWipeDisk>false</WillWipeDisk> <WillWipeDisk>false</WillWipeDisk>
<ModifyPartitions> <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> </ModifyPartitions>
</Disk> </Disk>
</DiskConfiguration> </DiskConfiguration>
@ -46,7 +32,7 @@
<WillShowUI>OnError</WillShowUI> <WillShowUI>OnError</WillShowUI>
<InstallTo> <InstallTo>
<DiskID>0</DiskID> <DiskID>0</DiskID>
<PartitionID>3</PartitionID> <PartitionID>%installto_partitionid%</PartitionID>
</InstallTo> </InstallTo>
</OSImage> </OSImage>
</ImageInstall> </ImageInstall>
@ -113,11 +99,11 @@
</RunSynchronousCommand> </RunSynchronousCommand>
<RunSynchronousCommand wcm:action="add"> <RunSynchronousCommand wcm:action="add">
<Order>2</Order> <Order>2</Order>
<Path>xcopy D:\resize.bat C:\Windows\Temp\</Path> <Path>C:\resize.bat</Path>
</RunSynchronousCommand> </RunSynchronousCommand>
<RunSynchronousCommand wcm:action="add"> <RunSynchronousCommand wcm:action="add">
<Order>3</Order> <Order>3</Order>
<Path>C:\Windows\Temp\resize.bat</Path> <Path>cmd /c "del C:\resize.bat"</Path>
</RunSynchronousCommand> </RunSynchronousCommand>
</RunSynchronous> </RunSynchronous>
</component> </component>

View File

@ -19,14 +19,14 @@ curl -O https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.sh
bash reinstall.sh centos-7 (或其他系统) bash reinstall.sh centos-7 (或其他系统)
安装 Windows: 安装 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 reboot
``` ```
#### 支持重装到: #### 支持重装到:
``` ```
centos-7/8/9 # centos 8/9 为 stream 版本 centos-7/8/9 (centos 8/9 为 stream 版本)
alma-8/9 alma-8/9
rocky-8/9 rocky-8/9
fedora-36/37/38 fedora-36/37/38
@ -37,13 +37,16 @@ windows (见下方注意事项)
``` ```
#### Windows 注意事项: #### Windows 注意事项:
``` ```
只支持 UEFI 引导的机器,已测试成功的系统有 7 10 11其他系统不保证成功 支持 x64 BIOS, x64 UEFI测试成功的系统有 7 10 11 2022
经测试不支持甲骨文云的 ARM
安装 Windows 需要以下参数 安装 Windows 需要以下参数
--iso iso 链接,不需要提前添加 virtio 驱动 --iso iso 链接,不需要提前添加 virtio 驱动
--image-name 系统全名,两边要有引号,例如: --image-name 系统全名,不区分大小写,两边要有引号,例如:
'Windows 7 Ultimate' 'Windows 7 Ultimate'
'Windows 10 Enterprise LTSC 2021' 'Windows 10 Enterprise LTSC 2021'
'Windows 11 Pro' 'Windows 11 Pro'
'Windows Server 2022 SERVERDATACENTER'
``` ```
#### 内存要求: #### 内存要求:
``` ```
@ -61,4 +64,11 @@ windows 1G
``` ```
root 123@@@ root 123@@@
administrator 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_size=$(blockdev --getsize64 /dev/$xda)
disk_2t=$((2 * 1024 * 1024 * 1024 * 1024)) disk_2t=$((2 * 1024 * 1024 * 1024 * 1024))
# 对于红帽系是临时分区表,安装时除了 installer 分区,其他分区会重建为默认的大小 # xda*1 星号用于 nvme0n1p1 的字母 p
# 对于ubuntu是最终分区表因为 ubuntu 的安装器不能调整个别分区,只能重建整个分区表
# {xda}*1 星号用于 nvme0n1p1 的字母 p
if [ "$distro" = windows ]; then 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 if [ -d /sys/firmware/efi ]; then
add_community_repo # efi
apk add dosfstools ntfs-3g ntfs-3g-progs fuse virt-what wimlib rsync efibootmgr apk add dosfstools
modprobe fuse
parted /dev/$xda -s -- \ parted /dev/$xda -s -- \
mklabel gpt \ mklabel gpt \
mkpart '" "' fat32 1MiB 1025MiB \ mkpart '" "' fat32 1MiB 1025MiB \
@ -154,16 +154,24 @@ if [ "$distro" = windows ]; then
set 2 msftres on \ set 2 msftres on \
set 3 msftdata on set 3 msftdata on
update_part /dev/$xda update_part /dev/$xda
mkfs.fat -F 32 -n efi /dev/${xda}*1 #1 efi mkfs.fat -F 32 -n efi /dev/$xda*1 #1 efi
echo #2 msr echo #2 msr
mkfs.ext4 -F -L os /dev/${xda}*3 #3 os mkfs.ext4 -F -L os /dev/$xda*3 #3 os
mkfs.ntfs -f -F -L installer /dev/${xda}*4 #4 installer mkfs.ntfs -f -F -L installer /dev/$xda*4 #4 installer
else else
echo "This script not support install windows on none-efi system" # bios
sleep 1m parted /dev/$xda -s -- \
exec reboot 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 fi
else else
# 对于红帽系是临时分区表,安装时除了 installer 分区,其他分区会重建为默认的大小
# 对于ubuntu是最终分区表因为 ubuntu 的安装器不能调整个别分区,只能重建整个分区表
if [ -d /sys/firmware/efi ]; then if [ -d /sys/firmware/efi ]; then
# efi # efi
apk add dosfstools apk add dosfstools
@ -174,9 +182,9 @@ else
mkpart '" "' ext4 -2GiB 100% \ mkpart '" "' ext4 -2GiB 100% \
set 1 boot on set 1 boot on
update_part /dev/$xda update_part /dev/$xda
mkfs.fat -F 32 -n efi /dev/${xda}*1 #1 efi 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 os /dev/$xda*2 #2 os
mkfs.ext4 -F -L installer /dev/${xda}*3 #3 installer mkfs.ext4 -F -L installer /dev/$xda*3 #3 installer
elif [ "$disk_size" -ge "$disk_2t" ]; then elif [ "$disk_size" -ge "$disk_2t" ]; then
# bios 2t # bios 2t
parted /dev/$xda -s -- \ parted /dev/$xda -s -- \
@ -186,9 +194,9 @@ else
mkpart '" "' ext4 -2GiB 100% \ mkpart '" "' ext4 -2GiB 100% \
set 1 bios_grub on set 1 bios_grub on
update_part /dev/$xda update_part /dev/$xda
echo #1 bios_boot echo #1 bios_boot
mkfs.ext4 -F -L os /dev/${xda}*2 #2 os mkfs.ext4 -F -L os /dev/$xda*2 #2 os
mkfs.ext4 -F -L installer /dev/${xda}*3 #3 installer mkfs.ext4 -F -L installer /dev/$xda*3 #3 installer
else else
# bios # bios
parted /dev/$xda -s -- \ parted /dev/$xda -s -- \
@ -197,8 +205,8 @@ else
mkpart primary ext4 -2GiB 100% \ mkpart primary ext4 -2GiB 100% \
set 1 boot on set 1 boot on
update_part /dev/$xda update_part /dev/$xda
mkfs.ext4 -F -L os /dev/${xda}*1 #1 os mkfs.ext4 -F -L os /dev/$xda*1 #1 os
mkfs.ext4 -F -L installer /dev/${xda}*2 #2 installer mkfs.ext4 -F -L installer /dev/$xda*2 #2 installer
fi fi
fi fi
@ -210,7 +218,9 @@ mount /dev/disk/by-label/os /os
# 挂载其他分区 # 挂载其他分区
mkdir -p /os/boot/efi 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 mkdir -p /os/installer
mount /dev/disk/by-label/installer /os/installer mount /dev/disk/by-label/installer /os/installer
@ -220,9 +230,8 @@ case "$basearch" in
"aarch64") basearch_alt=arm64 ;; "aarch64") basearch_alt=arm64 ;;
esac esac
# 目前只支持 efi
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [ "$distro" = "windows" ] && [ -d /sys/firmware/efi/ ]; then if [ "$distro" = "windows" ]; then
download $iso /os/windows.iso download $iso /os/windows.iso
mkdir /iso mkdir /iso
mount /os/windows.iso /iso mount /os/windows.iso /iso
@ -246,8 +255,7 @@ if [ "$distro" = "windows" ] && [ -d /sys/firmware/efi/ ]; then
if [ $sys = w7 ]; then if [ $sys = w7 ]; then
# https://github.com/virtio-win/virtio-win-pkg-scripts/issues/40 # 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.173-9
dir=archive-virtio/virtio-win-0.1.189-1
else else
dir=stable-virtio dir=stable-virtio
fi fi
@ -256,25 +264,60 @@ if [ "$distro" = "windows" ] && [ -d /sys/firmware/efi/ ]; then
mount /os/virtio-win.iso /virtio mount /os/virtio-win.iso /virtio
fi fi
# 复制启动文件到efi分区 # efi: 复制boot开头的文件+efi目录到efi分区复制iso全部文件(除了boot.wim)到installer分区
mkdir -p /os/boot/efi/sources/ # bios: 复制iso全部文件到installer分区
/bin/cp -rv /iso/boot* /iso/efi/ /os/boot/efi/ if [ -d /sys/firmware/efi/ ]; then
/bin/cp -rv /iso/sources/boot.wim /os/boot/efi/sources/ mkdir -p /os/boot/efi/sources/
/bin/cp -rv /iso/boot* /iso/efi/ /os/boot/efi/
# 复制全部文件到installer分区除了 boot.wim /bin/cp -rv /iso/sources/boot.wim /os/boot/efi/sources/
rsync -rv --exclude=/sources/boot.wim /iso/* /os/installer/ rsync -rv --exclude=/sources/boot.wim /iso/* /os/installer/
boot_wim=/os/boot/efi/sources/boot.wim
# 合并分区脚本 else
download $confhome/resize.bat /os/installer/resize.bat 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 download $confhome/Autounattend.xml /tmp/Autounattend.xml
locale=$(wiminfo /os/boot/efi/sources/boot.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%|$basearch_alt|; s|%image_name%|$image_name|; s|%locale%|$locale|; s|%confhome%|$confhome|" /tmp/Autounattend.xml 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 mkdir /wim
wimmountrw /os/boot/efi/sources/boot.wim 2 /wim/ wimmountrw $boot_wim 2 /wim/
# virtio 驱动 # virtio 驱动
if [ -d /virtio ]; then if [ -d /virtio ]; then
@ -283,21 +326,46 @@ if [ "$distro" = "windows" ] && [ -d /sys/firmware/efi/ ]; then
-ipath "*/$sys/$basearch_alt/*" \ -ipath "*/$sys/$basearch_alt/*" \
-not -iname '*.pdb' \ -not -iname '*.pdb' \
-not -iname '*.doc' \ -not -iname '*.doc' \
-exec /bin/cp -r {} /wim/virtio/ \; -exec /bin/cp -rf {} /wim/virtio/ \;
fi fi
# win7 要添加 bootx64.efi 到 efi 目录 # win7 要添加 bootx64.efi 到 efi 目录
[ $basearch = x86_64 ] && boot_efi=bootx64.efi || boot_efi=bootaa64.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/ mkdir -p /os/boot/efi/efi/boot/
cp /wim/Windows/Boot/EFI/bootmgfw.efi /os/boot/efi/efi/boot/$boot_efi cp /wim/Windows/Boot/EFI/bootmgfw.efi /os/boot/efi/efi/boot/$boot_efi
fi fi
# 应答文件 # 复制应答文件
cp /tmp/Autounattend.xml /wim/ cp /tmp/Autounattend.xml /wim/
# 提交修改 boot.wim
wimunmount --commit /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 exec reboot
fi fi
@ -341,7 +409,7 @@ if [ "$distro" = "ubuntu" ]; then
set timeout=5 set timeout=5
menuentry "reinstall" { menuentry "reinstall" {
# https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1851311 # https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1851311
rmmod tpm # rmmod tpm
search --no-floppy --label --set=root installer search --no-floppy --label --set=root installer
loopback loop $iso_file loopback loop $iso_file
linux (loop)/casper/vmlinuz iso-scan/filename=$iso_file autoinstall noprompt noeject cloud-config-url=$ks $extra_cmdline --- linux (loop)/casper/vmlinuz iso-scan/filename=$iso_file autoinstall noprompt noeject cloud-config-url=$ks $extra_cmdline ---