debi.sh 17 KB

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