debi.sh 22 KB

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