debi.sh 18 KB

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