debi.sh 19 KB

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