install.sh 28 KB

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