netboot.sh 15 KB

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