netboot.sh 13 KB

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