debi.sh 14 KB

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