debi.sh 16 KB

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