Tools/Linux_reinstall/Windows/windowsInit.sh

106 lines
4.1 KiB
Bash
Raw Normal View History

2023-07-03 20:31:46 +08:00
#!/bin/ash
#
# Alpine Linux use "ash" as the default shell.
exec >/dev/tty0 2>&1
# Delete the initial script itself to prevent to be executed in the new system.
rm -f /etc/local.d/windowsConf.start
rm -f /etc/runlevels/default/local
# Install necessary components.
apk update
2023-09-30 03:58:33 +08:00
apk add coreutils grep sed
2023-07-03 20:31:46 +08:00
# Get Windows static networking configurations.
confFile="/root/alpine.config"
2023-07-04 16:57:30 +08:00
# Read configs from initial file.
IncDisk=$(grep "IncDisk" $confFile | awk '{print $2}')
LinuxMirror=$(grep -w "LinuxMirror" $confFile | awk '{print $2}')
alpineVer=$(grep "alpineVer" $confFile | awk '{print $2}')
IPv4=$(grep "IPv4" $confFile | awk '{print $2}')
MASK=$(grep "MASK" $confFile | awk '{print $2}')
2023-07-04 23:48:45 +08:00
actualIp4Subnet=$(grep "actualIp4Subnet" $confFile | awk '{print $2}')
2023-07-04 16:57:30 +08:00
GATE=$(grep "GATE" $confFile | awk '{print $2}')
ipDNS1=$(grep "ipDNS1" $confFile | awk '{print $2}')
ipDNS2=$(grep "ipDNS2" $confFile | awk '{print $2}')
ip6Addr=$(grep "ip6Addr" $confFile | awk '{print $2}')
ip6Mask=$(grep "ip6Mask" $confFile | awk '{print $2}')
actualIp6Prefix=$(grep "actualIp6Prefix" $confFile | awk '{print $2}')
ip6Gate=$(grep "ip6Gate" $confFile | awk '{print $2}')
ip6DNS1=$(grep "ip6DNS1" $confFile | awk '{print $2}')
ip6DNS2=$(grep "ip6DNS2" $confFile | awk '{print $2}')
DDURL=$(grep "DDURL" $confFile | awk '{print $2}')
windowsStaticConfigCmd=$(grep "windowsStaticConfigCmd" $confFile | awk '{print $2}')
Network4Config=$(grep "Network4Config" $confFile | awk '{print $2}')
2023-10-13 16:10:54 +08:00
Network6Config=$(grep "Network6Config" $confFile | awk '{print $2}')
2023-07-04 16:57:30 +08:00
DEC_CMD=$(grep "DEC_CMD" $confFile | awk '{print $2}')
2023-10-01 00:56:31 +08:00
# Reset configurations of repositories.
2023-07-04 16:57:30 +08:00
true >/etc/apk/repositories
setup-apkrepos $LinuxMirror/$alpineVer/main
setup-apkcache /var/cache/apk
2023-10-01 00:56:31 +08:00
# Add community mirror.
2023-07-04 16:57:30 +08:00
sed -i '$a\'$LinuxMirror'/'$alpineVer'/community' /etc/apk/repositories
# Add edge testing to the repositories
2023-09-19 12:52:14 +08:00
# sed -i '$a\'$LinuxMirror'/edge/testing' /etc/apk/repositories
2023-07-04 16:57:30 +08:00
2023-10-01 00:56:31 +08:00
# Synchronize time from hardware.
2023-07-04 16:57:30 +08:00
hwclock -s
# Install necessary components.
apk update
2023-10-01 19:21:15 +08:00
apk add fuse gzip hdparm multipath-tools musl ntfs-3g util-linux wget xz
2023-07-04 16:57:30 +08:00
2023-10-01 00:56:31 +08:00
# Start dd.
2023-10-01 22:48:59 +08:00
wget --no-check-certificate --report-speed=bits --tries=0 --timeout=10 --wait=5 -O- "$DDURL" | $DEC_CMD | dd of="$IncDisk" status=progress
2023-07-04 16:57:30 +08:00
2023-10-01 00:56:31 +08:00
# Get valid loop device.
2023-07-04 16:57:30 +08:00
loopDevice=$(echo $(losetup -f))
loopDeviceNum=$(echo $(losetup -f) | cut -d'/' -f 3)
2023-10-01 00:56:31 +08:00
# Make a soft link between valid loop device and disk.
2023-07-04 16:57:30 +08:00
losetup $loopDevice $IncDisk
2023-10-01 00:56:31 +08:00
# Get mapper partition.
2023-10-07 07:23:08 +08:00
mapperDevice=$(kpartx -av $loopDevice | grep "$loopDeviceNum" | sort -rn | sed -n '1p' | awk '{print $3}')
2023-07-04 16:57:30 +08:00
2023-10-01 00:56:31 +08:00
# Mount Windows dd partition to /mnt .
2023-07-04 17:02:32 +08:00
ntfs-3g /dev/mapper/$mapperDevice /mnt
2023-07-04 19:03:52 +08:00
2023-10-01 00:56:31 +08:00
# Download initiate file.
2023-07-05 04:06:25 +08:00
setupCompleteFile='/mnt/Users/Administrator/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/SetupComplete.bat'
wget --no-check-certificate -qO "$setupCompleteFile" ''$windowsStaticConfigCmd''
2023-10-13 16:10:54 +08:00
# Write IPv4 static config script to setup step.
if [[ "$Network4Config" == "isStatic" && -n "$IPv4" && -n "$actualIp4Subnet" && -n "$GATE" ]]; then
2023-10-12 16:21:10 +08:00
sed -ri "s/IPv4/$IPv4/g" "$setupCompleteFile"
sed -ri "s/actualIp4Subnet/$actualIp4Subnet/g" "$setupCompleteFile"
sed -ri "s/GATE/$GATE/g" "$setupCompleteFile"
sed -ri "s/ipDNS1/$ipDNS1/g" "$setupCompleteFile"
sed -ri "s/ipDNS2/$ipDNS2/g" "$setupCompleteFile"
2023-07-05 04:06:25 +08:00
else
2023-10-13 16:10:54 +08:00
sed -ri "s/setipv4mode=on/setipv4mode=off/g" "$setupCompleteFile"
fi
# Write IPv6 static config script to setup step.
if [[ "$Network6Config" == "isStatic" && -n "$ip6Addr" && -n "$actualIp6Prefix" && -n "$ip6Gate" ]]; then
sed -ri "s/ip6Addr/$ip6Addr/g" "$setupCompleteFile"
sed -ri "s/actualIp6Prefix/$actualIp6Prefix/g" "$setupCompleteFile"
sed -ri "s/ip6Gate/$ip6Gate/g" "$setupCompleteFile"
sed -ri "s/ip6DNS1/$ip6DNS1/g" "$setupCompleteFile"
sed -ri "s/ip6DNS2/$ip6DNS2/g" "$setupCompleteFile"
else
sed -ri "s/setipv6mode=on/setipv6mode=off/g" "$setupCompleteFile"
2023-07-05 04:06:25 +08:00
fi
2023-07-04 19:03:52 +08:00
2023-10-01 00:56:31 +08:00
# Umount mounted directory and loop device.
umount /mnt
kpartx -dv $loopDevice
losetup -d $loopDevice
2023-10-03 13:24:49 +08:00
# Reboot, the temporary intermediary system of AlpineLinux which is executing in memory will be destroyed during the power down and then server will reboot to the newly installed system immediately.
2023-07-05 04:06:25 +08:00
exec reboot