debi.sh 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. buster|bullseye|oldoldstable|bookworm|oldstable|trixie|stable|forky|testing) return
  193. esac
  194. warn "No backports kernel is available for $suite"
  195. return 1
  196. }
  197. interface=auto
  198. ip=
  199. netmask=
  200. gateway=
  201. dns='1.1.1.1 1.0.0.1'
  202. dns6='2606:4700:4700::1111 2606:4700:4700::1001'
  203. dns10='1.1.1.1 2606:4700:4700::1111'
  204. hostname=
  205. network_console=false
  206. set_debian_version 13
  207. mirror_protocol=https
  208. mirror_host=deb.debian.org
  209. mirror_directory=/debian
  210. mirror_proxy=
  211. security_repository=mirror
  212. account_setup=true
  213. username=debian
  214. password=
  215. authorized_keys_url=
  216. sudo_with_password=false
  217. timezone=UTC
  218. ntp=time.google.com
  219. disk_partitioning=true
  220. disk="/dev/$(lsblk -no PKNAME "$(df /boot | grep -Eo '/dev/[a-z0-9]+')")"
  221. force_gpt=true
  222. efi=
  223. esp=106
  224. filesystem=ext4
  225. kernel=
  226. cloud_kernel=false
  227. bpo_kernel=false
  228. install_recommends=true
  229. install=
  230. upgrade=
  231. kernel_params=
  232. force_lowmem=
  233. bbr=false
  234. ssh_port=
  235. hold=false
  236. power_off=false
  237. architecture=
  238. firmware=false
  239. force_efi_extra_removable=true
  240. grub_timeout=5
  241. dry_run=false
  242. apt_non_free_firmware=true
  243. apt_non_free=false
  244. apt_contrib=false
  245. apt_src=true
  246. apt_backports=true
  247. cidata=
  248. while [ $# -gt 0 ]; do
  249. case $1 in
  250. --cdn)
  251. ;;
  252. --aws)
  253. mirror_host=cdn-aws.deb.debian.org
  254. ntp=time.aws.com
  255. ;;
  256. --cloudflare)
  257. dns='1.1.1.1 1.0.0.1'
  258. dns6='2606:4700:4700::1111 2606:4700:4700::1001'
  259. ntp=time.cloudflare.com
  260. ;;
  261. --aliyun)
  262. dns='223.5.5.5 223.6.6.6'
  263. dns6='2400:3200::1 2400:3200:baba::1'
  264. mirror_host=mirrors.aliyun.com
  265. ntp=time.amazonaws.cn
  266. ;;
  267. --ustc|--china)
  268. dns='119.29.29.29'
  269. dns6='2402:4e00::'
  270. mirror_host=mirrors.ustc.edu.cn
  271. ntp=time.amazonaws.cn
  272. ;;
  273. --tuna)
  274. dns='119.29.29.29'
  275. dns6='2402:4e00::'
  276. mirror_host=mirrors.tuna.tsinghua.edu.cn
  277. ntp=time.amazonaws.cn
  278. ;;
  279. --static-ipv4)
  280. ip=$(ip r get 1.1.1.1 | awk '/src/ {print $7}')
  281. gateway=$(ip r get 1.1.1.1 | awk '/via/ {print $3}')
  282. _cidr=$(ip -o -f inet addr show | grep -w "$ip" | awk '{print $4}' | cut -d'/' -f2)
  283. ip="$ip/$_cidr"
  284. hostname=$(hostname)
  285. interface=$(ip r get 1.1.1.1 | awk '/dev/ {print $5}')
  286. ;;
  287. --interface)
  288. interface=$2
  289. shift
  290. ;;
  291. --ip)
  292. ip=$2
  293. shift
  294. ;;
  295. --netmask)
  296. netmask=$2
  297. shift
  298. ;;
  299. --gateway)
  300. gateway=$2
  301. shift
  302. ;;
  303. --dns)
  304. dns=$2
  305. shift
  306. ;;
  307. --dns6)
  308. dns6=$2
  309. shift
  310. ;;
  311. --hostname)
  312. hostname=$2
  313. shift
  314. ;;
  315. --network-console)
  316. network_console=true
  317. ;;
  318. --version)
  319. set_debian_version "$2"
  320. shift
  321. ;;
  322. --suite)
  323. set_suite "$2"
  324. shift
  325. ;;
  326. --release-d-i)
  327. daily_d_i=false
  328. ;;
  329. --daily-d-i)
  330. daily_d_i=true
  331. ;;
  332. --mirror-protocol)
  333. mirror_protocol=$2
  334. shift
  335. ;;
  336. --https)
  337. mirror_protocol=https
  338. ;;
  339. --mirror-host)
  340. mirror_host=$2
  341. shift
  342. ;;
  343. --mirror-directory)
  344. mirror_directory=${2%/}
  345. shift
  346. ;;
  347. --mirror-proxy|--proxy)
  348. mirror_proxy=$2
  349. shift
  350. ;;
  351. --reuse-proxy)
  352. set_mirror_proxy
  353. ;;
  354. --security-repository)
  355. security_repository=$2
  356. shift
  357. ;;
  358. --no-user|--no-account-setup)
  359. account_setup=false
  360. ;;
  361. --user|--username)
  362. username=$2
  363. shift
  364. ;;
  365. --password)
  366. password=$2
  367. shift
  368. ;;
  369. --authorized-keys-url)
  370. authorized_keys_url=$2
  371. shift
  372. ;;
  373. --sudo-with-password)
  374. sudo_with_password=true
  375. ;;
  376. --timezone)
  377. timezone=$2
  378. shift
  379. ;;
  380. --ntp)
  381. ntp=$2
  382. shift
  383. ;;
  384. --no-part|--no-disk-partitioning)
  385. disk_partitioning=false
  386. ;;
  387. --force-lowmem)
  388. [ "$2" != 0 ] && [ "$2" != 1 ] && [ "$2" != 2 ] && err 'Low memory level can only be 0, 1 or 2'
  389. force_lowmem=$2
  390. shift
  391. ;;
  392. --disk)
  393. disk=$2
  394. shift
  395. ;;
  396. --no-force-gpt)
  397. force_gpt=false
  398. ;;
  399. --bios)
  400. efi=false
  401. ;;
  402. --efi)
  403. efi=true
  404. ;;
  405. --esp)
  406. esp=$2
  407. shift
  408. ;;
  409. --filesystem)
  410. filesystem=$2
  411. shift
  412. ;;
  413. --kernel)
  414. kernel=$2
  415. shift
  416. ;;
  417. --cloud-kernel)
  418. cloud_kernel=true
  419. ;;
  420. --bpo-kernel)
  421. bpo_kernel=true
  422. ;;
  423. --apt-non-free-firmware)
  424. apt_non_free_firmware=true
  425. ;;
  426. --apt-non-free)
  427. apt_non_free=true
  428. apt_contrib=true
  429. ;;
  430. --apt-contrib)
  431. apt_contrib=true
  432. ;;
  433. --apt-src)
  434. apt_src=true
  435. ;;
  436. --apt-backports)
  437. apt_backports=true
  438. ;;
  439. --no-apt-non-free-firmware)
  440. apt_non_free_firmware=false
  441. ;;
  442. --no-apt-non-free)
  443. apt_non_free=false
  444. ;;
  445. --no-apt-contrib)
  446. apt_contrib=false
  447. apt_non_free=false
  448. ;;
  449. --no-apt-src)
  450. apt_src=false
  451. ;;
  452. --no-apt-backports)
  453. apt_backports=false
  454. ;;
  455. --no-install-recommends)
  456. install_recommends=false
  457. ;;
  458. --install)
  459. install=$2
  460. shift
  461. ;;
  462. --no-upgrade)
  463. upgrade=none
  464. ;;
  465. --safe-upgrade)
  466. upgrade=safe-upgrade
  467. ;;
  468. --full-upgrade)
  469. upgrade=full-upgrade
  470. ;;
  471. --ethx)
  472. kernel_params="$kernel_params net.ifnames=0 biosdevname=0"
  473. ;;
  474. --bbr)
  475. bbr=true
  476. ;;
  477. --ssh-port)
  478. ssh_port=$2
  479. shift
  480. ;;
  481. --hold)
  482. hold=true
  483. ;;
  484. --power-off)
  485. power_off=true
  486. ;;
  487. --architecture)
  488. architecture=$2
  489. shift
  490. ;;
  491. --firmware)
  492. firmware=true
  493. ;;
  494. --no-force-efi-extra-removable)
  495. force_efi_extra_removable=false
  496. ;;
  497. --grub-timeout)
  498. grub_timeout=$2
  499. shift
  500. ;;
  501. --dry-run)
  502. dry_run=true
  503. ;;
  504. --cidata)
  505. cidata=$(realpath "$2")
  506. [ ! -f "$cidata/meta-data" ] && err 'No "meta-data" file found in the cloud-init directory'
  507. [ ! -f "$cidata/user-data" ] && err 'No "user-data" file found in the cloud-init directory'
  508. shift
  509. ;;
  510. *)
  511. err "Unknown option: \"$1\""
  512. esac
  513. shift
  514. done
  515. [ -z "$architecture" ] && {
  516. architecture=$(dpkg --print-architecture 2> /dev/null) || {
  517. case $(uname -m) in
  518. x86_64)
  519. architecture=amd64
  520. ;;
  521. aarch64)
  522. architecture=arm64
  523. ;;
  524. i386)
  525. architecture=i386
  526. ;;
  527. *)
  528. err 'No "--architecture" specified'
  529. esac
  530. }
  531. }
  532. [ -z "$kernel" ] && {
  533. kernel="linux-image-$architecture"
  534. [ "$cloud_kernel" = true ] && has_cloud_kernel && kernel="linux-image-cloud-$architecture"
  535. [ "$bpo_kernel" = true ] && has_backports && install="$kernel/$suite-backports $install"
  536. }
  537. [ -n "$authorized_keys_url" ] && ! download "$authorized_keys_url" /dev/null &&
  538. err "Failed to download SSH authorized public keys from \"$authorized_keys_url\""
  539. # Determines if the non-free-firmware repository component is available
  540. # Starting from Debian 12 (Bookworm), firmware was split into a separate component
  541. # to make it easier to include necessary firmware while keeping main free
  542. # - Bookworm onwards: non-free-firmware component available
  543. # - Bullseye and earlier: Firmware is in non-free component
  544. non_free_firmware_available=false
  545. case $suite in
  546. bookworm|oldstable|trixie|stable|forky|testing|sid|unstable)
  547. non_free_firmware_available=true
  548. ;;
  549. *)
  550. apt_non_free_firmware=false
  551. esac
  552. apt_components=main
  553. [ "$apt_contrib" = true ] && apt_components="$apt_components contrib"
  554. [ "$apt_non_free" = true ] && apt_components="$apt_components non-free"
  555. [ "$apt_non_free_firmware" = true ] && apt_components="$apt_components non-free-firmware"
  556. apt_services=updates
  557. [ "$apt_backports" = true ] && apt_services="$apt_services, backports"
  558. installer_directory="/boot/debian-$suite"
  559. save_preseed='cat'
  560. [ "$dry_run" = false ] && {
  561. [ "$(id -u)" -ne 0 ] && err 'root privilege is required'
  562. rm -rf "$installer_directory"
  563. mkdir -p "$installer_directory"
  564. cd "$installer_directory"
  565. save_preseed='tee -a preseed.cfg'
  566. }
  567. if [ "$account_setup" = true ]; then
  568. prompt_password
  569. elif [ "$network_console" = true ] && [ -z "$authorized_keys_url" ]; then
  570. prompt_password "Choose a password for the installer user of the SSH network console: "
  571. fi
  572. $save_preseed << EOF
  573. # Localization
  574. d-i debian-installer/language string en
  575. d-i debian-installer/country string US
  576. d-i debian-installer/locale string en_US.UTF-8
  577. d-i keyboard-configuration/xkb-keymap select us
  578. # Network configuration
  579. d-i netcfg/choose_interface select $interface
  580. EOF
  581. [ -n "$ip" ] && {
  582. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  583. echo "d-i netcfg/get_ipaddress string $ip" | $save_preseed
  584. [ -n "$netmask" ] && echo "d-i netcfg/get_netmask string $netmask" | $save_preseed
  585. [ -n "$gateway" ] && echo "d-i netcfg/get_gateway string $gateway" | $save_preseed
  586. [ -z "${ip%%*:*}" ] && [ -n "${dns%%*:*}" ] && dns="$dns6"
  587. [ -n "$dns" ] && echo "d-i netcfg/get_nameservers string $dns" | $save_preseed
  588. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  589. }
  590. [ -z "$ip" ] && {
  591. echo "d-i netcfg/get_nameservers string $dns10" | $save_preseed
  592. }
  593. if [ -n "$hostname" ]; then
  594. echo "d-i netcfg/hostname string $hostname" | $save_preseed
  595. hostname=debian
  596. domain=
  597. else
  598. hostname=$(cat /proc/sys/kernel/hostname)
  599. domain=$(cat /proc/sys/kernel/domainname)
  600. if [ "$domain" = '(none)' ]; then
  601. domain=
  602. else
  603. domain=" $domain"
  604. fi
  605. fi
  606. $save_preseed << EOF
  607. d-i netcfg/get_hostname string $hostname
  608. d-i netcfg/get_domain string$domain
  609. EOF
  610. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  611. [ "$network_console" = true ] && {
  612. $save_preseed << 'EOF'
  613. # Network console
  614. d-i anna/choose_modules string network-console
  615. d-i preseed/early_command string anna-install network-console
  616. EOF
  617. if [ -n "$authorized_keys_url" ]; then
  618. echo "d-i network-console/authorized_keys_url string $authorized_keys_url" | $save_preseed
  619. else
  620. $save_preseed << EOF
  621. d-i network-console/password password $password
  622. d-i network-console/password-again password $password
  623. EOF
  624. fi
  625. echo 'd-i network-console/start select Continue' | $save_preseed
  626. }
  627. $save_preseed << EOF
  628. # Mirror settings
  629. d-i mirror/country string manual
  630. d-i mirror/protocol string $mirror_protocol
  631. d-i mirror/$mirror_protocol/hostname string $mirror_host
  632. d-i mirror/$mirror_protocol/directory string $mirror_directory
  633. d-i mirror/$mirror_protocol/proxy string $mirror_proxy
  634. d-i mirror/suite string $suite
  635. EOF
  636. [ "$account_setup" = true ] && {
  637. password_hash=$(mkpasswd -m sha-256 "$password" 2> /dev/null) ||
  638. password_hash=$(openssl passwd -5 "$password" 2> /dev/null) ||
  639. password_hash=$(busybox mkpasswd -m sha256 "$password" 2> /dev/null) || {
  640. for python in python3 python python2; do
  641. password_hash=$("$python" -c 'import crypt, sys; print(crypt.crypt(sys.argv[1], crypt.mksalt(crypt.METHOD_SHA256)))' "$password" 2> /dev/null) && break
  642. done
  643. }
  644. $save_preseed << 'EOF'
  645. # Account setup
  646. EOF
  647. [ -n "$authorized_keys_url" ] && configure_sshd PasswordAuthentication no
  648. if [ "$username" = root ]; then
  649. if [ -z "$authorized_keys_url" ]; then
  650. configure_sshd PermitRootLogin yes
  651. else
  652. in_target "mkdir -m 0700 -p ~root/.ssh && busybox wget -O- \"$authorized_keys_url\" >> ~root/.ssh/authorized_keys"
  653. fi
  654. $save_preseed << 'EOF'
  655. d-i passwd/root-login boolean true
  656. d-i passwd/make-user boolean false
  657. EOF
  658. if [ -z "$password_hash" ]; then
  659. $save_preseed << EOF
  660. d-i passwd/root-password password $password
  661. d-i passwd/root-password-again password $password
  662. EOF
  663. else
  664. echo "d-i passwd/root-password-crypted password $password_hash" | $save_preseed
  665. fi
  666. else
  667. configure_sshd PermitRootLogin no
  668. [ -n "$authorized_keys_url" ] &&
  669. 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"
  670. [ "$sudo_with_password" = false ] &&
  671. in_target "echo \"$username ALL=(ALL:ALL) NOPASSWD:ALL\" > \"/etc/sudoers.d/90-user-$username\""
  672. $save_preseed << EOF
  673. d-i passwd/root-login boolean false
  674. d-i passwd/make-user boolean true
  675. d-i passwd/user-fullname string
  676. d-i passwd/username string $username
  677. EOF
  678. if [ -z "$password_hash" ]; then
  679. $save_preseed << EOF
  680. d-i passwd/user-password password $password
  681. d-i passwd/user-password-again password $password
  682. EOF
  683. else
  684. echo "d-i passwd/user-password-crypted password $password_hash" | $save_preseed
  685. fi
  686. fi
  687. }
  688. [ -n "$ssh_port" ] && configure_sshd Port "$ssh_port"
  689. $save_preseed << EOF
  690. # Clock and time zone setup
  691. d-i time/zone string $timezone
  692. d-i clock-setup/utc boolean true
  693. d-i clock-setup/ntp boolean true
  694. d-i clock-setup/ntp-server string $ntp
  695. # Partitioning
  696. EOF
  697. [ "$disk_partitioning" = true ] && {
  698. $save_preseed << 'EOF'
  699. d-i partman-auto/method string regular
  700. EOF
  701. if [ -n "$disk" ]; then
  702. echo "d-i partman-auto/disk string $disk" | $save_preseed
  703. else
  704. # shellcheck disable=SC2016
  705. echo 'd-i partman/early_command string debconf-set partman-auto/disk "$(list-devices disk | head -n 1)"' | $save_preseed
  706. fi
  707. }
  708. [ "$force_gpt" = true ] && {
  709. $save_preseed << 'EOF'
  710. d-i partman-partitioning/choose_label string gpt
  711. d-i partman-partitioning/default_label string gpt
  712. EOF
  713. }
  714. [ "$disk_partitioning" = true ] && {
  715. echo "d-i partman/default_filesystem string $filesystem" | $save_preseed
  716. [ -z "$efi" ] && {
  717. efi=false
  718. [ -d /sys/firmware/efi ] && efi=true
  719. }
  720. $save_preseed << 'EOF'
  721. d-i partman-auto/expert_recipe string \
  722. naive :: \
  723. EOF
  724. if [ "$efi" = true ]; then
  725. $save_preseed << EOF
  726. $esp $esp $esp free \\
  727. EOF
  728. $save_preseed << 'EOF'
  729. $iflabel{ gpt } \
  730. $reusemethod{ } \
  731. method{ efi } \
  732. format{ } \
  733. . \
  734. EOF
  735. else
  736. $save_preseed << 'EOF'
  737. 1 1 1 free \
  738. $iflabel{ gpt } \
  739. $reusemethod{ } \
  740. method{ biosgrub } \
  741. . \
  742. EOF
  743. fi
  744. $save_preseed << 'EOF'
  745. 1075 1076 -1 $default_filesystem \
  746. method{ format } \
  747. format{ } \
  748. use_filesystem{ } \
  749. $default_filesystem{ } \
  750. mountpoint{ / } \
  751. .
  752. EOF
  753. if [ "$efi" = true ]; then
  754. echo 'd-i partman-efi/non_efi_system boolean true' | $save_preseed
  755. fi
  756. $save_preseed << 'EOF'
  757. d-i partman-auto/choose_recipe select naive
  758. d-i partman-basicfilesystems/no_swap boolean false
  759. d-i partman-partitioning/confirm_write_new_label boolean true
  760. d-i partman/choose_partition select finish
  761. d-i partman/confirm boolean true
  762. d-i partman/confirm_nooverwrite boolean true
  763. d-i partman-lvm/device_remove_lvm boolean true
  764. EOF
  765. }
  766. $save_preseed << EOF
  767. # Base system installation
  768. d-i base-installer/kernel/image string $kernel
  769. EOF
  770. [ "$install_recommends" = false ] && echo "d-i base-installer/install-recommends boolean $install_recommends" | $save_preseed
  771. [ "$security_repository" = mirror ] && security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
  772. $save_preseed << EOF
  773. # Apt setup
  774. d-i apt-setup/contrib boolean $apt_contrib
  775. d-i apt-setup/non-free boolean $apt_non_free
  776. d-i apt-setup/enable-source-repositories boolean $apt_src
  777. d-i apt-setup/services-select multiselect $apt_services
  778. EOF
  779. [ "$non_free_firmware_available" = true ] && echo "d-i apt-setup/non-free-firmware boolean $apt_non_free_firmware" | $save_preseed
  780. # If not sid/unstable
  781. [ -n "$security_archive" ] && {
  782. $save_preseed << EOF
  783. d-i apt-setup/local0/repository string $security_repository $security_archive $apt_components
  784. d-i apt-setup/local0/source boolean $apt_src
  785. EOF
  786. }
  787. $save_preseed << 'EOF'
  788. # Package selection
  789. tasksel tasksel/first multiselect ssh-server
  790. EOF
  791. install="$install ca-certificates libpam-systemd"
  792. [ -n "$cidata" ] && install="$install cloud-init"
  793. [ -n "$install" ] && echo "d-i pkgsel/include string $install" | $save_preseed
  794. [ -n "$upgrade" ] && echo "d-i pkgsel/upgrade select $upgrade" | $save_preseed
  795. $save_preseed << 'EOF'
  796. popularity-contest popularity-contest/participate boolean false
  797. # Boot loader installation
  798. EOF
  799. if [ -n "$disk" ]; then
  800. echo "d-i grub-installer/bootdev string $disk" | $save_preseed
  801. else
  802. echo 'd-i grub-installer/bootdev string default' | $save_preseed
  803. fi
  804. [ "$force_efi_extra_removable" = true ] && echo 'd-i grub-installer/force-efi-extra-removable boolean true' | $save_preseed
  805. [ -n "$kernel_params" ] && echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
  806. $save_preseed << 'EOF'
  807. # Finishing up the installation
  808. EOF
  809. [ "$hold" = false ] && echo 'd-i finish-install/reboot_in_progress note' | $save_preseed
  810. [ "$bbr" = true ] && in_target '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
  811. [ -n "$cidata" ] && in_target 'echo "{ datasource_list: [ NoCloud ], datasource: { ConfigDrive: { dsmode: disabled }, NoCloud: { fs_label: ~, dsmode: local } } }" > /etc/cloud/cloud.cfg.d/99_mandate_nocloud.cfg'
  812. late_command='true'
  813. [ -n "$in_target_script" ] && late_command="$late_command; in-target sh -c '$in_target_script'"
  814. [ -n "$cidata" ] && late_command="$late_command; mkdir -p /target/var/lib/cloud/seed/nocloud; cp -r /cidata/. /target/var/lib/cloud/seed/nocloud/"
  815. echo "d-i preseed/late_command string $late_command" | $save_preseed
  816. [ "$power_off" = true ] && echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  817. save_grub_cfg='cat'
  818. [ "$dry_run" = false ] && {
  819. base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  820. [ "$suite" = stretch ] && [ "$efi" = true ] && base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/buster/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  821. [ "$daily_d_i" = true ] && base_url="https://d-i.debian.org/daily-images/$architecture/daily/netboot/debian-installer/$architecture"
  822. firmware_url="https://cdimage.debian.org/cdimage/unofficial/non-free/firmware/$suite/current/firmware.cpio.gz"
  823. download "$base_url/linux" linux
  824. download "$base_url/initrd.gz" initrd.gz
  825. [ "$firmware" = true ] && download "$firmware_url" firmware.cpio.gz
  826. gzip -d initrd.gz
  827. # cpio reads a list of file names from the standard input
  828. echo preseed.cfg | cpio -o -H newc -A -F initrd
  829. if [ -n "$cidata" ]; then
  830. cp -r "$cidata" cidata
  831. find cidata | cpio -o -H newc -A -F initrd
  832. fi
  833. gzip -1 initrd
  834. mkdir -p /etc/default/grub.d
  835. tee /etc/default/grub.d/zz-debi.cfg 1>&2 << EOF
  836. GRUB_DEFAULT=debi
  837. GRUB_TIMEOUT=$grub_timeout
  838. GRUB_TIMEOUT_STYLE=menu
  839. EOF
  840. if command_exists update-grub; then
  841. grub_cfg=/boot/grub/grub.cfg
  842. update-grub
  843. elif command_exists grub2-mkconfig; then
  844. tmp=$(mktemp)
  845. grep -vF zz_debi /etc/default/grub > "$tmp"
  846. cat "$tmp" > /etc/default/grub
  847. rm "$tmp"
  848. # shellcheck disable=SC2016
  849. echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
  850. grub_cfg=/boot/grub2/grub.cfg
  851. [ -d /sys/firmware/efi ] && grub_cfg=/boot/efi/EFI/*/grub.cfg
  852. grub2-mkconfig -o "$grub_cfg"
  853. elif command_exists grub-mkconfig; then
  854. tmp=$(mktemp)
  855. grep -vF zz_debi /etc/default/grub > "$tmp"
  856. cat "$tmp" > /etc/default/grub
  857. rm "$tmp"
  858. # shellcheck disable=SC2016
  859. echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
  860. grub_cfg=/boot/grub/grub.cfg
  861. grub-mkconfig -o "$grub_cfg"
  862. else
  863. err 'Could not find "update-grub" or "grub2-mkconfig" or "grub-mkconfig" command'
  864. fi
  865. save_grub_cfg="tee -a $grub_cfg"
  866. }
  867. mkrelpath=$installer_directory
  868. [ "$dry_run" = true ] && mkrelpath=/boot
  869. installer_directory=$(grub-mkrelpath "$mkrelpath" 2> /dev/null) ||
  870. installer_directory=$(grub2-mkrelpath "$mkrelpath" 2> /dev/null) || {
  871. err 'Could not find "grub-mkrelpath" or "grub2-mkrelpath" command'
  872. }
  873. [ "$dry_run" = true ] && installer_directory="$installer_directory/debian-$suite"
  874. kernel_params="$kernel_params lowmem/low=1"
  875. [ -n "$force_lowmem" ] && kernel_params="$kernel_params lowmem=+$force_lowmem"
  876. initrd="$installer_directory/initrd.gz"
  877. [ "$firmware" = true ] && initrd="$initrd $installer_directory/firmware.cpio.gz"
  878. $save_grub_cfg 1>&2 << EOF
  879. menuentry 'Debian Installer' --id debi {
  880. insmod part_msdos
  881. insmod part_gpt
  882. insmod ext2
  883. insmod xfs
  884. insmod btrfs
  885. linux $installer_directory/linux$kernel_params
  886. initrd $initrd
  887. }
  888. EOF