debi.sh 16 KB

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