netboot.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. #!/usr/bin/env bash
  2. set -eu
  3. _err() {
  4. printf 'Error: %s.' "$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:-stable}
  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 "cd ~$username && mkdir -m 700 .ssh && busybox wget -qO .ssh/authorized_keys $authorized_keys_url"
  246. _late_command 'sed -ri "s/^#?PasswordAuthentication .+/PasswordAuthentication no/" /etc/ssh/sshd_config'
  247. $save_preseed << EOF
  248. d-i network-console/password-disabled boolean true
  249. d-i network-console/authorized_keys_url string $authorized_keys_url
  250. EOF
  251. elif [ -n "$installer_password" ]; then
  252. $save_preseed << EOF
  253. d-i network-console/password-disabled boolean false
  254. d-i network-console/password password $installer_password
  255. d-i network-console/password-again password $installer_password
  256. EOF
  257. fi
  258. echo 'd-i network-console/start select Continue' | $save_preseed
  259. fi
  260. mirror_protocol=${mirror_protocol:-http}
  261. mirror_host=${mirror_host:-deb.debian.org}
  262. mirror_directory=${mirror_directory:-/debian}
  263. $save_preseed << EOF
  264. # Mirror settings
  265. d-i mirror/country string manual
  266. d-i mirror/protocol string $mirror_protocol
  267. d-i mirror/$mirror_protocol/hostname string $mirror_host
  268. d-i mirror/$mirror_protocol/directory string $mirror_directory
  269. d-i mirror/$mirror_protocol/proxy string
  270. d-i mirror/suite string $suite
  271. d-i mirror/udeb/suite string $suite
  272. EOF
  273. if [ "$skip_account_setup" != true ]; then
  274. username=${username:-debian}
  275. if command_exists mkpasswd; then
  276. if [ -z "$password" ]; then
  277. password="$(mkpasswd -m sha512crypt)"
  278. else
  279. password="$(mkpasswd -m sha512crypt "$password")"
  280. fi
  281. elif command_exists busybox && busybox mkpasswd --help >/dev/null 2>&1; then
  282. if [ -z "$password" ]; then
  283. password="$(busybox mkpasswd -m sha512)"
  284. else
  285. password="$(busybox mkpasswd -m sha512 "$password")"
  286. fi
  287. elif command_exists python3; then
  288. if [ -z "$password" ]; then
  289. password="$(python3 -c 'import crypt, getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))')"
  290. else
  291. password="$(python3 -c "import crypt; print(crypt.crypt(\"$password\", crypt.mksalt(crypt.METHOD_SHA512)))")"
  292. fi
  293. else
  294. cleartext_password=true
  295. if [ -z "$password" ]; then
  296. printf 'Password: '
  297. read -rs password
  298. fi
  299. fi
  300. $save_preseed << EOF
  301. # Account setup
  302. EOF
  303. if [ "$username" = root ]; then
  304. if [ -z "$authorized_keys_url" ]; then
  305. _late_command 'sed -ri "s/^#?PermitRootLogin .+/PermitRootLogin yes/" /etc/ssh/sshd_config'
  306. fi
  307. $save_preseed << EOF
  308. d-i passwd/root-login boolean true
  309. d-i passwd/make-user boolean false
  310. EOF
  311. if [ "$cleartext_password" = true ]; then
  312. $save_preseed << EOF
  313. d-i passwd/root-password password $password
  314. d-i passwd/root-password-again password $password
  315. EOF
  316. else
  317. echo "d-i passwd/root-password-crypted password $password" | $save_preseed
  318. fi
  319. else
  320. $save_preseed << EOF
  321. d-i passwd/root-login boolean false
  322. d-i passwd/make-user boolean true
  323. d-i passwd/user-fullname string
  324. d-i passwd/username string $username
  325. EOF
  326. if [ "$cleartext_password" = true ]; then
  327. $save_preseed << EOF
  328. d-i passwd/user-password password $password
  329. d-i passwd/user-password-again password $password
  330. EOF
  331. else
  332. echo "d-i passwd/user-password-crypted password $password" | $save_preseed
  333. fi
  334. fi
  335. fi
  336. timezone=${timezone:-UTC}
  337. ntp=${ntp:-0.debian.pool.ntp.org}
  338. $save_preseed << EOF
  339. # Clock and time zone setup
  340. d-i clock-setup/utc boolean true
  341. d-i time/zone string $timezone
  342. d-i clock-setup/ntp boolean true
  343. d-i clock-setup/ntp-server string $ntp
  344. EOF
  345. if [ "$skip_partitioning" != true ]; then
  346. filesystem=${filesystem:-ext4}
  347. partitioning_method=${partitioning_method:-regular}
  348. $save_preseed << EOF
  349. # Partitioning
  350. EOF
  351. if [ -n "$disk" ]; then
  352. echo "d-i partman-auto/disk string $disk" | $save_preseed
  353. fi
  354. $save_preseed << EOF
  355. d-i partman-auto/method string $partitioning_method
  356. d-i partman-lvm/device_remove_lvm boolean true
  357. d-i partman-md/device_remove_md boolean true
  358. d-i partman-lvm/confirm boolean true
  359. d-i partman-lvm/confirm_nooverwrite boolean true
  360. EOF
  361. if [ "$partitioning_method" = "regular" ]; then
  362. $save_preseed << EOF
  363. d-i partman/default_filesystem string $filesystem
  364. d-i partman-auto/expert_recipe string naive :: 0 1 -1 \$default_filesystem \$primary{ } \$bootable{ } method{ format } format{ } use_filesystem{ } \$default_filesystem{ } mountpoint{ / } .
  365. d-i partman-auto/choose_recipe select naive
  366. d-i partman-basicfilesystems/no_swap boolean false
  367. EOF
  368. fi
  369. $save_preseed << EOF
  370. d-i partman-partitioning/confirm_write_new_label boolean true
  371. d-i partman/choose_partition select finish
  372. d-i partman/confirm boolean true
  373. d-i partman/confirm_nooverwrite boolean true
  374. d-i partman/mount_style select uuid
  375. EOF
  376. fi
  377. $save_preseed << EOF
  378. # Base system installation
  379. d-i base-installer/install-recommends boolean false
  380. EOF
  381. if [ -n "$kernel" ]; then
  382. echo "d-i base-installer/kernel/image string $kernel" | $save_preseed
  383. fi
  384. if [ -z "$security_repository" ]; then
  385. security_repository=http://security.debian.org/debian-security
  386. else
  387. if [ "$security_repository" = true ]; then
  388. security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
  389. fi
  390. fi
  391. $save_preseed << EOF
  392. # Apt setup
  393. d-i apt-setup/services-select multiselect updates, backports
  394. d-i apt-setup/local0/repository string $security_repository $suite/updates main
  395. d-i apt-setup/local0/source boolean true
  396. EOF
  397. upgrade=${upgrade:-full-upgrade}
  398. $save_preseed << EOF
  399. # Package selection
  400. tasksel tasksel/first multiselect ssh-server
  401. EOF
  402. if [ -n "$install" ]; then
  403. echo "d-i pkgsel/include string $install" | $save_preseed
  404. fi
  405. $save_preseed << EOF
  406. d-i pkgsel/upgrade select $upgrade
  407. popularity-contest popularity-contest/participate boolean false
  408. # Boot loader installation
  409. d-i grub-installer/only_debian boolean true
  410. d-i grub-installer/bootdev string default
  411. EOF
  412. if [ -n "$kernel_params" ]; then
  413. echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
  414. fi
  415. $save_preseed << EOF
  416. # Finishing up the installation
  417. d-i finish-install/reboot_in_progress note
  418. EOF
  419. if [ "$bbr" = true ]; then
  420. _late_command '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
  421. fi
  422. if [ -n "$late_command" ]; then
  423. echo "d-i preseed/late_command string in-target sh -c '$late_command'" | $save_preseed
  424. fi
  425. if [ "$power_off" = true ]; then
  426. echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  427. fi
  428. save_grub_cfg="cat"
  429. if [ "$dry_run" != true ]; then
  430. if [ -z "$architecture" ]; then
  431. if command_exists dpkg; then
  432. architecture="$(dpkg --print-architecture)"
  433. else
  434. architecture=amd64
  435. fi
  436. fi
  437. base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  438. if command_exists wget; then
  439. wget "$base_url/linux" "$base_url/initrd.gz"
  440. elif command_exists curl; then
  441. curl -O "$base_url/linux" -O "$base_url/initrd.gz"
  442. elif command_exists busybox; then
  443. busybox wget "$base_url/linux" "$base_url/initrd.gz"
  444. else
  445. _err 'wget/curl/busybox is required to download files'
  446. exit 1
  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. exit 1
  460. fi
  461. save_grub_cfg="tee -a $grub_cfg"
  462. fi
  463. if [ "$boot_partition" = true ]; then
  464. boot_directory=/
  465. else
  466. boot_directory=/boot/
  467. fi
  468. installer_directory="$boot_directory$installer"
  469. $save_grub_cfg << EOF
  470. menuentry 'Debian Installer' --id debi {
  471. insmod part_msdos
  472. insmod part_gpt
  473. insmod ext2
  474. linux $installer_directory/linux$kernel_params
  475. initrd $installer_directory/initrd.gz
  476. }
  477. EOF