debi.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. if [ -z "$architecture" ]; then
  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. fi
  288. if [ -z "$kernel" ]; then
  289. if [ "$cloud_kernel" = true ]; then
  290. if [ "$architecture" != amd64 ] && [ "$architecture" != arm64 ]; then
  291. err 'Cloud kernel is only available for amd64 (x86_64) and arm64 (aarch64) architectures'
  292. fi
  293. kernel="linux-image-cloud-$architecture"
  294. else
  295. kernel="linux-image-$architecture"
  296. fi
  297. if [ "$bpo_kernel" = true ]; then
  298. if [ "$suite" = sid ] || [ "$suite" = unstable ]; then
  299. err 'Backports kernel is not available for sid/unstable distribution'
  300. fi
  301. kernel="$kernel/$suite-backports"
  302. fi
  303. fi
  304. if [ -n "$authorized_keys_url" ] && ! download "$authorized_keys_url" /dev/null; then
  305. err "Failed to download SSH authorized public keys from \"$authorized_keys_url\""
  306. fi
  307. installer="debian-$suite"
  308. installer_directory="/boot/$installer"
  309. save_preseed='cat'
  310. if [ "$dry_run" = false ]; then
  311. [ "$(id -u)" -ne 0 ] && err 'root privilege is required'
  312. rm -rf "$installer_directory"
  313. mkdir -p "$installer_directory"
  314. cd "$installer_directory"
  315. save_preseed='tee -a preseed.cfg'
  316. fi
  317. if [ "$account_setup" = true ]; then
  318. prompt_password
  319. elif [ "$network_console" = true ] && [ -z "$authorized_keys_url" ]; then
  320. prompt_password "Choose a password for the installer user of the SSH network console: "
  321. fi
  322. $save_preseed << 'EOF'
  323. # Localization
  324. d-i debian-installer/language string en
  325. d-i debian-installer/country string US
  326. d-i debian-installer/locale string en_US.UTF-8
  327. d-i keyboard-configuration/xkb-keymap select us
  328. # Network configuration
  329. d-i netcfg/choose_interface select auto
  330. EOF
  331. if [ -n "$ip" ]; then
  332. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  333. echo "d-i netcfg/get_ipaddress string $ip" | $save_preseed
  334. [ -n "$netmask" ] && echo "d-i netcfg/get_netmask string $netmask" | $save_preseed
  335. [ -n "$gateway" ] && echo "d-i netcfg/get_gateway string $gateway" | $save_preseed
  336. [ -z "${ip%%*:*}" ] && [ -n "${dns%%*:*}" ] && dns='2001:4860:4860::8888 2001:4860:4860::8844'
  337. [ -n "$dns" ] && echo "d-i netcfg/get_nameservers string $dns" | $save_preseed
  338. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  339. fi
  340. if [ -n "$hostname" ]; then
  341. echo "d-i netcfg/hostname string $hostname" | $save_preseed
  342. hostname=debian
  343. domain=
  344. else
  345. hostname=$(cat /proc/sys/kernel/hostname)
  346. domain=$(cat /proc/sys/kernel/domainname)
  347. if [ "$domain" = '(none)' ]; then
  348. domain=
  349. else
  350. domain=" $domain"
  351. fi
  352. fi
  353. $save_preseed << EOF
  354. d-i netcfg/get_hostname string $hostname
  355. d-i netcfg/get_domain string$domain
  356. EOF
  357. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  358. if [ "$network_console" = true ]; then
  359. $save_preseed << 'EOF'
  360. # Network console
  361. d-i anna/choose_modules string network-console
  362. d-i preseed/early_command string anna-install network-console
  363. EOF
  364. if [ -n "$authorized_keys_url" ]; then
  365. echo "d-i network-console/authorized_keys_url string $authorized_keys_url" | $save_preseed
  366. else
  367. $save_preseed << EOF
  368. d-i network-console/password password $password
  369. d-i network-console/password-again password $password
  370. EOF
  371. fi
  372. echo 'd-i network-console/start select Continue' | $save_preseed
  373. fi
  374. $save_preseed << EOF
  375. # Mirror settings
  376. d-i mirror/country string manual
  377. d-i mirror/protocol string $mirror_protocol
  378. d-i mirror/$mirror_protocol/hostname string $mirror_host
  379. d-i mirror/$mirror_protocol/directory string $mirror_directory
  380. d-i mirror/$mirror_protocol/proxy string
  381. d-i mirror/suite string $suite
  382. d-i mirror/udeb/suite string $suite
  383. EOF
  384. if [ "$account_setup" = true ]; then
  385. password_hash=$(mkpasswd -m sha-256 "$password" 2> /dev/null) ||
  386. password_hash=$(openssl passwd -5 "$password" 2> /dev/null) ||
  387. password_hash=$(busybox mkpasswd -m sha256 "$password" 2> /dev/null) || {
  388. for python in python3 python python2; do
  389. password_hash=$("$python" -c 'import crypt, sys; print(crypt.crypt(sys.argv[1], crypt.mksalt(crypt.METHOD_SHA256)))' "$password" 2> /dev/null) && break
  390. done
  391. }
  392. $save_preseed << 'EOF'
  393. # Account setup
  394. EOF
  395. if [ -n "$authorized_keys_url" ]; then
  396. configure_sshd PasswordAuthentication no
  397. fi
  398. if [ "$username" = root ]; then
  399. if [ -z "$authorized_keys_url" ]; then
  400. configure_sshd PermitRootLogin yes
  401. else
  402. in_target "mkdir -m 0700 -p ~root/.ssh && busybox wget -O- \"$authorized_keys_url\" >> ~root/.ssh/authorized_keys"
  403. fi
  404. $save_preseed << 'EOF'
  405. d-i passwd/root-login boolean true
  406. d-i passwd/make-user boolean false
  407. EOF
  408. if [ -z "$password_hash" ]; then
  409. $save_preseed << EOF
  410. d-i passwd/root-password password $password
  411. d-i passwd/root-password-again password $password
  412. EOF
  413. else
  414. echo "d-i passwd/root-password-crypted password $password_hash" | $save_preseed
  415. fi
  416. else
  417. configure_sshd PermitRootLogin no
  418. if [ -n "$authorized_keys_url" ]; then
  419. 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"
  420. fi
  421. if [ "$sudo_with_password" = false ]; then
  422. in_target "echo \"$username ALL=(ALL:ALL) NOPASSWD:ALL\" > \"/etc/sudoers.d/90-user-$username\""
  423. fi
  424. $save_preseed << EOF
  425. d-i passwd/root-login boolean false
  426. d-i passwd/make-user boolean true
  427. d-i passwd/user-fullname string
  428. d-i passwd/username string $username
  429. EOF
  430. if [ -z "$password_hash" ]; then
  431. $save_preseed << EOF
  432. d-i passwd/user-password password $password
  433. d-i passwd/user-password-again password $password
  434. EOF
  435. else
  436. echo "d-i passwd/user-password-crypted password $password_hash" | $save_preseed
  437. fi
  438. fi
  439. fi
  440. $save_preseed << EOF
  441. # Clock and time zone setup
  442. d-i time/zone string $timezone
  443. d-i clock-setup/utc boolean true
  444. d-i clock-setup/ntp boolean true
  445. d-i clock-setup/ntp-server string $ntp
  446. EOF
  447. if [ "$disk_partitioning" = true ]; then
  448. $save_preseed << 'EOF'
  449. # Partitioning
  450. d-i partman-auto/method string regular
  451. EOF
  452. if [ -n "$disk" ]; then
  453. echo "d-i partman-auto/disk string $disk" | $save_preseed
  454. else
  455. # shellcheck disable=SC2016
  456. echo 'd-i partman/early_command string debconf-set partman-auto/disk "$(list-devices disk | head -n 1)"' | $save_preseed
  457. fi
  458. [ "$force_gpt" = true ] && echo 'd-i partman-partitioning/default_label string gpt' | $save_preseed
  459. echo "d-i partman/default_filesystem string $filesystem" | $save_preseed
  460. if [ -z "$efi" ]; then
  461. efi=false
  462. [ -d /sys/firmware/efi ] && efi=true
  463. fi
  464. $save_preseed << 'EOF'
  465. d-i partman-auto/expert_recipe string \
  466. naive :: \
  467. EOF
  468. if [ "$efi" = true ]; then
  469. $save_preseed << 'EOF'
  470. 106 106 106 free \
  471. $iflabel{ gpt } \
  472. $reusemethod{ } \
  473. method{ efi } \
  474. format{ } \
  475. . \
  476. EOF
  477. else
  478. $save_preseed << 'EOF'
  479. 1 1 1 free \
  480. $iflabel{ gpt } \
  481. $reusemethod{ } \
  482. method{ biosgrub } \
  483. . \
  484. EOF
  485. fi
  486. $save_preseed << 'EOF'
  487. 1075 1076 -1 $default_filesystem \
  488. method{ format } \
  489. format{ } \
  490. use_filesystem{ } \
  491. $default_filesystem{ } \
  492. mountpoint{ / } \
  493. .
  494. EOF
  495. echo 'd-i partman-auto/choose_recipe select naive' | $save_preseed
  496. $save_preseed << 'EOF'
  497. d-i partman-basicfilesystems/no_swap boolean false
  498. d-i partman/choose_partition select finish
  499. d-i partman/confirm boolean true
  500. d-i partman/confirm_nooverwrite boolean true
  501. EOF
  502. fi
  503. $save_preseed << EOF
  504. # Base system installation
  505. d-i base-installer/kernel/image string $kernel
  506. EOF
  507. [ "$install_recommends" = false ] && echo "d-i base-installer/install-recommends boolean $install_recommends" | $save_preseed
  508. [ "$security_repository" = mirror ] && security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
  509. $save_preseed << EOF
  510. # Apt setup
  511. d-i apt-setup/services-select multiselect updates, backports
  512. d-i apt-setup/local0/repository string $security_repository $suite/updates main
  513. d-i apt-setup/local0/source boolean true
  514. EOF
  515. $save_preseed << 'EOF'
  516. # Package selection
  517. tasksel tasksel/first multiselect ssh-server
  518. EOF
  519. [ -n "$install" ] && echo "d-i pkgsel/include string $install" | $save_preseed
  520. [ -n "$upgrade" ] && echo "d-i pkgsel/upgrade select $upgrade" | $save_preseed
  521. $save_preseed << 'EOF'
  522. popularity-contest popularity-contest/participate boolean false
  523. # Boot loader installation
  524. d-i grub-installer/bootdev string default
  525. EOF
  526. [ "$force_efi_extra_removable" = true ] && echo 'd-i grub-installer/force-efi-extra-removable boolean true' | $save_preseed
  527. [ -n "$kernel_params" ] && echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
  528. $save_preseed << 'EOF'
  529. # Finishing up the installation
  530. EOF
  531. [ "$hold" = false ] && echo 'd-i finish-install/reboot_in_progress note' | $save_preseed
  532. [ "$bbr" = true ] && in_target '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
  533. [ -n "$late_command" ] && echo "d-i preseed/late_command string in-target sh -c '$late_command'" | $save_preseed
  534. [ "$power_off" = true ] && echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  535. save_grub_cfg='cat'
  536. if [ "$dry_run" = false ]; then
  537. base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  538. firmware_url="https://cdimage.debian.org/cdimage/unofficial/non-free/firmware/$suite/current/firmware.cpio.gz"
  539. download "$base_url/linux" linux
  540. download "$base_url/initrd.gz" initrd.gz
  541. [ "$firmware" = true ] && download "$firmware_url" firmware.cpio.gz
  542. gzip -d initrd.gz
  543. # cpio reads a list of file names from the standard input
  544. echo preseed.cfg | cpio -o -H newc -A -F initrd
  545. gzip -9 initrd
  546. mkdir -p /etc/default/grub.d
  547. tee /etc/default/grub.d/zz-debi.cfg 1>&2 << EOF
  548. GRUB_DEFAULT=debi
  549. GRUB_TIMEOUT=$grub_timeout
  550. GRUB_TIMEOUT_STYLE=menu
  551. EOF
  552. if command_exists update-grub; then
  553. grub_cfg=/boot/grub/grub.cfg
  554. update-grub
  555. elif command_exists grub2-mkconfig; then
  556. tmp=$(mktemp)
  557. grep -vF zz_debi /etc/default/grub > "$tmp"
  558. cat "$tmp" > /etc/default/grub
  559. rm "$tmp"
  560. # shellcheck disable=SC2016
  561. echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
  562. grub_cfg=/boot/grub2/grub.cfg
  563. grub2-mkconfig -o "$grub_cfg"
  564. else
  565. err 'Could not find "update-grub" or "grub2-mkconfig" command'
  566. fi
  567. save_grub_cfg="tee -a $grub_cfg"
  568. fi
  569. if [ -z "$boot_directory" ]; then
  570. if grep -q '\s/boot\s' /proc/mounts; then
  571. boot_directory=/
  572. else
  573. boot_directory=/boot/
  574. fi
  575. fi
  576. installer_directory="$boot_directory$installer"
  577. # shellcheck disable=SC2034
  578. mem=$(grep ^MemTotal: /proc/meminfo | { read -r x y z; echo "$y"; })
  579. [ $((mem / 1024)) -lt 483 ] && kernel_params="$kernel_params lowmem/low="
  580. initrd="$installer_directory/initrd.gz"
  581. [ "$firmware" = true ] && initrd="$initrd $installer_directory/firmware.cpio.gz"
  582. $save_grub_cfg 1>&2 << EOF
  583. menuentry 'Debian Installer' --id debi {
  584. insmod part_msdos
  585. insmod part_gpt
  586. insmod ext2
  587. linux $installer_directory/linux$kernel_params
  588. initrd $initrd
  589. }
  590. EOF