debi.sh 20 KB

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