debi.sh 26 KB

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