debi.sh 16 KB

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