debi.sh 24 KB

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