debi.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #!/bin/bash
  2. set -euo pipefail
  3. err() {
  4. echo "Error: $1." 1>&2
  5. exit 1
  6. }
  7. command_exists() {
  8. command -v "$1" > /dev/null 2>&1
  9. }
  10. late_command=
  11. run_later() {
  12. [ -z "$late_command" ] && late_command='true'
  13. late_command="$late_command; $1"
  14. }
  15. backup() {
  16. run_later "if [ ! -e \"$1.backup\" ]; then cp \"$1\" \"$1.backup\"; fi"
  17. }
  18. prompt_password() {
  19. if [ -z "$password" ]; then
  20. read -rs -p 'Password: ' password
  21. fi
  22. }
  23. ip=
  24. netmask=
  25. gateway=
  26. dns='8.8.8.8 8.8.4.4'
  27. hostname=
  28. installer_ssh=false
  29. installer_password=
  30. authorized_keys_url=
  31. suite=buster
  32. mirror_protocol=http
  33. mirror_host=deb.debian.org
  34. mirror_directory=/debian
  35. security_repository=http://security.debian.org/debian-security
  36. skip_account_setup=false
  37. username=debian
  38. password=
  39. sudo_password=false
  40. cleartext_password=false
  41. timezone=UTC
  42. ntp=0.debian.pool.ntp.org
  43. skip_partitioning=false
  44. disk=
  45. force_gpt=true
  46. efi=
  47. filesystem=ext4
  48. kernel=
  49. install_recommends=true
  50. install='ca-certificates libpam-systemd'
  51. upgrade=
  52. kernel_params=
  53. bbr=false
  54. hold=false
  55. power_off=false
  56. architecture=
  57. boot_directory=/boot/
  58. firmware=false
  59. force_efi_extra_removable=false
  60. grub_timeout=5
  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. --hold)
  206. hold=true
  207. ;;
  208. --power-off)
  209. power_off=true
  210. ;;
  211. --architecture)
  212. architecture=$2
  213. shift
  214. ;;
  215. --boot-partition)
  216. boot_directory=/
  217. ;;
  218. --firmware)
  219. firmware=true
  220. ;;
  221. --force-efi-extra-removable)
  222. force_efi_extra_removable=true
  223. ;;
  224. --grub-timeout)
  225. grub_timeout=$2
  226. shift
  227. ;;
  228. --dry-run)
  229. dry_run=true
  230. ;;
  231. *)
  232. err "Illegal option $1"
  233. esac
  234. shift
  235. done
  236. installer="debian-$suite"
  237. installer_directory="/boot/$installer"
  238. save_preseed='cat'
  239. if [ "$dry_run" != true ]; then
  240. [ "$(id -u)" -ne 0 ] && err 'root privilege is required'
  241. rm -rf "$installer_directory"
  242. mkdir -p "$installer_directory/initrd"
  243. cd "$installer_directory"
  244. save_preseed='tee -a initrd/preseed.cfg'
  245. fi
  246. $save_preseed << 'EOF'
  247. # Localization
  248. d-i debian-installer/language string en
  249. d-i debian-installer/country string US
  250. d-i debian-installer/locale string en_US.UTF-8
  251. d-i keyboard-configuration/xkb-keymap select us
  252. # Network configuration
  253. d-i netcfg/choose_interface select auto
  254. EOF
  255. if [ -n "$ip" ]; then
  256. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  257. echo "d-i netcfg/get_ipaddress string $ip" | $save_preseed
  258. [ -n "$netmask" ] && echo "d-i netcfg/get_netmask string $netmask" | $save_preseed
  259. [ -n "$gateway" ] && echo "d-i netcfg/get_gateway string $gateway" | $save_preseed
  260. [ -n "$dns" ] && echo "d-i netcfg/get_nameservers string $dns" | $save_preseed
  261. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  262. fi
  263. $save_preseed << 'EOF'
  264. d-i netcfg/get_hostname string debian
  265. d-i netcfg/get_domain string
  266. EOF
  267. if [ -n "$hostname" ]; then
  268. echo "d-i netcfg/hostname string $hostname" | $save_preseed
  269. fi
  270. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  271. if [ "$installer_ssh" = true ]; then
  272. $save_preseed << 'EOF'
  273. # Network console
  274. d-i anna/choose_modules string network-console
  275. d-i preseed/early_command string anna-install network-console
  276. EOF
  277. if [ -n "$authorized_keys_url" ]; then
  278. backup /etc/ssh/sshd_config
  279. run_later 'sed -Ei "s/^#?PasswordAuthentication .+/PasswordAuthentication no/" /etc/ssh/sshd_config'
  280. $save_preseed << EOF
  281. d-i network-console/password-disabled boolean true
  282. d-i network-console/authorized_keys_url string $authorized_keys_url
  283. EOF
  284. elif [ -n "$installer_password" ]; then
  285. $save_preseed << EOF
  286. d-i network-console/password-disabled boolean false
  287. d-i network-console/password password $installer_password
  288. d-i network-console/password-again password $installer_password
  289. EOF
  290. fi
  291. echo 'd-i network-console/start select Continue' | $save_preseed
  292. fi
  293. $save_preseed << EOF
  294. # Mirror settings
  295. d-i mirror/country string manual
  296. d-i mirror/protocol string $mirror_protocol
  297. d-i mirror/$mirror_protocol/hostname string $mirror_host
  298. d-i mirror/$mirror_protocol/directory string $mirror_directory
  299. d-i mirror/$mirror_protocol/proxy string
  300. d-i mirror/suite string $suite
  301. d-i mirror/udeb/suite string $suite
  302. EOF
  303. if [ "$skip_account_setup" != true ]; then
  304. if command_exists mkpasswd; then
  305. if [ -z "$password" ]; then
  306. password="$(mkpasswd -m sha-512)"
  307. else
  308. password="$(mkpasswd -m sha-512 "$password")"
  309. fi
  310. elif command_exists busybox && busybox mkpasswd --help >/dev/null 2>&1; then
  311. prompt_password
  312. password="$(busybox mkpasswd -m sha512 "$password")"
  313. elif command_exists python3; then
  314. if [ -z "$password" ]; then
  315. password="$(python3 -c 'import crypt, getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))')"
  316. else
  317. password="$(python3 -c "import crypt; print(crypt.crypt('$password', crypt.mksalt(crypt.METHOD_SHA512)))")"
  318. fi
  319. else
  320. cleartext_password=true
  321. prompt_password
  322. fi
  323. $save_preseed << 'EOF'
  324. # Account setup
  325. EOF
  326. if [ "$username" = root ]; then
  327. if [ -z "$authorized_keys_url" ]; then
  328. backup /etc/ssh/sshd_config
  329. run_later 'sed -Ei "s/^#?PermitRootLogin .+/PermitRootLogin yes/" /etc/ssh/sshd_config'
  330. else
  331. run_later "mkdir -m 0700 -p ~root/.ssh && busybox wget -O - \"$authorized_keys_url\" >> ~root/.ssh/authorized_keys"
  332. fi
  333. $save_preseed << 'EOF'
  334. d-i passwd/root-login boolean true
  335. d-i passwd/make-user boolean false
  336. EOF
  337. if [ "$cleartext_password" = true ]; then
  338. $save_preseed << EOF
  339. d-i passwd/root-password password $password
  340. d-i passwd/root-password-again password $password
  341. EOF
  342. else
  343. echo "d-i passwd/root-password-crypted password $password" | $save_preseed
  344. fi
  345. else
  346. backup /etc/ssh/sshd_config
  347. run_later 'sed -Ei "s/^#?PermitRootLogin .+/PermitRootLogin no/" /etc/ssh/sshd_config'
  348. if [ -n "$authorized_keys_url" ]; then
  349. 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"
  350. fi
  351. if [ "$sudo_password" = false ]; then
  352. run_later "echo \"$username ALL=(ALL:ALL) NOPASSWD:ALL\" > \"/etc/sudoers.d/90-user-$username\""
  353. fi
  354. $save_preseed << EOF
  355. d-i passwd/root-login boolean false
  356. d-i passwd/make-user boolean true
  357. d-i passwd/user-fullname string
  358. d-i passwd/username string $username
  359. EOF
  360. if [ "$cleartext_password" = true ]; then
  361. $save_preseed << EOF
  362. d-i passwd/user-password password $password
  363. d-i passwd/user-password-again password $password
  364. EOF
  365. else
  366. echo "d-i passwd/user-password-crypted password $password" | $save_preseed
  367. fi
  368. fi
  369. fi
  370. $save_preseed << EOF
  371. # Clock and time zone setup
  372. d-i time/zone string $timezone
  373. d-i clock-setup/utc boolean true
  374. d-i clock-setup/ntp boolean true
  375. d-i clock-setup/ntp-server string $ntp
  376. EOF
  377. if [ "$skip_partitioning" != true ]; then
  378. $save_preseed << 'EOF'
  379. # Partitioning
  380. d-i partman-auto/method string regular
  381. EOF
  382. [ -n "$disk" ] && echo "d-i partman-auto/disk string $disk" | $save_preseed
  383. [ "$force_gpt" = true ] && echo 'd-i partman-partitioning/default_label string gpt' | $save_preseed
  384. echo "d-i partman/default_filesystem string $filesystem" | $save_preseed
  385. if [ -z "$efi" ]; then
  386. efi=false
  387. [ -d /sys/firmware/efi ] && efi=true
  388. fi
  389. $save_preseed << 'EOF'
  390. d-i partman-auto/expert_recipe string \
  391. naive :: \
  392. EOF
  393. if [ "$efi" = true ]; then
  394. $save_preseed << 'EOF'
  395. 538 538 1075 free \
  396. $iflabel{ gpt } \
  397. $reusemethod{ } \
  398. method{ efi } \
  399. format{ } \
  400. . \
  401. EOF
  402. else
  403. $save_preseed << 'EOF'
  404. 1 1 1 free \
  405. $iflabel{ gpt } \
  406. $reusemethod{ } \
  407. method{ biosgrub } \
  408. . \
  409. EOF
  410. fi
  411. $save_preseed << 'EOF'
  412. 2149 2150 -1 $default_filesystem \
  413. method{ format } \
  414. format{ } \
  415. use_filesystem{ } \
  416. $default_filesystem{ } \
  417. mountpoint{ / } \
  418. .
  419. EOF
  420. echo "d-i partman-auto/choose_recipe select naive" | $save_preseed
  421. $save_preseed << 'EOF'
  422. d-i partman-basicfilesystems/no_swap boolean false
  423. d-i partman/choose_partition select finish
  424. d-i partman/confirm boolean true
  425. EOF
  426. fi
  427. $save_preseed << 'EOF'
  428. # Base system installation
  429. EOF
  430. [ "$install_recommends" = false ] && echo "d-i base-installer/install-recommends boolean $install_recommends" | $save_preseed
  431. [ -n "$kernel" ] && echo "d-i base-installer/kernel/image string $kernel" | $save_preseed
  432. [ "$security_repository" = mirror ] && security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
  433. $save_preseed << EOF
  434. # Apt setup
  435. d-i apt-setup/services-select multiselect updates, backports
  436. d-i apt-setup/local0/repository string $security_repository $suite/updates main
  437. d-i apt-setup/local0/source boolean true
  438. EOF
  439. $save_preseed << 'EOF'
  440. # Package selection
  441. tasksel tasksel/first multiselect ssh-server
  442. EOF
  443. [ -n "$install" ] && echo "d-i pkgsel/include string $install" | $save_preseed
  444. [ -n "$upgrade" ] && echo "d-i pkgsel/upgrade select $upgrade" | $save_preseed
  445. $save_preseed << 'EOF'
  446. popularity-contest popularity-contest/participate boolean false
  447. # Boot loader installation
  448. d-i grub-installer/bootdev string default
  449. EOF
  450. [ "$force_efi_extra_removable" = true ] && echo "d-i grub-installer/force-efi-extra-removable boolean true" | $save_preseed
  451. [ -n "$kernel_params" ] && echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
  452. $save_preseed << 'EOF'
  453. # Finishing up the installation
  454. EOF
  455. [ "$hold" != true ] && echo 'd-i finish-install/reboot_in_progress note' | $save_preseed
  456. [ "$bbr" = true ] && run_later '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
  457. [ -n "$late_command" ] && echo "d-i preseed/late_command string in-target bash -c '$late_command'" | $save_preseed
  458. [ "$power_off" = true ] && echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  459. save_grub_cfg='cat'
  460. if [ "$dry_run" != true ]; then
  461. if [ -z "$architecture" ]; then
  462. architecture=amd64
  463. command_exists dpkg && architecture="$(dpkg --print-architecture)"
  464. fi
  465. base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  466. firmware_url="https://cdimage.debian.org/cdimage/unofficial/non-free/firmware/$suite/current/firmware.cpio.gz"
  467. if command_exists wget; then
  468. wget "$base_url/linux" "$base_url/initrd.gz"
  469. [ "$firmware" = true ] && wget "$firmware_url"
  470. elif command_exists curl; then
  471. curl -f -L -O "$base_url/linux" -O "$base_url/initrd.gz"
  472. [ "$firmware" = true ] && curl -f -L -O "$firmware_url"
  473. elif command_exists busybox && busybox wget --help >/dev/null 2>&1; then
  474. busybox wget "$base_url/linux" "$base_url/initrd.gz"
  475. [ "$firmware" = true ] && busybox wget "$firmware_url"
  476. else
  477. err 'Could not find "wget" or "curl" or "busybox wget" command to download files'
  478. fi
  479. cd initrd
  480. gzip -d -c ../initrd.gz | cpio -i -d --no-absolute-filenames
  481. [ "$firmware" = true ] && gzip -d -c ../firmware.cpio.gz | cpio -i -d --no-absolute-filenames
  482. find . | cpio -o -H newc | gzip -9 > ../initrd.gz
  483. cd ..
  484. mkdir -p /etc/default/grub.d
  485. tee /etc/default/grub.d/zz-debi.cfg 1>&2 << EOF
  486. GRUB_DEFAULT=debi
  487. GRUB_TIMEOUT=$grub_timeout
  488. GRUB_TIMEOUT_STYLE=menu
  489. EOF
  490. if command_exists update-grub; then
  491. grub_cfg=/boot/grub/grub.cfg
  492. update-grub
  493. elif command_exists grub2-mkconfig; then
  494. tmp="$(mktemp)"
  495. grep -vF zz_debi /etc/default/grub > "$tmp"
  496. cat "$tmp" > /etc/default/grub
  497. rm "$tmp"
  498. # shellcheck disable=SC2016
  499. echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
  500. grub_cfg=/boot/grub2/grub.cfg
  501. grub2-mkconfig -o "$grub_cfg"
  502. else
  503. err 'Could not find "update-grub" or "grub2-mkconfig" command'
  504. fi
  505. save_grub_cfg="tee -a $grub_cfg"
  506. fi
  507. installer_directory="$boot_directory$installer"
  508. # shellcheck disable=SC2034
  509. mem="$(grep ^MemTotal: /proc/meminfo | { read -r x y z; echo "$y"; })"
  510. [ $((mem / 1024)) -lt 483 ] && kernel_params="$kernel_params lowmem/low="
  511. $save_grub_cfg 1>&2 << EOF
  512. menuentry 'Debian Installer' --id debi {
  513. insmod part_msdos
  514. insmod part_gpt
  515. insmod ext2
  516. linux $installer_directory/linux$kernel_params
  517. initrd $installer_directory/initrd.gz
  518. }
  519. EOF