install.sh 28 KB

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