debi.sh 18 KB

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