netboot.sh 14 KB

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