netboot.sh 14 KB

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