debi.sh 20 KB

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