debi.sh 21 KB

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