netboot.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. ;;
  184. --full-upgrade)
  185. upgrade=full-upgrade
  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. EOF
  362. if [ "$efi" = true ]; then
  363. $save_preseed << 'EOF'
  364. 538 538 1075 free \
  365. $iflabel{ gpt } \
  366. $reusemethod{ } \
  367. method{ efi } \
  368. format{ } \
  369. . \
  370. EOF
  371. else
  372. $save_preseed << 'EOF'
  373. 1 1 1 free \
  374. $iflabel{ gpt } \
  375. $reusemethod{ } \
  376. method{ biosgrub } \
  377. . \
  378. EOF
  379. fi
  380. $save_preseed << 'EOF'
  381. 2149 2150 -1 $default_filesystem \
  382. method{ format } \
  383. format{ } \
  384. use_filesystem{ } \
  385. $default_filesystem{ } \
  386. mountpoint{ / } \
  387. .
  388. EOF
  389. echo "d-i partman-auto/choose_recipe select naive" | $save_preseed
  390. fi
  391. $save_preseed << 'EOF'
  392. d-i partman-basicfilesystems/no_swap boolean false
  393. d-i partman/choose_partition select finish
  394. d-i partman/confirm boolean true
  395. EOF
  396. fi
  397. $save_preseed << 'EOF'
  398. # Base system installation
  399. EOF
  400. [ "$install_recommends" = false ] && echo "d-i base-installer/install-recommends boolean $install_recommends" | $save_preseed
  401. [ -n "$kernel" ] && echo "d-i base-installer/kernel/image string $kernel" | $save_preseed
  402. [ "$security_repository" = true ] && security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
  403. $save_preseed << EOF
  404. # Apt setup
  405. d-i apt-setup/services-select multiselect updates, backports
  406. d-i apt-setup/local0/repository string $security_repository $suite/updates main
  407. d-i apt-setup/local0/source boolean true
  408. EOF
  409. $save_preseed << 'EOF'
  410. # Package selection
  411. tasksel tasksel/first multiselect ssh-server
  412. EOF
  413. [ -n "$install" ] && echo "d-i pkgsel/include string $install" | $save_preseed
  414. [ -n "$upgrade" ] && echo "d-i pkgsel/upgrade select $upgrade" | $save_preseed
  415. $save_preseed << 'EOF'
  416. popularity-contest popularity-contest/participate boolean false
  417. # Boot loader installation
  418. d-i grub-installer/bootdev string default
  419. EOF
  420. [ -n "$kernel_params" ] && echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
  421. $save_preseed << 'EOF'
  422. # Finishing up the installation
  423. d-i finish-install/reboot_in_progress note
  424. EOF
  425. [ "$bbr" = true ] && _late_command '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
  426. [ -n "$late_command" ] && echo "d-i preseed/late_command string in-target sh -c '$late_command'" | $save_preseed
  427. [ "$power_off" = true ] && echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  428. save_grub_cfg='cat'
  429. if [ "$dry_run" != true ]; then
  430. [ -z "$architecture" ] && architecture=amd64 && _command_exists dpkg && architecture="$(dpkg --print-architecture)"
  431. base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  432. if _command_exists wget; then
  433. wget "$base_url/linux" "$base_url/initrd.gz"
  434. elif _command_exists curl; then
  435. curl -O "$base_url/linux" -O "$base_url/initrd.gz"
  436. elif _command_exists busybox; then
  437. busybox wget "$base_url/linux" "$base_url/initrd.gz"
  438. else
  439. _err 'wget/curl/busybox is required to download files'
  440. fi
  441. gunzip initrd.gz
  442. echo preseed.cfg | cpio -H newc -o -A -F initrd
  443. gzip initrd
  444. if _command_exists update-grub; then
  445. grub_cfg=/boot/grub/grub.cfg
  446. update-grub
  447. elif _command_exists grub2-mkconfig; then
  448. grub_cfg=/boot/grub2/grub.cfg
  449. grub2-mkconfig -o "$grub_cfg"
  450. else
  451. _err 'update-grub/grub2-mkconfig command not found'
  452. fi
  453. save_grub_cfg="tee -a $grub_cfg"
  454. fi
  455. installer_directory="$boot_directory$installer"
  456. $save_grub_cfg << EOF
  457. menuentry 'Debian Installer' --id debi {
  458. insmod part_msdos
  459. insmod part_gpt
  460. insmod ext2
  461. linux $installer_directory/linux$kernel_params
  462. initrd $installer_directory/initrd.gz
  463. }
  464. EOF