1
0

install.sh 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. #!/usr/bin/env bash
  2. set -Eeuo pipefail
  3. TMP="$STORAGE/tmp"
  4. DIR="$TMP/unpack"
  5. FB="falling back to manual installation!"
  6. ETFS="boot/etfsboot.com"
  7. EFISYS="efi/microsoft/boot/efisys_noprompt.bin"
  8. skipInstall() {
  9. local iso="$1"
  10. local magic byte
  11. local boot="$STORAGE/windows.boot"
  12. local previous="$STORAGE/windows.base"
  13. if [ -f "$previous" ]; then
  14. previous=$(<"$previous")
  15. if [ -n "$previous" ]; then
  16. previous="$STORAGE/$previous"
  17. if [[ "${previous,,}" != "${iso,,}" ]]; then
  18. if [ -f "$boot" ] && hasDisk; then
  19. info "Detected that the version was changed, but ignoring this because Windows is already installed."
  20. info "Please start with an empty /storage folder, if you want to install a different version of Windows."
  21. return 0
  22. fi
  23. [ -f "$previous" ] && rm -f "$previous"
  24. return 1
  25. fi
  26. fi
  27. fi
  28. [ -f "$boot" ] && hasDisk && return 0
  29. [ ! -f "$iso" ] && return 1
  30. [ ! -s "$iso" ] && return 1
  31. # Check if the ISO was already processed by our script
  32. magic=$(dd if="$iso" seek=0 bs=1 count=1 status=none | tr -d '\000')
  33. magic="$(printf '%s' "$magic" | od -A n -t x1 -v | tr -d ' \n')"
  34. byte="16" && [[ "$MANUAL" == [Yy1]* ]] && byte="17"
  35. if [[ "$magic" != "$byte" ]]; then
  36. info "The ISO will be processed again because the configuration was changed..."
  37. return 1
  38. fi
  39. return 0
  40. }
  41. startInstall() {
  42. html "Starting $APP..."
  43. if [ -z "$CUSTOM" ]; then
  44. local file="${VERSION//\//}.iso"
  45. if [[ "${VERSION,,}" == "http"* ]]; then
  46. file=$(basename "${VERSION%%\?*}")
  47. : "${file//+/ }"; printf -v file '%b' "${_//%/\\x}"
  48. file=$(echo "$file" | sed -e 's/[^A-Za-z0-9._-]/_/g')
  49. else
  50. local language
  51. language=$(getLanguage "$LANGUAGE" "culture")
  52. language="${language%%-*}"
  53. if [ -n "$language" ] && [[ "${language,,}" != "en" ]]; then
  54. file="${VERSION//\//}_${language,,}.iso"
  55. fi
  56. fi
  57. BOOT="$STORAGE/$file"
  58. fi
  59. skipInstall "$BOOT" && return 1
  60. rm -rf "$TMP"
  61. mkdir -p "$TMP"
  62. if [ -z "$CUSTOM" ]; then
  63. ISO=$(basename "$BOOT")
  64. ISO="$TMP/$ISO"
  65. if [ -f "$BOOT" ] && [ -s "$BOOT" ]; then
  66. mv -f "$BOOT" "$ISO"
  67. fi
  68. fi
  69. rm -f "$BOOT"
  70. return 0
  71. }
  72. finishInstall() {
  73. local iso="$1"
  74. local aborted="$2"
  75. local base byte
  76. if [ ! -s "$iso" ] || [ ! -f "$iso" ]; then
  77. error "Failed to find ISO file: $iso" && return 1
  78. fi
  79. if [[ "$aborted" != [Yy1]* ]]; then
  80. # Mark ISO as prepared via magic byte
  81. byte="16" && [[ "$MANUAL" == [Yy1]* ]] && byte="17"
  82. if ! printf '%b' "\x$byte" | dd of="$iso" bs=1 seek=0 count=1 conv=notrunc status=none; then
  83. warn "failed to set magic byte in ISO file: $iso"
  84. fi
  85. fi
  86. rm -f "$STORAGE/windows.old"
  87. rm -f "$STORAGE/windows.vga"
  88. rm -f "$STORAGE/windows.args"
  89. rm -f "$STORAGE/windows.base"
  90. rm -f "$STORAGE/windows.boot"
  91. rm -f "$STORAGE/windows.mode"
  92. rm -f "$STORAGE/windows.type"
  93. cp -f /run/version "$STORAGE/windows.ver"
  94. if [[ "$iso" == "$STORAGE/"* ]]; then
  95. if [[ "$aborted" != [Yy1]* ]] || [ -z "$CUSTOM" ]; then
  96. base=$(basename "$iso")
  97. echo "$base" > "$STORAGE/windows.base"
  98. fi
  99. fi
  100. if [[ "${PLATFORM,,}" == "x64" ]]; then
  101. if [[ "${BOOT_MODE,,}" == "windows_legacy" ]]; then
  102. echo "$BOOT_MODE" > "$STORAGE/windows.mode"
  103. if [[ "${MACHINE,,}" != "q35" ]]; then
  104. echo "$MACHINE" > "$STORAGE/windows.old"
  105. fi
  106. else
  107. # Enable secure boot + TPM on manual installs as Win11 requires
  108. if [[ "$MANUAL" == [Yy1]* ]] || [[ "$aborted" == [Yy1]* ]]; then
  109. if [[ "${DETECTED,,}" == "win11"* ]]; then
  110. BOOT_MODE="windows_secure"
  111. echo "$BOOT_MODE" > "$STORAGE/windows.mode"
  112. fi
  113. fi
  114. # Enable secure boot on multi-socket systems to workaround freeze
  115. if [ -n "$SOCKETS" ] && [[ "$SOCKETS" != "1" ]]; then
  116. BOOT_MODE="windows_secure"
  117. echo "$BOOT_MODE" > "$STORAGE/windows.mode"
  118. fi
  119. fi
  120. fi
  121. if [ -n "${ARGS:-}" ]; then
  122. ARGUMENTS="$ARGS ${ARGUMENTS:-}"
  123. echo "$ARGS" > "$STORAGE/windows.args"
  124. fi
  125. if [ -n "${DISK_TYPE:-}" ] && [[ "${DISK_TYPE:-}" != "scsi" ]]; then
  126. echo "$DISK_TYPE" > "$STORAGE/windows.type"
  127. fi
  128. rm -rf "$TMP"
  129. return 0
  130. }
  131. abortInstall() {
  132. local dir="$1"
  133. local iso="$2"
  134. local efi
  135. [[ "${iso,,}" == *".esd" ]] && exit 60
  136. efi=$(find "$dir" -maxdepth 1 -type d -iname efi | head -n 1)
  137. if [ -z "$efi" ]; then
  138. [[ "${PLATFORM,,}" == "x64" ]] && BOOT_MODE="windows_legacy"
  139. fi
  140. if [ -n "$CUSTOM" ]; then
  141. BOOT="$iso"
  142. REMOVE="N"
  143. else
  144. if [[ "$iso" != "$BOOT" ]]; then
  145. if ! mv -f "$iso" "$BOOT"; then
  146. error "Failed to move ISO file: $iso" && return 1
  147. fi
  148. fi
  149. fi
  150. finishInstall "$BOOT" "Y" && return 0
  151. return 1
  152. }
  153. detectCustom() {
  154. local file base
  155. local fname="custom.iso"
  156. CUSTOM=""
  157. if [ -d "/$fname" ]; then
  158. error "The file /$fname has an invalid path!" && return 1
  159. fi
  160. file=$(find / -maxdepth 1 -type f -iname "$fname" | head -n 1)
  161. [ ! -s "$file" ] && file=$(find "$STORAGE" -maxdepth 1 -type f -iname "$fname" | head -n 1)
  162. if [ ! -s "$file" ] && [[ "${VERSION,,}" != "http"* ]]; then
  163. base=$(basename "$VERSION")
  164. file="$STORAGE/$base"
  165. fi
  166. if [ ! -f "$file" ] || [ ! -s "$file" ]; then
  167. return 0
  168. fi
  169. local size
  170. size="$(stat -c%s "$file")"
  171. [ -z "$size" ] || [[ "$size" == "0" ]] && return 0
  172. ISO="$file"
  173. CUSTOM="$ISO"
  174. BOOT="$STORAGE/windows.$size.iso"
  175. return 0
  176. }
  177. extractESD() {
  178. local iso="$1"
  179. local dir="$2"
  180. local version="$3"
  181. local desc="$4"
  182. local size size_gb space space_gb desc
  183. local msg="Extracting $desc bootdisk..."
  184. info "$msg" && html "$msg"
  185. if [ "$(stat -c%s "$iso")" -lt 100000000 ]; then
  186. error "Invalid ESD file: Size is smaller than 100 MB" && return 1
  187. fi
  188. rm -rf "$dir"
  189. mkdir -p "$dir"
  190. size=16106127360
  191. size_gb=$(formatBytes "$size")
  192. space=$(df --output=avail -B 1 "$dir" | tail -n 1)
  193. space_gb=$(formatBytes "$space")
  194. if (( size > space )); then
  195. error "Not enough free space in $STORAGE, have $space_gb available but need at least $size_gb." && return 1
  196. fi
  197. local esdImageCount
  198. esdImageCount=$(wimlib-imagex info "$iso" | awk '/Image Count:/ {print $3}')
  199. wimlib-imagex apply "$iso" 1 "$dir" --quiet 2>/dev/null || {
  200. retVal=$?
  201. error "Extracting $desc bootdisk failed" && return $retVal
  202. }
  203. local bootWimFile="$dir/sources/boot.wim"
  204. local installWimFile="$dir/sources/install.wim"
  205. local msg="Extracting $desc environment..."
  206. info "$msg" && html "$msg"
  207. wimlib-imagex export "$iso" 2 "$bootWimFile" --compress=none --quiet || {
  208. retVal=$?
  209. error "Adding WinPE failed" && return ${retVal}
  210. }
  211. local msg="Extracting $desc setup..."
  212. info "$msg" && html "$msg"
  213. wimlib-imagex export "$iso" 3 "$bootWimFile" --compress=none --boot --quiet || {
  214. retVal=$?
  215. error "Adding Windows Setup failed" && return ${retVal}
  216. }
  217. if [[ "${PLATFORM,,}" == "x64" ]]; then
  218. LABEL="CCCOMA_X64FRE_EN-US_DV9"
  219. else
  220. LABEL="CPBA_A64FRE_EN-US_DV9"
  221. fi
  222. local msg="Extracting $desc image..."
  223. info "$msg" && html "$msg"
  224. local edition imageIndex imageEdition
  225. edition=$(getCatalog "$version" "name")
  226. if [ -z "$edition" ]; then
  227. error "Invalid VERSION specified, value \"$version\" is not recognized!" && return 1
  228. fi
  229. for (( imageIndex=4; imageIndex<=esdImageCount; imageIndex++ )); do
  230. imageEdition=$(wimlib-imagex info "$iso" ${imageIndex} | grep '^Description:' | sed 's/Description:[ \t]*//')
  231. [[ "${imageEdition,,}" != "${edition,,}" ]] && continue
  232. wimlib-imagex export "$iso" ${imageIndex} "$installWimFile" --compress=LZMS --chunk-size 128K --quiet || {
  233. retVal=$?
  234. error "Addition of $imageIndex to the $desc image failed" && return $retVal
  235. }
  236. return 0
  237. done
  238. error "Failed to find product '$edition' in install.wim!" && return 1
  239. }
  240. extractImage() {
  241. local iso="$1"
  242. local dir="$2"
  243. local version="$3"
  244. local desc="local ISO"
  245. local size size_gb space space_gb
  246. if [ -z "$CUSTOM" ]; then
  247. desc="downloaded ISO"
  248. if [[ "$version" != "http"* ]]; then
  249. desc=$(printVersion "$version" "$desc")
  250. fi
  251. fi
  252. if [[ "${iso,,}" == *".esd" ]]; then
  253. extractESD "$iso" "$dir" "$version" "$desc" && return 0
  254. return 1
  255. fi
  256. local msg="Extracting $desc image..."
  257. info "$msg" && html "$msg"
  258. rm -rf "$dir"
  259. mkdir -p "$dir"
  260. size=$(stat -c%s "$iso")
  261. size_gb=$(formatBytes "$size")
  262. space=$(df --output=avail -B 1 "$dir" | tail -n 1)
  263. space_gb=$(formatBytes "$space")
  264. if ((size<100000000)); then
  265. error "Invalid ISO file: Size is smaller than 100 MB" && return 1
  266. fi
  267. if (( size > space )); then
  268. error "Not enough free space in $STORAGE, have $space_gb available but need at least $size_gb." && return 1
  269. fi
  270. rm -rf "$dir"
  271. if ! 7z x "$iso" -o"$dir" > /dev/null; then
  272. error "Failed to extract ISO file: $iso" && return 1
  273. fi
  274. LABEL=$(isoinfo -d -i "$iso" | sed -n 's/Volume id: //p')
  275. return 0
  276. }
  277. getPlatform() {
  278. local xml="$1"
  279. local tag="ARCH"
  280. local platform="x64"
  281. local arch
  282. arch=$(sed -n "/$tag/{s/.*<$tag>\(.*\)<\/$tag>.*/\1/;p}" <<< "$xml")
  283. case "${arch,,}" in
  284. "0" ) platform="x86" ;;
  285. "9" ) platform="x64" ;;
  286. "12" )platform="arm64" ;;
  287. esac
  288. echo "$platform"
  289. return 0
  290. }
  291. checkPlatform() {
  292. local xml="$1"
  293. local platform compat
  294. platform=$(getPlatform "$xml")
  295. case "${platform,,}" in
  296. "x86" ) compat="x64" ;;
  297. "x64" ) compat="$platform" ;;
  298. "arm64" ) compat="$platform" ;;
  299. * ) compat="${PLATFORM,,}" ;;
  300. esac
  301. [[ "${compat,,}" == "${PLATFORM,,}" ]] && return 0
  302. error "You cannot boot ${platform^^} images on a $PLATFORM CPU!"
  303. return 1
  304. }
  305. hasVersion() {
  306. local id="$1"
  307. local tag="$2"
  308. local xml="$3"
  309. local edition
  310. [ ! -f "/run/assets/$id.xml" ] && return 1
  311. edition=$(printEdition "$id" "")
  312. [ -z "$edition" ] && return 1
  313. [[ "${xml,,}" != *"<${tag,,}>${edition,,}</${tag,,}>"* ]] && return 1
  314. return 0
  315. }
  316. selectVersion() {
  317. local tag="$1"
  318. local xml="$2"
  319. local platform="$3"
  320. local id name prefer
  321. name=$(sed -n "/$tag/{s/.*<$tag>\(.*\)<\/$tag>.*/\1/;p}" <<< "$xml")
  322. [[ "$name" == *"Operating System"* ]] && name=""
  323. [ -z "$name" ] && return 0
  324. id=$(fromName "$name" "$platform")
  325. [ -z "$id" ] && warn "Unknown ${tag,,}: '$name'" && return 0
  326. prefer="$id-enterprise"
  327. hasVersion "$prefer" "$tag" "$xml" && echo "$prefer" && return 0
  328. prefer="$id-ultimate"
  329. hasVersion "$prefer" "$tag" "$xml" && echo "$prefer" && return 0
  330. prefer="$id"
  331. hasVersion "$prefer" "$tag" "$xml" && echo "$prefer" && return 0
  332. prefer=$(getVersion "$name" "$platform")
  333. echo "$prefer"
  334. return 0
  335. }
  336. detectVersion() {
  337. local xml="$1"
  338. local id platform
  339. platform=$(getPlatform "$xml")
  340. id=$(selectVersion "DISPLAYNAME" "$xml" "$platform")
  341. [ -z "$id" ] && id=$(selectVersion "PRODUCTNAME" "$xml" "$platform")
  342. [ -z "$id" ] && id=$(selectVersion "NAME" "$xml" "$platform")
  343. echo "$id"
  344. return 0
  345. }
  346. detectLanguage() {
  347. local xml="$1"
  348. local lang=""
  349. if [[ "$xml" == *"LANGUAGE><DEFAULT>"* ]]; then
  350. lang="${xml#*LANGUAGE><DEFAULT>}"
  351. lang="${lang%%<*}"
  352. else
  353. if [[ "$xml" == *"FALLBACK><DEFAULT>"* ]]; then
  354. lang="${xml#*FALLBACK><DEFAULT>}"
  355. lang="${lang%%<*}"
  356. fi
  357. fi
  358. if [ -z "$lang" ]; then
  359. warn "Language could not be detected from ISO!" && return 0
  360. fi
  361. local culture
  362. culture=$(getLanguage "$lang" "culture")
  363. [ -n "$culture" ] && LANGUAGE="$lang" && return 0
  364. warn "Invalid language detected: \"$lang\""
  365. return 0
  366. }
  367. setXML() {
  368. local file="/custom.xml"
  369. if [ -d "$file" ]; then
  370. warn "The file $file has an invalid path!"
  371. fi
  372. [ ! -f "$file" ] || [ ! -s "$file" ] && file="$STORAGE/custom.xml"
  373. [ ! -f "$file" ] || [ ! -s "$file" ] && file="/run/assets/custom.xml"
  374. [ ! -f "$file" ] || [ ! -s "$file" ] && file="$1"
  375. [ ! -f "$file" ] || [ ! -s "$file" ] && file="/run/assets/$DETECTED.xml"
  376. [ ! -f "$file" ] || [ ! -s "$file" ] && return 1
  377. XML="$file"
  378. return 0
  379. }
  380. detectImage() {
  381. local dir="$1"
  382. local version="$2"
  383. local desc msg find language
  384. XML=""
  385. if [ -z "$DETECTED" ] && [ -z "$CUSTOM" ]; then
  386. [[ "${version,,}" != "http"* ]] && DETECTED="$version"
  387. fi
  388. if [ -n "$DETECTED" ]; then
  389. skipVersion "${DETECTED,,}" && return 0
  390. if ! setXML "" && [[ "$MANUAL" != [Yy1]* ]]; then
  391. MANUAL="Y"
  392. desc=$(printEdition "$DETECTED" "this version")
  393. warn "the answer file for $desc was not found ($DETECTED.xml), $FB."
  394. fi
  395. return 0
  396. fi
  397. info "Detecting version from ISO image..."
  398. if detectLegacy "$dir"; then
  399. desc=$(printEdition "$DETECTED" "$DETECTED")
  400. info "Detected: $desc"
  401. return 0
  402. fi
  403. local src wim info
  404. src=$(find "$dir" -maxdepth 1 -type d -iname sources | head -n 1)
  405. if [ ! -d "$src" ]; then
  406. warn "failed to locate 'sources' folder in ISO image, $FB" && return 1
  407. fi
  408. wim=$(find "$src" -maxdepth 1 -type f -iname install.wim | head -n 1)
  409. [ ! -f "$wim" ] && wim=$(find "$src" -maxdepth 1 -type f -iname install.esd | head -n 1)
  410. if [ ! -f "$wim" ]; then
  411. warn "failed to locate 'install.wim' or 'install.esd' in ISO image, $FB" && return 1
  412. fi
  413. info=$(wimlib-imagex info -xml "$wim" | tr -d '\000')
  414. checkPlatform "$info" || exit 67
  415. DETECTED=$(detectVersion "$info")
  416. if [ -z "$DETECTED" ]; then
  417. msg="Failed to determine Windows version from image"
  418. if setXML "" || [[ "$MANUAL" == [Yy1]* ]]; then
  419. info "${msg}!"
  420. else
  421. MANUAL="Y"
  422. warn "${msg}, $FB."
  423. fi
  424. return 0
  425. fi
  426. desc=$(printEdition "$DETECTED" "$DETECTED")
  427. detectLanguage "$info"
  428. if [[ "${LANGUAGE,,}" != "en" ]] && [[ "${LANGUAGE,,}" != "en-"* ]]; then
  429. language=$(getLanguage "$LANGUAGE" "desc")
  430. desc+=" ($language)"
  431. fi
  432. info "Detected: $desc"
  433. setXML "" && return 0
  434. msg="the answer file for $desc was not found ($DETECTED.xml)"
  435. local fallback="/run/assets/${DETECTED%%-*}.xml"
  436. if setXML "$fallback" || [[ "$MANUAL" == [Yy1]* ]]; then
  437. [[ "$MANUAL" != [Yy1]* ]] && warn "${msg}."
  438. else
  439. MANUAL="Y"
  440. warn "${msg}, $FB."
  441. fi
  442. return 0
  443. }
  444. prepareImage() {
  445. local iso="$1"
  446. local dir="$2"
  447. local desc missing
  448. desc=$(printVersion "$DETECTED" "$DETECTED")
  449. setMachine "$DETECTED" "$iso" "$dir" "$desc" || return 1
  450. skipVersion "$DETECTED" && return 0
  451. if [[ "${BOOT_MODE,,}" != "windows_legacy" ]]; then
  452. [ -f "$dir/$ETFS" ] && [ -f "$dir/$EFISYS" ] && return 0
  453. missing=$(basename "$dir/$EFISYS")
  454. [ ! -f "$dir/$ETFS" ] && missing=$(basename "$dir/$ETFS")
  455. error "Failed to locate file \"${missing,,}\" in ISO image!"
  456. return 1
  457. fi
  458. prepareLegacy "$iso" "$dir" "$desc" && return 0
  459. error "Failed to extract boot image from ISO image!"
  460. return 1
  461. }
  462. updateXML() {
  463. local asset="$1"
  464. local language="$2"
  465. local culture region user admin pass keyboard
  466. if [ -n "${VM_NET_IP:-}" ]; then
  467. sed -i "s/ 20.20.20.1 / ${VM_NET_IP%.*}.1 /g" "$asset"
  468. fi
  469. [ -z "$HEIGHT" ] && HEIGHT="720"
  470. [ -z "$WIDTH" ] && WIDTH="1280"
  471. sed -i "s/<VerticalResolution>1080<\/VerticalResolution>/<VerticalResolution>$HEIGHT<\/VerticalResolution>/g" "$asset"
  472. sed -i "s/<HorizontalResolution>1920<\/HorizontalResolution>/<HorizontalResolution>$WIDTH<\/HorizontalResolution>/g" "$asset"
  473. culture=$(getLanguage "$language" "culture")
  474. if [ -n "$culture" ] && [[ "${culture,,}" != "en-us" ]]; then
  475. sed -i "s/<UILanguage>en-US<\/UILanguage>/<UILanguage>$culture<\/UILanguage>/g" "$asset"
  476. fi
  477. region="$REGION"
  478. [ -z "$region" ] && region="$culture"
  479. if [ -n "$region" ] && [[ "${region,,}" != "en-us" ]]; then
  480. sed -i "s/<UserLocale>en-US<\/UserLocale>/<UserLocale>$region<\/UserLocale>/g" "$asset"
  481. sed -i "s/<SystemLocale>en-US<\/SystemLocale>/<SystemLocale>$region<\/SystemLocale>/g" "$asset"
  482. fi
  483. keyboard="$KEYBOARD"
  484. [ -z "$keyboard" ] && keyboard="$culture"
  485. if [ -n "$keyboard" ] && [[ "${keyboard,,}" != "en-us" ]]; then
  486. sed -i "s/<InputLocale>en-US<\/InputLocale>/<InputLocale>$keyboard<\/InputLocale>/g" "$asset"
  487. sed -i "s/<InputLocale>0409:00000409<\/InputLocale>/<InputLocale>$keyboard<\/InputLocale>/g" "$asset"
  488. fi
  489. user=$(echo "$USERNAME" | sed 's/[^[:alnum:]@!._-]//g')
  490. if [ -n "$user" ]; then
  491. sed -i "s/<Name>Docker<\/Name>/<Name>$user<\/Name>/g" "$asset"
  492. sed -i "s/where name=\"Docker\"/where name=\"$user\"/g" "$asset"
  493. sed -i "s/<FullName>Docker<\/FullName>/<FullName>$user<\/FullName>/g" "$asset"
  494. sed -i "s/<Username>Docker<\/Username>/<Username>$user<\/Username>/g" "$asset"
  495. fi
  496. if [ -n "$PASSWORD" ]; then
  497. pass=$(printf '%s' "${PASSWORD}Password" | iconv -f utf-8 -t utf-16le | base64 -w 0)
  498. admin=$(printf '%s' "${PASSWORD}AdministratorPassword" | iconv -f utf-8 -t utf-16le | base64 -w 0)
  499. sed -i "s/<Value>password<\/Value>/<Value>$admin<\/Value>/g" "$asset"
  500. sed -i "s/<PlainText>true<\/PlainText>/<PlainText>false<\/PlainText>/g" "$asset"
  501. sed -z "s/<Password>...........<Value \/>/<Password>\n <Value>$pass<\/Value>/g" -i "$asset"
  502. sed -z "s/<Password>...............<Value \/>/<Password>\n <Value>$pass<\/Value>/g" -i "$asset"
  503. sed -z "s/<AdministratorPassword>...........<Value \/>/<AdministratorPassword>\n <Value>$admin<\/Value>/g" -i "$asset"
  504. sed -z "s/<AdministratorPassword>...............<Value \/>/<AdministratorPassword>\n <Value>$admin<\/Value>/g" -i "$asset"
  505. fi
  506. if [ -n "$EDITION" ]; then
  507. [[ "${EDITION^^}" == "CORE" ]] && EDITION="STANDARDCORE"
  508. sed -i "s/SERVERSTANDARD<\/Value>/SERVER${EDITION^^}<\/Value>/g" "$asset"
  509. fi
  510. if [ -n "$KEY" ]; then
  511. sed -i '/<ProductKey>/,/<\/ProductKey>/d' "$asset"
  512. sed -i "s/<\/UserData>/ <ProductKey>\n <Key>${KEY}<\/Key>\n <WillShowUI>OnError<\/WillShowUI>\n <\/ProductKey>\n <\/UserData>/g" "$asset"
  513. fi
  514. return 0
  515. }
  516. addDriver() {
  517. local id="$1"
  518. local path="$2"
  519. local target="$3"
  520. local driver="$4"
  521. local desc=""
  522. local folder=""
  523. if [ -z "$id" ]; then
  524. warn "no Windows version specified for \"$driver\" driver!" && return 0
  525. fi
  526. case "${id,,}" in
  527. "win7x86"* ) folder="w7/x86" ;;
  528. "win7x64"* ) folder="w7/amd64" ;;
  529. "win81x64"* ) folder="w8.1/amd64" ;;
  530. "win10x64"* ) folder="w10/amd64" ;;
  531. "win11x64"* ) folder="w11/amd64" ;;
  532. "win2025"* ) folder="2k25/amd64" ;;
  533. "win2022"* ) folder="2k22/amd64" ;;
  534. "win2019"* ) folder="2k19/amd64" ;;
  535. "win2016"* ) folder="2k16/amd64" ;;
  536. "win2012"* ) folder="2k12R2/amd64" ;;
  537. "win2008"* ) folder="2k8R2/amd64" ;;
  538. "win10arm64"* ) folder="w10/ARM64" ;;
  539. "win11arm64"* ) folder="w11/ARM64" ;;
  540. "winvistax86"* ) folder="2k8/x86" ;;
  541. "winvistax64"* ) folder="2k8/amd64" ;;
  542. esac
  543. if [ -z "$folder" ]; then
  544. desc=$(printVersion "$id" "$id")
  545. warn "no \"$driver\" driver available for \"$desc\" !" && return 0
  546. fi
  547. [ ! -d "$path/$driver/$folder" ] && return 0
  548. case "${id,,}" in
  549. "winvista"* )
  550. [[ "${driver,,}" == "viorng" ]] && return 0
  551. ;;
  552. esac
  553. local dest="$path/$target/$driver"
  554. mkdir -p "$dest" || return 1
  555. cp -Lr "$path/$driver/$folder/." "$dest" || return 1
  556. return 0
  557. }
  558. addDrivers() {
  559. local src="$1"
  560. local tmp="$2"
  561. local file="$3"
  562. local index="$4"
  563. local version="$5"
  564. local drivers="$tmp/drivers"
  565. rm -rf "$drivers"
  566. mkdir -p "$drivers"
  567. local msg="Adding drivers to image..."
  568. info "$msg" && html "$msg"
  569. if [ -z "$version" ]; then
  570. version="win11x64"
  571. warn "Windows version unknown, falling back to Windows 11 drivers..."
  572. fi
  573. if ! bsdtar -xf /drivers.txz -C "$drivers"; then
  574. error "Failed to extract drivers from archive!" && return 1
  575. fi
  576. local target="\$WinPEDriver\$"
  577. local dest="$drivers/$target"
  578. mkdir -p "$dest" || return 1
  579. wimlib-imagex update "$file" "$index" --command "delete --force --recursive /$target" >/dev/null || true
  580. addDriver "$version" "$drivers" "$target" "qxl" || return 1
  581. addDriver "$version" "$drivers" "$target" "viofs" || return 1
  582. addDriver "$version" "$drivers" "$target" "sriov" || return 1
  583. addDriver "$version" "$drivers" "$target" "smbus" || return 1
  584. addDriver "$version" "$drivers" "$target" "qxldod" || return 1
  585. addDriver "$version" "$drivers" "$target" "viorng" || return 1
  586. addDriver "$version" "$drivers" "$target" "viostor" || return 1
  587. addDriver "$version" "$drivers" "$target" "viomem" || return 1
  588. addDriver "$version" "$drivers" "$target" "NetKVM" || return 1
  589. addDriver "$version" "$drivers" "$target" "Balloon" || return 1
  590. addDriver "$version" "$drivers" "$target" "vioscsi" || return 1
  591. addDriver "$version" "$drivers" "$target" "pvpanic" || return 1
  592. addDriver "$version" "$drivers" "$target" "vioinput" || return 1
  593. addDriver "$version" "$drivers" "$target" "viogpudo" || return 1
  594. addDriver "$version" "$drivers" "$target" "vioserial" || return 1
  595. addDriver "$version" "$drivers" "$target" "qemupciserial" || return 1
  596. case "${version,,}" in
  597. "win11x64"* | "win2025"* )
  598. # Workaround Virtio GPU driver bug
  599. local dst="$src/\$OEM\$/\$\$/Drivers"
  600. mkdir -p "$dst" || return 1
  601. cp -Lr "$dest/." "$dst" || return 1
  602. rm -rf "$dest/viogpudo"
  603. ;;
  604. esac
  605. if ! wimlib-imagex update "$file" "$index" --command "add $dest /$target" >/dev/null; then
  606. return 1
  607. fi
  608. rm -rf "$drivers"
  609. return 0
  610. }
  611. updateImage() {
  612. local dir="$1"
  613. local asset="$2"
  614. local language="$3"
  615. local tmp="/tmp/install"
  616. local file="autounattend.xml"
  617. local org="${file//.xml/.org}"
  618. local dat="${file//.xml/.dat}"
  619. local desc path src wim xml index result
  620. skipVersion "${DETECTED,,}" && return 0
  621. if [ ! -s "$asset" ] || [ ! -f "$asset" ]; then
  622. asset=""
  623. if [[ "$MANUAL" != [Yy1]* ]]; then
  624. MANUAL="Y"
  625. warn "no answer file provided, $FB."
  626. fi
  627. fi
  628. rm -rf "$tmp"
  629. mkdir -p "$tmp"
  630. src=$(find "$dir" -maxdepth 1 -type d -iname sources | head -n 1)
  631. if [ ! -d "$src" ]; then
  632. error "failed to locate 'sources' folder in ISO image, $FB" && return 1
  633. fi
  634. wim=$(find "$src" -maxdepth 1 -type f -iname boot.wim | head -n 1)
  635. [ ! -f "$wim" ] && wim=$(find "$src" -maxdepth 1 -type f -iname boot.esd | head -n 1)
  636. if [ ! -f "$wim" ]; then
  637. error "failed to locate 'boot.wim' or 'boot.esd' in ISO image, $FB" && return 1
  638. fi
  639. index="1"
  640. result=$(wimlib-imagex info -xml "$wim" | tr -d '\000')
  641. if [[ "${result^^}" == *"<IMAGE INDEX=\"2\">"* ]]; then
  642. index="2"
  643. fi
  644. if ! addDrivers "$src" "$tmp" "$wim" "$index" "$DETECTED"; then
  645. error "Failed to add drivers to image!"
  646. fi
  647. if ! addFolder "$src"; then
  648. error "Failed to add OEM folder to image!"
  649. fi
  650. if wimlib-imagex extract "$wim" "$index" "/$file" "--dest-dir=$tmp" >/dev/null 2>&1; then
  651. if ! wimlib-imagex extract "$wim" "$index" "/$dat" "--dest-dir=$tmp" >/dev/null 2>&1; then
  652. if ! wimlib-imagex extract "$wim" "$index" "/$org" "--dest-dir=$tmp" >/dev/null 2>&1; then
  653. if ! wimlib-imagex update "$wim" "$index" --command "rename /$file /$org" > /dev/null; then
  654. warn "failed to backup original answer file ($file)."
  655. fi
  656. fi
  657. fi
  658. fi
  659. if [[ "$MANUAL" != [Yy1]* ]]; then
  660. xml=$(basename "$asset")
  661. info "Adding $xml for automatic installation..."
  662. local answer="$tmp/$xml"
  663. cp "$asset" "$answer"
  664. updateXML "$answer" "$language"
  665. if ! wimlib-imagex update "$wim" "$index" --command "add $answer /$file" > /dev/null; then
  666. MANUAL="Y"
  667. warn "failed to add answer file ($xml) to ISO image, $FB"
  668. else
  669. wimlib-imagex update "$wim" "$index" --command "add $answer /$dat" > /dev/null || true
  670. fi
  671. fi
  672. if [[ "$MANUAL" == [Yy1]* ]]; then
  673. wimlib-imagex update "$wim" "$index" --command "delete --force /$file" > /dev/null || true
  674. if wimlib-imagex extract "$wim" "$index" "/$org" "--dest-dir=$tmp" >/dev/null 2>&1; then
  675. if ! wimlib-imagex update "$wim" "$index" --command "add $tmp/$org /$file" > /dev/null; then
  676. warn "failed to restore original answer file ($org)."
  677. fi
  678. fi
  679. fi
  680. local find="$file"
  681. [[ "$MANUAL" == [Yy1]* ]] && find="$org"
  682. path=$(find "$dir" -maxdepth 1 -type f -iname "$find" | head -n 1)
  683. if [ -f "$path" ]; then
  684. if [[ "$MANUAL" != [Yy1]* ]]; then
  685. mv -f "$path" "${path%.*}.org"
  686. else
  687. mv -f "$path" "${path%.*}.xml"
  688. fi
  689. fi
  690. rm -rf "$tmp"
  691. return 0
  692. }
  693. removeImage() {
  694. local iso="$1"
  695. [ ! -f "$iso" ] && return 0
  696. [ -n "$CUSTOM" ] && return 0
  697. rm -f "$iso" 2> /dev/null || warn "failed to remove $iso !"
  698. return 0
  699. }
  700. buildImage() {
  701. local dir="$1"
  702. local failed=""
  703. local cat="BOOT.CAT"
  704. local log="/run/shm/iso.log"
  705. local base size size_gb space space_gb desc
  706. if [ -f "$BOOT" ]; then
  707. error "File $BOOT does already exist?!" && return 1
  708. fi
  709. base=$(basename "$BOOT")
  710. local out="$TMP/${base%.*}.tmp"
  711. rm -f "$out"
  712. desc=$(printVersion "$DETECTED" "ISO")
  713. local msg="Building $desc image..."
  714. info "$msg" && html "$msg"
  715. [ -z "$LABEL" ] && LABEL="Windows"
  716. if [ ! -f "$dir/$ETFS" ]; then
  717. error "Failed to locate file \"$ETFS\" in ISO image!" && return 1
  718. fi
  719. size=$(du -h -b --max-depth=0 "$dir" | cut -f1)
  720. size_gb=$(formatBytes "$size")
  721. space=$(df --output=avail -B 1 "$TMP" | tail -n 1)
  722. space_gb=$(formatBytes "$space")
  723. if (( size > space )); then
  724. error "Not enough free space in $STORAGE, have $space_gb available but need at least $size_gb." && return 1
  725. fi
  726. if [[ "${BOOT_MODE,,}" != "windows_legacy" ]]; then
  727. genisoimage -o "$out" -b "$ETFS" -no-emul-boot -c "$cat" -iso-level 4 -J -l -D -N -joliet-long -relaxed-filenames -V "${LABEL::30}" \
  728. -udf -boot-info-table -eltorito-alt-boot -eltorito-boot "$EFISYS" -no-emul-boot -allow-limited-size -quiet "$dir" 2> "$log" || failed="y"
  729. else
  730. case "${DETECTED,,}" in
  731. "win2k"* | "winxp"* | "win2003"* )
  732. genisoimage -o "$out" -b "$ETFS" -no-emul-boot -boot-load-seg 1984 -boot-load-size 4 -c "$cat" -iso-level 2 -J -l -D -N -joliet-long \
  733. -relaxed-filenames -V "${LABEL::30}" -quiet "$dir" 2> "$log" || failed="y" ;;
  734. "win9"* )
  735. genisoimage -o "$out" -b "$ETFS" -J -r -V "${LABEL::30}" -quiet "$dir" 2> "$log" || failed="y" ;;
  736. * )
  737. genisoimage -o "$out" -b "$ETFS" -no-emul-boot -c "$cat" -iso-level 2 -J -l -D -N -joliet-long -relaxed-filenames -V "${LABEL::30}" \
  738. -udf -allow-limited-size -quiet "$dir" 2> "$log" || failed="y" ;;
  739. esac
  740. fi
  741. if [ -n "$failed" ]; then
  742. [ -s "$log" ] && echo "$(<"$log")"
  743. error "Failed to build image!" && return 1
  744. fi
  745. local error=""
  746. local hide="Warning: creating filesystem that does not conform to ISO-9660."
  747. [ -s "$log" ] && error="$(<"$log")"
  748. [[ "$error" != "$hide" ]] && echo "$error"
  749. mv -f "$out" "$BOOT" || return 1
  750. return 0
  751. }
  752. bootWindows() {
  753. rm -rf "$TMP"
  754. if [ -f "$STORAGE/windows.args" ]; then
  755. ARGS=$(<"$STORAGE/windows.args")
  756. ARGUMENTS="$ARGS ${ARGUMENTS:-}"
  757. fi
  758. if [ -s "$STORAGE/windows.type" ] && [ -f "$STORAGE/windows.type" ]; then
  759. [ -z "${DISK_TYPE:-}" ] && DISK_TYPE=$(<"$STORAGE/windows.type")
  760. fi
  761. if [ -s "$STORAGE/windows.mode" ] && [ -f "$STORAGE/windows.mode" ]; then
  762. BOOT_MODE=$(<"$STORAGE/windows.mode")
  763. fi
  764. if [ -s "$STORAGE/windows.old" ] && [ -f "$STORAGE/windows.old" ]; then
  765. [[ "${PLATFORM,,}" == "x64" ]] && MACHINE=$(<"$STORAGE/windows.old")
  766. fi
  767. return 0
  768. }
  769. ######################################
  770. ! parseVersion && exit 58
  771. ! parseLanguage && exit 56
  772. ! detectCustom && exit 59
  773. if ! startInstall; then
  774. bootWindows && return 0
  775. exit 68
  776. fi
  777. if [ ! -s "$ISO" ] || [ ! -f "$ISO" ]; then
  778. if ! downloadImage "$ISO" "$VERSION" "$LANGUAGE"; then
  779. rm -f "$ISO" 2> /dev/null || true
  780. exit 61
  781. fi
  782. fi
  783. if ! extractImage "$ISO" "$DIR" "$VERSION"; then
  784. rm -f "$ISO" 2> /dev/null || true
  785. exit 62
  786. fi
  787. if ! detectImage "$DIR" "$VERSION"; then
  788. abortInstall "$DIR" "$ISO" && return 0
  789. exit 60
  790. fi
  791. if ! prepareImage "$ISO" "$DIR"; then
  792. abortInstall "$DIR" "$ISO" && return 0
  793. exit 66
  794. fi
  795. if ! updateImage "$DIR" "$XML" "$LANGUAGE"; then
  796. abortInstall "$DIR" "$ISO" && return 0
  797. exit 63
  798. fi
  799. if ! removeImage "$ISO"; then
  800. exit 64
  801. fi
  802. if ! buildImage "$DIR"; then
  803. exit 65
  804. fi
  805. if ! finishInstall "$BOOT" "N"; then
  806. exit 69
  807. fi
  808. html "Successfully prepared image for installation..."
  809. return 0