debi.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. #!/bin/bash
  2. set -eu
  3. err() {
  4. printf 'Error: %s.\n' "$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 "[ ! -e \"$1.backup\" ] && cp \"$1\" \"$1.backup\""
  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. cleartext_password=false
  40. timezone=UTC
  41. ntp=0.debian.pool.ntp.org
  42. skip_partitioning=false
  43. partitioning_method=regular
  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. --timezone)
  144. timezone=$2
  145. shift
  146. ;;
  147. --ntp)
  148. ntp=$2
  149. shift
  150. ;;
  151. --skip-partitioning)
  152. skip_partitioning=true
  153. ;;
  154. --partitioning-method)
  155. partitioning_method=$2
  156. shift
  157. ;;
  158. --disk)
  159. disk=$2
  160. shift
  161. ;;
  162. --force-gpt)
  163. force_gpt=true
  164. ;;
  165. --no-force-gpt)
  166. force_gpt=false
  167. ;;
  168. --bios)
  169. efi=false
  170. ;;
  171. --efi)
  172. efi=true
  173. ;;
  174. --filesystem)
  175. filesystem=$2
  176. shift
  177. ;;
  178. --kernel)
  179. kernel=$2
  180. shift
  181. ;;
  182. --cloud-kernel)
  183. kernel=linux-image-cloud-amd64
  184. ;;
  185. --no-install-recommends)
  186. install_recommends=false
  187. ;;
  188. --install)
  189. install=$2
  190. shift
  191. ;;
  192. --no-upgrade)
  193. upgrade=none
  194. ;;
  195. --safe-upgrade)
  196. upgrade=safe-upgrade
  197. ;;
  198. --full-upgrade)
  199. upgrade=full-upgrade
  200. ;;
  201. --eth)
  202. kernel_params=' net.ifnames=0 biosdevname=0'
  203. ;;
  204. --bbr)
  205. bbr=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. user="$(id -un 2>/dev/null || true)"
  236. [ "$user" != root ] && err 'root privilege is required'
  237. rm -rf "$installer_directory"
  238. mkdir -p "$installer_directory/initrd"
  239. cd "$installer_directory"
  240. save_preseed='tee -a initrd/preseed.cfg'
  241. fi
  242. $save_preseed << 'EOF'
  243. # Localization
  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. $save_preseed << EOF
  346. d-i passwd/root-login boolean false
  347. d-i passwd/make-user boolean true
  348. d-i passwd/user-fullname string
  349. d-i passwd/username string $username
  350. EOF
  351. if [ "$cleartext_password" = true ]; then
  352. $save_preseed << EOF
  353. d-i passwd/user-password password $password
  354. d-i passwd/user-password-again password $password
  355. EOF
  356. else
  357. echo "d-i passwd/user-password-crypted password $password" | $save_preseed
  358. fi
  359. fi
  360. fi
  361. $save_preseed << EOF
  362. # Clock and time zone setup
  363. d-i time/zone string $timezone
  364. d-i clock-setup/utc boolean true
  365. d-i clock-setup/ntp boolean true
  366. d-i clock-setup/ntp-server string $ntp
  367. EOF
  368. if [ "$skip_partitioning" != true ]; then
  369. $save_preseed << 'EOF'
  370. # Partitioning
  371. EOF
  372. if [ -n "$disk" ]; then
  373. echo "d-i partman-auto/disk string $disk" | $save_preseed
  374. fi
  375. echo "d-i partman-auto/method string $partitioning_method" | $save_preseed
  376. if [ "$partitioning_method" = regular ]; then
  377. [ "$force_gpt" = true ] && $save_preseed << 'EOF'
  378. d-i partman-partitioning/default_label string gpt
  379. EOF
  380. echo "d-i partman/default_filesystem string $filesystem" | $save_preseed
  381. if [ -z "$efi" ]; then
  382. efi=false
  383. [ -d /sys/firmware/efi ] && efi=true
  384. fi
  385. $save_preseed << 'EOF'
  386. d-i partman-auto/expert_recipe string \
  387. naive :: \
  388. EOF
  389. if [ "$efi" = true ]; then
  390. $save_preseed << 'EOF'
  391. 538 538 1075 free \
  392. $iflabel{ gpt } \
  393. $reusemethod{ } \
  394. method{ efi } \
  395. format{ } \
  396. . \
  397. EOF
  398. else
  399. $save_preseed << 'EOF'
  400. 1 1 1 free \
  401. $iflabel{ gpt } \
  402. $reusemethod{ } \
  403. method{ biosgrub } \
  404. . \
  405. EOF
  406. fi
  407. $save_preseed << 'EOF'
  408. 2149 2150 -1 $default_filesystem \
  409. method{ format } \
  410. format{ } \
  411. use_filesystem{ } \
  412. $default_filesystem{ } \
  413. mountpoint{ / } \
  414. .
  415. EOF
  416. echo "d-i partman-auto/choose_recipe select naive" | $save_preseed
  417. fi
  418. $save_preseed << 'EOF'
  419. d-i partman-basicfilesystems/no_swap boolean false
  420. d-i partman/choose_partition select finish
  421. d-i partman/confirm boolean true
  422. EOF
  423. fi
  424. $save_preseed << 'EOF'
  425. # Base system installation
  426. EOF
  427. [ "$install_recommends" = false ] && echo "d-i base-installer/install-recommends boolean $install_recommends" | $save_preseed
  428. [ -n "$kernel" ] && echo "d-i base-installer/kernel/image string $kernel" | $save_preseed
  429. [ "$security_repository" = mirror ] && security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
  430. $save_preseed << EOF
  431. # Apt setup
  432. d-i apt-setup/services-select multiselect updates, backports
  433. d-i apt-setup/local0/repository string $security_repository $suite/updates main
  434. d-i apt-setup/local0/source boolean true
  435. EOF
  436. $save_preseed << 'EOF'
  437. # Package selection
  438. tasksel tasksel/first multiselect ssh-server
  439. EOF
  440. [ -n "$install" ] && echo "d-i pkgsel/include string $install" | $save_preseed
  441. [ -n "$upgrade" ] && echo "d-i pkgsel/upgrade select $upgrade" | $save_preseed
  442. $save_preseed << 'EOF'
  443. popularity-contest popularity-contest/participate boolean false
  444. # Boot loader installation
  445. d-i grub-installer/bootdev string default
  446. EOF
  447. [ "$force_efi_extra_removable" = true ] && echo "d-i grub-installer/force-efi-extra-removable boolean true" | $save_preseed
  448. [ -n "$kernel_params" ] && echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
  449. $save_preseed << 'EOF'
  450. # Finishing up the installation
  451. d-i finish-install/reboot_in_progress note
  452. EOF
  453. [ "$bbr" = true ] && run_later '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
  454. [ -n "$late_command" ] && echo "d-i preseed/late_command string in-target bash -c '$late_command'" | $save_preseed
  455. [ "$power_off" = true ] && echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  456. save_grub_cfg='cat'
  457. if [ "$dry_run" != true ]; then
  458. if [ -z "$architecture" ]; then
  459. architecture=amd64
  460. command_exists dpkg && architecture="$(dpkg --print-architecture)"
  461. fi
  462. base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  463. firmware_url="https://cdimage.debian.org/cdimage/unofficial/non-free/firmware/$suite/current/firmware.cpio.gz"
  464. if command_exists wget; then
  465. wget "$base_url/linux" "$base_url/initrd.gz"
  466. [ "$firmware" = true ] && wget "$firmware_url"
  467. elif command_exists curl; then
  468. curl -f -L -O "$base_url/linux" -O "$base_url/initrd.gz"
  469. [ "$firmware" = true ] && curl -f -L -O "$firmware_url"
  470. elif command_exists busybox && busybox wget --help >/dev/null 2>&1; then
  471. busybox wget "$base_url/linux" "$base_url/initrd.gz"
  472. [ "$firmware" = true ] && busybox wget "$firmware_url"
  473. else
  474. err '"wget" or "curl" or "busybox wget" is required to download files'
  475. fi
  476. cd initrd
  477. gzip -d -c ../initrd.gz | cpio -i -d --no-absolute-filenames
  478. [ "$firmware" = true ] && gzip -d -c ../firmware.cpio.gz | cpio -i -d --no-absolute-filenames
  479. find . | cpio -o -H newc | gzip -9 > ../initrd.gz
  480. cd ..
  481. if command_exists update-grub; then
  482. grub_cfg=/boot/grub/grub.cfg
  483. update-grub
  484. elif command_exists grub2-mkconfig; then
  485. grub_cfg=/boot/grub2/grub.cfg
  486. grub2-mkconfig -o "$grub_cfg"
  487. else
  488. err 'update-grub/grub2-mkconfig command not found'
  489. fi
  490. save_grub_cfg="tee -a $grub_cfg"
  491. fi
  492. installer_directory="$boot_directory$installer"
  493. $save_grub_cfg << EOF
  494. menuentry 'Debian Installer' --id debi {
  495. insmod part_msdos
  496. insmod part_gpt
  497. insmod ext2
  498. linux $installer_directory/linux$kernel_params
  499. initrd $installer_directory/initrd.gz
  500. }
  501. EOF