mirror of
https://github.com/bin456789/reinstall.git
synced 2025-12-10 15:37:43 +08:00
支持 BIOS 机器重装到 Windows
This commit is contained in:
156
trans.sh
156
trans.sh
@ -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 ---
|
||||
|
||||
Reference in New Issue
Block a user