install.sh 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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=99999"
  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"
  642. echo ""
  643. echo "[Networking]"
  644. echo "InstallDefaultComponents=Yes"
  645. echo ""
  646. echo "[RegionalSettings]"
  647. echo "Language=00000409"
  648. echo ""
  649. echo "[TerminalServices]"
  650. echo "AllowConnections=1"
  651. } > "$sif"
  652. return 0
  653. }
  654. prepareLegacy() {
  655. local iso="$1"
  656. local dir="$2"
  657. ETFS="boot.img"
  658. BOOT_MODE="windows_legacy"
  659. local len offset
  660. len=$(isoinfo -d -i "$iso" | grep "Nsect " | grep -o "[^ ]*$")
  661. offset=$(isoinfo -d -i "$iso" | grep "Bootoff " | grep -o "[^ ]*$")
  662. if ! dd "if=$iso" "of=$dir/$ETFS" bs=2048 "count=$len" "skip=$offset" status=none; then
  663. error "Failed to extract boot image from ISO!"
  664. exit 67
  665. fi
  666. return 0
  667. }
  668. prepareImage() {
  669. local iso="$1"
  670. local dir="$2"
  671. if [[ "${BOOT_MODE,,}" == "windows" ]]; then
  672. if [[ "${DETECTED,,}" != "winxp"* ]] && [[ "${DETECTED,,}" != "win2008"* ]]; then
  673. if [[ "${DETECTED,,}" != "winvista"* ]] && [[ "${DETECTED,,}" != "win7"* ]]; then
  674. if [ -f "$dir/$ETFS" ] && [ -f "$dir/$EFISYS" ]; then
  675. return 0
  676. fi
  677. if [ ! -f "$dir/$ETFS" ]; then
  678. warn "failed to locate file 'etfsboot.com' in ISO image, falling back to legacy boot!"
  679. else
  680. warn "failed to locate file 'efisys_noprompt.bin' in ISO image, falling back to legacy boot!"
  681. fi
  682. fi
  683. fi
  684. fi
  685. if [[ "${DETECTED,,}" == "winxp"* ]]; then
  686. if ! prepareXP "$iso" "$dir"; then
  687. error "Failed to prepare Windows XP ISO!"
  688. return 1
  689. fi
  690. else
  691. if ! prepareLegacy "$iso" "$dir"; then
  692. error "Failed to prepare Windows ISO!"
  693. return 1
  694. fi
  695. fi
  696. return 0
  697. }
  698. updateImage() {
  699. local iso="$1"
  700. local dir="$2"
  701. local asset="/run/assets/$3"
  702. local index result
  703. [ ! -f "$asset" ] && return 0
  704. replaceXML "$dir" "$asset"
  705. local loc="$dir/sources/boot.wim"
  706. [ ! -f "$loc" ] && loc="$dir/sources/boot.esd"
  707. if [ ! -f "$loc" ]; then
  708. warn "failed to locate 'boot.wim' or 'boot.esd' in ISO image, $FB"
  709. BOOT_MODE="windows_legacy"
  710. return 1
  711. fi
  712. info "Adding XML file for automatic installation..."
  713. index="1"
  714. result=$(wimlib-imagex info -xml "$loc" | tr -d '\000')
  715. if [[ "${result^^}" == *"<IMAGE INDEX=\"2\">"* ]]; then
  716. index="2"
  717. fi
  718. if ! wimlib-imagex update "$loc" "$index" --command "add $asset /autounattend.xml" > /dev/null; then
  719. warn "failed to add XML to ISO image, $FB"
  720. return 1
  721. fi
  722. return 0
  723. }
  724. buildImage() {
  725. local dir="$1"
  726. local cat="BOOT.CAT"
  727. local label="${BASE%.*}"
  728. local log="/run/shm/iso.log"
  729. local size size_gb space space_gb desc
  730. label="${label::30}"
  731. local out="$TMP/$label.tmp"
  732. rm -f "$out"
  733. desc=$(printVersion "$DETECTED")
  734. [ -z "$desc" ] && desc="ISO"
  735. local msg="Building $desc image..."
  736. info "$msg" && html "$msg"
  737. size=$(du -h -b --max-depth=0 "$dir" | cut -f1)
  738. size_gb=$(( (size + 1073741823)/1073741824 ))
  739. space=$(df --output=avail -B 1 "$TMP" | tail -n 1)
  740. space_gb=$(( (space + 1073741823)/1073741824 ))
  741. if (( size > space )); then
  742. error "Not enough free space in $STORAGE, have $space_gb GB available but need at least $size_gb GB."
  743. return 1
  744. fi
  745. if [[ "${BOOT_MODE,,}" != "windows_legacy" ]]; then
  746. if ! genisoimage -o "$out" -b "$ETFS" -no-emul-boot -c "$cat" -iso-level 4 -J -l -D -N -joliet-long -relaxed-filenames -V "$label" \
  747. -udf -boot-info-table -eltorito-alt-boot -eltorito-boot "$EFISYS" -no-emul-boot -allow-limited-size -quiet "$dir" 2> "$log"; then
  748. [ -f "$log" ] && echo "$(<"$log")"
  749. return 1
  750. fi
  751. else
  752. if [[ "${DETECTED,,}" != "winxp"* ]]; then
  753. if ! genisoimage -o "$out" -b "$ETFS" -no-emul-boot -c "$cat" -iso-level 2 -J -l -D -N -joliet-long -relaxed-filenames -V "$label" \
  754. -udf -allow-limited-size -quiet "$dir" 2> "$log"; then
  755. [ -f "$log" ] && echo "$(<"$log")"
  756. return 1
  757. fi
  758. else
  759. 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 \
  760. -relaxed-filenames -V "$label" -quiet "$dir" 2> "$log"; then
  761. [ -f "$log" ] && echo "$(<"$log")"
  762. return 1
  763. fi
  764. fi
  765. fi
  766. local error=""
  767. local hide="Warning: creating filesystem that does not conform to ISO-9660."
  768. [ -f "$log" ] && error="$(<"$log")"
  769. [[ "$error" != "$hide" ]] && echo "$error"
  770. if [ -f "$STORAGE/$BASE" ]; then
  771. error "File $STORAGE/$BASE does already exist?!"
  772. return 1
  773. fi
  774. mv "$out" "$STORAGE/$BASE"
  775. return 0
  776. }
  777. ######################################
  778. if ! startInstall; then
  779. if [ -f "$STORAGE/windows.old" ]; then
  780. MACHINE=$(<"$STORAGE/windows.old")
  781. [ -z "$MACHINE" ] && MACHINE="q35"
  782. BOOT_MODE="windows_legacy"
  783. fi
  784. rm -rf "$TMP"
  785. return 0
  786. fi
  787. if [ ! -f "$ISO" ]; then
  788. if ! downloadImage "$ISO" "$VERSION"; then
  789. error "Failed to download $VERSION"
  790. exit 61
  791. fi
  792. fi
  793. if ! extractImage "$ISO" "$DIR"; then
  794. abortInstall "$ISO"
  795. return 0
  796. fi
  797. if ! detectImage "$DIR"; then
  798. abortInstall "$ISO"
  799. return 0
  800. fi
  801. if ! prepareImage "$ISO" "$DIR"; then
  802. abortInstall "$ISO"
  803. return 0
  804. fi
  805. if ! updateImage "$ISO" "$DIR" "$XML"; then
  806. abortInstall "$ISO"
  807. return 0
  808. fi
  809. rm -f "$ISO"
  810. if ! buildImage "$DIR"; then
  811. error "Failed to build image!"
  812. exit 65
  813. fi
  814. finishInstall "$STORAGE/$BASE"
  815. html "Successfully prepared image for installation..."
  816. return 0