windows-docker/src/install.sh

386 lines
9.9 KiB
Bash
Raw Normal View History

2024-01-14 16:39:24 +01:00
#!/usr/bin/env bash
2024-01-14 15:19:58 +01:00
set -Eeuo pipefail
2024-01-19 01:25:39 +01:00
: "${MANUAL:=""}"
2024-01-14 20:32:24 +01:00
: "${VERSION:="win11x64"}"
2024-01-17 03:40:19 +01: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 01:25:39 +01: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 [ -z "$MANUAL" ]; then
MANUAL="N"
[[ "${BASE,,}" == "tiny10"* ]] && MANUAL="Y"
2024-01-19 01:25:39 +01:00
fi
CUSTOM="custom.iso"
[ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="Custom.iso"
[ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="custom.ISO"
[ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="CUSTOM.ISO"
[ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="custom.img"
[ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="Custom.img"
[ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="custom.IMG"
[ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="CUSTOM.IMG"
2024-01-20 17:03:59 +01:00
MSG="Windows is being started, please wait..."
2024-01-17 03:40:19 +01:00
if [ -f "$STORAGE/$CUSTOM" ]; then
EXTERNAL="Y"
BASE="$CUSTOM"
else
2024-01-17 03:40:19 +01:00
CUSTOM=""
2024-01-17 03:40:19 +01:00
if [[ "${VERSION,,}" == "http"* ]]; then
EXTERNAL="Y"
else
EXTERNAL="N"
2024-01-22 02:56:28 +01:00
fi
2024-01-17 03:40:19 +01:00
if [[ "$EXTERNAL" != [Yy1]* ]]; then
BASE="$VERSION.iso"
if [ ! -f "$STORAGE/$BASE" ]; then
MSG="Windows is being downloaded, please wait..."
fi
2024-01-19 04:28:03 +01:00
else
BASE=$(basename "${VERSION%%\?*}")
: "${BASE//+/ }"; printf -v BASE '%b' "${_//%/\\x}"
BASE=$(echo "$BASE" | sed -e 's/[^A-Za-z0-9._-]/_/g')
if [ ! -f "$STORAGE/$BASE" ]; then
MSG="Image '$BASE' is being downloaded, please wait..."
fi
2024-01-17 03:40:19 +01:00
fi
2024-01-22 02:56:28 +01:00
[[ "${BASE,,}" == "custom."* ]] && BASE="windows.iso"
2024-01-17 03:40:19 +01:00
fi
2024-01-22 02:56:28 +01:00
2024-01-20 17:03:59 +01:00
html "$MSG"
2024-01-22 02:56:28 +01:00
TMP="$STORAGE/tmp"
2024-01-16 19:58:55 +01:00
2024-01-22 02:56:28 +01:00
if [ -f "$STORAGE/$BASE" ]; then
2024-01-14 15:19:58 +01:00
# Check if the ISO was already processed by our script
MAGIC=$(dd if="$STORAGE/$BASE" seek=0 bs=1 count=1 status=none | tr -d '\000')
MAGIC="$(printf '%s' "$MAGIC" | od -A n -t x1 -v | tr -d ' \n')"
2024-01-14 15:19:58 +01:00
if [[ "$MAGIC" == "16" ]]; then
2024-01-14 15:19:58 +01:00
if [[ "$MANUAL" = [Yy1]* ]]; then
rm -rf "$TMP"
return 0
fi
2024-01-14 15:19:58 +01:00
FOUND="N"
if [ -f "$STORAGE/data.img" ] || [ -f "$STORAGE/data.qcow2" ]; then
FOUND="Y"
else
[ -b "${DEVICE:-}" ] && FOUND="Y"
fi
if [[ "$FOUND" == "Y" ]]; then
rm -rf "$TMP"
return 0
fi
fi
EXTERNAL="Y"
CUSTOM="$BASE"
fi
mkdir -p "$TMP"
2024-01-17 03:40:19 +01:00
2024-01-22 02:56:28 +01:00
if [ ! -f "$STORAGE/$CUSTOM" ]; then
CUSTOM=""
ISO="$TMP/$BASE"
2024-01-17 03:40:19 +01:00
else
2024-01-22 02:56:28 +01:00
ISO="$STORAGE/$CUSTOM"
fi
2024-01-17 03:40:19 +01:00
rm -f "$TMP/$BASE"
2024-01-22 02:56:28 +01:00
if [ ! -f "$ISO" ]; then
2024-01-22 02:56:28 +01:00
if [[ "$EXTERNAL" != [Yy1]* ]]; then
cd "$TMP"
/run/mido.sh "$VERSION"
cd /run
2024-01-15 15:15:12 +01:00
2024-01-17 03:40:19 +01:00
else
2024-01-22 02:56:28 +01:00
info "Downloading $BASE as boot image..."
2024-01-17 03:40:19 +01:00
2024-01-22 02:56:28 +01: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
2024-01-14 15:19:58 +01:00
2024-01-22 02:56:28 +01:00
{ wget "$VERSION" -O "$ISO" -q --no-check-certificate --show-progress "$PROGRESS"; rc=$?; } || :
2024-01-15 15:15:12 +01:00
(( rc != 0 )) && error "Failed to download $VERSION, reason: $rc" && exit 60
2024-01-22 02:56:28 +01:00
fi
[ ! -f "$ISO" ] && error "Failed to download $VERSION" && exit 61
2024-01-22 02:56:28 +01:00
fi
2024-01-17 03:40:19 +01:00
SIZE=$(stat -c%s "$ISO")
2024-01-22 02:56:28 +01:00
SIZE_GB=$(( (SIZE + 1073741823)/1073741824 ))
2024-01-17 03:40:19 +01:00
if ((SIZE<10000000)); then
error "Invalid ISO file: Size is smaller than 10 MB" && exit 62
2024-01-17 03:40:19 +01:00
fi
2024-01-22 02:56:28 +01:00
SPACE=$(df --output=avail -B 1 "$TMP" | tail -n 1)
SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
if (( SIZE > SPACE )); then
error "Not enough free space in $STORAGE, have $SPACE_GB GB available but need at least $SIZE_GB GB." && exit 63
fi
if [ -n "$CUSTOM" ]; then
MSG="Extracting local ISO image..."
2024-01-22 02:56:28 +01:00
else
MSG="Extracting downloaded ISO image..."
fi
info "$MSG" && html "$MSG"
2024-01-14 15:19:58 +01:00
2024-01-15 15:15:12 +01:00
DIR="$TMP/unpack"
2024-01-17 03:40:19 +01:00
rm -rf "$DIR"
2024-01-19 01:25:39 +01:00
7z x "$ISO" -o"$DIR" > /dev/null
FB="falling back to manual installation!"
2024-01-19 15:18:56 +01:00
ETFS="boot/etfsboot.com"
EFISYS="efi/microsoft/boot/efisys_noprompt.bin"
if [ ! -f "$DIR/$ETFS" ] || [ ! -f "$DIR/$EFISYS" ]; then
2024-01-19 15:18:56 +01:00
if [ ! -f "$DIR/$ETFS" ]; then
warn "failed to locate file 'etfsboot.com' in ISO image, $FB"
else
warn "failed to locate file 'efisys_noprompt.bin' in ISO image, $FB"
fi
2024-01-19 01:25:39 +01:00
# Mark ISO as prepared via magic byte
2024-01-22 20:46:51 +01:00
printf '\x16' | dd of="$ISO" bs=1 seek=0 count=1 conv=notrunc status=none
2024-01-22 02:56:28 +01:00
[[ "$ISO" != "$STORAGE/$BASE" ]] && mv -f "$ISO" "$STORAGE/$BASE"
2024-01-19 01:25:39 +01:00
rm -f "$STORAGE/windows.ver"
cp /run/version "$STORAGE/windows.ver"
2024-01-19 01:25:39 +01:00
rm -rf "$TMP"
return 0
2024-01-19 01:25:39 +01:00
fi
2024-01-15 23:45:35 +01:00
[ -z "$CUSTOM" ] && rm -f "$ISO"
2024-01-19 15:18:56 +01:00
XML=""
2024-01-17 03:40:19 +01:00
if [[ "$MANUAL" != [Yy1]* ]]; then
2024-01-15 23:45:35 +01:00
if [[ "$EXTERNAL" != [Yy1]* ]]; then
[ -z "$CUSTOM" ] && XML="$VERSION.xml"
fi
2024-01-15 23:45:35 +01:00
2024-01-22 02:56:28 +01:00
if [ ! -f "/run/assets/$XML" ]; then
2024-01-19 01:25:39 +01:00
2024-01-20 17:03:59 +01:00
MSG="Detecting Windows version from ISO image..."
info "$MSG" && html "$MSG"
2024-01-19 01:25:39 +01:00
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")
[[ "${NAME,,}" == *"windows 11"* ]] && DETECTED="win11x64"
[[ "${NAME,,}" == *"windows 10"* ]] && DETECTED="win10x64"
[[ "${NAME,,}" == *"windows 8"* ]] && DETECTED="win81x64"
2024-01-19 01:25:39 +01:00
[[ "${NAME,,}" == *"server 2022"* ]] && DETECTED="win2022-eval"
[[ "${NAME,,}" == *"server 2019"* ]] && DETECTED="win2019-eval"
[[ "${NAME,,}" == *"server 2016"* ]] && DETECTED="win2016-eval"
if [ -z "$DETECTED" ]; then
TAG="PRODUCTNAME"
NAME2=$(sed -n "/$TAG/{s/.*<$TAG>\(.*\)<\/$TAG>.*/\1/;p}" <<< "$RESULT")
[ -z "$NAME" ] && NAME="$NAME2"
[[ "${NAME2,,}" == *"windows 11"* ]] && DETECTED="win11x64"
[[ "${NAME2,,}" == *"windows 10"* ]] && DETECTED="win10x64"
[[ "${NAME2,,}" == *"windows 8"* ]] && DETECTED="win81x64"
[[ "${NAME2,,}" == *"server 2022"* ]] && DETECTED="win2022-eval"
[[ "${NAME2,,}" == *"server 2019"* ]] && DETECTED="win2019-eval"
[[ "${NAME2,,}" == *"server 2016"* ]] && DETECTED="win2016-eval"
fi
2024-01-19 01:25:39 +01:00
if [ -n "$DETECTED" ]; then
XML="$DETECTED.xml"
2024-01-22 02:56:28 +01:00
if [ -f "/run/assets/$XML" ]; then
echo "Detected image of type '$DETECTED', which supports automatic installation."
2024-01-22 02:56:28 +01:00
else
XML=""
warn "detected image of type '$DETECTED', but no matching XML file exists, $FB."
2024-01-22 02:56:28 +01:00
fi
2024-01-19 01:25:39 +01:00
else
if [ -z "$NAME" ]; then
2024-01-19 15:18:56 +01:00
warn "failed to detect Windows version from image, $FB"
2024-01-19 01:25:39 +01:00
else
if [[ "${NAME,,}" == "windows 7" ]]; then
2024-01-19 15:18:56 +01:00
warn "detected Windows 7 image, $FB"
2024-01-19 01:25:39 +01:00
else
2024-01-19 15:18:56 +01:00
warn "failed to detect Windows version from string '$NAME', $FB"
2024-01-19 01:25:39 +01:00
fi
fi
fi
else
2024-01-19 15:18:56 +01:00
warn "failed to locate 'install.wim' or 'install.esd' in ISO image, $FB"
2024-01-17 03:40:19 +01:00
fi
2024-01-15 23:45:35 +01:00
fi
fi
2024-01-19 01:25:39 +01:00
ASSET="/run/assets/$XML"
2024-01-17 03:40:19 +01:00
2024-01-19 01:25:39 +01:00
if [ -f "$ASSET" ]; then
2024-01-17 03:40:19 +01:00
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"
2024-01-19 01:25:39 +01:00
LOC="$DIR/sources/boot.wim"
[ ! -f "$LOC" ] && LOC="$DIR/sources/boot.esd"
2024-01-17 03:40:19 +01:00
2024-01-19 01:25:39 +01:00
if [ -f "$LOC" ]; then
2024-01-20 17:03:59 +01:00
MSG="Adding XML file for automatic installation..."
info "$MSG" && html "$MSG"
2024-01-19 01:25:39 +01:00
RESULT=$(wimlib-imagex info -xml "$LOC" | tr -d '\000')
2024-01-14 15:19:58 +01:00
2024-01-19 01:25:39 +01: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
ASSET=""
warn "failed to locate 'boot.wim' or 'boot.esd' in ISO image, $FB"
2024-01-19 01:25:39 +01:00
fi
2024-01-19 01:25:39 +01:00
fi
2024-01-19 15:18:56 +01:00
CAT="BOOT.CAT"
LABEL="${BASE%.*}"
LABEL="${LABEL::30}"
2024-01-22 02:56:28 +01:00
OUT="$TMP/$LABEL.tmp"
rm -f "$OUT"
SPACE=$(df --output=avail -B 1 "$TMP" | tail -n 1)
SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
if (( SIZE > SPACE )); then
error "Not enough free space in $STORAGE, have $SPACE_GB GB available but need at least $SIZE_GB GB." && exit 63
fi
2024-01-19 04:28:03 +01:00
2024-01-20 17:03:59 +01:00
MSG="Generating new ISO image for installation..."
info "$MSG" && html "$MSG"
2024-01-19 01:25:39 +01:00
2024-01-19 15:18:56 +01:00
genisoimage -b "$ETFS" -no-emul-boot -c "$CAT" -iso-level 4 -J -l -D -N -joliet-long -relaxed-filenames -quiet -V "$LABEL" -udf \
2024-01-22 02:56:28 +01:00
-boot-info-table -eltorito-alt-boot -eltorito-boot "$EFISYS" -no-emul-boot -o "$OUT" -allow-limited-size "$DIR"
# Mark ISO as prepared via magic byte
2024-01-22 20:46:51 +01:00
printf '\x16' | dd of="$OUT" bs=1 seek=0 count=1 conv=notrunc status=none
2024-01-22 02:56:28 +01:00
[ -n "$CUSTOM" ] && rm -f "$STORAGE/$CUSTOM"
2024-01-19 01:25:39 +01:00
if [ -f "$STORAGE/$BASE" ]; then
error "File $STORAGE/$BASE does already exist ?!" && exit 64
fi
2024-01-22 02:56:28 +01:00
mv "$OUT" "$STORAGE/$BASE"
rm -f "$STORAGE/windows.ver"
cp /run/version "$STORAGE/windows.ver"
2024-01-15 15:15:12 +01:00
rm -rf "$TMP"
2024-01-14 15:19:58 +01:00
2024-01-20 17:03:59 +01:00
html "Successfully prepared image for installation..."
2024-01-14 15:19:58 +01:00
return 0