debi.sh 21 KB

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