install.sh 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. #!/usr/bin/env bash
  2. set -Eeuo pipefail
  3. : "${MANUAL:=""}"
  4. : "${DETECTED:=""}"
  5. : "${VERSION:="win11x64"}"
  6. if [[ "${VERSION}" == \"*\" || "${VERSION}" == \'*\' ]]; then
  7. VERSION="${VERSION:1:-1}"
  8. fi
  9. [[ "${VERSION,,}" == "11" ]] && VERSION="win11x64"
  10. [[ "${VERSION,,}" == "win11" ]] && VERSION="win11x64"
  11. [[ "${VERSION,,}" == "10" ]] && VERSION="win10x64"
  12. [[ "${VERSION,,}" == "win10" ]] && VERSION="win10x64"
  13. [[ "${VERSION,,}" == "8" ]] && VERSION="win81x64"
  14. [[ "${VERSION,,}" == "81" ]] && VERSION="win81x64"
  15. [[ "${VERSION,,}" == "8.1" ]] && VERSION="win81x64"
  16. [[ "${VERSION,,}" == "win8" ]] && VERSION="win81x64"
  17. [[ "${VERSION,,}" == "win81" ]] && VERSION="win81x64"
  18. [[ "${VERSION,,}" == "7" ]] && VERSION="win7x64"
  19. [[ "${VERSION,,}" == "win7" ]] && VERSION="win7x64"
  20. [[ "${VERSION,,}" == "vista" ]] && VERSION="winvistax64"
  21. [[ "${VERSION,,}" == "winvista" ]] && VERSION="winvistax64"
  22. [[ "${VERSION,,}" == "xp" ]] && VERSION="winxpx86"
  23. [[ "${VERSION,,}" == "winxp" ]] && VERSION="winxpx86"
  24. [[ "${VERSION,,}" == "22" ]] && VERSION="win2022-eval"
  25. [[ "${VERSION,,}" == "2022" ]] && VERSION="win2022-eval"
  26. [[ "${VERSION,,}" == "win22" ]] && VERSION="win2022-eval"
  27. [[ "${VERSION,,}" == "win2022" ]] && VERSION="win2022-eval"
  28. [[ "${VERSION,,}" == "19" ]] && VERSION="win2019-eval"
  29. [[ "${VERSION,,}" == "2019" ]] && VERSION="win2019-eval"
  30. [[ "${VERSION,,}" == "win19" ]] && VERSION="win2019-eval"
  31. [[ "${VERSION,,}" == "win2019" ]] && VERSION="win2019-eval"
  32. [[ "${VERSION,,}" == "16" ]] && VERSION="win2016-eval"
  33. [[ "${VERSION,,}" == "2016" ]] && VERSION="win2016-eval"
  34. [[ "${VERSION,,}" == "win16" ]] && VERSION="win2016-eval"
  35. [[ "${VERSION,,}" == "win2016" ]] && VERSION="win2016-eval"
  36. [[ "${VERSION,,}" == "2012" ]] && VERSION="win2012r2-eval"
  37. [[ "${VERSION,,}" == "win2012" ]] && VERSION="win2012r2-eval"
  38. [[ "${VERSION,,}" == "2008" ]] && VERSION="win2008r2"
  39. [[ "${VERSION,,}" == "win2008" ]] && VERSION="win2008r2"
  40. [[ "${VERSION,,}" == "ltsc10" ]] && VERSION="win10x64-enterprise-ltsc-eval"
  41. [[ "${VERSION,,}" == "10ltsc" ]] && VERSION="win10x64-enterprise-ltsc-eval"
  42. [[ "${VERSION,,}" == "win10-ltsc" ]] && VERSION="win10x64-enterprise-ltsc-eval"
  43. [[ "${VERSION,,}" == "win10x64-ltsc" ]] && VERSION="win10x64-enterprise-ltsc-eval"
  44. if [[ "${VERSION,,}" == "win10x64-enterprise-ltsc-eval" ]]; then
  45. DETECTED="win10x64-ltsc"
  46. fi
  47. if [[ "${VERSION,,}" == "win7x64" ]]; then
  48. DETECTED="win7x64"
  49. VERSION="https://dl.bobpony.com/windows/7/en_windows_7_enterprise_with_sp1_x64_dvd_u_677651.iso"
  50. fi
  51. if [[ "${VERSION,,}" == "winvistax64" ]]; then
  52. DETECTED="winvistax64"
  53. VERSION="https://dl.bobpony.com/windows/vista/en_windows_vista_sp2_x64_dvd_342267.iso"
  54. fi
  55. if [[ "${VERSION,,}" == "winxpx86" ]]; then
  56. DETECTED="winxpx86"
  57. VERSION="https://dl.bobpony.com/windows/xp/professional/en_windows_xp_professional_with_service_pack_3_x86_cd_vl_x14-73974.iso"
  58. fi
  59. if [[ "${VERSION,,}" == "core11" ]]; then
  60. DETECTED="win11x64"
  61. VERSION="https://archive.org/download/tiny-11-core-x-64-beta-1/tiny11%20core%20x64%20beta%201.iso"
  62. fi
  63. if [[ "${VERSION,,}" == "tiny11" ]]; then
  64. DETECTED="win11x64"
  65. VERSION="https://archive.org/download/tiny11-2311/tiny11%202311%20x64.iso"
  66. fi
  67. if [[ "${VERSION,,}" == "tiny10" ]]; then
  68. DETECTED="win10x64-ltsc"
  69. VERSION="https://archive.org/download/tiny-10-23-h2/tiny10%20x64%2023h2.iso"
  70. fi
  71. CUSTOM="custom.iso"
  72. [ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="Custom.iso"
  73. [ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="custom.ISO"
  74. [ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="CUSTOM.ISO"
  75. [ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="custom.img"
  76. [ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="Custom.img"
  77. [ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="custom.IMG"
  78. [ ! -f "$STORAGE/$CUSTOM" ] && CUSTOM="CUSTOM.IMG"
  79. ESD_URL=""
  80. MACHINE="q35"
  81. TMP="$STORAGE/tmp"
  82. DIR="$TMP/unpack"
  83. FB="falling back to manual installation!"
  84. ETFS="boot/etfsboot.com"
  85. EFISYS="efi/microsoft/boot/efisys_noprompt.bin"
  86. printVersion() {
  87. local id="$1"
  88. local desc=""
  89. [[ "$id" == "win7"* ]] && desc="Windows 7"
  90. [[ "$id" == "win8"* ]] && desc="Windows 8"
  91. [[ "$id" == "win10"* ]] && desc="Windows 10"
  92. [[ "$id" == "win11"* ]] && desc="Windows 11"
  93. [[ "$id" == "winxp"* ]] && desc="Windows XP"
  94. [[ "$id" == "winvista"* ]] && desc="Windows Vista"
  95. [[ "$id" == "win2025"* ]] && desc="Windows Server 2025"
  96. [[ "$id" == "win2022"* ]] && desc="Windows Server 2022"
  97. [[ "$id" == "win2019"* ]] && desc="Windows Server 2019"
  98. [[ "$id" == "win2016"* ]] && desc="Windows Server 2016"
  99. [[ "$id" == "win2012"* ]] && desc="Windows Server 2012"
  100. [[ "$id" == "win2008"* ]] && desc="Windows Server 2008"
  101. [[ "$id" == "win10x64-ltsc" ]] && desc="Windows 10 LTSC"
  102. echo "$desc"
  103. return 0
  104. }
  105. getName() {
  106. local file="$1"
  107. local desc=""
  108. [[ "${file,,}" == "win11"* ]] && desc="Windows 11"
  109. [[ "${file,,}" == "win10"* ]] && desc="Windows 10"
  110. [[ "${file,,}" == "win8.1"* ]] && desc="Windows 8"
  111. [[ "${file,,}" == "win8"* ]] && desc="Windows 8"
  112. [[ "${file,,}" == "win7"* ]] && desc="Windows 7"
  113. [[ "${file,,}" == "winxp"* ]] && desc="Windows XP"
  114. [[ "${file,,}" == "winvista"* ]] && desc="Windows Vista"
  115. [[ "${file,,}" == "tiny10"* ]] && desc="Tiny 10"
  116. [[ "${file,,}" == "tiny11"* ]] && desc="Tiny 11"
  117. [[ "${file,,}" == "tiny11_core"* ]] && desc="Tiny 11 Core"
  118. [[ "${file,,}" == *"windows11"* ]] && desc="Windows 11"
  119. [[ "${file,,}" == *"windows10"* ]] && desc="Windows 10"
  120. [[ "${file,,}" == *"windows8.1"* ]] && desc="Windows 8"
  121. [[ "${file,,}" == *"windows8"* ]] && desc="Windows 8"
  122. [[ "${file,,}" == *"windows7"* ]] && desc="Windows 7"
  123. [[ "${file,,}" == *"windowsxp"* ]] && desc="Windows XP"
  124. [[ "${file,,}" == *"windowsvista"* ]] && desc="Windows Vista"
  125. [[ "${file,,}" == *"windows_11"* ]] && desc="Windows 11"
  126. [[ "${file,,}" == *"windows_10"* ]] && desc="Windows 10"
  127. [[ "${file,,}" == *"windows_8.1"* ]] && desc="Windows 8"
  128. [[ "${file,,}" == *"windows_8"* ]] && desc="Windows 8"
  129. [[ "${file,,}" == *"windows_7"* ]] && desc="Windows 7"
  130. [[ "${file,,}" == *"windows_xp"* ]] && desc="Windows XP"
  131. [[ "${file,,}" == *"windows_vista"* ]] && desc="Windows Vista"
  132. [[ "${file,,}" == *"server2008"* ]] && desc="Windows Server 2008"
  133. [[ "${file,,}" == *"server2012"* ]] && desc="Windows Server 2012"
  134. [[ "${file,,}" == *"server2016"* ]] && desc="Windows Server 2016"
  135. [[ "${file,,}" == *"server2019"* ]] && desc="Windows Server 2019"
  136. [[ "${file,,}" == *"server2022"* ]] && desc="Windows Server 2022"
  137. [[ "${file,,}" == *"server2025"* ]] && desc="Windows Server 2025"
  138. [[ "${file,,}" == *"server_2008"* ]] && desc="Windows Server 2008"
  139. [[ "${file,,}" == *"server_2012"* ]] && desc="Windows Server 2012"
  140. [[ "${file,,}" == *"server_2016"* ]] && desc="Windows Server 2016"
  141. [[ "${file,,}" == *"server_2019"* ]] && desc="Windows Server 2019"
  142. [[ "${file,,}" == *"server_2022"* ]] && desc="Windows Server 2022"
  143. [[ "${file,,}" == *"server_2025"* ]] && desc="Windows Server 2025"
  144. echo "$desc"
  145. return 0
  146. }
  147. getVersion() {
  148. local name="$1"
  149. local detected=""
  150. [[ "${name,,}" == *"windows 7"* ]] && detected="win7x64"
  151. [[ "${name,,}" == *"windows 8"* ]] && detected="win81x64"
  152. [[ "${name,,}" == *"windows 11"* ]] && detected="win11x64"
  153. [[ "${name,,}" == *"windows vista"* ]] && detected="winvistax64"
  154. [[ "${name,,}" == *"server 2025"* ]] && detected="win2025-eval"
  155. [[ "${name,,}" == *"server 2022"* ]] && detected="win2022-eval"
  156. [[ "${name,,}" == *"server 2019"* ]] && detected="win2019-eval"
  157. [[ "${name,,}" == *"server 2016"* ]] && detected="win2016-eval"
  158. [[ "${name,,}" == *"server 2012"* ]] && detected="win2012r2-eval"
  159. [[ "${name,,}" == *"server 2008"* ]] && detected="win2008r2"
  160. if [[ "${name,,}" == *"windows 10"* ]]; then
  161. if [[ "${name,,}" == *"ltsc"* ]]; then
  162. detected="win10x64-ltsc"
  163. else
  164. detected="win10x64"
  165. fi
  166. fi
  167. echo "$detected"
  168. return 0
  169. }
  170. replaceXML() {
  171. local dir="$1"
  172. local asset="$2"
  173. local path="$dir/autounattend.xml"
  174. [ -f "$path" ] && cp "$asset" "$path"
  175. path="$dir/Autounattend.xml"
  176. [ -f "$path" ] && cp "$asset" "$path"
  177. path="$dir/AutoUnattend.xml"
  178. [ -f "$path" ] && cp "$asset" "$path"
  179. path="$dir/autounattend.XML"
  180. [ -f "$path" ] && cp "$asset" "$path"
  181. path="$dir/Autounattend.XML"
  182. [ -f "$path" ] && cp "$asset" "$path"
  183. path="$dir/AutoUnattend.XML"
  184. [ -f "$path" ] && cp "$asset" "$path"
  185. path="$dir/AUTOUNATTEND.xml"
  186. [ -f "$path" ] && cp "$asset" "$path"
  187. path="$dir/AUTOUNATTEND.XML"
  188. [ -f "$path" ] && cp "$asset" "$path"
  189. return 0
  190. }
  191. hasDisk() {
  192. [ -b "${DEVICE:-}" ] && return 0
  193. if [ -f "$STORAGE/data.img" ] || [ -f "$STORAGE/data.qcow2" ]; then
  194. return 0
  195. fi
  196. return 1
  197. }
  198. skipInstall() {
  199. if hasDisk && [ -f "$STORAGE/windows.boot" ]; then
  200. return 0
  201. fi
  202. return 1
  203. }
  204. finishInstall() {
  205. local iso="$1"
  206. # Mark ISO as prepared via magic byte
  207. printf '\x16' | dd of="$iso" bs=1 seek=0 count=1 conv=notrunc status=none
  208. rm -f "$STORAGE/windows.boot"
  209. cp /run/version "$STORAGE/windows.ver"
  210. if [[ "${BOOT_MODE,,}" == "windows_legacy" ]]; then
  211. echo "$MACHINE" > "$STORAGE/windows.old"
  212. else
  213. rm -f "$STORAGE/windows.old"
  214. fi
  215. rm -rf "$TMP"
  216. return 0
  217. }
  218. abortInstall() {
  219. local iso="$1"
  220. if [[ "$iso" != "$STORAGE/$BASE" ]]; then
  221. mv -f "$iso" "$STORAGE/$BASE"
  222. fi
  223. finishInstall "$STORAGE/$BASE"
  224. return 0
  225. }
  226. startInstall() {
  227. html "Starting Windows..."
  228. if [ -f "$STORAGE/$CUSTOM" ]; then
  229. EXTERNAL="Y"
  230. BASE="$CUSTOM"
  231. else
  232. CUSTOM=""
  233. if [[ "${VERSION,,}" == "http"* ]]; then
  234. EXTERNAL="Y"
  235. else
  236. EXTERNAL="N"
  237. fi
  238. if [[ "$EXTERNAL" != [Yy1]* ]]; then
  239. BASE="$VERSION.iso"
  240. else
  241. BASE=$(basename "${VERSION%%\?*}")
  242. : "${BASE//+/ }"; printf -v BASE '%b' "${_//%/\\x}"
  243. BASE=$(echo "$BASE" | sed -e 's/[^A-Za-z0-9._-]/_/g')
  244. fi
  245. [[ "${BASE,,}" == "custom."* ]] && BASE="windows.iso"
  246. fi
  247. [ -z "$MANUAL" ] && MANUAL="N"
  248. if [ -f "$STORAGE/$BASE" ]; then
  249. # Check if the ISO was already processed by our script
  250. local magic=""
  251. magic=$(dd if="$STORAGE/$BASE" seek=0 bs=1 count=1 status=none | tr -d '\000')
  252. magic="$(printf '%s' "$magic" | od -A n -t x1 -v | tr -d ' \n')"
  253. if [[ "$magic" == "16" ]]; then
  254. if hasDisk || [[ "$MANUAL" = [Yy1]* ]]; then
  255. return 1
  256. fi
  257. fi
  258. EXTERNAL="Y"
  259. CUSTOM="$BASE"
  260. else
  261. if skipInstall; then
  262. BASE=""
  263. return 1
  264. fi
  265. fi
  266. mkdir -p "$TMP"
  267. if [ ! -f "$STORAGE/$CUSTOM" ]; then
  268. CUSTOM=""
  269. ISO="$TMP/$BASE"
  270. else
  271. ISO="$STORAGE/$CUSTOM"
  272. fi
  273. rm -f "$TMP/$BASE"
  274. return 0
  275. }
  276. getESD() {
  277. local dir="$1"
  278. local file="$2"
  279. local architecture="x64"
  280. local winCatalog size
  281. case "${VERSION,,}" in
  282. win11x64)
  283. winCatalog="https://go.microsoft.com/fwlink?linkid=2156292"
  284. ;;
  285. win10x64)
  286. winCatalog="https://go.microsoft.com/fwlink/?LinkId=841361"
  287. ;;
  288. *)
  289. error "Invalid ESD version specified: $VERSION"
  290. return 1
  291. ;;
  292. esac
  293. local msg="Downloading product information from Microsoft..."
  294. info "$msg" && html "$msg"
  295. rm -rf "$dir"
  296. mkdir -p "$dir"
  297. local wFile="catalog.cab"
  298. { wget "$winCatalog" -O "$dir/$wFile" -q --no-check-certificate; rc=$?; } || :
  299. (( rc != 0 )) && error "Failed to download $winCatalog , reason: $rc" && return 1
  300. cd "$dir"
  301. if ! cabextract "$wFile" > /dev/null; then
  302. cd /run
  303. error "Failed to extract CAB file!" && return 1
  304. fi
  305. cd /run
  306. if [ ! -f "$dir/products.xml" ]; then
  307. error "Failed to find products.xml!" && return 1
  308. fi
  309. local esdLang="en-us"
  310. local editionName="Professional"
  311. local edQuery='//File[Architecture="'${architecture}'"][Edition="'${editionName}'"]'
  312. echo -e '<Catalog>' > "${dir}/products_filter.xml"
  313. xmllint --nonet --xpath "${edQuery}" "${dir}/products.xml" >> "${dir}/products_filter.xml" 2>/dev/null
  314. echo -e '</Catalog>'>> "${dir}/products_filter.xml"
  315. xmllint --nonet --xpath '//File[LanguageCode="'${esdLang}'"]' "${dir}/products_filter.xml" >"${dir}/esd_edition.xml"
  316. size=$(stat -c%s "${dir}/esd_edition.xml")
  317. if ((size<20)); then
  318. error "Failed to find Windows product!" && return 1
  319. fi
  320. ESD_URL=$(xmllint --nonet --xpath '//FilePath' "${dir}/esd_edition.xml" | sed -E -e 's/<[\/]?FilePath>//g')
  321. if [ -z "$ESD_URL" ]; then
  322. error "Failed to find ESD url!" && return 1
  323. fi
  324. rm -rf "$dir"
  325. return 0
  326. }
  327. downloadImage() {
  328. local iso="$1"
  329. local url="$2"
  330. local file="$iso"
  331. local desc rc progress
  332. rm -f "$iso"
  333. if [[ "$EXTERNAL" != [Yy1]* ]]; then
  334. file="$iso.PART"
  335. desc=$(printVersion "$VERSION")
  336. [ -z "$desc" ] && desc="Windows"
  337. else
  338. desc=$(getName "$BASE")
  339. [ -z "$desc" ] && desc="$BASE"
  340. fi
  341. local msg="Downloading $desc..."
  342. info "$msg" && html "$msg"
  343. /run/progress.sh "$file" "Downloading $desc ([P])..." &
  344. if [[ "$EXTERNAL" != [Yy1]* ]]; then
  345. cd "$TMP"
  346. { /run/mido.sh "$url"; rc=$?; } || :
  347. cd /run
  348. fKill "progress.sh"
  349. if (( rc == 0 )); then
  350. [ ! -f "$iso" ] && return 1
  351. html "Download finished successfully..."
  352. return 0
  353. fi
  354. if [[ "$VERSION" != "win10x64"* ]] && [[ "$VERSION" != "win11x64" ]]; then
  355. return 1
  356. fi
  357. info "Failed to download $desc using Mido, will try a different method now..."
  358. ISO="$TMP/$VERSION.esd"
  359. iso="$ISO"
  360. file="$ISO"
  361. rm -f "$iso"
  362. if ! getESD "$TMP/esd" "$iso"; then
  363. return 1
  364. fi
  365. url="$ESD_URL"
  366. msg="Downloading $desc..."
  367. info "$msg" && html "$msg"
  368. /run/progress.sh "$iso" "Downloading $desc ([P])..." &
  369. fi
  370. # Check if running with interactive TTY or redirected to docker log
  371. if [ -t 1 ]; then
  372. progress="--progress=bar:noscroll"
  373. else
  374. progress="--progress=dot:giga"
  375. fi
  376. { wget "$url" -O "$iso" -q --no-check-certificate --show-progress "$progress"; rc=$?; } || :
  377. fKill "progress.sh"
  378. (( rc != 0 )) && error "Failed to download $url , reason: $rc" && exit 60
  379. [ ! -f "$iso" ] && return 1
  380. html "Download finished successfully..."
  381. return 0
  382. }
  383. extractESD() {
  384. local iso="$1"
  385. local dir="$2"
  386. local size size_gb space space_gb desc
  387. desc=$(printVersion "$VERSION")
  388. local msg="Extracting $desc bootdisk..."
  389. info "$msg" && html "$msg"
  390. size=16106127360
  391. size_gb=$(( (size + 1073741823)/1073741824 ))
  392. space=$(df --output=avail -B 1 "$TMP" | tail -n 1)
  393. space_gb=$(( (space + 1073741823)/1073741824 ))
  394. if ((size<10000000)); then
  395. error "Invalid ESD file: Size is smaller than 10 MB" && exit 62
  396. fi
  397. if (( size > space )); then
  398. error "Not enough free space in $STORAGE, have $space_gb GB available but need at least $size_gb GB." && exit 63
  399. fi
  400. rm -rf "$dir"
  401. mkdir -p "$dir"
  402. local esdImageCount
  403. esdImageCount=$(wimlib-imagex info "${iso}" | awk '/Image Count:/ {print $3}')
  404. wimlib-imagex apply "$iso" 1 "${dir}" --quiet 2>/dev/null || {
  405. retVal=$?
  406. error "Extracting bootdisk failed" && return $retVal
  407. }
  408. local bootWimFile="${dir}/sources/boot.wim"
  409. local installWimFile="${dir}/sources/install.wim"
  410. local msg="Extracting $desc environment..."
  411. info "$msg" && html "$msg"
  412. wimlib-imagex export "${iso}" 2 "${bootWimFile}" --compress=LZX --chunk-size 32K --quiet || {
  413. retVal=$?
  414. error "Adding WinPE failed" && return ${retVal}
  415. }
  416. local msg="Extracting $desc setup..."
  417. info "$msg" && html "$msg"
  418. wimlib-imagex export "${iso}" 3 "$bootWimFile" --compress=LZX --chunk-size 32K --boot --quiet || {
  419. retVal=$?
  420. error "Adding Windows Setup failed" && return ${retVal}
  421. }
  422. local msg="Extracting $desc image..."
  423. info "$msg" && html "$msg"
  424. local edition imageIndex imageEdition
  425. case "${VERSION,,}" in
  426. win11x64)
  427. edition="11 pro"
  428. ;;
  429. win10x64)
  430. edition="10 pro"
  431. ;;
  432. *)
  433. error "Invalid version specified: $VERSION"
  434. return 1
  435. ;;
  436. esac
  437. for (( imageIndex=4; imageIndex<=esdImageCount; imageIndex++ )); do
  438. imageEdition=$(wimlib-imagex info "${iso}" ${imageIndex} | grep '^Description:' | sed 's/Description:[ \t]*//')
  439. [[ "${imageEdition,,}" != *"$edition"* ]] && continue
  440. wimlib-imagex export "${iso}" ${imageIndex} "${installWimFile}" --compress=LZMS --chunk-size 128K --quiet || {
  441. retVal=$?
  442. error "Addition of ${imageIndex} to the image failed" && return $retVal
  443. }
  444. return 0
  445. done
  446. error "Failed to find product in install.wim!"
  447. return 1
  448. }
  449. extractImage() {
  450. local iso="$1"
  451. local dir="$2"
  452. local desc="downloaded ISO"
  453. local size size_gb space space_gb
  454. if [[ "${iso,,}" == *".esd" ]]; then
  455. if ! extractESD "$iso" "$dir"; then
  456. error "Failed to extract ESD file!"
  457. exit 67
  458. fi
  459. return 0
  460. fi
  461. if [[ "$EXTERNAL" != [Yy1]* ]] && [ -z "$CUSTOM" ]; then
  462. desc=$(printVersion "$VERSION")
  463. [ -z "$desc" ] && desc="downloaded ISO"
  464. fi
  465. local msg="Extracting $desc image..."
  466. [ -n "$CUSTOM" ] && msg="Extracting local ISO image..."
  467. info "$msg" && html "$msg"
  468. size=$(stat -c%s "$iso")
  469. size_gb=$(( (size + 1073741823)/1073741824 ))
  470. space=$(df --output=avail -B 1 "$TMP" | tail -n 1)
  471. space_gb=$(( (space + 1073741823)/1073741824 ))
  472. if ((size<10000000)); then
  473. error "Invalid ISO file: Size is smaller than 10 MB" && exit 62
  474. fi
  475. if (( size > space )); then
  476. error "Not enough free space in $STORAGE, have $space_gb GB available but need at least $size_gb GB." && exit 63
  477. fi
  478. rm -rf "$dir"
  479. if ! 7z x "$iso" -o"$dir" > /dev/null; then
  480. error "Failed to extract ISO file!"
  481. exit 66
  482. fi
  483. return 0
  484. }
  485. detectImage() {
  486. XML=""
  487. local dir="$1"
  488. if [ -n "$CUSTOM" ]; then
  489. DETECTED=""
  490. else
  491. if [ -z "$DETECTED" ] && [[ "$EXTERNAL" != [Yy1]* ]]; then
  492. DETECTED="$VERSION"
  493. fi
  494. fi
  495. if [ -n "$DETECTED" ]; then
  496. if [ -f "/run/assets/$DETECTED.xml" ]; then
  497. [[ "$MANUAL" != [Yy1]* ]] && XML="$DETECTED.xml"
  498. return 0
  499. fi
  500. if [[ "${DETECTED,,}" != "winxp"* ]]; then
  501. local dsc
  502. dsc=$(printVersion "$DETECTED")
  503. [ -z "$dsc" ] && dsc="$DETECTED"
  504. warn "got $dsc, but no matching XML file exists, $FB."
  505. fi
  506. return 0
  507. fi
  508. info "Detecting Windows version from ISO image..."
  509. if [ -f "$dir/WIN51" ] || [ -f "$dir/SETUPXP.HTM" ]; then
  510. DETECTED="winxpx86"
  511. info "Detected: Windows XP"
  512. return 0
  513. fi
  514. local tag result name name2 desc
  515. local loc="$dir/sources/install.wim"
  516. [ ! -f "$loc" ] && loc="$dir/sources/install.esd"
  517. if [ ! -f "$loc" ]; then
  518. warn "failed to locate 'install.wim' or 'install.esd' in ISO image, $FB"
  519. BOOT_MODE="windows_legacy"
  520. return 1
  521. fi
  522. tag="DISPLAYNAME"
  523. result=$(wimlib-imagex info -xml "$loc" | tr -d '\000')
  524. name=$(sed -n "/$tag/{s/.*<$tag>\(.*\)<\/$tag>.*/\1/;p}" <<< "$result")
  525. DETECTED=$(getVersion "$name")
  526. if [ -z "$DETECTED" ]; then
  527. tag="PRODUCTNAME"
  528. name2=$(sed -n "/$tag/{s/.*<$tag>\(.*\)<\/$tag>.*/\1/;p}" <<< "$result")
  529. [ -z "$name" ] && name="$name2"
  530. DETECTED=$(getVersion "$name2")
  531. fi
  532. if [ -z "$DETECTED" ]; then
  533. warn "failed to determine Windows version from string '$name', $FB"
  534. return 0
  535. fi
  536. desc=$(printVersion "$DETECTED")
  537. [ -z "$desc" ] && desc="$DETECTED"
  538. if [ -f "/run/assets/$DETECTED.xml" ]; then
  539. [[ "$MANUAL" != [Yy1]* ]] && XML="$DETECTED.xml"
  540. info "Detected: $desc"
  541. else
  542. warn "detected $desc, but no matching XML file exists, $FB."
  543. fi
  544. return 0
  545. }
  546. prepareXP() {
  547. local iso="$1"
  548. local dir="$2"
  549. local arch="x86"
  550. local target="$dir/I386"
  551. if [ -d "$dir/AMD64" ]; then
  552. arch="amd64"
  553. target="$dir/AMD64"
  554. fi
  555. MACHINE="pc-q35-2.10"
  556. BOOT_MODE="windows_legacy"
  557. ETFS="[BOOT]/Boot-NoEmul.img"
  558. [[ "$MANUAL" == [Yy1]* ]] && return 0
  559. local drivers="$TMP/drivers"
  560. rm -rf "$drivers"
  561. if ! 7z x /run/drivers.iso -o"$drivers" > /dev/null; then
  562. error "Failed to extract driver ISO file!"
  563. exit 66
  564. fi
  565. cp "$drivers/viostor/xp/$arch/viostor.sys" "$target"
  566. mkdir -p "$dir/\$OEM\$/\$1/Drivers/viostor"
  567. cp "$drivers/viostor/xp/$arch/viostor.cat" "$dir/\$OEM\$/\$1/Drivers/viostor"
  568. cp "$drivers/viostor/xp/$arch/viostor.inf" "$dir/\$OEM\$/\$1/Drivers/viostor"
  569. cp "$drivers/viostor/xp/$arch/viostor.sys" "$dir/\$OEM\$/\$1/Drivers/viostor"
  570. mkdir -p "$dir/\$OEM\$/\$1/Drivers/NetKVM"
  571. cp "$drivers/NetKVM/xp/$arch/netkvm.cat" "$dir/\$OEM\$/\$1/Drivers/NetKVM"
  572. cp "$drivers/NetKVM/xp/$arch/netkvm.inf" "$dir/\$OEM\$/\$1/Drivers/NetKVM"
  573. cp "$drivers/NetKVM/xp/$arch/netkvm.sys" "$dir/\$OEM\$/\$1/Drivers/NetKVM"
  574. sed -i '/^\[SCSI.Load\]/s/$/\nviostor=viostor.sys,4/' "$target/TXTSETUP.SIF"
  575. sed -i '/^\[SourceDisksFiles.'"$arch"'\]/s/$/\nviostor.sys=1,,,,,,4_,4,1,,,1,4/' "$target/TXTSETUP.SIF"
  576. sed -i '/^\[SCSI\]/s/$/\nviostor=\"Red Hat VirtIO SCSI Disk Device\"/' "$target/TXTSETUP.SIF"
  577. sed -i '/^\[HardwareIdsDatabase\]/s/$/\nPCI\\VEN_1AF4\&DEV_1001\&SUBSYS_00000000=\"viostor\"/' "$target/TXTSETUP.SIF"
  578. sed -i '/^\[HardwareIdsDatabase\]/s/$/\nPCI\\VEN_1AF4\&DEV_1001\&SUBSYS_00020000=\"viostor\"/' "$target/TXTSETUP.SIF"
  579. sed -i '/^\[HardwareIdsDatabase\]/s/$/\nPCI\\VEN_1AF4\&DEV_1001\&SUBSYS_00021AF4=\"viostor\"/' "$target/TXTSETUP.SIF"
  580. sed -i '/^\[HardwareIdsDatabase\]/s/$/\nPCI\\VEN_1AF4\&DEV_1001\&SUBSYS_00000000=\"viostor\"/' "$target/TXTSETUP.SIF"
  581. mkdir -p "$dir/\$OEM\$/\$1/Drivers/sata"
  582. cp -a "$drivers/sata/xp/$arch/." "$dir/\$OEM\$/\$1/Drivers/sata"
  583. cp -a "$drivers/sata/xp/$arch/." "$target"
  584. sed -i '/^\[SCSI.Load\]/s/$/\niaStor=iaStor.sys,4/' "$target/TXTSETUP.SIF"
  585. sed -i '/^\[FileFlags\]/s/$/\niaStor.sys = 16/' "$target/TXTSETUP.SIF"
  586. sed -i '/^\[SourceDisksFiles.'"$arch"'\]/s/$/\niaStor.cat = 1,,,,,,,1,0,0/' "$target/TXTSETUP.SIF"
  587. sed -i '/^\[SourceDisksFiles.'"$arch"'\]/s/$/\niaStor.inf = 1,,,,,,,1,0,0/' "$target/TXTSETUP.SIF"
  588. sed -i '/^\[SourceDisksFiles.'"$arch"'\]/s/$/\niaStor.sys = 1,,,,,,4_,4,1,,,1,4/' "$target/TXTSETUP.SIF"
  589. sed -i '/^\[SourceDisksFiles.'"$arch"'\]/s/$/\niaStor.sys = 1,,,,,,,1,0,0/' "$target/TXTSETUP.SIF"
  590. sed -i '/^\[SourceDisksFiles.'"$arch"'\]/s/$/\niaahci.cat = 1,,,,,,,1,0,0/' "$target/TXTSETUP.SIF"
  591. sed -i '/^\[SourceDisksFiles.'"$arch"'\]/s/$/\niaAHCI.inf = 1,,,,,,,1,0,0/' "$target/TXTSETUP.SIF"
  592. sed -i '/^\[SCSI\]/s/$/\niaStor=\"Intel\(R\) SATA RAID\/AHCI Controller\"/' "$target/TXTSETUP.SIF"
  593. sed -i '/^\[HardwareIdsDatabase\]/s/$/\nPCI\\VEN_8086\&DEV_2922\&CC_0106=\"iaStor\"/' "$target/TXTSETUP.SIF"
  594. rm -f "$target/winnt.sif"
  595. rm -f "$target/Winnt.sif"
  596. rm -f "$target/winnt.SIF"
  597. rm -f "$target/WinNT.sif"
  598. rm -f "$target/WINNT.sif"
  599. rm -f "$target/WINNT.SIF"
  600. local key="M6TF9-8XQ2M-YQK9F-7TBB2-XGG88"
  601. [[ "${arch,,}" == "amd64" ]] && key="B66VY-4D94T-TPPD4-43F72-8X4FY"
  602. local sif="$target/WINNT.SIF"
  603. { echo "[Data]"
  604. echo "AutoPartition=1"
  605. echo "MsDosInitiated=\"0\""
  606. echo "UnattendedInstall=\"Yes\""
  607. echo "AutomaticUpdates=\"Yes\""
  608. echo ""
  609. echo "[Unattended]"
  610. echo "UnattendSwitch=Yes"
  611. echo "UnattendMode=FullUnattended"
  612. echo "FileSystem=NTFS"
  613. echo "OemSkipEula=Yes"
  614. echo "OemPreinstall=Yes"
  615. echo "Repartition=Yes"
  616. echo "WaitForReboot=\"No\""
  617. echo "DriverSigningPolicy=\"Ignore\""
  618. echo "NonDriverSigningPolicy=\"Ignore\""
  619. echo "OemPnPDriversPath=\"Drivers\viostor;Drivers\NetKVM;Drivers\sata\""
  620. echo "NoWaitAfterTextMode=1"
  621. echo "NoWaitAfterGUIMode=1"
  622. echo "FileSystem-ConvertNTFS"
  623. echo "ExtendOemPartition=0"
  624. echo "Hibernation=\"No\""
  625. echo ""
  626. echo "[GuiUnattended]"
  627. echo "OEMSkipRegional=1"
  628. echo "OemSkipWelcome=1"
  629. echo "AdminPassword=*"
  630. echo "TimeZone=0"
  631. echo "AutoLogon=Yes"
  632. echo "AutoLogonCount=65432"
  633. echo ""
  634. echo "[UserData]"
  635. echo "FullName=\"Docker\""
  636. echo "ComputerName=\"*\""
  637. echo "OrgName=\"Windows for Docker\""
  638. echo "ProductKey=$key"
  639. echo ""
  640. echo "[Identification]"
  641. echo "JoinWorkgroup = WORKGROUP"
  642. echo ""
  643. echo "[Networking]"
  644. echo "InstallDefaultComponents=Yes"
  645. echo ""
  646. echo "[URL]"
  647. echo "Home_Page = http://www.google.com"
  648. echo "Search_Page = http://www.google.com/ie_rsearch.html"
  649. echo "AutoConfig = 0"
  650. echo ""
  651. echo "[RegionalSettings]"
  652. echo "Language=00000409"
  653. echo ""
  654. echo "[TerminalServices]"
  655. echo "AllowConnections=1"
  656. } > "$sif"
  657. return 0
  658. }
  659. prepareLegacy() {
  660. local iso="$1"
  661. local dir="$2"
  662. ETFS="boot.img"
  663. BOOT_MODE="windows_legacy"
  664. local len offset
  665. len=$(isoinfo -d -i "$iso" | grep "Nsect " | grep -o "[^ ]*$")
  666. offset=$(isoinfo -d -i "$iso" | grep "Bootoff " | grep -o "[^ ]*$")
  667. if ! dd "if=$iso" "of=$dir/$ETFS" bs=2048 "count=$len" "skip=$offset" status=none; then
  668. error "Failed to extract boot image from ISO!"
  669. exit 67
  670. fi
  671. return 0
  672. }
  673. prepareImage() {
  674. local iso="$1"
  675. local dir="$2"
  676. if [[ "${BOOT_MODE,,}" == "windows" ]]; then
  677. if [[ "${DETECTED,,}" != "winxp"* ]] && [[ "${DETECTED,,}" != "win2008"* ]]; then
  678. if [[ "${DETECTED,,}" != "winvista"* ]] && [[ "${DETECTED,,}" != "win7"* ]]; then
  679. if [ -f "$dir/$ETFS" ] && [ -f "$dir/$EFISYS" ]; then
  680. return 0
  681. fi
  682. if [ ! -f "$dir/$ETFS" ]; then
  683. warn "failed to locate file 'etfsboot.com' in ISO image, falling back to legacy boot!"
  684. else
  685. warn "failed to locate file 'efisys_noprompt.bin' in ISO image, falling back to legacy boot!"
  686. fi
  687. fi
  688. fi
  689. fi
  690. if [[ "${DETECTED,,}" == "winxp"* ]]; then
  691. if ! prepareXP "$iso" "$dir"; then
  692. error "Failed to prepare Windows XP ISO!"
  693. return 1
  694. fi
  695. else
  696. if ! prepareLegacy "$iso" "$dir"; then
  697. error "Failed to prepare Windows ISO!"
  698. return 1
  699. fi
  700. fi
  701. return 0
  702. }
  703. updateImage() {
  704. local iso="$1"
  705. local dir="$2"
  706. local asset="/run/assets/$3"
  707. local index result
  708. [ ! -f "$asset" ] && return 0
  709. replaceXML "$dir" "$asset"
  710. local loc="$dir/sources/boot.wim"
  711. [ ! -f "$loc" ] && loc="$dir/sources/boot.esd"
  712. if [ ! -f "$loc" ]; then
  713. warn "failed to locate 'boot.wim' or 'boot.esd' in ISO image, $FB"
  714. BOOT_MODE="windows_legacy"
  715. return 1
  716. fi
  717. info "Adding XML file for automatic installation..."
  718. index="1"
  719. result=$(wimlib-imagex info -xml "$loc" | tr -d '\000')
  720. if [[ "${result^^}" == *"<IMAGE INDEX=\"2\">"* ]]; then
  721. index="2"
  722. fi
  723. if ! wimlib-imagex update "$loc" "$index" --command "add $asset /autounattend.xml" > /dev/null; then
  724. warn "failed to add XML to ISO image, $FB"
  725. return 1
  726. fi
  727. return 0
  728. }
  729. buildImage() {
  730. local dir="$1"
  731. local cat="BOOT.CAT"
  732. local label="${BASE%.*}"
  733. local log="/run/shm/iso.log"
  734. local size size_gb space space_gb desc
  735. label="${label::30}"
  736. local out="$TMP/$label.tmp"
  737. rm -f "$out"
  738. desc=$(printVersion "$DETECTED")
  739. [ -z "$desc" ] && desc="ISO"
  740. local msg="Building $desc image..."
  741. info "$msg" && html "$msg"
  742. size=$(du -h -b --max-depth=0 "$dir" | cut -f1)
  743. size_gb=$(( (size + 1073741823)/1073741824 ))
  744. space=$(df --output=avail -B 1 "$TMP" | tail -n 1)
  745. space_gb=$(( (space + 1073741823)/1073741824 ))
  746. if (( size > space )); then
  747. error "Not enough free space in $STORAGE, have $space_gb GB available but need at least $size_gb GB."
  748. return 1
  749. fi
  750. if [[ "${BOOT_MODE,,}" != "windows_legacy" ]]; then
  751. if ! genisoimage -o "$out" -b "$ETFS" -no-emul-boot -c "$cat" -iso-level 4 -J -l -D -N -joliet-long -relaxed-filenames -V "$label" \
  752. -udf -boot-info-table -eltorito-alt-boot -eltorito-boot "$EFISYS" -no-emul-boot -allow-limited-size -quiet "$dir" 2> "$log"; then
  753. [ -f "$log" ] && echo "$(<"$log")"
  754. return 1
  755. fi
  756. else
  757. if [[ "${DETECTED,,}" != "winxp"* ]]; then
  758. if ! genisoimage -o "$out" -b "$ETFS" -no-emul-boot -c "$cat" -iso-level 2 -J -l -D -N -joliet-long -relaxed-filenames -V "$label" \
  759. -udf -allow-limited-size -quiet "$dir" 2> "$log"; then
  760. [ -f "$log" ] && echo "$(<"$log")"
  761. return 1
  762. fi
  763. else
  764. if ! 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 \
  765. -relaxed-filenames -V "$label" -quiet "$dir" 2> "$log"; then
  766. [ -f "$log" ] && echo "$(<"$log")"
  767. return 1
  768. fi
  769. fi
  770. fi
  771. local error=""
  772. local hide="Warning: creating filesystem that does not conform to ISO-9660."
  773. [ -f "$log" ] && error="$(<"$log")"
  774. [[ "$error" != "$hide" ]] && echo "$error"
  775. if [ -f "$STORAGE/$BASE" ]; then
  776. error "File $STORAGE/$BASE does already exist?!"
  777. return 1
  778. fi
  779. mv "$out" "$STORAGE/$BASE"
  780. return 0
  781. }
  782. ######################################
  783. if ! startInstall; then
  784. if [ -f "$STORAGE/windows.old" ]; then
  785. MACHINE=$(<"$STORAGE/windows.old")
  786. [ -z "$MACHINE" ] && MACHINE="q35"
  787. BOOT_MODE="windows_legacy"
  788. fi
  789. rm -rf "$TMP"
  790. return 0
  791. fi
  792. if [ ! -f "$ISO" ]; then
  793. if ! downloadImage "$ISO" "$VERSION"; then
  794. error "Failed to download $VERSION"
  795. exit 61
  796. fi
  797. fi
  798. if ! extractImage "$ISO" "$DIR"; then
  799. abortInstall "$ISO"
  800. return 0
  801. fi
  802. if ! detectImage "$DIR"; then
  803. abortInstall "$ISO"
  804. return 0
  805. fi
  806. if ! prepareImage "$ISO" "$DIR"; then
  807. abortInstall "$ISO"
  808. return 0
  809. fi
  810. if ! updateImage "$ISO" "$DIR" "$XML"; then
  811. abortInstall "$ISO"
  812. return 0
  813. fi
  814. rm -f "$ISO"
  815. if ! buildImage "$DIR"; then
  816. error "Failed to build image!"
  817. exit 65
  818. fi
  819. finishInstall "$STORAGE/$BASE"
  820. html "Successfully prepared image for installation..."
  821. return 0