Update install.sh

This commit is contained in:
Kroese 2024-01-24 04:23:56 +01:00 committed by GitHub
parent 3bf41d9172
commit d6091e7e9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -45,6 +45,12 @@ CUSTOM="custom.iso"
[ ! -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"
replaceXML() {
local dir="$1"
@ -90,8 +96,6 @@ skipInstall() {
return 1
}
TMP="$STORAGE/tmp"
finishInstall() {
local iso="$1"
@ -107,14 +111,16 @@ finishInstall() {
return 0
}
MSG="Windows is being started, please wait..."
startInstall() {
if [ -f "$STORAGE/$CUSTOM" ]; then
local msg="Windows is being started, please wait..."
if [ -f "$STORAGE/$CUSTOM" ]; then
EXTERNAL="Y"
BASE="$CUSTOM"
else
else
CUSTOM=""
@ -129,7 +135,7 @@ else
BASE="$VERSION.iso"
if ! skipInstall && [ ! -f "$STORAGE/$BASE" ]; then
MSG="Windows is being downloaded, please wait..."
msg="Windows is being downloaded, please wait..."
fi
else
@ -139,30 +145,29 @@ else
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..."
msg="Image '$BASE' is being downloaded, please wait..."
fi
fi
[[ "${BASE,,}" == "custom."* ]] && BASE="windows.iso"
fi
fi
html "$MSG"
html "$msg"
[ -z "$MANUAL" ] && MANUAL="N"
[ -z "$MANUAL" ] && MANUAL="N"
if [ -f "$STORAGE/$BASE" ]; then
if [ -f "$STORAGE/$BASE" ]; then
# 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')"
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')"
if [[ "$MAGIC" == "16" ]]; then
if [[ "$magic" == "16" ]]; then
if hasDisk || [[ "$MANUAL" = [Yy1]* ]]; then
rm -rf "$TMP"
return 0
return 1
fi
fi
@ -170,103 +175,103 @@ if [ -f "$STORAGE/$BASE" ]; then
EXTERNAL="Y"
CUSTOM="$BASE"
else
else
if skipInstall; then
BASE=""
rm -rf "$TMP"
return 0
return 1
fi
fi
fi
mkdir -p "$TMP"
mkdir -p "$TMP"
if [ ! -f "$STORAGE/$CUSTOM" ]; then
if [ ! -f "$STORAGE/$CUSTOM" ]; then
CUSTOM=""
ISO="$TMP/$BASE"
else
else
ISO="$STORAGE/$CUSTOM"
fi
fi
rm -f "$TMP/$BASE"
rm -f "$TMP/$BASE"
return 0
}
if [ ! -f "$ISO" ]; then
downloadImage() {
local url="$1"
local iso="$2"
local progress
rm -f "$iso"
if [[ "$EXTERNAL" != [Yy1]* ]]; then
cd "$TMP"
/run/mido.sh "$VERSION"
/run/mido.sh "$url"
cd /run
else
[ ! -f "$iso" ] && error "Failed to download $url" && exit 61
return 0
fi
info "Downloading $BASE as boot image..."
# Check if running with interactive TTY or redirected to docker log
if [ -t 1 ]; then
PROGRESS="--progress=bar:noscroll"
progress="--progress=bar:noscroll"
else
PROGRESS="--progress=dot:giga"
progress="--progress=dot:giga"
fi
{ wget "$VERSION" -O "$ISO" -q --no-check-certificate --show-progress "$PROGRESS"; rc=$?; } || :
{ wget "$url" -O "$iso" -q --no-check-certificate --show-progress "$progress"; rc=$?; } || :
(( rc != 0 )) && error "Failed to download $VERSION, reason: $rc" && exit 60
(( rc != 0 )) && error "Failed to download $url, reason: $rc" && exit 60
[ ! -f "$iso" ] && error "Failed to download $url" && exit 61
fi
return 0
}
[ ! -f "$ISO" ] && error "Failed to download $VERSION" && exit 61
extractImage() {
fi
local dir="$1"
local iso="$2"
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 ))
SIZE=$(stat -c%s "$ISO")
SIZE_GB=$(( (SIZE + 1073741823)/1073741824 ))
if ((SIZE<10000000)); then
if ((size<10000000)); then
error "Invalid ISO file: Size is smaller than 10 MB" && exit 62
fi
fi
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 (( 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
local msg="Extracting downloaded ISO image..."
[ -n "$CUSTOM" ] && msg="Extracting local ISO image..."
if [ -n "$CUSTOM" ]; then
MSG="Extracting local ISO image..."
else
MSG="Extracting downloaded ISO image..."
fi
info "$msg" && html "$msg"
info "$MSG" && html "$MSG"
rm -rf "$dir"
7z x "$iso" -o"$dir" > /dev/null
DIR="$TMP/unpack"
rm -rf "$DIR"
if [ ! -f "$dir/$ETFS" ] || [ ! -f "$dir/$EFISYS" ]; then
7z x "$ISO" -o"$DIR" > /dev/null
FB="falling back to manual installation!"
ETFS="boot/etfsboot.com"
EFISYS="efi/microsoft/boot/efisys_noprompt.bin"
if [ ! -f "$DIR/$ETFS" ] || [ ! -f "$DIR/$EFISYS" ]; then
if [ ! -f "$DIR/$ETFS" ]; 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
[[ "$ISO" != "$STORAGE/$BASE" ]] && mv -f "$ISO" "$STORAGE/$BASE"
return 1
fi
[ -z "$CUSTOM" ] && rm -f "$iso"
finishInstall "$STORAGE/$BASE"
return 0
fi
[ -z "$CUSTOM" ] && rm -f "$ISO"
}
findVersion() {
@ -284,126 +289,161 @@ findVersion() {
return 0
}
XML=""
getXML() {
if [[ "$MANUAL" != [Yy1]* ]]; then
local dir="$1"
if [[ "$EXTERNAL" != [Yy1]* ]]; then
[ -z "$CUSTOM" ] && XML="$VERSION.xml"
fi
if [ ! -f "/run/assets/$XML" ]; then
MSG="Detecting Windows version from ISO image..."
info "$MSG" && html "$MSG"
LOC="$DIR/sources/install.wim"
[ ! -f "$LOC" ] && LOC="$DIR/sources/install.esd"
if [ -f "$LOC" ]; then
TAG="DISPLAYNAME"
RESULT=$(wimlib-imagex info -xml "$LOC" | tr -d '\000')
NAME=$(sed -n "/$TAG/{s/.*<$TAG>\(.*\)<\/$TAG>.*/\1/;p}" <<< "$RESULT")
DETECTED=$(findVersion "$NAME")
if [ -z "$DETECTED" ]; then
TAG="PRODUCTNAME"
NAME2=$(sed -n "/$TAG/{s/.*<$TAG>\(.*\)<\/$TAG>.*/\1/;p}" <<< "$RESULT")
[ -z "$NAME" ] && NAME="$NAME2"
DETECTED=$(findVersion "$NAME2")
fi
if [ -n "$DETECTED" ]; then
XML="$DETECTED.xml"
if [ -f "/run/assets/$XML" ]; then
echo "Detected image of type '$DETECTED', which supports automatic installation."
else
XML=""
warn "detected image of type '$DETECTED', but no matching XML file exists, $FB."
[[ "$MANUAL" == [Yy1]* ]] && return 0
if [[ "$EXTERNAL" != [Yy1]* ]] && [ -z "$CUSTOM" ]; then
XML="$VERSION.xml"
[ -f "/run/assets/$XML" ] && return 0
fi
info "Detecting Windows version from ISO image..."
local loc="$dir/sources/install.wim"
[ ! -f "$loc" ] && loc="$dir/sources/install.esd"
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
if [ -n "$detected" ]; then
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
if [ -z "$NAME" ]; then
if [ -z "$name" ]; then
warn "failed to detect Windows version from image, $FB"
else
if [[ "${NAME,,}" == "windows 7" ]]; then
if [[ "${name,,}" == "windows 7" ]]; then
warn "detected Windows 7 image, $FB"
else
warn "failed to detect Windows version from string '$NAME', $FB"
warn "failed to detect Windows version from string '$name', $FB"
fi
fi
fi
else
warn "failed to locate 'install.wim' or 'install.esd' in ISO image, $FB"
fi
fi
fi
ASSET="/run/assets/$XML"
if [ -f "$ASSET" ]; then
replaceXML "$DIR" "$ASSET"
LOC="$DIR/sources/boot.wim"
[ ! -f "$LOC" ] && LOC="$DIR/sources/boot.esd"
if [ -f "$LOC" ]; then
MSG="Adding XML file for automatic installation..."
info "$MSG" && html "$MSG"
RESULT=$(wimlib-imagex info -xml "$LOC" | tr -d '\000')
if [[ "${RESULT^^}" == *"<IMAGE INDEX=\"2\">"* ]]; then
INDEX="2"
else
INDEX="1"
fi
wimlib-imagex update "$LOC" "$INDEX" --command "add $ASSET /autounattend.xml" > /dev/null
return 0
}
else
updateImage() {
ASSET=""
local dir="$1"
local asset="$2"
[ ! -f "$asset" ] && return 0
replaceXML "$dir" "$asset"
local loc="$dir/sources/boot.wim"
[ ! -f "$loc" ] && loc="$dir/sources/boot.esd"
if [ ! -f "$loc" ]; then
warn "failed to locate 'boot.wim' or 'boot.esd' in ISO image, $FB"
return 0
fi
fi
CAT="BOOT.CAT"
LABEL="${BASE%.*}"
LABEL="${LABEL::30}"
OUT="$TMP/$LABEL.tmp"
rm -f "$OUT"
info "Adding XML file for automatic installation..."
SPACE=$(df --output=avail -B 1 "$TMP" | tail -n 1)
SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
local index="1"
local result=$(wimlib-imagex info -xml "$loc" | tr -d '\000')
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 [[ "${result^^}" == *"<IMAGE INDEX=\"2\">"* ]]; then
index="2"
fi
MSG="Generating the ISO image..."
info "$MSG" && html "$MSG"
wimlib-imagex update "$loc" "$index" --command "add $asset /autounattend.xml" > /dev/null
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"
return 0
}
[ -n "$CUSTOM" ] && rm -f "$STORAGE/$CUSTOM"
buildImage() {
if [ -f "$STORAGE/$BASE" ]; then
local dir="$1"
local iso="$2"
local cat="BOOT.CAT"
local label="${BASE%.*}"
label="${label::30}"
local out="$TMP/$label.tmp"
rm -f "$out"
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 > space )); then
error "Not enough free space in $STORAGE, have $space_gb GB available but need at least $size_gb GB." && exit 63
fi
local msg="Generating updated ISO image..."
info "$msg" && html "$msg"
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
}
#######################
# #
#######################
if ! startInstall; then
rm -rf "$TMP"
return 0
fi
mv "$OUT" "$STORAGE/$BASE"
if [ ! -f "$ISO" ]; then
downloadImage "$VERSION" "$ISO"
fi
if ! extractImage "$DIR" "$ISO"; then
if [[ "$ISO" != "$STORAGE/$BASE" ]]; then
mv -f "$ISO" "$STORAGE/$BASE"
fi
finishInstall "$STORAGE/$BASE"
return 0
fi
getXML "$DIR"
updateImage "$DIR" "/run/assets/$XML"
buildImage "$DIR" "$ISO"
finishInstall "$STORAGE/$BASE"
html "Successfully prepared image for installation..."
return 0