windows/src/install.sh

275 lines
7.0 KiB
Bash
Raw Normal View History

2024-01-14 15:39:24 +00:00
#!/usr/bin/env bash
2024-01-14 14:19:58 +00:00
set -Eeuo pipefail
2024-01-19 00:25:39 +00:00
: "${MANUAL:=""}"
: "${EXTERNAL:=""}"
2024-01-14 19:32:24 +00:00
: "${VERSION:="win11x64"}"
2024-01-17 02:40:19 +00:00
[[ "${VERSION,,}" == "11" ]] && VERSION="win11x64"
[[ "${VERSION,,}" == "win11" ]] && VERSION="win11x64"
[[ "${VERSION,,}" == "10" ]] && VERSION="win10x64"
[[ "${VERSION,,}" == "win10" ]] && VERSION="win10x64"
[[ "${VERSION,,}" == "8" ]] && VERSION="win81x64"
[[ "${VERSION,,}" == "81" ]] && VERSION="win81x64"
[[ "${VERSION,,}" == "8.1" ]] && VERSION="win81x64"
[[ "${VERSION,,}" == "win81" ]] && VERSION="win81x64"
[[ "${VERSION,,}" == "win8" ]] && VERSION="win81x64"
[[ "${VERSION,,}" == "22" ]] && VERSION="win2022-eval"
[[ "${VERSION,,}" == "2022" ]] && VERSION="win2022-eval"
[[ "${VERSION,,}" == "win22" ]] && VERSION="win2022-eval"
[[ "${VERSION,,}" == "win2022" ]] && VERSION="win2022-eval"
[[ "${VERSION,,}" == "19" ]] && VERSION="win2019-eval"
[[ "${VERSION,,}" == "2019" ]] && VERSION="win2019-eval"
[[ "${VERSION,,}" == "win19" ]] && VERSION="win2019-eval"
[[ "${VERSION,,}" == "win2019" ]] && VERSION="win2019-eval"
[[ "${VERSION,,}" == "16" ]] && VERSION="win2016-eval"
[[ "${VERSION,,}" == "2016" ]] && VERSION="win2016-eval"
[[ "${VERSION,,}" == "win16" ]] && VERSION="win2016-eval"
[[ "${VERSION,,}" == "win2016" ]] && VERSION="win2016-eval"
2024-01-19 00:25:39 +00:00
if [[ "${VERSION,,}" == "tiny10" ]]; then
VERSION="https://archive.org/download/tiny-10-23-h2/tiny10%20x64%2023h2.iso"
fi
if [[ "${VERSION,,}" == "tiny11" ]]; then
VERSION="https://archive.org/download/tiny-11-core-x-64-beta-1/tiny11%20core%20x64%20beta%201.iso"
fi
if [[ "${VERSION,,}" == "http"* ]]; then
EXTERNAL="Y"
else
EXTERNAL="N"
fi
2024-01-17 02:40:19 +00:00
MSG="Please wait while Windows is being started..."
2024-01-19 03:28:03 +00:00
BASE="custom.iso"
if [ ! -f "$STORAGE/$BASE" ]; then
2024-01-17 02:40:19 +00:00
if [[ "$EXTERNAL" != [Yy1]* ]]; then
2024-01-19 03:28:03 +00:00
BASE="$VERSION.iso"
if [ ! -f "$STORAGE/$BASE" ]; then
2024-01-17 02:40:19 +00:00
MSG="Please wait while Windows is being downloaded..."
fi
else
2024-01-19 03:28:03 +00:00
BASE=$(basename "${VERSION%%\?*}")
: "${BASE//+/ }"; printf -v BASE '%b' "${_//%/\\x}"
BASE=$(echo "$BASE" | sed -e 's/[^A-Za-z0-9._-]/_/g')
2024-01-17 02:40:19 +00:00
if [ ! -f "$STORAGE/$BASE" ]; then
MSG="Please wait while '$BASE' is being downloaded..."
fi
fi
fi
2024-01-16 18:58:55 +00:00
# Display wait message
/run/server.sh "Windows" "$MSG" &
2024-01-14 15:35:58 +00:00
[ -f "$STORAGE/$BASE" ] && return 0
2024-01-14 14:19:58 +00:00
2024-01-15 14:15:12 +00:00
TMP="$STORAGE/tmp"
2024-01-19 00:25:39 +00:00
rm -rf "$TMP"
mkdir -p "$TMP"
2024-01-14 14:19:58 +00:00
2024-01-17 02:40:19 +00:00
ISO="$TMP/$BASE"
rm -f "$ISO"
2024-01-14 14:19:58 +00:00
2024-01-17 02:40:19 +00:00
if [[ "$EXTERNAL" != [Yy1]* ]]; then
2024-01-14 14:19:58 +00:00
2024-01-15 14:15:12 +00:00
SCRIPT="$TMP/mido.sh"
2024-01-14 14:19:58 +00:00
2024-01-17 02:40:19 +00:00
rm -f "$SCRIPT"
2024-01-15 14:15:12 +00:00
cp /run/mido.sh "$SCRIPT"
chmod +x "$SCRIPT"
cd "$TMP"
bash "$SCRIPT" "$VERSION"
rm -f "$SCRIPT"
2024-01-17 02:40:19 +00:00
cd /run
else
info "Downloading $BASE as boot image..."
2024-01-15 14:15:12 +00:00
2024-01-17 02:40:19 +00:00
# Check if running with interactive TTY or redirected to docker log
if [ -t 1 ]; then
PROGRESS="--progress=bar:noscroll"
else
PROGRESS="--progress=dot:giga"
fi
{ wget "$VERSION" -O "$ISO" -q --no-check-certificate --show-progress "$PROGRESS"; rc=$?; } || :
2024-01-19 00:25:39 +00:00
(( rc != 0 )) && echo && error "Failed to download $VERSION, reason: $rc" && exit 60
2024-01-14 14:19:58 +00:00
2024-01-15 14:15:12 +00:00
fi
2024-01-19 00:25:39 +00:00
[ ! -f "$ISO" ] && echo && error "Failed to download $VERSION" && exit 61
2024-01-17 02:40:19 +00:00
SIZE=$(stat -c%s "$ISO")
if ((SIZE<10000000)); then
2024-01-19 00:25:39 +00:00
echo && error "Invalid ISO file: Size is smaller than 10 MB" && exit 62
2024-01-17 02:40:19 +00:00
fi
2024-01-19 00:25:39 +00:00
echo && info "Extracting downloaded ISO image..."
2024-01-14 14:19:58 +00:00
2024-01-15 14:15:12 +00:00
DIR="$TMP/unpack"
2024-01-17 02:40:19 +00:00
rm -rf "$DIR"
2024-01-19 00:25:39 +00:00
7z x "$ISO" -o"$DIR" > /dev/null
echo
XML=""
FB="falling back to manual installation!"
if [ -z "$MANUAL" ]; then
MANUAL="N"
if [[ "$EXTERNAL" == [Yy1]* ]]; then
[[ "${BASE,,}" == "tiny10"* ]] && MANUAL="Y"
fi
fi
2024-01-15 22:45:35 +00:00
2024-01-17 02:40:19 +00:00
if [[ "$MANUAL" != [Yy1]* ]]; then
if [[ "$EXTERNAL" != [Yy1]* ]]; then
2024-01-15 22:45:35 +00:00
2024-01-19 00:25:39 +00:00
XML="$VERSION.xml"
2024-01-15 22:45:35 +00:00
2024-01-19 00:25:39 +00:00
else
info "Detecting Windows version from ISO image..."
LOC="$DIR/sources/install.wim"
[ ! -f "$LOC" ] && LOC="$DIR/sources/install.esd"
if [ -f "$LOC" ]; then
DETECTED=""
TAG="DISPLAYNAME"
RESULT=$(wimlib-imagex info -xml "$LOC" | tr -d '\000')
NAME=$(sed -n "/$TAG/{s/.*<$TAG>\(.*\)<\/$TAG>.*/\1/;p}" <<< "$RESULT")
if [ -z "$NAME" ]; then
TAG="PRODUCTNAME"
NAME=$(sed -n "/$TAG/{s/.*<$TAG>\(.*\)<\/$TAG>.*/\1/;p}" <<< "$RESULT")
fi
[[ "${NAME,,}" == "windows 11"* ]] && DETECTED="win11x64"
[[ "${NAME,,}" == "windows 10"* ]] && DETECTED="win10x64"
[[ "${NAME,,}" == "windows 8"* ]] && DETECTED="win81x64"
[[ "${NAME,,}" == *"server 2022"* ]] && DETECTED="win2022-eval"
[[ "${NAME,,}" == *"server 2019"* ]] && DETECTED="win2019-eval"
[[ "${NAME,,}" == *"server 2016"* ]] && DETECTED="win2016-eval"
if [ -n "$DETECTED" ]; then
XML="$DETECTED.xml"
echo "Detected image of type '$DETECTED', will apply autounattend.xml file."
else
if [ -z "$NAME" ]; then
error "Warning: failed to detect Windows version from image, $FB"
else
if [[ "${NAME,,}" == "windows 7" ]]; then
error "Warning: detected Windows 7 image, $FB"
else
error "Warning: failed to detect Windows version from string '$NAME', $FB"
fi
fi
fi
else
error "Warning: failed to locate 'install.wim' or 'install.esd' in ISO image, $FB"
2024-01-17 02:40:19 +00:00
fi
2024-01-19 00:25:39 +00:00
echo
2024-01-15 22:45:35 +00:00
fi
fi
2024-01-19 00:25:39 +00:00
ASSET="/run/assets/$XML"
2024-01-17 02:40:19 +00:00
2024-01-19 00:25:39 +00:00
if [ -f "$ASSET" ]; then
2024-01-17 02:40:19 +00:00
2024-01-19 00:25:39 +00:00
LOC="$DIR/sources/boot.wim"
[ ! -f "$LOC" ] && LOC="$DIR/sources/boot.esd"
2024-01-17 02:40:19 +00:00
2024-01-19 00:25:39 +00:00
if [ -f "$LOC" ]; then
info "Adding XML file for automatic installation..."
RESULT=$(wimlib-imagex info -xml "$LOC" | tr -d '\000')
2024-01-14 14:19:58 +00:00
2024-01-19 00:25:39 +00:00
if [[ "${RESULT^^}" == *"<IMAGE INDEX=\"2\">"* ]]; then
INDEX="2"
else
INDEX="1"
fi
wimlib-imagex update "$LOC" "$INDEX" --command "add $ASSET /autounattend.xml" > /dev/null
else
error "Warning: failed to locate 'boot.wim' or 'boot.esd' in ISO image, $FB"
fi
LOC="$DIR/autounattend.xml"
[ -f "$LOC" ] && mv -f "$ASSET" "$LOC"
LOC="$DIR/Autounattend.xml"
[ -f "$LOC" ] && mv -f "$ASSET" "$LOC"
LOC="$DIR/AutoUnattend.xml"
[ -f "$LOC" ] && mv -f "$ASSET" "$LOC"
LOC="$DIR/autounattend.XML"
[ -f "$LOC" ] && mv -f "$ASSET" "$LOC"
LOC="$DIR/Autounattend.XML"
[ -f "$LOC" ] && mv -f "$ASSET" "$LOC"
LOC="$DIR/AutoUnattend.XML"
[ -f "$LOC" ] && mv -f "$ASSET" "$LOC"
LOC="$DIR/AUTOUNATTEND.xml"
[ -f "$LOC" ] && mv -f "$ASSET" "$LOC"
LOC="$DIR/AUTOUNATTEND.XML"
[ -f "$LOC" ] && mv -f "$ASSET" "$LOC"
echo
else
[ -n "$XML" ] && error "Warning: XML file '$XML' does not exist, $FB" && echo
fi
ETFS="boot/etfsboot.com"
EFISYS="efi/microsoft/boot/efisys_noprompt.bin"
if [ -f "$DIR/$ETFS" ]; then
if [ -f "$DIR/$EFISYS" ]; then
CAT="BOOT.CAT"
LABEL="${BASE%.*}"
LABEL="${LABEL::32}"
ISO="$TMP/$LABEL.tmp"
rm -f "$ISO"
2024-01-19 03:28:03 +00:00
info "Generating new ISO image for installation..."
2024-01-19 00:25:39 +00:00
genisoimage -b "$ETFS" -no-emul-boot -c "$CAT" -iso-level 4 -J -l -D -N -joliet-long -relaxed-filenames -quiet -V "$LABEL" -udf \
-boot-info-table -eltorito-alt-boot -eltorito-boot "$EFISYS" -no-emul-boot -o "$ISO" -allow-limited-size "$DIR"
else
error "Failed to locate file 'efisys_noprompt.bin' in ISO image, $FB"
fi
else
error "Failed to locate file 'etfsboot.com' in ISO image, $FB"
fi
mv "$ISO" "$STORAGE/$BASE"
2024-01-15 14:15:12 +00:00
rm -rf "$TMP"
2024-01-14 14:19:58 +00:00
2024-01-19 00:25:39 +00:00
echo
2024-01-14 14:19:58 +00:00
return 0