debi.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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=
  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. --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. --power-off)
  205. power_off=true
  206. ;;
  207. --architecture)
  208. architecture=$2
  209. shift
  210. ;;
  211. --boot-partition)
  212. boot_directory=/
  213. ;;
  214. --firmware)
  215. firmware=true
  216. ;;
  217. --force-efi-extra-removable)
  218. force_efi_extra_removable=true
  219. ;;
  220. --dry-run)
  221. dry_run=true
  222. ;;
  223. *)
  224. err "Illegal option $1"
  225. esac
  226. shift
  227. done
  228. installer="debian-$suite"
  229. installer_directory="/boot/$installer"
  230. save_preseed='cat'
  231. if [ "$dry_run" != true ]; then
  232. user="$(id -un 2>/dev/null || true)"
  233. [ "$user" != root ] && err 'root privilege is required'
  234. rm -rf "$installer_directory"
  235. mkdir -p "$installer_directory/initrd"
  236. cd "$installer_directory"
  237. save_preseed='tee -a initrd/preseed.cfg'
  238. fi
  239. $save_preseed << 'EOF'
  240. # Localization
  241. d-i debian-installer/locale string en_US.UTF-8
  242. d-i keyboard-configuration/xkb-keymap select us
  243. # Network configuration
  244. d-i netcfg/choose_interface select auto
  245. EOF
  246. if [ -n "$ip" ]; then
  247. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  248. echo "d-i netcfg/get_ipaddress string $ip" | $save_preseed
  249. [ -n "$netmask" ] && echo "d-i netcfg/get_netmask string $netmask" | $save_preseed
  250. [ -n "$gateway" ] && echo "d-i netcfg/get_gateway string $gateway" | $save_preseed
  251. [ -n "$dns" ] && echo "d-i netcfg/get_nameservers string $dns" | $save_preseed
  252. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  253. fi
  254. $save_preseed << 'EOF'
  255. d-i netcfg/get_hostname string debian
  256. d-i netcfg/get_domain string
  257. EOF
  258. if [ -n "$hostname" ]; then
  259. echo "d-i netcfg/hostname string $hostname" | $save_preseed
  260. fi
  261. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  262. if [ "$installer_ssh" = true ]; then
  263. $save_preseed << 'EOF'
  264. # Network console
  265. d-i anna/choose_modules string network-console
  266. d-i preseed/early_command string anna-install network-console
  267. EOF
  268. if [ -n "$authorized_keys_url" ]; then
  269. backup /etc/ssh/sshd_config
  270. run_later 'sed -Ei "s/^#?PasswordAuthentication.*/PasswordAuthentication no/" /etc/ssh/sshd_config'
  271. $save_preseed << EOF
  272. d-i network-console/password-disabled boolean true
  273. d-i network-console/authorized_keys_url string $authorized_keys_url
  274. EOF
  275. elif [ -n "$installer_password" ]; then
  276. $save_preseed << EOF
  277. d-i network-console/password-disabled boolean false
  278. d-i network-console/password password $installer_password
  279. d-i network-console/password-again password $installer_password
  280. EOF
  281. fi
  282. echo 'd-i network-console/start select Continue' | $save_preseed
  283. fi
  284. $save_preseed << EOF
  285. # Mirror settings
  286. d-i mirror/country string manual
  287. d-i mirror/protocol string $mirror_protocol
  288. d-i mirror/$mirror_protocol/hostname string $mirror_host
  289. d-i mirror/$mirror_protocol/directory string $mirror_directory
  290. d-i mirror/$mirror_protocol/proxy string
  291. d-i mirror/suite string $suite
  292. d-i mirror/udeb/suite string $suite
  293. EOF
  294. if [ "$skip_account_setup" != true ]; then
  295. if command_exists mkpasswd; then
  296. if [ -z "$password" ]; then
  297. password="$(mkpasswd -m sha-512)"
  298. else
  299. password="$(mkpasswd -m sha-512 "$password")"
  300. fi
  301. elif command_exists busybox && busybox mkpasswd --help >/dev/null 2>&1; then
  302. prompt_password
  303. password="$(busybox mkpasswd -m sha512 "$password")"
  304. elif command_exists python3; then
  305. if [ -z "$password" ]; then
  306. password="$(python3 -c 'import crypt, getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))')"
  307. else
  308. password="$(python3 -c "import crypt; print(crypt.crypt('$password', crypt.mksalt(crypt.METHOD_SHA512)))")"
  309. fi
  310. else
  311. cleartext_password=true
  312. prompt_password
  313. fi
  314. $save_preseed << 'EOF'
  315. # Account setup
  316. EOF
  317. if [ "$username" = root ]; then
  318. if [ -z "$authorized_keys_url" ]; then
  319. backup /etc/ssh/sshd_config
  320. run_later 'sed -Ei "s/^#?PermitRootLogin.*/PermitRootLogin yes/" /etc/ssh/sshd_config'
  321. else
  322. run_later "mkdir -m 0700 -p ~root/.ssh && busybox wget -O - \"$authorized_keys_url\" >> ~root/.ssh/authorized_keys"
  323. fi
  324. $save_preseed << 'EOF'
  325. d-i passwd/root-login boolean true
  326. d-i passwd/make-user boolean false
  327. EOF
  328. if [ "$cleartext_password" = true ]; then
  329. $save_preseed << EOF
  330. d-i passwd/root-password password $password
  331. d-i passwd/root-password-again password $password
  332. EOF
  333. else
  334. echo "d-i passwd/root-password-crypted password $password" | $save_preseed
  335. fi
  336. else
  337. backup /etc/ssh/sshd_config
  338. run_later 'sed -Ei "s/^#?PermitRootLogin.*/PermitRootLogin no/" /etc/ssh/sshd_config'
  339. if [ -n "$authorized_keys_url" ]; then
  340. 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"
  341. fi
  342. $save_preseed << EOF
  343. d-i passwd/root-login boolean false
  344. d-i passwd/make-user boolean true
  345. d-i passwd/user-fullname string
  346. d-i passwd/username string $username
  347. EOF
  348. if [ "$cleartext_password" = true ]; then
  349. $save_preseed << EOF
  350. d-i passwd/user-password password $password
  351. d-i passwd/user-password-again password $password
  352. EOF
  353. else
  354. echo "d-i passwd/user-password-crypted password $password" | $save_preseed
  355. fi
  356. fi
  357. fi
  358. $save_preseed << EOF
  359. # Clock and time zone setup
  360. d-i time/zone string $timezone
  361. d-i clock-setup/utc boolean true
  362. d-i clock-setup/ntp boolean true
  363. d-i clock-setup/ntp-server string $ntp
  364. EOF
  365. if [ "$skip_partitioning" != true ]; then
  366. $save_preseed << 'EOF'
  367. # Partitioning
  368. EOF
  369. if [ -n "$disk" ]; then
  370. echo "d-i partman-auto/disk string $disk" | $save_preseed
  371. fi
  372. echo "d-i partman-auto/method string $partitioning_method" | $save_preseed
  373. if [ "$partitioning_method" = regular ]; then
  374. [ "$force_gpt" = true ] && $save_preseed << 'EOF'
  375. d-i partman-partitioning/default_label string gpt
  376. EOF
  377. echo "d-i partman/default_filesystem string $filesystem" | $save_preseed
  378. if [ -z "$efi" ]; then
  379. efi=false
  380. [ -d /sys/firmware/efi ] && efi=true
  381. fi
  382. $save_preseed << 'EOF'
  383. d-i partman-auto/expert_recipe string \
  384. naive :: \
  385. EOF
  386. if [ "$efi" = true ]; then
  387. $save_preseed << 'EOF'
  388. 538 538 1075 free \
  389. $iflabel{ gpt } \
  390. $reusemethod{ } \
  391. method{ efi } \
  392. format{ } \
  393. . \
  394. EOF
  395. else
  396. $save_preseed << 'EOF'
  397. 1 1 1 free \
  398. $iflabel{ gpt } \
  399. $reusemethod{ } \
  400. method{ biosgrub } \
  401. . \
  402. EOF
  403. fi
  404. $save_preseed << 'EOF'
  405. 2149 2150 -1 $default_filesystem \
  406. method{ format } \
  407. format{ } \
  408. use_filesystem{ } \
  409. $default_filesystem{ } \
  410. mountpoint{ / } \
  411. .
  412. EOF
  413. echo "d-i partman-auto/choose_recipe select naive" | $save_preseed
  414. fi
  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 '"wget" or "curl" or "busybox wget" is required 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. if command_exists update-grub; then
  479. grub_cfg=/boot/grub/grub.cfg
  480. update-grub
  481. elif command_exists grub2-mkconfig; then
  482. grub_cfg=/boot/grub2/grub.cfg
  483. grub2-mkconfig -o "$grub_cfg"
  484. else
  485. err 'update-grub/grub2-mkconfig command not found'
  486. fi
  487. save_grub_cfg="tee -a $grub_cfg"
  488. fi
  489. installer_directory="$boot_directory$installer"
  490. $save_grub_cfg << EOF
  491. menuentry 'Debian Installer' --id debi {
  492. insmod part_msdos
  493. insmod part_gpt
  494. insmod ext2
  495. linux $installer_directory/linux$kernel_params
  496. initrd $installer_directory/initrd.gz
  497. }
  498. EOF