netboot.sh 13 KB

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