debi.sh 25 KB

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