debi.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. late_command=
  17. in_target() {
  18. local command=
  19. for argument in "$@"; do
  20. command="$command $argument"
  21. done
  22. if [ -n "$command" ]; then
  23. [ -z "$late_command" ] && late_command='true'
  24. late_command="$late_command;$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. set_security_archive() {
  94. case $suite in
  95. buster|oldoldstable|bullseye|oldstable)
  96. security_archive="$suite/updates"
  97. ;;
  98. bookworm|stable|trixie|testing)
  99. security_archive="$suite-security"
  100. ;;
  101. sid|unstable)
  102. security_archive=''
  103. ;;
  104. *)
  105. err "Unsupported suite: $suite"
  106. esac
  107. }
  108. set_daily_d_i() {
  109. case $suite in
  110. buster|oldoldstable|bullseye|oldstable|bookworm|stable)
  111. daily_d_i=false
  112. ;;
  113. trixie|testing|sid|unstable)
  114. daily_d_i=true
  115. ;;
  116. *)
  117. err "Unsupported suite: $suite"
  118. esac
  119. }
  120. set_suite() {
  121. suite=$1
  122. set_daily_d_i
  123. set_security_archive
  124. }
  125. set_debian_version() {
  126. case $1 in
  127. 10|buster|oldoldstable)
  128. set_suite buster
  129. ;;
  130. 11|bullseye|oldstable)
  131. set_suite bullseye
  132. ;;
  133. 12|bookworm|stable)
  134. set_suite bookworm
  135. ;;
  136. 13|trixie|testing)
  137. set_suite bookworm
  138. ;;
  139. sid|unstable)
  140. set_suite sid
  141. ;;
  142. *)
  143. err "Unsupported version: $1"
  144. esac
  145. }
  146. has_cloud_kernel() {
  147. case $suite in
  148. buster|oldoldstable)
  149. [ "$architecture" = amd64 ] && return
  150. [ "$architecture" = arm64 ] && [ "$bpo_kernel" = true ] && return
  151. ;;
  152. bullseye|oldstable|bookworm|stable|trixie|testing|sid|unstable)
  153. [ "$architecture" = amd64 ] || [ "$architecture" = arm64 ] && return
  154. esac
  155. local tmp; tmp=''; [ "$bpo_kernel" = true ] && tmp='-backports'
  156. warn "No cloud kernel is available for $architecture/$suite$tmp"
  157. return 1
  158. }
  159. has_backports() {
  160. case $suite in
  161. buster|oldoldstable|bullseye|oldstable|bookworm|stable|trixie|testing) return
  162. esac
  163. warn "No backports kernel is available for $suite"
  164. return 1
  165. }
  166. interface=auto
  167. ip=
  168. netmask=
  169. gateway=
  170. dns='8.8.8.8 8.8.4.4'
  171. hostname=
  172. network_console=false
  173. set_debian_version 12
  174. mirror_protocol=http
  175. mirror_host=deb.debian.org
  176. mirror_directory=/debian
  177. mirror_proxy=
  178. security_repository=http://security.debian.org/debian-security
  179. account_setup=true
  180. username=debian
  181. password=
  182. authorized_keys_url=
  183. sudo_with_password=false
  184. timezone=UTC
  185. ntp=0.debian.pool.ntp.org
  186. disk_partitioning=true
  187. disk=
  188. force_gpt=true
  189. efi=
  190. esp=106
  191. filesystem=ext4
  192. kernel=
  193. cloud_kernel=false
  194. bpo_kernel=false
  195. install_recommends=true
  196. install='ca-certificates libpam-systemd'
  197. upgrade=
  198. kernel_params=
  199. force_lowmem=
  200. bbr=false
  201. ssh_port=
  202. hold=false
  203. power_off=false
  204. architecture=
  205. firmware=false
  206. force_efi_extra_removable=true
  207. grub_timeout=5
  208. dry_run=false
  209. apt_non_free_firmware=true
  210. apt_non_free=false
  211. apt_contrib=false
  212. apt_src=true
  213. apt_backports=true
  214. while [ $# -gt 0 ]; do
  215. case $1 in
  216. --cdn|--aws)
  217. mirror_protocol=https
  218. [ "$1" = '--aws' ] && mirror_host=cdn-aws.deb.debian.org
  219. security_repository=mirror
  220. ;;
  221. --china)
  222. dns='223.5.5.5 223.6.6.6'
  223. mirror_protocol=https
  224. mirror_host=mirrors.aliyun.com
  225. ntp=ntp.aliyun.com
  226. security_repository=mirror
  227. ;;
  228. --interface)
  229. interface=$2
  230. shift
  231. ;;
  232. --ip)
  233. ip=$2
  234. shift
  235. ;;
  236. --netmask)
  237. netmask=$2
  238. shift
  239. ;;
  240. --gateway)
  241. gateway=$2
  242. shift
  243. ;;
  244. --dns)
  245. dns=$2
  246. shift
  247. ;;
  248. --hostname)
  249. hostname=$2
  250. shift
  251. ;;
  252. --network-console)
  253. network_console=true
  254. ;;
  255. --version)
  256. set_debian_version "$2"
  257. shift
  258. ;;
  259. --suite)
  260. set_suite "$2"
  261. shift
  262. ;;
  263. --release-d-i)
  264. daily_d_i=false
  265. ;;
  266. --daily-d-i)
  267. daily_d_i=true
  268. ;;
  269. --mirror-protocol)
  270. mirror_protocol=$2
  271. shift
  272. ;;
  273. --https)
  274. mirror_protocol=https
  275. ;;
  276. --mirror-host)
  277. mirror_host=$2
  278. shift
  279. ;;
  280. --mirror-directory)
  281. mirror_directory=${2%/}
  282. shift
  283. ;;
  284. --mirror-proxy|--proxy)
  285. mirror_proxy=$2
  286. shift
  287. ;;
  288. --reuse-proxy)
  289. set_mirror_proxy
  290. ;;
  291. --security-repository)
  292. security_repository=$2
  293. shift
  294. ;;
  295. --no-user|--no-account-setup)
  296. account_setup=false
  297. ;;
  298. --user|--username)
  299. username=$2
  300. shift
  301. ;;
  302. --password)
  303. password=$2
  304. shift
  305. ;;
  306. --authorized-keys-url)
  307. authorized_keys_url=$2
  308. shift
  309. ;;
  310. --sudo-with-password)
  311. sudo_with_password=true
  312. ;;
  313. --timezone)
  314. timezone=$2
  315. shift
  316. ;;
  317. --ntp)
  318. ntp=$2
  319. shift
  320. ;;
  321. --no-part|--no-disk-partitioning)
  322. disk_partitioning=false
  323. ;;
  324. --force-lowmem)
  325. [ "$2" != 0 ] && [ "$2" != 1 ] && [ "$2" != 2 ] && err 'Low memory level can only be 0, 1 or 2'
  326. force_lowmem=$2
  327. shift
  328. ;;
  329. --disk)
  330. disk=$2
  331. shift
  332. ;;
  333. --no-force-gpt)
  334. force_gpt=false
  335. ;;
  336. --bios)
  337. efi=false
  338. ;;
  339. --efi)
  340. efi=true
  341. ;;
  342. --esp)
  343. esp=$2
  344. shift
  345. ;;
  346. --filesystem)
  347. filesystem=$2
  348. shift
  349. ;;
  350. --kernel)
  351. kernel=$2
  352. shift
  353. ;;
  354. --cloud-kernel)
  355. cloud_kernel=true
  356. ;;
  357. --bpo-kernel)
  358. bpo_kernel=true
  359. ;;
  360. --apt-non-free-firmware)
  361. apt_non_free_firmware=true
  362. ;;
  363. --apt-non-free)
  364. apt_non_free=true
  365. apt_contrib=true
  366. ;;
  367. --apt-contrib)
  368. apt_contrib=true
  369. ;;
  370. --apt-src)
  371. apt_src=true
  372. ;;
  373. --apt-backports)
  374. apt_backports=true
  375. ;;
  376. --no-apt-non-free-firmware)
  377. apt_non_free_firmware=false
  378. ;;
  379. --no-apt-non-free)
  380. apt_non_free=false
  381. ;;
  382. --no-apt-contrib)
  383. apt_contrib=false
  384. apt_non_free=false
  385. ;;
  386. --no-apt-src)
  387. apt_src=false
  388. ;;
  389. --no-apt-backports)
  390. apt_backports=false
  391. ;;
  392. --no-install-recommends)
  393. install_recommends=false
  394. ;;
  395. --install)
  396. install=$2
  397. shift
  398. ;;
  399. --no-upgrade)
  400. upgrade=none
  401. ;;
  402. --safe-upgrade)
  403. upgrade=safe-upgrade
  404. ;;
  405. --full-upgrade)
  406. upgrade=full-upgrade
  407. ;;
  408. --ethx)
  409. kernel_params="$kernel_params net.ifnames=0 biosdevname=0"
  410. ;;
  411. --bbr)
  412. bbr=true
  413. ;;
  414. --ssh-port)
  415. ssh_port=$2
  416. shift
  417. ;;
  418. --hold)
  419. hold=true
  420. ;;
  421. --power-off)
  422. power_off=true
  423. ;;
  424. --architecture)
  425. architecture=$2
  426. shift
  427. ;;
  428. --firmware)
  429. firmware=true
  430. ;;
  431. --no-force-efi-extra-removable)
  432. force_efi_extra_removable=false
  433. ;;
  434. --grub-timeout)
  435. grub_timeout=$2
  436. shift
  437. ;;
  438. --dry-run)
  439. dry_run=true
  440. ;;
  441. *)
  442. err "Unknown option: \"$1\""
  443. esac
  444. shift
  445. done
  446. [ -z "$architecture" ] && {
  447. architecture=$(dpkg --print-architecture 2> /dev/null) || {
  448. case $(uname -m) in
  449. x86_64)
  450. architecture=amd64
  451. ;;
  452. aarch64)
  453. architecture=arm64
  454. ;;
  455. i386)
  456. architecture=i386
  457. ;;
  458. *)
  459. err 'No "--architecture" specified'
  460. esac
  461. }
  462. }
  463. [ -z "$kernel" ] && {
  464. kernel="linux-image-$architecture"
  465. [ "$cloud_kernel" = true ] && has_cloud_kernel && kernel="linux-image-cloud-$architecture"
  466. [ "$bpo_kernel" = true ] && has_backports && install="$kernel/$suite-backports $install"
  467. }
  468. [ -n "$authorized_keys_url" ] && ! download "$authorized_keys_url" /dev/null &&
  469. err "Failed to download SSH authorized public keys from \"$authorized_keys_url\""
  470. non_free_firmware_available=false
  471. case $suite in
  472. bookworm|stable|trixie|testing|sid|unstable)
  473. non_free_firmware_available=true
  474. ;;
  475. *)
  476. apt_non_free_firmware=false
  477. esac
  478. apt_components=main
  479. [ "$apt_contrib" = true ] && apt_components="$apt_components contrib"
  480. [ "$apt_non_free" = true ] && apt_components="$apt_components non-free"
  481. [ "$apt_non_free_firmware" = true ] && apt_components="$apt_components non-free-firmware"
  482. apt_services=updates
  483. [ "$apt_backports" = true ] && apt_services="$apt_services, backports"
  484. installer_directory="/boot/debian-$suite"
  485. save_preseed='cat'
  486. [ "$dry_run" = false ] && {
  487. [ "$(id -u)" -ne 0 ] && err 'root privilege is required'
  488. rm -rf "$installer_directory"
  489. mkdir -p "$installer_directory"
  490. cd "$installer_directory"
  491. save_preseed='tee -a preseed.cfg'
  492. }
  493. if [ "$account_setup" = true ]; then
  494. prompt_password
  495. elif [ "$network_console" = true ] && [ -z "$authorized_keys_url" ]; then
  496. prompt_password "Choose a password for the installer user of the SSH network console: "
  497. fi
  498. $save_preseed << EOF
  499. # Localization
  500. d-i debian-installer/language string en
  501. d-i debian-installer/country string US
  502. d-i debian-installer/locale string en_US.UTF-8
  503. d-i keyboard-configuration/xkb-keymap select us
  504. # Network configuration
  505. d-i netcfg/choose_interface select $interface
  506. EOF
  507. [ -n "$ip" ] && {
  508. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  509. echo "d-i netcfg/get_ipaddress string $ip" | $save_preseed
  510. [ -n "$netmask" ] && echo "d-i netcfg/get_netmask string $netmask" | $save_preseed
  511. [ -n "$gateway" ] && echo "d-i netcfg/get_gateway string $gateway" | $save_preseed
  512. [ -z "${ip%%*:*}" ] && [ -n "${dns%%*:*}" ] && dns='2001:4860:4860::8888 2001:4860:4860::8844'
  513. [ -n "$dns" ] && echo "d-i netcfg/get_nameservers string $dns" | $save_preseed
  514. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  515. }
  516. if [ -n "$hostname" ]; then
  517. echo "d-i netcfg/hostname string $hostname" | $save_preseed
  518. hostname=debian
  519. domain=
  520. else
  521. hostname=$(cat /proc/sys/kernel/hostname)
  522. domain=$(cat /proc/sys/kernel/domainname)
  523. if [ "$domain" = '(none)' ]; then
  524. domain=
  525. else
  526. domain=" $domain"
  527. fi
  528. fi
  529. $save_preseed << EOF
  530. d-i netcfg/get_hostname string $hostname
  531. d-i netcfg/get_domain string$domain
  532. EOF
  533. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  534. [ "$network_console" = true ] && {
  535. $save_preseed << 'EOF'
  536. # Network console
  537. d-i anna/choose_modules string network-console
  538. d-i preseed/early_command string anna-install network-console
  539. EOF
  540. if [ -n "$authorized_keys_url" ]; then
  541. echo "d-i network-console/authorized_keys_url string $authorized_keys_url" | $save_preseed
  542. else
  543. $save_preseed << EOF
  544. d-i network-console/password password $password
  545. d-i network-console/password-again password $password
  546. EOF
  547. fi
  548. echo 'd-i network-console/start select Continue' | $save_preseed
  549. }
  550. $save_preseed << EOF
  551. # Mirror settings
  552. d-i mirror/country string manual
  553. d-i mirror/protocol string $mirror_protocol
  554. d-i mirror/$mirror_protocol/hostname string $mirror_host
  555. d-i mirror/$mirror_protocol/directory string $mirror_directory
  556. d-i mirror/$mirror_protocol/proxy string $mirror_proxy
  557. d-i mirror/suite string $suite
  558. EOF
  559. [ "$account_setup" = true ] && {
  560. password_hash=$(mkpasswd -m sha-256 "$password" 2> /dev/null) ||
  561. password_hash=$(openssl passwd -5 "$password" 2> /dev/null) ||
  562. password_hash=$(busybox mkpasswd -m sha256 "$password" 2> /dev/null) || {
  563. for python in python3 python python2; do
  564. password_hash=$("$python" -c 'import crypt, sys; print(crypt.crypt(sys.argv[1], crypt.mksalt(crypt.METHOD_SHA256)))' "$password" 2> /dev/null) && break
  565. done
  566. }
  567. $save_preseed << 'EOF'
  568. # Account setup
  569. EOF
  570. [ -n "$authorized_keys_url" ] && configure_sshd PasswordAuthentication no
  571. if [ "$username" = root ]; then
  572. if [ -z "$authorized_keys_url" ]; then
  573. configure_sshd PermitRootLogin yes
  574. else
  575. in_target "mkdir -m 0700 -p ~root/.ssh && busybox wget -O- \"$authorized_keys_url\" >> ~root/.ssh/authorized_keys"
  576. fi
  577. $save_preseed << 'EOF'
  578. d-i passwd/root-login boolean true
  579. d-i passwd/make-user boolean false
  580. EOF
  581. if [ -z "$password_hash" ]; then
  582. $save_preseed << EOF
  583. d-i passwd/root-password password $password
  584. d-i passwd/root-password-again password $password
  585. EOF
  586. else
  587. echo "d-i passwd/root-password-crypted password $password_hash" | $save_preseed
  588. fi
  589. else
  590. configure_sshd PermitRootLogin no
  591. [ -n "$authorized_keys_url" ] &&
  592. 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"
  593. [ "$sudo_with_password" = false ] &&
  594. in_target "echo \"$username ALL=(ALL:ALL) NOPASSWD:ALL\" > \"/etc/sudoers.d/90-user-$username\""
  595. $save_preseed << EOF
  596. d-i passwd/root-login boolean false
  597. d-i passwd/make-user boolean true
  598. d-i passwd/user-fullname string
  599. d-i passwd/username string $username
  600. EOF
  601. if [ -z "$password_hash" ]; then
  602. $save_preseed << EOF
  603. d-i passwd/user-password password $password
  604. d-i passwd/user-password-again password $password
  605. EOF
  606. else
  607. echo "d-i passwd/user-password-crypted password $password_hash" | $save_preseed
  608. fi
  609. fi
  610. }
  611. [ -n "$ssh_port" ] && configure_sshd Port "$ssh_port"
  612. $save_preseed << EOF
  613. # Clock and time zone setup
  614. d-i time/zone string $timezone
  615. d-i clock-setup/utc boolean true
  616. d-i clock-setup/ntp boolean true
  617. d-i clock-setup/ntp-server string $ntp
  618. # Partitioning
  619. EOF
  620. [ "$disk_partitioning" = true ] && {
  621. $save_preseed << 'EOF'
  622. d-i partman-auto/method string regular
  623. EOF
  624. if [ -n "$disk" ]; then
  625. echo "d-i partman-auto/disk string $disk" | $save_preseed
  626. else
  627. # shellcheck disable=SC2016
  628. echo 'd-i partman/early_command string debconf-set partman-auto/disk "$(list-devices disk | head -n 1)"' | $save_preseed
  629. fi
  630. }
  631. [ "$force_gpt" = true ] && {
  632. $save_preseed << 'EOF'
  633. d-i partman-partitioning/choose_label string gpt
  634. d-i partman-partitioning/default_label string gpt
  635. EOF
  636. }
  637. [ "$disk_partitioning" = true ] && {
  638. echo "d-i partman/default_filesystem string $filesystem" | $save_preseed
  639. [ -z "$efi" ] && {
  640. efi=false
  641. [ -d /sys/firmware/efi ] && efi=true
  642. }
  643. $save_preseed << 'EOF'
  644. d-i partman-auto/expert_recipe string \
  645. naive :: \
  646. EOF
  647. if [ "$efi" = true ]; then
  648. $save_preseed << EOF
  649. $esp $esp $esp free \\
  650. EOF
  651. $save_preseed << 'EOF'
  652. $iflabel{ gpt } \
  653. $reusemethod{ } \
  654. method{ efi } \
  655. format{ } \
  656. . \
  657. EOF
  658. else
  659. $save_preseed << 'EOF'
  660. 1 1 1 free \
  661. $iflabel{ gpt } \
  662. $reusemethod{ } \
  663. method{ biosgrub } \
  664. . \
  665. EOF
  666. fi
  667. $save_preseed << 'EOF'
  668. 1075 1076 -1 $default_filesystem \
  669. method{ format } \
  670. format{ } \
  671. use_filesystem{ } \
  672. $default_filesystem{ } \
  673. mountpoint{ / } \
  674. .
  675. EOF
  676. if [ "$efi" = true ]; then
  677. echo 'd-i partman-efi/non_efi_system boolean true' | $save_preseed
  678. fi
  679. $save_preseed << 'EOF'
  680. d-i partman-auto/choose_recipe select naive
  681. d-i partman-basicfilesystems/no_swap boolean false
  682. d-i partman-partitioning/confirm_write_new_label boolean true
  683. d-i partman/choose_partition select finish
  684. d-i partman/confirm boolean true
  685. d-i partman/confirm_nooverwrite boolean true
  686. EOF
  687. }
  688. $save_preseed << EOF
  689. # Base system installation
  690. d-i base-installer/kernel/image string $kernel
  691. EOF
  692. [ "$install_recommends" = false ] && echo "d-i base-installer/install-recommends boolean $install_recommends" | $save_preseed
  693. [ "$security_repository" = mirror ] && security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
  694. $save_preseed << EOF
  695. # Apt setup
  696. d-i apt-setup/contrib boolean $apt_contrib
  697. d-i apt-setup/non-free boolean $apt_non_free
  698. d-i apt-setup/enable-source-repositories boolean $apt_src
  699. d-i apt-setup/services-select multiselect $apt_services
  700. EOF
  701. [ "$non_free_firmware_available" = true ] && echo "d-i apt-setup/non-free-firmware boolean $apt_non_free_firmware" | $save_preseed
  702. # If not sid/unstable
  703. [ -n "$security_archive" ] && {
  704. $save_preseed << EOF
  705. d-i apt-setup/local0/repository string $security_repository $security_archive $apt_components
  706. d-i apt-setup/local0/source boolean $apt_src
  707. EOF
  708. }
  709. $save_preseed << 'EOF'
  710. # Package selection
  711. tasksel tasksel/first multiselect ssh-server
  712. EOF
  713. [ -n "$install" ] && echo "d-i pkgsel/include string $install" | $save_preseed
  714. [ -n "$upgrade" ] && echo "d-i pkgsel/upgrade select $upgrade" | $save_preseed
  715. $save_preseed << 'EOF'
  716. popularity-contest popularity-contest/participate boolean false
  717. # Boot loader installation
  718. EOF
  719. if [ -n "$disk" ]; then
  720. echo "d-i grub-installer/bootdev string $disk" | $save_preseed
  721. else
  722. echo 'd-i grub-installer/bootdev string default' | $save_preseed
  723. fi
  724. [ "$force_efi_extra_removable" = true ] && echo 'd-i grub-installer/force-efi-extra-removable boolean true' | $save_preseed
  725. [ -n "$kernel_params" ] && echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
  726. $save_preseed << 'EOF'
  727. # Finishing up the installation
  728. EOF
  729. [ "$hold" = false ] && echo 'd-i finish-install/reboot_in_progress note' | $save_preseed
  730. [ "$bbr" = true ] && in_target '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
  731. [ -n "$late_command" ] && echo "d-i preseed/late_command string in-target sh -c '$late_command'" | $save_preseed
  732. [ "$power_off" = true ] && echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  733. save_grub_cfg='cat'
  734. [ "$dry_run" = false ] && {
  735. base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  736. [ "$suite" = stretch ] && [ "$efi" = true ] && base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/buster/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  737. [ "$daily_d_i" = true ] && base_url="https://d-i.debian.org/daily-images/$architecture/daily/netboot/debian-installer/$architecture"
  738. firmware_url="https://cdimage.debian.org/cdimage/unofficial/non-free/firmware/$suite/current/firmware.cpio.gz"
  739. download "$base_url/linux" linux
  740. download "$base_url/initrd.gz" initrd.gz
  741. [ "$firmware" = true ] && download "$firmware_url" firmware.cpio.gz
  742. gzip -d initrd.gz
  743. # cpio reads a list of file names from the standard input
  744. echo preseed.cfg | cpio -o -H newc -A -F initrd
  745. gzip -1 initrd
  746. mkdir -p /etc/default/grub.d
  747. tee /etc/default/grub.d/zz-debi.cfg 1>&2 << EOF
  748. GRUB_DEFAULT=debi
  749. GRUB_TIMEOUT=$grub_timeout
  750. GRUB_TIMEOUT_STYLE=menu
  751. EOF
  752. if command_exists update-grub; then
  753. grub_cfg=/boot/grub/grub.cfg
  754. update-grub
  755. elif command_exists grub2-mkconfig; then
  756. tmp=$(mktemp)
  757. grep -vF zz_debi /etc/default/grub > "$tmp"
  758. cat "$tmp" > /etc/default/grub
  759. rm "$tmp"
  760. # shellcheck disable=SC2016
  761. echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
  762. grub_cfg=/boot/grub2/grub.cfg
  763. grub2-mkconfig -o "$grub_cfg"
  764. else
  765. err 'Could not find "update-grub" or "grub2-mkconfig" command'
  766. fi
  767. save_grub_cfg="tee -a $grub_cfg"
  768. }
  769. mkrelpath=$installer_directory
  770. [ "$dry_run" = true ] && mkrelpath=/boot
  771. installer_directory=$(grub-mkrelpath "$mkrelpath" 2> /dev/null) ||
  772. installer_directory=$(grub2-mkrelpath "$mkrelpath" 2> /dev/null) || {
  773. err 'Could not find "grub-mkrelpath" or "grub2-mkrelpath" command'
  774. }
  775. [ "$dry_run" = true ] && installer_directory="$installer_directory/debian-$suite"
  776. kernel_params="$kernel_params lowmem/low=1"
  777. [ -n "$force_lowmem" ] && kernel_params="$kernel_params lowmem=+$force_lowmem"
  778. initrd="$installer_directory/initrd.gz"
  779. [ "$firmware" = true ] && initrd="$initrd $installer_directory/firmware.cpio.gz"
  780. $save_grub_cfg 1>&2 << EOF
  781. menuentry 'Debian Installer' --id debi {
  782. insmod part_msdos
  783. insmod part_gpt
  784. insmod ext2
  785. insmod xfs
  786. insmod btrfs
  787. linux $installer_directory/linux$kernel_params
  788. initrd $initrd
  789. }
  790. EOF