netboot.sh 14 KB

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