debi.sh 16 KB

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