debi.sh 15 KB

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