debi.sh 16 KB

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