netboot.sh 15 KB

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