debi.sh 17 KB

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