debi.sh 17 KB

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