debi.sh 18 KB

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