netboot.sh 14 KB

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