debi.sh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. in_target_script=
  17. in_target() {
  18. local command=
  19. for argument in "$@"; do
  20. command="$command $argument"
  21. done
  22. if [ -n "$command" ]; then
  23. [ -z "$in_target_script" ] && in_target_script='true'
  24. in_target_script="$in_target_script;$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. buster|oldoldstable)
  96. security_archive="$suite/updates"
  97. ;;
  98. bullseye|oldstable|bookworm|stable|trixie|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. buster|oldoldstable|bullseye|oldstable|bookworm|stable)
  111. daily_d_i=false
  112. ;;
  113. trixie|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. 10|buster|oldoldstable)
  128. set_suite buster
  129. ;;
  130. 11|bullseye|oldstable)
  131. set_suite bullseye
  132. ;;
  133. 12|bookworm|stable)
  134. set_suite bookworm
  135. ;;
  136. 13|trixie|testing)
  137. set_suite trixie
  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. buster|oldoldstable)
  149. [ "$architecture" = amd64 ] && return
  150. [ "$architecture" = arm64 ] && [ "$bpo_kernel" = true ] && return
  151. ;;
  152. bullseye|oldstable|bookworm|stable|trixie|testing|sid|unstable)
  153. [ "$architecture" = amd64 ] || [ "$architecture" = arm64 ] && return
  154. esac
  155. local tmp; tmp=''; [ "$bpo_kernel" = true ] && tmp='-backports'
  156. warn "No cloud kernel is available for $architecture/$suite$tmp"
  157. return 1
  158. }
  159. has_backports() {
  160. case $suite in
  161. buster|oldoldstable|bullseye|oldstable|bookworm|stable|trixie|testing) return
  162. esac
  163. warn "No backports kernel is available for $suite"
  164. return 1
  165. }
  166. interface=auto
  167. ip=
  168. netmask=
  169. gateway=
  170. dns='8.8.8.8 8.8.4.4'
  171. dns6='2001:4860:4860::8888 2001:4860:4860::8844'
  172. hostname=
  173. network_console=false
  174. set_debian_version 12
  175. mirror_protocol=https
  176. mirror_host=deb.debian.org
  177. mirror_directory=/debian
  178. mirror_proxy=
  179. security_repository=mirror
  180. account_setup=true
  181. username=debian
  182. password=
  183. authorized_keys_url=
  184. sudo_with_password=false
  185. timezone=UTC
  186. ntp=time.google.com
  187. disk_partitioning=true
  188. disk="/dev/$(lsblk -no PKNAME "$(df /boot | grep -Eo '/dev/[a-z0-9]+')")"
  189. force_gpt=true
  190. efi=
  191. esp=106
  192. filesystem=ext4
  193. kernel=
  194. cloud_kernel=false
  195. bpo_kernel=false
  196. install_recommends=true
  197. install=
  198. upgrade=
  199. kernel_params=
  200. force_lowmem=
  201. bbr=false
  202. ssh_port=
  203. hold=false
  204. power_off=false
  205. architecture=
  206. firmware=false
  207. force_efi_extra_removable=true
  208. grub_timeout=5
  209. dry_run=false
  210. apt_non_free_firmware=true
  211. apt_non_free=false
  212. apt_contrib=false
  213. apt_src=true
  214. apt_backports=true
  215. cidata=
  216. while [ $# -gt 0 ]; do
  217. case $1 in
  218. --cdn)
  219. ;;
  220. --aws)
  221. mirror_host=cdn-aws.deb.debian.org
  222. ntp=time.aws.com
  223. ;;
  224. --cloudflare)
  225. dns='1.1.1.1 1.0.0.1'
  226. dns6='2606:4700:4700::1111 2606:4700:4700::1001'
  227. ntp=time.cloudflare.com
  228. ;;
  229. --aliyun)
  230. dns='223.5.5.5 223.6.6.6'
  231. dns6='2400:3200::1 2400:3200:baba::1'
  232. mirror_host=mirrors.aliyun.com
  233. ntp=time.amazonaws.cn
  234. ;;
  235. --ustc|--china)
  236. dns='119.29.29.29'
  237. dns6='2402:4e00::'
  238. mirror_host=mirrors.ustc.edu.cn
  239. ntp=time.amazonaws.cn
  240. ;;
  241. --tuna)
  242. dns='119.29.29.29'
  243. dns6='2402:4e00::'
  244. mirror_host=mirrors.tuna.tsinghua.edu.cn
  245. ntp=time.amazonaws.cn
  246. ;;
  247. --static-ipv4)
  248. ip=$(ip r get 1.1.1.1 | awk '/src/ {print $7}')
  249. gateway=$(ip r get 1.1.1.1 | awk '/via/ {print $3}')
  250. _cidr=$(ip -o -f inet addr show | grep -w "$ip" | awk '{print $4}' | cut -d'/' -f2)
  251. ip="$ip/$_cidr"
  252. hostname=$(hostname)
  253. interface=$(ip r get 1.1.1.1 | awk '/dev/ {print $5}')
  254. ;;
  255. --interface)
  256. interface=$2
  257. shift
  258. ;;
  259. --ip)
  260. ip=$2
  261. shift
  262. ;;
  263. --netmask)
  264. netmask=$2
  265. shift
  266. ;;
  267. --gateway)
  268. gateway=$2
  269. shift
  270. ;;
  271. --dns)
  272. dns=$2
  273. shift
  274. ;;
  275. --dns6)
  276. dns6=$2
  277. shift
  278. ;;
  279. --hostname)
  280. hostname=$2
  281. shift
  282. ;;
  283. --network-console)
  284. network_console=true
  285. ;;
  286. --version)
  287. set_debian_version "$2"
  288. shift
  289. ;;
  290. --suite)
  291. set_suite "$2"
  292. shift
  293. ;;
  294. --release-d-i)
  295. daily_d_i=false
  296. ;;
  297. --daily-d-i)
  298. daily_d_i=true
  299. ;;
  300. --mirror-protocol)
  301. mirror_protocol=$2
  302. shift
  303. ;;
  304. --https)
  305. mirror_protocol=https
  306. ;;
  307. --mirror-host)
  308. mirror_host=$2
  309. shift
  310. ;;
  311. --mirror-directory)
  312. mirror_directory=${2%/}
  313. shift
  314. ;;
  315. --mirror-proxy|--proxy)
  316. mirror_proxy=$2
  317. shift
  318. ;;
  319. --reuse-proxy)
  320. set_mirror_proxy
  321. ;;
  322. --security-repository)
  323. security_repository=$2
  324. shift
  325. ;;
  326. --no-user|--no-account-setup)
  327. account_setup=false
  328. ;;
  329. --user|--username)
  330. username=$2
  331. shift
  332. ;;
  333. --password)
  334. password=$2
  335. shift
  336. ;;
  337. --authorized-keys-url)
  338. authorized_keys_url=$2
  339. shift
  340. ;;
  341. --sudo-with-password)
  342. sudo_with_password=true
  343. ;;
  344. --timezone)
  345. timezone=$2
  346. shift
  347. ;;
  348. --ntp)
  349. ntp=$2
  350. shift
  351. ;;
  352. --no-part|--no-disk-partitioning)
  353. disk_partitioning=false
  354. ;;
  355. --force-lowmem)
  356. [ "$2" != 0 ] && [ "$2" != 1 ] && [ "$2" != 2 ] && err 'Low memory level can only be 0, 1 or 2'
  357. force_lowmem=$2
  358. shift
  359. ;;
  360. --disk)
  361. disk=$2
  362. shift
  363. ;;
  364. --no-force-gpt)
  365. force_gpt=false
  366. ;;
  367. --bios)
  368. efi=false
  369. ;;
  370. --efi)
  371. efi=true
  372. ;;
  373. --esp)
  374. esp=$2
  375. shift
  376. ;;
  377. --filesystem)
  378. filesystem=$2
  379. shift
  380. ;;
  381. --kernel)
  382. kernel=$2
  383. shift
  384. ;;
  385. --cloud-kernel)
  386. cloud_kernel=true
  387. ;;
  388. --bpo-kernel)
  389. bpo_kernel=true
  390. ;;
  391. --apt-non-free-firmware)
  392. apt_non_free_firmware=true
  393. ;;
  394. --apt-non-free)
  395. apt_non_free=true
  396. apt_contrib=true
  397. ;;
  398. --apt-contrib)
  399. apt_contrib=true
  400. ;;
  401. --apt-src)
  402. apt_src=true
  403. ;;
  404. --apt-backports)
  405. apt_backports=true
  406. ;;
  407. --no-apt-non-free-firmware)
  408. apt_non_free_firmware=false
  409. ;;
  410. --no-apt-non-free)
  411. apt_non_free=false
  412. ;;
  413. --no-apt-contrib)
  414. apt_contrib=false
  415. apt_non_free=false
  416. ;;
  417. --no-apt-src)
  418. apt_src=false
  419. ;;
  420. --no-apt-backports)
  421. apt_backports=false
  422. ;;
  423. --no-install-recommends)
  424. install_recommends=false
  425. ;;
  426. --install)
  427. install=$2
  428. shift
  429. ;;
  430. --no-upgrade)
  431. upgrade=none
  432. ;;
  433. --safe-upgrade)
  434. upgrade=safe-upgrade
  435. ;;
  436. --full-upgrade)
  437. upgrade=full-upgrade
  438. ;;
  439. --ethx)
  440. kernel_params="$kernel_params net.ifnames=0 biosdevname=0"
  441. ;;
  442. --bbr)
  443. bbr=true
  444. ;;
  445. --ssh-port)
  446. ssh_port=$2
  447. shift
  448. ;;
  449. --hold)
  450. hold=true
  451. ;;
  452. --power-off)
  453. power_off=true
  454. ;;
  455. --architecture)
  456. architecture=$2
  457. shift
  458. ;;
  459. --firmware)
  460. firmware=true
  461. ;;
  462. --no-force-efi-extra-removable)
  463. force_efi_extra_removable=false
  464. ;;
  465. --grub-timeout)
  466. grub_timeout=$2
  467. shift
  468. ;;
  469. --dry-run)
  470. dry_run=true
  471. ;;
  472. --cidata)
  473. cidata=$(realpath "$2")
  474. [ ! -f "$cidata/meta-data" ] && err 'No "meta-data" file found in the cloud-init directory'
  475. [ ! -f "$cidata/user-data" ] && err 'No "user-data" file found in the cloud-init directory'
  476. shift
  477. ;;
  478. *)
  479. err "Unknown option: \"$1\""
  480. esac
  481. shift
  482. done
  483. [ -z "$architecture" ] && {
  484. architecture=$(dpkg --print-architecture 2> /dev/null) || {
  485. case $(uname -m) in
  486. x86_64)
  487. architecture=amd64
  488. ;;
  489. aarch64)
  490. architecture=arm64
  491. ;;
  492. i386)
  493. architecture=i386
  494. ;;
  495. *)
  496. err 'No "--architecture" specified'
  497. esac
  498. }
  499. }
  500. [ -z "$kernel" ] && {
  501. kernel="linux-image-$architecture"
  502. [ "$cloud_kernel" = true ] && has_cloud_kernel && kernel="linux-image-cloud-$architecture"
  503. [ "$bpo_kernel" = true ] && has_backports && install="$kernel/$suite-backports $install"
  504. }
  505. [ -n "$authorized_keys_url" ] && ! download "$authorized_keys_url" /dev/null &&
  506. err "Failed to download SSH authorized public keys from \"$authorized_keys_url\""
  507. non_free_firmware_available=false
  508. case $suite in
  509. bookworm|stable|trixie|testing|sid|unstable)
  510. non_free_firmware_available=true
  511. ;;
  512. *)
  513. apt_non_free_firmware=false
  514. esac
  515. apt_components=main
  516. [ "$apt_contrib" = true ] && apt_components="$apt_components contrib"
  517. [ "$apt_non_free" = true ] && apt_components="$apt_components non-free"
  518. [ "$apt_non_free_firmware" = true ] && apt_components="$apt_components non-free-firmware"
  519. apt_services=updates
  520. [ "$apt_backports" = true ] && apt_services="$apt_services, backports"
  521. installer_directory="/boot/debian-$suite"
  522. save_preseed='cat'
  523. [ "$dry_run" = false ] && {
  524. [ "$(id -u)" -ne 0 ] && err 'root privilege is required'
  525. rm -rf "$installer_directory"
  526. mkdir -p "$installer_directory"
  527. cd "$installer_directory"
  528. save_preseed='tee -a preseed.cfg'
  529. }
  530. if [ "$account_setup" = true ]; then
  531. prompt_password
  532. elif [ "$network_console" = true ] && [ -z "$authorized_keys_url" ]; then
  533. prompt_password "Choose a password for the installer user of the SSH network console: "
  534. fi
  535. $save_preseed << EOF
  536. # Localization
  537. d-i debian-installer/language string en
  538. d-i debian-installer/country string US
  539. d-i debian-installer/locale string en_US.UTF-8
  540. d-i keyboard-configuration/xkb-keymap select us
  541. # Network configuration
  542. d-i netcfg/choose_interface select $interface
  543. EOF
  544. [ -n "$ip" ] && {
  545. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  546. echo "d-i netcfg/get_ipaddress string $ip" | $save_preseed
  547. [ -n "$netmask" ] && echo "d-i netcfg/get_netmask string $netmask" | $save_preseed
  548. [ -n "$gateway" ] && echo "d-i netcfg/get_gateway string $gateway" | $save_preseed
  549. [ -z "${ip%%*:*}" ] && [ -n "${dns%%*:*}" ] && dns="$dns6"
  550. [ -n "$dns" ] && echo "d-i netcfg/get_nameservers string $dns" | $save_preseed
  551. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  552. }
  553. if [ -n "$hostname" ]; then
  554. echo "d-i netcfg/hostname string $hostname" | $save_preseed
  555. hostname=debian
  556. domain=
  557. else
  558. hostname=$(cat /proc/sys/kernel/hostname)
  559. domain=$(cat /proc/sys/kernel/domainname)
  560. if [ "$domain" = '(none)' ]; then
  561. domain=
  562. else
  563. domain=" $domain"
  564. fi
  565. fi
  566. $save_preseed << EOF
  567. d-i netcfg/get_hostname string $hostname
  568. d-i netcfg/get_domain string$domain
  569. EOF
  570. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  571. [ "$network_console" = true ] && {
  572. $save_preseed << 'EOF'
  573. # Network console
  574. d-i anna/choose_modules string network-console
  575. d-i preseed/early_command string anna-install network-console
  576. EOF
  577. if [ -n "$authorized_keys_url" ]; then
  578. echo "d-i network-console/authorized_keys_url string $authorized_keys_url" | $save_preseed
  579. else
  580. $save_preseed << EOF
  581. d-i network-console/password password $password
  582. d-i network-console/password-again password $password
  583. EOF
  584. fi
  585. echo 'd-i network-console/start select Continue' | $save_preseed
  586. }
  587. $save_preseed << EOF
  588. # Mirror settings
  589. d-i mirror/country string manual
  590. d-i mirror/protocol string $mirror_protocol
  591. d-i mirror/$mirror_protocol/hostname string $mirror_host
  592. d-i mirror/$mirror_protocol/directory string $mirror_directory
  593. d-i mirror/$mirror_protocol/proxy string $mirror_proxy
  594. d-i mirror/suite string $suite
  595. EOF
  596. [ "$account_setup" = true ] && {
  597. password_hash=$(mkpasswd -m sha-256 "$password" 2> /dev/null) ||
  598. password_hash=$(openssl passwd -5 "$password" 2> /dev/null) ||
  599. password_hash=$(busybox mkpasswd -m sha256 "$password" 2> /dev/null) || {
  600. for python in python3 python python2; do
  601. password_hash=$("$python" -c 'import crypt, sys; print(crypt.crypt(sys.argv[1], crypt.mksalt(crypt.METHOD_SHA256)))' "$password" 2> /dev/null) && break
  602. done
  603. }
  604. $save_preseed << 'EOF'
  605. # Account setup
  606. EOF
  607. [ -n "$authorized_keys_url" ] && configure_sshd PasswordAuthentication no
  608. if [ "$username" = root ]; then
  609. if [ -z "$authorized_keys_url" ]; then
  610. configure_sshd PermitRootLogin yes
  611. else
  612. in_target "mkdir -m 0700 -p ~root/.ssh && busybox wget -O- \"$authorized_keys_url\" >> ~root/.ssh/authorized_keys"
  613. fi
  614. $save_preseed << 'EOF'
  615. d-i passwd/root-login boolean true
  616. d-i passwd/make-user boolean false
  617. EOF
  618. if [ -z "$password_hash" ]; then
  619. $save_preseed << EOF
  620. d-i passwd/root-password password $password
  621. d-i passwd/root-password-again password $password
  622. EOF
  623. else
  624. echo "d-i passwd/root-password-crypted password $password_hash" | $save_preseed
  625. fi
  626. else
  627. configure_sshd PermitRootLogin no
  628. [ -n "$authorized_keys_url" ] &&
  629. 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"
  630. [ "$sudo_with_password" = false ] &&
  631. in_target "echo \"$username ALL=(ALL:ALL) NOPASSWD:ALL\" > \"/etc/sudoers.d/90-user-$username\""
  632. $save_preseed << EOF
  633. d-i passwd/root-login boolean false
  634. d-i passwd/make-user boolean true
  635. d-i passwd/user-fullname string
  636. d-i passwd/username string $username
  637. EOF
  638. if [ -z "$password_hash" ]; then
  639. $save_preseed << EOF
  640. d-i passwd/user-password password $password
  641. d-i passwd/user-password-again password $password
  642. EOF
  643. else
  644. echo "d-i passwd/user-password-crypted password $password_hash" | $save_preseed
  645. fi
  646. fi
  647. }
  648. [ -n "$ssh_port" ] && configure_sshd Port "$ssh_port"
  649. $save_preseed << EOF
  650. # Clock and time zone setup
  651. d-i time/zone string $timezone
  652. d-i clock-setup/utc boolean true
  653. d-i clock-setup/ntp boolean true
  654. d-i clock-setup/ntp-server string $ntp
  655. # Partitioning
  656. EOF
  657. [ "$disk_partitioning" = true ] && {
  658. $save_preseed << 'EOF'
  659. d-i partman-auto/method string regular
  660. EOF
  661. if [ -n "$disk" ]; then
  662. echo "d-i partman-auto/disk string $disk" | $save_preseed
  663. else
  664. # shellcheck disable=SC2016
  665. echo 'd-i partman/early_command string debconf-set partman-auto/disk "$(list-devices disk | head -n 1)"' | $save_preseed
  666. fi
  667. }
  668. [ "$force_gpt" = true ] && {
  669. $save_preseed << 'EOF'
  670. d-i partman-partitioning/choose_label string gpt
  671. d-i partman-partitioning/default_label string gpt
  672. EOF
  673. }
  674. [ "$disk_partitioning" = true ] && {
  675. echo "d-i partman/default_filesystem string $filesystem" | $save_preseed
  676. [ -z "$efi" ] && {
  677. efi=false
  678. [ -d /sys/firmware/efi ] && efi=true
  679. }
  680. $save_preseed << 'EOF'
  681. d-i partman-auto/expert_recipe string \
  682. naive :: \
  683. EOF
  684. if [ "$efi" = true ]; then
  685. $save_preseed << EOF
  686. $esp $esp $esp free \\
  687. EOF
  688. $save_preseed << 'EOF'
  689. $iflabel{ gpt } \
  690. $reusemethod{ } \
  691. method{ efi } \
  692. format{ } \
  693. . \
  694. EOF
  695. else
  696. $save_preseed << 'EOF'
  697. 1 1 1 free \
  698. $iflabel{ gpt } \
  699. $reusemethod{ } \
  700. method{ biosgrub } \
  701. . \
  702. EOF
  703. fi
  704. $save_preseed << 'EOF'
  705. 1075 1076 -1 $default_filesystem \
  706. method{ format } \
  707. format{ } \
  708. use_filesystem{ } \
  709. $default_filesystem{ } \
  710. mountpoint{ / } \
  711. .
  712. EOF
  713. if [ "$efi" = true ]; then
  714. echo 'd-i partman-efi/non_efi_system boolean true' | $save_preseed
  715. fi
  716. $save_preseed << 'EOF'
  717. d-i partman-auto/choose_recipe select naive
  718. d-i partman-basicfilesystems/no_swap boolean false
  719. d-i partman-partitioning/confirm_write_new_label boolean true
  720. d-i partman/choose_partition select finish
  721. d-i partman/confirm boolean true
  722. d-i partman/confirm_nooverwrite boolean true
  723. d-i partman-lvm/device_remove_lvm boolean true
  724. EOF
  725. }
  726. $save_preseed << EOF
  727. # Base system installation
  728. d-i base-installer/kernel/image string $kernel
  729. EOF
  730. [ "$install_recommends" = false ] && echo "d-i base-installer/install-recommends boolean $install_recommends" | $save_preseed
  731. [ "$security_repository" = mirror ] && security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
  732. $save_preseed << EOF
  733. # Apt setup
  734. d-i apt-setup/contrib boolean $apt_contrib
  735. d-i apt-setup/non-free boolean $apt_non_free
  736. d-i apt-setup/enable-source-repositories boolean $apt_src
  737. d-i apt-setup/services-select multiselect $apt_services
  738. EOF
  739. [ "$non_free_firmware_available" = true ] && echo "d-i apt-setup/non-free-firmware boolean $apt_non_free_firmware" | $save_preseed
  740. # If not sid/unstable
  741. [ -n "$security_archive" ] && {
  742. $save_preseed << EOF
  743. d-i apt-setup/local0/repository string $security_repository $security_archive $apt_components
  744. d-i apt-setup/local0/source boolean $apt_src
  745. EOF
  746. }
  747. $save_preseed << 'EOF'
  748. # Package selection
  749. tasksel tasksel/first multiselect ssh-server
  750. EOF
  751. install="$install ca-certificates libpam-systemd"
  752. [ -n "$cidata" ] && install="$install cloud-init"
  753. [ -n "$install" ] && echo "d-i pkgsel/include string $install" | $save_preseed
  754. [ -n "$upgrade" ] && echo "d-i pkgsel/upgrade select $upgrade" | $save_preseed
  755. $save_preseed << 'EOF'
  756. popularity-contest popularity-contest/participate boolean false
  757. # Boot loader installation
  758. EOF
  759. if [ -n "$disk" ]; then
  760. echo "d-i grub-installer/bootdev string $disk" | $save_preseed
  761. else
  762. echo 'd-i grub-installer/bootdev string default' | $save_preseed
  763. fi
  764. [ "$force_efi_extra_removable" = true ] && echo 'd-i grub-installer/force-efi-extra-removable boolean true' | $save_preseed
  765. [ -n "$kernel_params" ] && echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
  766. $save_preseed << 'EOF'
  767. # Finishing up the installation
  768. EOF
  769. [ "$hold" = false ] && echo 'd-i finish-install/reboot_in_progress note' | $save_preseed
  770. [ "$bbr" = true ] && in_target '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
  771. [ -n "$cidata" ] && in_target 'echo "{ datasource_list: [ NoCloud ], datasource: { NoCloud: { fs_label: ~ } } }" > /etc/cloud/cloud.cfg.d/99_debi.cfg'
  772. late_command='true'
  773. [ -n "$in_target_script" ] && late_command="$late_command; in-target sh -c '$in_target_script'"
  774. [ -n "$cidata" ] && late_command="$late_command; mkdir -p /target/var/lib/cloud/seed/nocloud; cp -r /cidata/. /target/var/lib/cloud/seed/nocloud/"
  775. echo "d-i preseed/late_command string $late_command" | $save_preseed
  776. [ "$power_off" = true ] && echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  777. save_grub_cfg='cat'
  778. [ "$dry_run" = false ] && {
  779. base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  780. [ "$suite" = stretch ] && [ "$efi" = true ] && base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/buster/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  781. [ "$daily_d_i" = true ] && base_url="https://d-i.debian.org/daily-images/$architecture/daily/netboot/debian-installer/$architecture"
  782. firmware_url="https://cdimage.debian.org/cdimage/unofficial/non-free/firmware/$suite/current/firmware.cpio.gz"
  783. download "$base_url/linux" linux
  784. download "$base_url/initrd.gz" initrd.gz
  785. [ "$firmware" = true ] && download "$firmware_url" firmware.cpio.gz
  786. gzip -d initrd.gz
  787. # cpio reads a list of file names from the standard input
  788. echo preseed.cfg | cpio -o -H newc -A -F initrd
  789. if [ -n "$cidata" ]; then
  790. cp -r "$cidata" cidata
  791. find cidata | cpio -o -H newc -A -F initrd
  792. fi
  793. gzip -1 initrd
  794. mkdir -p /etc/default/grub.d
  795. tee /etc/default/grub.d/zz-debi.cfg 1>&2 << EOF
  796. GRUB_DEFAULT=debi
  797. GRUB_TIMEOUT=$grub_timeout
  798. GRUB_TIMEOUT_STYLE=menu
  799. EOF
  800. if command_exists update-grub; then
  801. grub_cfg=/boot/grub/grub.cfg
  802. update-grub
  803. elif command_exists grub2-mkconfig; then
  804. tmp=$(mktemp)
  805. grep -vF zz_debi /etc/default/grub > "$tmp"
  806. cat "$tmp" > /etc/default/grub
  807. rm "$tmp"
  808. # shellcheck disable=SC2016
  809. echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
  810. grub_cfg=/boot/grub2/grub.cfg
  811. [ -d /sys/firmware/efi ] && grub_cfg=/boot/efi/EFI/*/grub.cfg
  812. grub2-mkconfig -o "$grub_cfg"
  813. elif command_exists grub-mkconfig; then
  814. tmp=$(mktemp)
  815. grep -vF zz_debi /etc/default/grub > "$tmp"
  816. cat "$tmp" > /etc/default/grub
  817. rm "$tmp"
  818. # shellcheck disable=SC2016
  819. echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
  820. grub_cfg=/boot/grub/grub.cfg
  821. grub-mkconfig -o "$grub_cfg"
  822. else
  823. err 'Could not find "update-grub" or "grub2-mkconfig" or "grub-mkconfig" command'
  824. fi
  825. save_grub_cfg="tee -a $grub_cfg"
  826. }
  827. mkrelpath=$installer_directory
  828. [ "$dry_run" = true ] && mkrelpath=/boot
  829. installer_directory=$(grub-mkrelpath "$mkrelpath" 2> /dev/null) ||
  830. installer_directory=$(grub2-mkrelpath "$mkrelpath" 2> /dev/null) || {
  831. err 'Could not find "grub-mkrelpath" or "grub2-mkrelpath" command'
  832. }
  833. [ "$dry_run" = true ] && installer_directory="$installer_directory/debian-$suite"
  834. kernel_params="$kernel_params lowmem/low=1"
  835. [ -n "$force_lowmem" ] && kernel_params="$kernel_params lowmem=+$force_lowmem"
  836. initrd="$installer_directory/initrd.gz"
  837. [ "$firmware" = true ] && initrd="$initrd $installer_directory/firmware.cpio.gz"
  838. $save_grub_cfg 1>&2 << EOF
  839. menuentry 'Debian Installer' --id debi {
  840. insmod part_msdos
  841. insmod part_gpt
  842. insmod ext2
  843. insmod xfs
  844. insmod btrfs
  845. linux $installer_directory/linux$kernel_params
  846. initrd $initrd
  847. }
  848. EOF