debi.sh 16 KB

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