debi.sh 14 KB

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