debi.sh 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. #!/bin/sh
  2. # shellcheck shell=dash
  3. set -eu
  4. err() {
  5. printf "\nError: %s.\n" "$1" 1>&2
  6. exit 1
  7. }
  8. warn() {
  9. printf "\nWarning: %s.\nContinuing with the default...\n" "$1" 1>&2
  10. sleep 5
  11. }
  12. command_exists() {
  13. command -v "$1" > /dev/null 2>&1
  14. }
  15. # Sets variable:
  16. in_target_script=
  17. in_target() {
  18. local command=
  19. for argument in "$@"; do
  20. command="$command $argument"
  21. done
  22. if [ -n "$command" ]; then
  23. [ -z "$in_target_script" ] && in_target_script='true'
  24. in_target_script="$in_target_script;$command"
  25. fi
  26. }
  27. in_target_backup() {
  28. in_target "if [ ! -e \"$1.backup\" ]; then cp \"$1\" \"$1.backup\"; fi"
  29. }
  30. configure_sshd() {
  31. # !isset($sshd_config_backup)
  32. [ -z "${sshd_config_backup+1s}" ] && in_target_backup /etc/ssh/sshd_config
  33. sshd_config_backup=
  34. in_target sed -Ei \""s/^#?$1 .+/$1 $2/"\" /etc/ssh/sshd_config
  35. }
  36. prompt_password() {
  37. local prompt=
  38. if [ $# -gt 0 ]; then
  39. prompt=$1
  40. elif [ "$username" = root ]; then
  41. prompt="Choose a password for the root user: "
  42. else
  43. prompt="Choose a password for user $username: "
  44. fi
  45. stty -echo
  46. trap 'stty echo' EXIT
  47. while [ -z "$password" ]; do
  48. echo -n "$prompt" > /dev/tty
  49. read -r password < /dev/tty
  50. echo > /dev/tty
  51. done
  52. stty echo
  53. trap - EXIT
  54. }
  55. download() {
  56. # Set "$http/https/ftp_proxy" with "$mirror_proxy"
  57. # only when none of those have ever been set
  58. [ -n "$mirror_proxy" ] &&
  59. [ -z "${http_proxy+1s}" ] &&
  60. [ -z "${https_proxy+1s}" ] &&
  61. [ -z "${ftp_proxy+1s}" ] &&
  62. export http_proxy="$mirror_proxy" &&
  63. export https_proxy="$mirror_proxy" &&
  64. export ftp_proxy="$mirror_proxy"
  65. if command_exists wget; then
  66. wget -O "$2" "$1"
  67. elif command_exists curl; then
  68. curl -fL "$1" -o "$2"
  69. elif command_exists busybox && busybox wget --help > /dev/null 2>&1; then
  70. busybox wget -O "$2" "$1"
  71. else
  72. err 'Cannot find "wget", "curl" or "busybox wget" to download files'
  73. fi
  74. }
  75. # Set "$mirror_proxy" with "$http/https/ftp_proxy"
  76. # only when it is empty and one of those is not empty
  77. set_mirror_proxy() {
  78. [ -n "$mirror_proxy" ] && return
  79. case $mirror_protocol in
  80. http)
  81. if [ -n "${http_proxy+1s}" ]; then mirror_proxy="$http_proxy"; fi
  82. ;;
  83. https)
  84. if [ -n "${https_proxy+1s}" ]; then mirror_proxy="$https_proxy"; fi
  85. ;;
  86. ftp)
  87. if [ -n "${ftp_proxy+1s}" ]; then mirror_proxy="$ftp_proxy"; fi
  88. ;;
  89. *)
  90. err "Unsupported protocol: $mirror_protocol"
  91. esac
  92. }
  93. # Determines the security repository path format based on Debian suite
  94. # - Buster (Debian 10): Uses old format "buster/updates"
  95. # - Bullseye onwards and testing: Uses new format "suite-security"
  96. # - Sid/unstable: No security repository (fixes go directly to unstable)
  97. set_security_archive() {
  98. case $suite in
  99. buster)
  100. security_archive="$suite/updates"
  101. ;;
  102. bullseye|oldoldstable|bookworm|oldstable|trixie|stable|forky|testing)
  103. security_archive="$suite-security"
  104. ;;
  105. sid|unstable)
  106. security_archive=''
  107. ;;
  108. *)
  109. err "Unsupported suite: $suite"
  110. esac
  111. }
  112. # Determines whether to use daily or stable installer builds
  113. # - Stable/oldstable releases: Use stable installer builds (thoroughly tested)
  114. # - Testing/unstable: Use daily installer builds (latest changes, may be less stable)
  115. # Daily builds are necessary for testing/unstable as they need the latest kernel and packages
  116. set_daily_d_i() {
  117. case $suite in
  118. buster|bullseye|oldoldstable|bookworm|oldstable|trixie|stable)
  119. daily_d_i=false
  120. ;;
  121. forky|testing|sid|unstable)
  122. daily_d_i=true
  123. ;;
  124. *)
  125. err "Unsupported suite: $suite"
  126. esac
  127. }
  128. set_suite() {
  129. suite=$1
  130. set_daily_d_i
  131. set_security_archive
  132. }
  133. # Maps version numbers and release aliases to actual suite names
  134. # Accepts: version number (10-14), suite name (buster, bullseye, etc.), or alias (stable, testing, etc.)
  135. # Current mapping:
  136. # - 10/buster: No longer supported as oldoldstable (archived)
  137. # - 11/bullseye/oldoldstable: LTS support
  138. # - 12/bookworm/oldstable: Previous stable
  139. # - 13/trixie/stable: Current stable release
  140. # - 14/forky/testing: Next release in development
  141. # - sid/unstable: Rolling development branch
  142. set_debian_version() {
  143. case $1 in
  144. 10|buster)
  145. set_suite buster
  146. ;;
  147. 11|bullseye|oldoldstable)
  148. set_suite bullseye
  149. ;;
  150. 12|bookworm|oldstable)
  151. set_suite bookworm
  152. ;;
  153. 13|trixie|stable)
  154. set_suite trixie
  155. ;;
  156. 14|forky|testing)
  157. set_suite forky
  158. ;;
  159. sid|unstable)
  160. set_suite sid
  161. ;;
  162. *)
  163. err "Unsupported version: $1"
  164. esac
  165. }
  166. # Checks if cloud-optimized kernels are available for the current architecture and suite
  167. # Cloud kernels are minimal kernels optimized for cloud/virtual environments
  168. # - Buster: Only amd64 has cloud kernel, arm64 requires backports
  169. # - Bullseye onwards: Both amd64 and arm64 have cloud kernels
  170. # - Other architectures (i386, etc.): No cloud kernels available
  171. # Returns 0 if available, 1 if not
  172. has_cloud_kernel() {
  173. case $suite in
  174. buster)
  175. [ "$architecture" = amd64 ] && return
  176. [ "$architecture" = arm64 ] && [ "$bpo_kernel" = true ] && return
  177. ;;
  178. bullseye|oldoldstable|bookworm|oldstable|trixie|stable|forky|testing|sid|unstable)
  179. [ "$architecture" = amd64 ] || [ "$architecture" = arm64 ] && return
  180. esac
  181. local tmp; tmp=''; [ "$bpo_kernel" = true ] && tmp='-backports'
  182. warn "No cloud kernel is available for $architecture/$suite$tmp"
  183. return 1
  184. }
  185. # Checks if backports repository is available for the current suite
  186. # Backports provide newer versions of packages from testing/unstable rebuilt for stable releases
  187. # - All stable releases and testing: Have backports available
  188. # - Sid/unstable: No backports (already has the latest packages)
  189. # Returns 0 if available, 1 if not
  190. has_backports() {
  191. case $suite in
  192. bookworm|oldstable|trixie|stable|forky|testing) return
  193. esac
  194. # buster|bullseye|oldoldstable DO have backports, but it's in archive.debian.org now
  195. # considering mirrors support varies and the code complexity we must accommodate, we just treat them as no backports available
  196. warn "No backports kernel is available for $suite"
  197. return 1
  198. }
  199. interface=auto
  200. ip=
  201. netmask=
  202. gateway=
  203. dns='1.1.1.1 1.0.0.1'
  204. dns6='2606:4700:4700::1111 2606:4700:4700::1001'
  205. dns10='1.1.1.1 2606:4700:4700::1111'
  206. hostname=
  207. network_console=false
  208. set_debian_version 13
  209. mirror_protocol=https
  210. mirror_host=deb.debian.org
  211. mirror_directory=/debian
  212. mirror_proxy=
  213. security_repository=mirror
  214. account_setup=true
  215. username=debian
  216. password=
  217. authorized_keys_url=
  218. sudo_with_password=false
  219. timezone=UTC
  220. ntp=time.google.com
  221. disk_partitioning=true
  222. disk="/dev/$(lsblk -no PKNAME "$(df /boot | grep -Eo '/dev/[a-z0-9]+')")"
  223. force_gpt=true
  224. efi=
  225. esp=106
  226. filesystem=ext4
  227. kernel=
  228. cloud_kernel=false
  229. bpo_kernel=false
  230. install_recommends=true
  231. install=
  232. upgrade=
  233. kernel_params=
  234. force_lowmem=
  235. bbr=false
  236. ssh_port=
  237. hold=false
  238. power_off=false
  239. architecture=
  240. firmware=false
  241. force_efi_extra_removable=true
  242. grub_timeout=5
  243. dry_run=false
  244. apt_non_free_firmware=true
  245. apt_non_free=false
  246. apt_contrib=false
  247. apt_src=true
  248. apt_backports=true
  249. cidata=
  250. while [ $# -gt 0 ]; do
  251. case $1 in
  252. --cdn)
  253. ;;
  254. --aws)
  255. mirror_host=cdn-aws.deb.debian.org
  256. ntp=time.aws.com
  257. ;;
  258. --cloudflare)
  259. dns='1.1.1.1 1.0.0.1'
  260. dns6='2606:4700:4700::1111 2606:4700:4700::1001'
  261. ntp=time.cloudflare.com
  262. ;;
  263. --aliyun)
  264. dns='223.5.5.5 223.6.6.6'
  265. dns6='2400:3200::1 2400:3200:baba::1'
  266. mirror_host=mirrors.aliyun.com
  267. ntp=time.amazonaws.cn
  268. ;;
  269. --ustc|--china)
  270. dns='119.29.29.29'
  271. dns6='2402:4e00::'
  272. mirror_host=mirrors.ustc.edu.cn
  273. ntp=time.amazonaws.cn
  274. ;;
  275. --tuna)
  276. dns='119.29.29.29'
  277. dns6='2402:4e00::'
  278. mirror_host=mirrors.tuna.tsinghua.edu.cn
  279. ntp=time.amazonaws.cn
  280. ;;
  281. --static-ipv4)
  282. ip=$(ip r get 1.1.1.1 | awk '/src/ {print $7}')
  283. gateway=$(ip r get 1.1.1.1 | awk '/via/ {print $3}')
  284. _cidr=$(ip -o -f inet addr show | grep -w "$ip" | awk '{print $4}' | cut -d'/' -f2)
  285. ip="$ip/$_cidr"
  286. hostname=$(hostname)
  287. interface=$(ip r get 1.1.1.1 | awk '/dev/ {print $5}')
  288. ;;
  289. --interface)
  290. interface=$2
  291. shift
  292. ;;
  293. --ip)
  294. ip=$2
  295. shift
  296. ;;
  297. --netmask)
  298. netmask=$2
  299. shift
  300. ;;
  301. --gateway)
  302. gateway=$2
  303. shift
  304. ;;
  305. --dns)
  306. dns=$2
  307. shift
  308. ;;
  309. --dns6)
  310. dns6=$2
  311. shift
  312. ;;
  313. --hostname)
  314. hostname=$2
  315. shift
  316. ;;
  317. --network-console)
  318. network_console=true
  319. ;;
  320. --version)
  321. set_debian_version "$2"
  322. shift
  323. ;;
  324. --suite)
  325. set_suite "$2"
  326. shift
  327. ;;
  328. --release-d-i)
  329. daily_d_i=false
  330. ;;
  331. --daily-d-i)
  332. daily_d_i=true
  333. ;;
  334. --mirror-protocol)
  335. mirror_protocol=$2
  336. shift
  337. ;;
  338. --https)
  339. mirror_protocol=https
  340. ;;
  341. --mirror-host)
  342. mirror_host=$2
  343. shift
  344. ;;
  345. --mirror-directory)
  346. mirror_directory=${2%/}
  347. shift
  348. ;;
  349. --mirror-proxy|--proxy)
  350. mirror_proxy=$2
  351. shift
  352. ;;
  353. --reuse-proxy)
  354. set_mirror_proxy
  355. ;;
  356. --security-repository)
  357. security_repository=$2
  358. shift
  359. ;;
  360. --no-user|--no-account-setup)
  361. account_setup=false
  362. ;;
  363. --user|--username)
  364. username=$2
  365. shift
  366. ;;
  367. --password)
  368. password=$2
  369. shift
  370. ;;
  371. --authorized-keys-url)
  372. authorized_keys_url=$2
  373. shift
  374. ;;
  375. --sudo-with-password)
  376. sudo_with_password=true
  377. ;;
  378. --timezone)
  379. timezone=$2
  380. shift
  381. ;;
  382. --ntp)
  383. ntp=$2
  384. shift
  385. ;;
  386. --no-part|--no-disk-partitioning)
  387. disk_partitioning=false
  388. ;;
  389. --force-lowmem)
  390. [ "$2" != 0 ] && [ "$2" != 1 ] && [ "$2" != 2 ] && err 'Low memory level can only be 0, 1 or 2'
  391. force_lowmem=$2
  392. shift
  393. ;;
  394. --disk)
  395. disk=$2
  396. shift
  397. ;;
  398. --no-force-gpt)
  399. force_gpt=false
  400. ;;
  401. --bios)
  402. efi=false
  403. ;;
  404. --efi)
  405. efi=true
  406. ;;
  407. --esp)
  408. esp=$2
  409. shift
  410. ;;
  411. --filesystem)
  412. filesystem=$2
  413. shift
  414. ;;
  415. --kernel)
  416. kernel=$2
  417. shift
  418. ;;
  419. --cloud-kernel)
  420. cloud_kernel=true
  421. ;;
  422. --bpo-kernel)
  423. bpo_kernel=true
  424. ;;
  425. --apt-non-free-firmware)
  426. apt_non_free_firmware=true
  427. ;;
  428. --apt-non-free)
  429. apt_non_free=true
  430. apt_contrib=true
  431. ;;
  432. --apt-contrib)
  433. apt_contrib=true
  434. ;;
  435. --apt-src)
  436. apt_src=true
  437. ;;
  438. --apt-backports)
  439. apt_backports=true
  440. ;;
  441. --no-apt-non-free-firmware)
  442. apt_non_free_firmware=false
  443. ;;
  444. --no-apt-non-free)
  445. apt_non_free=false
  446. ;;
  447. --no-apt-contrib)
  448. apt_contrib=false
  449. apt_non_free=false
  450. ;;
  451. --no-apt-src)
  452. apt_src=false
  453. ;;
  454. --no-apt-backports)
  455. apt_backports=false
  456. ;;
  457. --no-install-recommends)
  458. install_recommends=false
  459. ;;
  460. --install)
  461. install=$2
  462. shift
  463. ;;
  464. --no-upgrade)
  465. upgrade=none
  466. ;;
  467. --safe-upgrade)
  468. upgrade=safe-upgrade
  469. ;;
  470. --full-upgrade)
  471. upgrade=full-upgrade
  472. ;;
  473. --ethx)
  474. kernel_params="$kernel_params net.ifnames=0 biosdevname=0"
  475. ;;
  476. --bbr)
  477. bbr=true
  478. ;;
  479. --ssh-port)
  480. ssh_port=$2
  481. shift
  482. ;;
  483. --hold)
  484. hold=true
  485. ;;
  486. --power-off)
  487. power_off=true
  488. ;;
  489. --architecture)
  490. architecture=$2
  491. shift
  492. ;;
  493. --firmware)
  494. firmware=true
  495. ;;
  496. --no-force-efi-extra-removable)
  497. force_efi_extra_removable=false
  498. ;;
  499. --grub-timeout)
  500. grub_timeout=$2
  501. shift
  502. ;;
  503. --dry-run)
  504. dry_run=true
  505. ;;
  506. --cidata)
  507. cidata=$(realpath "$2")
  508. [ ! -f "$cidata/meta-data" ] && err 'No "meta-data" file found in the cloud-init directory'
  509. [ ! -f "$cidata/user-data" ] && err 'No "user-data" file found in the cloud-init directory'
  510. shift
  511. ;;
  512. *)
  513. err "Unknown option: \"$1\""
  514. esac
  515. shift
  516. done
  517. [ -z "$architecture" ] && {
  518. architecture=$(dpkg --print-architecture 2> /dev/null) || {
  519. case $(uname -m) in
  520. x86_64)
  521. architecture=amd64
  522. ;;
  523. aarch64)
  524. architecture=arm64
  525. ;;
  526. i386)
  527. architecture=i386
  528. ;;
  529. *)
  530. err 'No "--architecture" specified'
  531. esac
  532. }
  533. }
  534. [ -z "$kernel" ] && {
  535. kernel="linux-image-$architecture"
  536. [ "$cloud_kernel" = true ] && has_cloud_kernel && kernel="linux-image-cloud-$architecture"
  537. [ "$bpo_kernel" = true ] && has_backports && install="$kernel/$suite-backports $install"
  538. }
  539. [ -n "$authorized_keys_url" ] && ! download "$authorized_keys_url" /dev/null &&
  540. err "Failed to download SSH authorized public keys from \"$authorized_keys_url\""
  541. # Determines if the non-free-firmware repository component is available
  542. # Starting from Debian 12 (Bookworm), firmware was split into a separate component
  543. # to make it easier to include necessary firmware while keeping main free
  544. # - Bookworm onwards: non-free-firmware component available
  545. # - Bullseye and earlier: Firmware is in non-free component
  546. non_free_firmware_available=false
  547. case $suite in
  548. bookworm|oldstable|trixie|stable|forky|testing|sid|unstable)
  549. non_free_firmware_available=true
  550. ;;
  551. *)
  552. apt_non_free_firmware=false
  553. esac
  554. apt_components=main
  555. [ "$apt_contrib" = true ] && apt_components="$apt_components contrib"
  556. [ "$apt_non_free" = true ] && apt_components="$apt_components non-free"
  557. [ "$apt_non_free_firmware" = true ] && apt_components="$apt_components non-free-firmware"
  558. apt_services=updates
  559. [ "$apt_backports" = true ] && apt_services="$apt_services, backports"
  560. installer_directory="/boot/debian-$suite"
  561. save_preseed='cat'
  562. [ "$dry_run" = false ] && {
  563. [ "$(id -u)" -ne 0 ] && err 'root privilege is required'
  564. rm -rf "$installer_directory"
  565. mkdir -p "$installer_directory"
  566. cd "$installer_directory"
  567. save_preseed='tee -a preseed.cfg'
  568. }
  569. if [ "$account_setup" = true ]; then
  570. prompt_password
  571. elif [ "$network_console" = true ] && [ -z "$authorized_keys_url" ]; then
  572. prompt_password "Choose a password for the installer user of the SSH network console: "
  573. fi
  574. $save_preseed << EOF
  575. # Localization
  576. d-i debian-installer/language string en
  577. d-i debian-installer/country string US
  578. d-i debian-installer/locale string en_US.UTF-8
  579. d-i keyboard-configuration/xkb-keymap select us
  580. # Network configuration
  581. d-i netcfg/choose_interface select $interface
  582. EOF
  583. [ -n "$ip" ] && {
  584. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  585. echo "d-i netcfg/get_ipaddress string $ip" | $save_preseed
  586. [ -n "$netmask" ] && echo "d-i netcfg/get_netmask string $netmask" | $save_preseed
  587. [ -n "$gateway" ] && echo "d-i netcfg/get_gateway string $gateway" | $save_preseed
  588. [ -z "${ip%%*:*}" ] && [ -n "${dns%%*:*}" ] && dns="$dns6"
  589. [ -n "$dns" ] && echo "d-i netcfg/get_nameservers string $dns" | $save_preseed
  590. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  591. }
  592. [ -z "$ip" ] && {
  593. echo "d-i netcfg/get_nameservers string $dns10" | $save_preseed
  594. }
  595. if [ -n "$hostname" ]; then
  596. echo "d-i netcfg/hostname string $hostname" | $save_preseed
  597. hostname=debian
  598. domain=
  599. else
  600. hostname=$(cat /proc/sys/kernel/hostname)
  601. domain=$(cat /proc/sys/kernel/domainname)
  602. if [ "$domain" = '(none)' ]; then
  603. domain=
  604. else
  605. domain=" $domain"
  606. fi
  607. fi
  608. $save_preseed << EOF
  609. d-i netcfg/get_hostname string $hostname
  610. d-i netcfg/get_domain string$domain
  611. EOF
  612. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  613. [ "$network_console" = true ] && {
  614. $save_preseed << 'EOF'
  615. # Network console
  616. d-i anna/choose_modules string network-console
  617. d-i preseed/early_command string anna-install network-console
  618. EOF
  619. if [ -n "$authorized_keys_url" ]; then
  620. echo "d-i network-console/authorized_keys_url string $authorized_keys_url" | $save_preseed
  621. else
  622. $save_preseed << EOF
  623. d-i network-console/password password $password
  624. d-i network-console/password-again password $password
  625. EOF
  626. fi
  627. echo 'd-i network-console/start select Continue' | $save_preseed
  628. }
  629. $save_preseed << EOF
  630. # Mirror settings
  631. d-i mirror/country string manual
  632. d-i mirror/protocol string $mirror_protocol
  633. d-i mirror/$mirror_protocol/hostname string $mirror_host
  634. d-i mirror/$mirror_protocol/directory string $mirror_directory
  635. d-i mirror/$mirror_protocol/proxy string $mirror_proxy
  636. d-i mirror/suite string $suite
  637. EOF
  638. [ "$account_setup" = true ] && {
  639. password_hash=$(mkpasswd -m sha-256 "$password" 2> /dev/null) ||
  640. password_hash=$(openssl passwd -5 "$password" 2> /dev/null) ||
  641. password_hash=$(busybox mkpasswd -m sha256 "$password" 2> /dev/null) || {
  642. for python in python3 python python2; do
  643. password_hash=$("$python" -c 'import crypt, sys; print(crypt.crypt(sys.argv[1], crypt.mksalt(crypt.METHOD_SHA256)))' "$password" 2> /dev/null) && break
  644. done
  645. }
  646. $save_preseed << 'EOF'
  647. # Account setup
  648. EOF
  649. [ -n "$authorized_keys_url" ] && configure_sshd PasswordAuthentication no
  650. if [ "$username" = root ]; then
  651. if [ -z "$authorized_keys_url" ]; then
  652. configure_sshd PermitRootLogin yes
  653. else
  654. in_target "mkdir -m 0700 -p ~root/.ssh && busybox wget -O- \"$authorized_keys_url\" >> ~root/.ssh/authorized_keys"
  655. fi
  656. $save_preseed << 'EOF'
  657. d-i passwd/root-login boolean true
  658. d-i passwd/make-user boolean false
  659. EOF
  660. if [ -z "$password_hash" ]; then
  661. $save_preseed << EOF
  662. d-i passwd/root-password password $password
  663. d-i passwd/root-password-again password $password
  664. EOF
  665. else
  666. echo "d-i passwd/root-password-crypted password $password_hash" | $save_preseed
  667. fi
  668. else
  669. configure_sshd PermitRootLogin no
  670. [ -n "$authorized_keys_url" ] &&
  671. in_target "sudo -u $username mkdir -m 0700 -p ~$username/.ssh && busybox wget -O - \"$authorized_keys_url\" | sudo -u $username tee -a ~$username/.ssh/authorized_keys"
  672. [ "$sudo_with_password" = false ] &&
  673. in_target "echo \"$username ALL=(ALL:ALL) NOPASSWD:ALL\" > \"/etc/sudoers.d/90-user-$username\""
  674. $save_preseed << EOF
  675. d-i passwd/root-login boolean false
  676. d-i passwd/make-user boolean true
  677. d-i passwd/user-fullname string
  678. d-i passwd/username string $username
  679. EOF
  680. if [ -z "$password_hash" ]; then
  681. $save_preseed << EOF
  682. d-i passwd/user-password password $password
  683. d-i passwd/user-password-again password $password
  684. EOF
  685. else
  686. echo "d-i passwd/user-password-crypted password $password_hash" | $save_preseed
  687. fi
  688. fi
  689. }
  690. [ -n "$ssh_port" ] && configure_sshd Port "$ssh_port"
  691. $save_preseed << EOF
  692. # Clock and time zone setup
  693. d-i time/zone string $timezone
  694. d-i clock-setup/utc boolean true
  695. d-i clock-setup/ntp boolean true
  696. d-i clock-setup/ntp-server string $ntp
  697. # Partitioning
  698. EOF
  699. [ "$disk_partitioning" = true ] && {
  700. $save_preseed << 'EOF'
  701. d-i partman-auto/method string regular
  702. EOF
  703. if [ -n "$disk" ]; then
  704. echo "d-i partman-auto/disk string $disk" | $save_preseed
  705. else
  706. # shellcheck disable=SC2016
  707. echo 'd-i partman/early_command string debconf-set partman-auto/disk "$(list-devices disk | head -n 1)"' | $save_preseed
  708. fi
  709. }
  710. [ "$force_gpt" = true ] && {
  711. $save_preseed << 'EOF'
  712. d-i partman-partitioning/choose_label string gpt
  713. d-i partman-partitioning/default_label string gpt
  714. EOF
  715. }
  716. [ "$disk_partitioning" = true ] && {
  717. echo "d-i partman/default_filesystem string $filesystem" | $save_preseed
  718. [ -z "$efi" ] && {
  719. efi=false
  720. [ -d /sys/firmware/efi ] && efi=true
  721. }
  722. $save_preseed << 'EOF'
  723. d-i partman-auto/expert_recipe string \
  724. naive :: \
  725. EOF
  726. if [ "$efi" = true ]; then
  727. $save_preseed << EOF
  728. $esp $esp $esp free \\
  729. EOF
  730. $save_preseed << 'EOF'
  731. $iflabel{ gpt } \
  732. $reusemethod{ } \
  733. method{ efi } \
  734. format{ } \
  735. . \
  736. EOF
  737. else
  738. $save_preseed << 'EOF'
  739. 1 1 1 free \
  740. $iflabel{ gpt } \
  741. $reusemethod{ } \
  742. method{ biosgrub } \
  743. . \
  744. EOF
  745. fi
  746. $save_preseed << 'EOF'
  747. 1075 1076 -1 $default_filesystem \
  748. method{ format } \
  749. format{ } \
  750. use_filesystem{ } \
  751. $default_filesystem{ } \
  752. mountpoint{ / } \
  753. .
  754. EOF
  755. if [ "$efi" = true ]; then
  756. echo 'd-i partman-efi/non_efi_system boolean true' | $save_preseed
  757. fi
  758. $save_preseed << 'EOF'
  759. d-i partman-auto/choose_recipe select naive
  760. d-i partman-basicfilesystems/no_swap boolean false
  761. d-i partman-partitioning/confirm_write_new_label boolean true
  762. d-i partman/choose_partition select finish
  763. d-i partman/confirm boolean true
  764. d-i partman/confirm_nooverwrite boolean true
  765. d-i partman-lvm/device_remove_lvm boolean true
  766. EOF
  767. }
  768. $save_preseed << EOF
  769. # Base system installation
  770. d-i base-installer/kernel/image string $kernel
  771. EOF
  772. [ "$install_recommends" = false ] && echo "d-i base-installer/install-recommends boolean $install_recommends" | $save_preseed
  773. [ "$security_repository" = mirror ] && security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
  774. $save_preseed << EOF
  775. # Apt setup
  776. d-i apt-setup/contrib boolean $apt_contrib
  777. d-i apt-setup/non-free boolean $apt_non_free
  778. d-i apt-setup/enable-source-repositories boolean $apt_src
  779. d-i apt-setup/services-select multiselect $apt_services
  780. EOF
  781. [ "$non_free_firmware_available" = true ] && echo "d-i apt-setup/non-free-firmware boolean $apt_non_free_firmware" | $save_preseed
  782. # If not sid/unstable
  783. [ -n "$security_archive" ] && {
  784. $save_preseed << EOF
  785. d-i apt-setup/local0/repository string $security_repository $security_archive $apt_components
  786. d-i apt-setup/local0/source boolean $apt_src
  787. EOF
  788. }
  789. $save_preseed << 'EOF'
  790. # Package selection
  791. tasksel tasksel/first multiselect ssh-server
  792. EOF
  793. install="$install ca-certificates libpam-systemd"
  794. [ -n "$cidata" ] && install="$install cloud-init"
  795. [ -n "$install" ] && echo "d-i pkgsel/include string $install" | $save_preseed
  796. [ -n "$upgrade" ] && echo "d-i pkgsel/upgrade select $upgrade" | $save_preseed
  797. $save_preseed << 'EOF'
  798. popularity-contest popularity-contest/participate boolean false
  799. # Boot loader installation
  800. EOF
  801. if [ -n "$disk" ]; then
  802. echo "d-i grub-installer/bootdev string $disk" | $save_preseed
  803. else
  804. echo 'd-i grub-installer/bootdev string default' | $save_preseed
  805. fi
  806. [ "$force_efi_extra_removable" = true ] && echo 'd-i grub-installer/force-efi-extra-removable boolean true' | $save_preseed
  807. [ -n "$kernel_params" ] && echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
  808. $save_preseed << 'EOF'
  809. # Finishing up the installation
  810. EOF
  811. [ "$hold" = false ] && echo 'd-i finish-install/reboot_in_progress note' | $save_preseed
  812. [ "$bbr" = true ] && in_target '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
  813. [ -n "$cidata" ] && {
  814. in_target 'echo "datasource_list: [ NoCloud ]" > /etc/cloud/cloud.cfg.d/91_set_datasource.cfg'
  815. in_target 'echo "" >> /etc/cloud/cloud.cfg.d/91_set_datasource.cfg'
  816. in_target 'echo "datasource:" >> /etc/cloud/cloud.cfg.d/91_set_datasource.cfg'
  817. in_target 'echo " ConfigDrive:" >> /etc/cloud/cloud.cfg.d/91_set_datasource.cfg'
  818. in_target 'echo " dsmode: disabled" >> /etc/cloud/cloud.cfg.d/91_set_datasource.cfg'
  819. in_target 'echo " NoCloud:" >> /etc/cloud/cloud.cfg.d/91_set_datasource.cfg'
  820. in_target 'echo " fs_label: ~" >> /etc/cloud/cloud.cfg.d/91_set_datasource.cfg'
  821. in_target 'echo " dsmode: local" >> /etc/cloud/cloud.cfg.d/91_set_datasource.cfg'
  822. }
  823. late_command='true'
  824. [ -n "$in_target_script" ] && late_command="$late_command; in-target sh -c '$in_target_script'"
  825. [ -n "$cidata" ] && late_command="$late_command; mkdir -p /target/var/lib/cloud/seed/nocloud; cp -r /cidata/. /target/var/lib/cloud/seed/nocloud/"
  826. echo "d-i preseed/late_command string $late_command" | $save_preseed
  827. [ "$power_off" = true ] && echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  828. save_grub_cfg='cat'
  829. [ "$dry_run" = false ] && {
  830. base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  831. [ "$suite" = stretch ] && [ "$efi" = true ] && base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/buster/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  832. [ "$daily_d_i" = true ] && base_url="https://d-i.debian.org/daily-images/$architecture/daily/netboot/debian-installer/$architecture"
  833. firmware_url="https://cdimage.debian.org/cdimage/unofficial/non-free/firmware/$suite/current/firmware.cpio.gz"
  834. download "$base_url/linux" linux
  835. download "$base_url/initrd.gz" initrd.gz
  836. [ "$firmware" = true ] && download "$firmware_url" firmware.cpio.gz
  837. gzip -d initrd.gz
  838. # cpio reads a list of file names from the standard input
  839. echo preseed.cfg | cpio -o -H newc -A -F initrd
  840. if [ -n "$cidata" ]; then
  841. cp -r "$cidata" cidata
  842. find cidata | cpio -o -H newc -A -F initrd
  843. fi
  844. gzip -1 initrd
  845. mkdir -p /etc/default/grub.d
  846. tee /etc/default/grub.d/zz-debi.cfg 1>&2 << EOF
  847. GRUB_DEFAULT=debi
  848. GRUB_TIMEOUT=$grub_timeout
  849. GRUB_TIMEOUT_STYLE=menu
  850. EOF
  851. if command_exists update-grub; then
  852. grub_cfg=/boot/grub/grub.cfg
  853. update-grub
  854. elif command_exists grub2-mkconfig; then
  855. tmp=$(mktemp)
  856. grep -vF zz_debi /etc/default/grub > "$tmp"
  857. cat "$tmp" > /etc/default/grub
  858. rm "$tmp"
  859. # shellcheck disable=SC2016
  860. echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
  861. grub_cfg=/boot/grub2/grub.cfg
  862. [ -d /sys/firmware/efi ] && grub_cfg=/boot/efi/EFI/*/grub.cfg
  863. grub2-mkconfig -o "$grub_cfg"
  864. elif command_exists grub-mkconfig; then
  865. tmp=$(mktemp)
  866. grep -vF zz_debi /etc/default/grub > "$tmp"
  867. cat "$tmp" > /etc/default/grub
  868. rm "$tmp"
  869. # shellcheck disable=SC2016
  870. echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
  871. grub_cfg=/boot/grub/grub.cfg
  872. grub-mkconfig -o "$grub_cfg"
  873. else
  874. err 'Could not find "update-grub" or "grub2-mkconfig" or "grub-mkconfig" command'
  875. fi
  876. save_grub_cfg="tee -a $grub_cfg"
  877. }
  878. mkrelpath=$installer_directory
  879. [ "$dry_run" = true ] && mkrelpath=/boot
  880. installer_directory=$(grub-mkrelpath "$mkrelpath" 2> /dev/null) ||
  881. installer_directory=$(grub2-mkrelpath "$mkrelpath" 2> /dev/null) || {
  882. err 'Could not find "grub-mkrelpath" or "grub2-mkrelpath" command'
  883. }
  884. [ "$dry_run" = true ] && installer_directory="$installer_directory/debian-$suite"
  885. kernel_params="$kernel_params lowmem/low=1"
  886. [ -n "$force_lowmem" ] && kernel_params="$kernel_params lowmem=+$force_lowmem"
  887. initrd="$installer_directory/initrd.gz"
  888. [ "$firmware" = true ] && initrd="$initrd $installer_directory/firmware.cpio.gz"
  889. $save_grub_cfg 1>&2 << EOF
  890. menuentry 'Debian Installer' --id debi {
  891. insmod part_msdos
  892. insmod part_gpt
  893. insmod ext2
  894. insmod xfs
  895. insmod btrfs
  896. linux $installer_directory/linux$kernel_params
  897. initrd $initrd
  898. }
  899. EOF