debi.sh 21 KB

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