debi.sh 17 KB

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