windows-docker/src/install.sh

450 lines
10 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"}"
if [[ "${VERSION}" == \"*\" || "${VERSION}" == \'*\' ]]; then
VERSION="${VERSION:1:-1}"
fi
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"
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"
TMP="$STORAGE/tmp"
DIR="$TMP/unpack"
FB="falling back to manual installation!"
ETFS="boot/etfsboot.com"
EFISYS="efi/microsoft/boot/efisys_noprompt.bin"
2024-01-17 03:40:19 +01:00
replaceXML() {
local dir="$1"
local asset="$2"
local path="$dir/autounattend.xml"
[ -f "$path" ] && mv -f "$asset" "$path"
path="$dir/Autounattend.xml"
[ -f "$path" ] && mv -f "$asset" "$path"
path="$dir/AutoUnattend.xml"
[ -f "$path" ] && mv -f "$asset" "$path"
path="$dir/autounattend.XML"
[ -f "$path" ] && mv -f "$asset" "$path"
path="$dir/Autounattend.XML"
[ -f "$path" ] && mv -f "$asset" "$path"
path="$dir/AutoUnattend.XML"
[ -f "$path" ] && mv -f "$asset" "$path"
path="$dir/AUTOUNATTEND.xml"
[ -f "$path" ] && mv -f "$asset" "$path"
path="$dir/AUTOUNATTEND.XML"
[ -f "$path" ] && mv -f "$asset" "$path"
return 0
}
hasDisk() {
2024-01-17 03:40:19 +01:00
[ -b "${DEVICE:-}" ] && return 0
2024-01-17 03:40:19 +01:00
if [ -f "$STORAGE/data.img" ] || [ -f "$STORAGE/data.qcow2" ]; then
return 0
2024-01-22 02:56:28 +01:00
fi
2024-01-17 03:40:19 +01:00
return 1
}
skipInstall() {
if hasDisk && [ -f "$STORAGE/windows.boot" ]; then
return 0
fi
2024-01-19 04:28:03 +01:00
return 1
}
finishInstall() {
local iso="$1"
2024-01-17 03:40:19 +01:00
# Mark ISO as prepared via magic byte
printf '\x16' | dd of="$iso" bs=1 seek=0 count=1 conv=notrunc status=none
2024-01-22 02:56:28 +01:00
rm -f "$STORAGE/windows.ver"
rm -f "$STORAGE/windows.boot"
cp /run/version "$STORAGE/windows.ver"
2024-01-17 03:40:19 +01:00
rm -rf "$TMP"
return 0
}
2024-01-22 02:56:28 +01:00
startInstall() {
2024-01-16 19:58:55 +01:00
local msg="Windows is being started, please wait..."
2024-01-22 23:31:35 +01:00
if [ -f "$STORAGE/$CUSTOM" ]; then
2024-01-14 15:19:58 +01:00
EXTERNAL="Y"
BASE="$CUSTOM"
2024-01-14 15:19:58 +01:00
else
2024-01-14 15:19:58 +01:00
CUSTOM=""
2024-01-14 15:19:58 +01:00
if [[ "${VERSION,,}" == "http"* ]]; then
EXTERNAL="Y"
else
EXTERNAL="N"
fi
if [[ "$EXTERNAL" != [Yy1]* ]]; then
BASE="$VERSION.iso"
if ! skipInstall && [ ! -f "$STORAGE/$BASE" ]; then
msg="Windows is being downloaded, please wait..."
fi
else
BASE=$(basename "${VERSION%%\?*}")
: "${BASE//+/ }"; printf -v BASE '%b' "${_//%/\\x}"
BASE=$(echo "$BASE" | sed -e 's/[^A-Za-z0-9._-]/_/g')
if ! skipInstall && [ ! -f "$STORAGE/$BASE" ]; then
msg="Image '$BASE' is being downloaded, please wait..."
fi
fi
[[ "${BASE,,}" == "custom."* ]] && BASE="windows.iso"
fi
html "$msg"
[ -z "$MANUAL" ] && MANUAL="N"
if [ -f "$STORAGE/$BASE" ]; then
2024-01-17 03:40:19 +01:00
# Check if the ISO was already processed by our script
local 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-17 03:40:19 +01:00
if [[ "$magic" == "16" ]]; then
if hasDisk || [[ "$MANUAL" = [Yy1]* ]]; then
return 1
fi
fi
2024-01-22 02:56:28 +01:00
EXTERNAL="Y"
CUSTOM="$BASE"
2024-01-15 15:15:12 +01:00
2024-01-17 03:40:19 +01:00
else
if skipInstall; then
BASE=""
return 1
2024-01-22 02:56:28 +01:00
fi
2024-01-14 15:19:58 +01:00
fi
2024-01-15 15:15:12 +01:00
mkdir -p "$TMP"
2024-01-22 02:56:28 +01:00
if [ ! -f "$STORAGE/$CUSTOM" ]; then
CUSTOM=""
ISO="$TMP/$BASE"
else
ISO="$STORAGE/$CUSTOM"
2024-01-22 02:56:28 +01:00
fi
rm -f "$TMP/$BASE"
return 0
}
downloadImage() {
2024-01-17 03:40:19 +01:00
local iso="$1"
local url="$2"
local progress
rm -f "$iso"
2024-01-17 03:40:19 +01:00
if [[ "$EXTERNAL" != [Yy1]* ]]; then
2024-01-17 03:40:19 +01:00
cd "$TMP"
/run/mido.sh "$url"
cd /run
2024-01-22 02:56:28 +01:00
[ ! -f "$iso" ] && error "Failed to download $url" && exit 61
return 0
2024-01-22 02:56:28 +01:00
fi
2024-01-22 02:56:28 +01:00
info "Downloading $BASE as boot image..."
2024-01-14 15:19:58 +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-17 03:40:19 +01:00
{ wget "$url" -O "$iso" -q --no-check-certificate --show-progress "$progress"; rc=$?; } || :
2024-01-19 01:25:39 +01:00
(( rc != 0 )) && error "Failed to download $url, reason: $rc" && exit 60
[ ! -f "$iso" ] && error "Failed to download $url" && exit 61
2024-01-19 15:18:56 +01:00
return 0
}
extractImage() {
local iso="$1"
local dir="$2"
local msg="Extracting downloaded ISO image..."
[ -n "$CUSTOM" ] && msg="Extracting local ISO image..."
info "$msg" && html "$msg"
local size=$(stat -c%s "$iso")
local size_gb=$(( (size + 1073741823)/1073741824 ))
local space=$(df --output=avail -B 1 "$TMP" | tail -n 1)
local space_gb=$(( (space + 1073741823)/1073741824 ))
if ((size<10000000)); then
error "Invalid ISO file: Size is smaller than 10 MB" && exit 62
2024-01-19 15:18:56 +01:00
fi
2024-01-19 01:25:39 +01:00
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-22 02:56:28 +01:00
rm -rf "$dir"
7z x "$iso" -o"$dir" > /dev/null
2024-01-19 01:25:39 +01:00
if [ ! -f "$dir/$ETFS" ] || [ ! -f "$dir/$EFISYS" ]; then
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
return 1
fi
[ -z "$CUSTOM" ] && rm -f "$iso"
2024-01-19 01:25:39 +01:00
return 0
}
2024-01-15 23:45:35 +01:00
findVersion() {
local name="$1"
local detected=""
2024-01-19 15:18:56 +01:00
[[ "${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"
2024-01-15 23:45:35 +01:00
echo "$detected"
return 0
}
2024-01-15 23:45:35 +01:00
selectXML() {
2024-01-19 01:25:39 +01:00
local dir="$1"
2024-01-19 01:25:39 +01:00
XML=""
[[ "$MANUAL" == [Yy1]* ]] && return 0
2024-01-19 01:25:39 +01:00
if [[ "$EXTERNAL" != [Yy1]* ]] && [ -z "$CUSTOM" ]; then
XML="$VERSION.xml"
[ -f "/run/assets/$XML" ] && return 0
fi
2024-01-19 01:25:39 +01:00
info "Detecting Windows version from ISO image..."
2024-01-19 01:25:39 +01:00
local loc="$dir/sources/install.wim"
[ ! -f "$loc" ] && loc="$dir/sources/install.esd"
2024-01-19 01:25:39 +01:00
if [ ! -f "$loc" ]; then
warn "failed to locate 'install.wim' or 'install.esd' in ISO image, $FB"
return 0
fi
local tag="DISPLAYNAME"
local result=$(wimlib-imagex info -xml "$loc" | tr -d '\000')
local name=$(sed -n "/$tag/{s/.*<$tag>\(.*\)<\/$tag>.*/\1/;p}" <<< "$result")
local detected=$(findVersion "$name")
if [ -z "$detected" ]; then
tag="PRODUCTNAME"
local name2=$(sed -n "/$tag/{s/.*<$tag>\(.*\)<\/$tag>.*/\1/;p}" <<< "$result")
[ -z "$name" ] && name="$name2"
detected=$(findVersion "$name2")
fi
2024-01-19 01:25:39 +01:00
if [ -n "$detected" ]; then
2024-01-22 02:56:28 +01:00
if [ -f "/run/assets/$detected.xml" ]; then
XML="$detected.xml"
echo "Detected image of type '$detected', which supports automatic installation."
else
warn "detected image of type '$detected', but no matching XML file exists, $FB."
fi
else
2024-01-19 01:25:39 +01:00
if [ -z "$name" ]; then
warn "failed to detect Windows version from image, $FB"
else
if [[ "${name,,}" == "windows 7" ]]; then
warn "detected Windows 7 image, $FB"
2024-01-19 01:25:39 +01:00
else
warn "failed to detect Windows version from string '$name', $FB"
2024-01-19 01:25:39 +01:00
fi
2024-01-17 03:40:19 +01:00
fi
2024-01-15 23:45:35 +01:00
fi
return 0
}
2024-01-17 03:40:19 +01:00
updateImage() {
2024-01-17 03:40:19 +01:00
local dir="$1"
local asset="$2"
[ ! -f "$asset" ] && return 0
replaceXML "$dir" "$asset"
2024-01-17 03:40:19 +01:00
local loc="$dir/sources/boot.wim"
[ ! -f "$loc" ] && loc="$dir/sources/boot.esd"
2024-01-19 01:25:39 +01:00
if [ ! -f "$loc" ]; then
warn "failed to locate 'boot.wim' or 'boot.esd' in ISO image, $FB"
return 0
fi
2024-01-19 01:25:39 +01:00
info "Adding XML file for automatic installation..."
2024-01-14 15:19:58 +01:00
local index="1"
local result=$(wimlib-imagex info -xml "$loc" | tr -d '\000')
2024-01-19 01:25:39 +01:00
if [[ "${result^^}" == *"<IMAGE INDEX=\"2\">"* ]]; then
index="2"
fi
2024-01-19 01:25:39 +01:00
wimlib-imagex update "$loc" "$index" --command "add $asset /autounattend.xml" > /dev/null
2024-01-19 01:25:39 +01:00
return 0
}
buildImage() {
local dir="$1"
local cat="BOOT.CAT"
local label="${BASE%.*}"
label="${label::30}"
local out="$TMP/$label.tmp"
rm -f "$out"
local msg="Generating updated ISO image..."
info "$msg" && html "$msg"
2024-01-19 01:25:39 +01:00
local size=$(du -h -b --max-depth=0 "$dir" | cut -f1)
local size_gb=$(( (size + 1073741823)/1073741824 ))
local space=$(df --output=avail -B 1 "$TMP" | tail -n 1)
local 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 01:25:39 +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 \
-boot-info-table -eltorito-alt-boot -eltorito-boot "$EFISYS" -no-emul-boot -o "$out" -allow-limited-size "$dir"
[ -n "$CUSTOM" ] && rm -f "$STORAGE/$CUSTOM"
if [ -f "$STORAGE/$BASE" ]; then
error "File $STORAGE/$BASE does already exist ?!" && exit 64
fi
mv "$out" "$STORAGE/$BASE"
return 0
}
2024-01-22 02:56:28 +01:00
######################################
2024-01-22 02:56:28 +01:00
if ! startInstall; then
rm -rf "$TMP"
return 0
2024-01-22 02:56:28 +01:00
fi
2024-01-19 04:28:03 +01:00
if [ ! -f "$ISO" ]; then
downloadImage "$ISO" "$VERSION"
fi
2024-01-19 01:25:39 +01:00
if ! extractImage "$ISO" "$DIR"; then
2024-01-22 02:56:28 +01:00
if [[ "$ISO" != "$STORAGE/$BASE" ]]; then
mv -f "$ISO" "$STORAGE/$BASE"
fi
finishInstall "$STORAGE/$BASE"
return 0
2024-01-19 01:25:39 +01:00
fi
selectXML "$DIR"
updateImage "$DIR" "/run/assets/$XML"
buildImage "$DIR"
2024-01-14 15:19:58 +01:00
finishInstall "$STORAGE/$BASE"
2024-01-20 17:03:59 +01:00
html "Successfully prepared image for installation..."
2024-01-14 15:19:58 +01:00
return 0