netboot.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #!/usr/bin/env sh
  2. set -e
  3. echo_stderr() {
  4. echo "$@" 1>&2
  5. }
  6. command_exists() {
  7. command -v "$@" >/dev/null 2>&1
  8. }
  9. read_secret()
  10. {
  11. stty -echo
  12. trap 'stty echo' EXIT
  13. read -r "$@"
  14. stty echo
  15. trap - EXIT
  16. echo
  17. }
  18. while [ $# -gt 0 ]; do
  19. case $1 in
  20. --preset)
  21. DEBI_PRESET=$2
  22. shift
  23. ;;
  24. --ip)
  25. DEBI_IP=$2
  26. shift
  27. ;;
  28. --netmask)
  29. DEBI_NETMASK=$2
  30. shift
  31. ;;
  32. --gateway)
  33. DEBI_GATEWAY=$2
  34. shift
  35. ;;
  36. --ns)
  37. DEBI_NS=$2
  38. shift
  39. ;;
  40. --hostname)
  41. DEBI_HOSTNAME=$2
  42. shift
  43. ;;
  44. --ethn)
  45. DEBI_KERNEL_PARAMS=' net.ifnames=0 biosdevname=0'
  46. ;;
  47. --ssh-password)
  48. DEBI_SSH=true
  49. DEBI_SSH_PASSWORD=$2
  50. shift
  51. ;;
  52. --ssh-keys)
  53. DEBI_SSH=true
  54. DEBI_SSH_KEYS=$2
  55. shift
  56. ;;
  57. --protocol)
  58. DEBI_PROTOCOL=$2
  59. shift
  60. ;;
  61. --mirror)
  62. DEBI_MIRROR=$2
  63. shift
  64. ;;
  65. --directory)
  66. DEBI_DIRECTORY=${2%/}
  67. shift
  68. ;;
  69. --suite)
  70. DEBI_SUITE=$2
  71. shift
  72. ;;
  73. --skip-user)
  74. DEBI_SKIP_USER=true
  75. ;;
  76. --username)
  77. DEBI_USERNAME=$2
  78. shift
  79. ;;
  80. --password)
  81. DEBI_PASSWORD=$2
  82. shift
  83. ;;
  84. --timezone)
  85. DEBI_TIMEZONE=$2
  86. shift
  87. ;;
  88. --ntp)
  89. DEBI_NTP=$2
  90. shift
  91. ;;
  92. --skip-part)
  93. DEBI_SKIP_PART=true
  94. ;;
  95. --disk)
  96. DEBI_DISK=$2
  97. shift
  98. ;;
  99. --part)
  100. DEBI_PART=$2
  101. shift
  102. ;;
  103. --fs)
  104. DEBI_FS=$2
  105. shift
  106. ;;
  107. --kernel)
  108. DEBI_KERNEL=$2
  109. shift
  110. ;;
  111. --security)
  112. DEBI_SECURITY=$2
  113. shift
  114. ;;
  115. --install)
  116. DEBI_INSTALL=$2
  117. shift
  118. ;;
  119. --upgrade)
  120. DEBI_UPGRADE=$2
  121. shift
  122. ;;
  123. --poweroff)
  124. DEBI_POWEROFF=true
  125. ;;
  126. --arch)
  127. DEBI_ARCH=$2
  128. shift
  129. ;;
  130. --boot-partition)
  131. DEBI_BOOT_PARTITION=true
  132. ;;
  133. --dry-run)
  134. DEBI_DRY_RUN=true
  135. ;;
  136. *)
  137. echo_stderr "Error: Illegal option $1"
  138. exit 1
  139. esac
  140. shift
  141. done
  142. if [ -n "$DEBI_PRESET" ]; then
  143. case "$DEBI_PRESET" in
  144. china)
  145. DEBI_NS=${DEBI_NS:-223.5.5.5 223.6.6.6}
  146. DEBI_PROTOCOL=${DEBI_PROTOCOL:-https}
  147. DEBI_MIRROR=${DEBI_MIRROR:-mirrors.aliyun.com}
  148. DEBI_TIMEZONE=${DEBI_TIMEZONE:-Asia/Shanghai}
  149. DEBI_NTP=${DEBI_NTP:-ntp.aliyun.com}
  150. DEBI_SECURITY=${DEBI_SECURITY:-true}
  151. ;;
  152. cloud)
  153. DEBI_PROTOCOL=${DEBI_PROTOCOL:-https}
  154. DEBI_MIRROR=${DEBI_MIRROR:-deb.debian.org}
  155. DEBI_SECURITY=${DEBI_SECURITY:-true}
  156. ;;
  157. *)
  158. echo_stderr "Error: No such preset $DEBI_PRESET"
  159. exit 1
  160. esac
  161. fi
  162. DEBI_SUITE=${DEBI_SUITE:-stable}
  163. DEBI_NEW="debian-$DEBI_SUITE"
  164. DEBI_NEW_DIR="/boot/$DEBI_NEW"
  165. save_preseed="cat"
  166. if [ "$DEBI_DRY_RUN" != true ]; then
  167. user="$(id -un 2>/dev/null || true)"
  168. if [ "$user" != root ]; then
  169. echo_stderr 'Error: Require root.'
  170. exit 1
  171. fi
  172. rm -rf "$DEBI_NEW_DIR"
  173. mkdir -p "$DEBI_NEW_DIR"
  174. cd "$DEBI_NEW_DIR"
  175. save_preseed='tee -a preseed.cfg'
  176. fi
  177. $save_preseed << EOF
  178. # Localization
  179. d-i debian-installer/locale string en_US.UTF-8
  180. d-i keyboard-configuration/xkb-keymap select us
  181. # Network configuration
  182. d-i netcfg/choose_interface select auto
  183. EOF
  184. DEBI_NS=${DEBI_NS:-8.8.8.8 8.8.4.4}
  185. if [ -n "$DEBI_IP" ]; then
  186. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  187. echo "d-i netcfg/get_ipaddress string $DEBI_IP" | $save_preseed
  188. if [ -n "$DEBI_NETMASK" ]; then
  189. echo "d-i netcfg/get_netmask string $DEBI_NETMASK" | $save_preseed
  190. fi
  191. if [ -n "$DEBI_GATEWAY" ]; then
  192. echo "d-i netcfg/get_gateway string $DEBI_GATEWAY" | $save_preseed
  193. fi
  194. if [ -n "$DEBI_NS" ]; then
  195. echo "d-i netcfg/get_nameservers string $DEBI_NS" | $save_preseed
  196. fi
  197. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  198. fi
  199. $save_preseed << EOF
  200. d-i netcfg/get_hostname string debian
  201. d-i netcfg/get_domain string
  202. EOF
  203. if [ -n "$DEBI_HOSTNAME" ]; then
  204. echo "d-i netcfg/hostname string $DEBI_HOSTNAME" | $save_preseed
  205. fi
  206. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  207. if [ "$DEBI_SSH" = true ]; then
  208. $save_preseed << EOF
  209. # Network console
  210. d-i anna/choose_modules string network-console
  211. d-i preseed/early_command string anna-install network-console
  212. EOF
  213. if [ -n "$DEBI_SSH_PASSWORD" ]; then
  214. echo "d-i network-console/password password $DEBI_SSH_PASSWORD" | $save_preseed
  215. echo "d-i network-console/password-again password $DEBI_SSH_PASSWORD" | $save_preseed
  216. fi
  217. if [ -n "$DEBI_SSH_KEYS" ]; then
  218. echo "d-i network-console/authorized_keys_url string $DEBI_SSH_KEYS" | $save_preseed
  219. fi
  220. echo 'd-i network-console/start select Continue' | $save_preseed
  221. fi
  222. DEBI_PROTOCOL=${DEBI_PROTOCOL:-http}
  223. DEBI_MIRROR=${DEBI_MIRROR:-deb.debian.org}
  224. DEBI_DIRECTORY=${DEBI_DIRECTORY:-/debian}
  225. $save_preseed << EOF
  226. # Mirror settings
  227. d-i mirror/country string manual
  228. d-i mirror/protocol string $DEBI_PROTOCOL
  229. d-i mirror/$DEBI_PROTOCOL/hostname string $DEBI_MIRROR
  230. d-i mirror/$DEBI_PROTOCOL/directory string $DEBI_DIRECTORY
  231. d-i mirror/$DEBI_PROTOCOL/proxy string
  232. d-i mirror/suite string $DEBI_SUITE
  233. d-i mirror/udeb/suite string $DEBI_SUITE
  234. EOF
  235. if [ "$DEBI_SKIP_USER" != true ]; then
  236. DEBI_USERNAME=${DEBI_USERNAME:-debian}
  237. if command_exists mkpasswd; then
  238. if [ -z "$DEBI_PASSWORD" ]; then
  239. DEBI_PASSWORD="$(mkpasswd -m sha512crypt)"
  240. else
  241. DEBI_PASSWORD="$(mkpasswd -m sha512crypt "$DEBI_PASSWORD")"
  242. fi
  243. elif command_exists busybox && busybox mkpasswd --help >/dev/null 2>&1; then
  244. if [ -z "$DEBI_PASSWORD" ]; then
  245. DEBI_PASSWORD="$(busybox mkpasswd -m sha512)"
  246. else
  247. DEBI_PASSWORD="$(busybox mkpasswd -m sha512 "$DEBI_PASSWORD")"
  248. fi
  249. elif command_exists python3; then
  250. if [ -z "$DEBI_PASSWORD" ]; then
  251. DEBI_PASSWORD="$(python3 -c 'import crypt, getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))')"
  252. else
  253. DEBI_PASSWORD="$(python3 -c "import crypt; print(crypt.crypt(\"$DEBI_PASSWORD\", crypt.mksalt(crypt.METHOD_SHA512)))")"
  254. fi
  255. else
  256. DEBI_CLEARTEXT=true
  257. if [ -z "$DEBI_PASSWORD" ]; then
  258. printf 'Password: '
  259. read_secret DEBI_PASSWORD
  260. fi
  261. fi
  262. $save_preseed << EOF
  263. # Account setup
  264. EOF
  265. if [ "$DEBI_USERNAME" = root ]; then
  266. echo 'd-i passwd/make-user boolean false' | $save_preseed
  267. if [ "$DEBI_CLEARTEXT" = true ]; then
  268. $save_preseed << EOF
  269. d-i passwd/root-password password $DEBI_PASSWORD
  270. d-i passwd/root-password-again password $DEBI_PASSWORD
  271. EOF
  272. else
  273. echo "d-i passwd/root-password-crypted password $DEBI_PASSWORD" | $save_preseed
  274. fi
  275. else
  276. $save_preseed << EOF
  277. d-i passwd/root-login boolean false
  278. d-i passwd/user-fullname string
  279. d-i passwd/username string $DEBI_USERNAME
  280. EOF
  281. if [ "$DEBI_CLEARTEXT" = true ]; then
  282. $save_preseed << EOF
  283. d-i passwd/user-password password $DEBI_PASSWORD
  284. d-i passwd/user-password-again password $DEBI_PASSWORD
  285. EOF
  286. else
  287. echo "d-i passwd/user-password-crypted password $DEBI_PASSWORD" | $save_preseed
  288. fi
  289. fi
  290. fi
  291. DEBI_TIMEZONE=${DEBI_TIMEZONE:-UTC}
  292. DEBI_NTP=${DEBI_NTP:-0.debian.pool.ntp.org}
  293. $save_preseed << EOF
  294. # Clock and time zone setup
  295. d-i clock-setup/utc boolean true
  296. d-i time/zone string $DEBI_TIMEZONE
  297. d-i clock-setup/ntp boolean true
  298. d-i clock-setup/ntp-server string $DEBI_NTP
  299. EOF
  300. if [ "$DEBI_SKIP_PART" != true ]; then
  301. DEBI_FS=${DEBI_FS:-ext4}
  302. DEBI_PART=${DEBI_PART:-regular}
  303. $save_preseed << EOF
  304. # Partitioning
  305. EOF
  306. if [ -n "$DEBI_DISK" ]; then
  307. echo "d-i partman-auto/disk string $DEBI_DISK" | $save_preseed
  308. fi
  309. $save_preseed << EOF
  310. d-i partman-auto/method string $DEBI_PART
  311. d-i partman-lvm/device_remove_lvm boolean true
  312. d-i partman-md/device_remove_md boolean true
  313. d-i partman-lvm/confirm boolean true
  314. d-i partman-lvm/confirm_nooverwrite boolean true
  315. EOF
  316. if [ "$DEBI_PART" = "regular" ]; then
  317. $save_preseed << EOF
  318. d-i partman/default_filesystem string $DEBI_FS
  319. d-i partman-auto/expert_recipe string naive :: 0 1 -1 \$default_filesystem \$primary{ } \$bootable{ } method{ format } format{ } use_filesystem{ } \$default_filesystem{ } mountpoint{ / } .
  320. d-i partman-auto/choose_recipe select naive
  321. d-i partman-basicfilesystems/no_swap boolean false
  322. EOF
  323. fi
  324. $save_preseed << EOF
  325. d-i partman-partitioning/confirm_write_new_label boolean true
  326. d-i partman/choose_partition select finish
  327. d-i partman/confirm boolean true
  328. d-i partman/confirm_nooverwrite boolean true
  329. d-i partman/mount_style select uuid
  330. EOF
  331. fi
  332. $save_preseed << EOF
  333. # Base system installation
  334. d-i base-installer/install-recommends boolean false
  335. EOF
  336. if [ -n "$DEBI_KERNEL" ]; then
  337. echo "d-i base-installer/kernel/image string $DEBI_KERNEL" | $save_preseed
  338. fi
  339. if [ -z "$DEBI_SECURITY" ]; then
  340. DEBI_SECURITY=http://security.debian.org/debian-security
  341. else
  342. if [ "$DEBI_SECURITY" = true ]; then
  343. DEBI_SECURITY=$DEBI_PROTOCOL://$DEBI_MIRROR${DEBI_DIRECTORY%/*}/debian-security
  344. fi
  345. fi
  346. $save_preseed << EOF
  347. # Apt setup
  348. d-i apt-setup/services-select multiselect updates, backports
  349. d-i apt-setup/local0/repository string $DEBI_SECURITY $DEBI_SUITE/updates main
  350. d-i apt-setup/local0/source boolean true
  351. EOF
  352. DEBI_UPGRADE=${DEBI_UPGRADE:-full-upgrade}
  353. $save_preseed << EOF
  354. # Package selection
  355. tasksel tasksel/first multiselect ssh-server
  356. EOF
  357. if [ -n "$DEBI_INSTALL" ]; then
  358. echo "d-i pkgsel/include string $DEBI_INSTALL" | $save_preseed
  359. fi
  360. $save_preseed << EOF
  361. d-i pkgsel/upgrade select $DEBI_UPGRADE
  362. popularity-contest popularity-contest/participate boolean false
  363. # Boot loader installation
  364. d-i grub-installer/only_debian boolean true
  365. d-i grub-installer/bootdev string default
  366. EOF
  367. if [ -n "$DEBI_KERNEL_PARAMS" ]; then
  368. echo "d-i debian-installer/add-kernel-opts string$DEBI_KERNEL_PARAMS" | $save_preseed
  369. fi
  370. $save_preseed << EOF
  371. # Finishing up the installation
  372. d-i finish-install/reboot_in_progress note
  373. EOF
  374. if [ "$DEBI_POWEROFF" = true ]; then
  375. echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  376. fi
  377. save_grubcfg="cat"
  378. if [ "$DEBI_DRY_RUN" != true ]; then
  379. if [ -z "$DEBI_ARCH" ]; then
  380. if command_exists dpkg; then
  381. DEBI_ARCH="$(dpkg --print-architecture)"
  382. else
  383. DEBI_ARCH=amd64
  384. fi
  385. fi
  386. DEBI_BASE_URL="$DEBI_PROTOCOL://$DEBI_MIRROR$DEBI_DIRECTORY/dists/$DEBI_SUITE/main/installer-$DEBI_ARCH/current/images/netboot/debian-installer/$DEBI_ARCH"
  387. if command_exists wget; then
  388. wget "$DEBI_BASE_URL/linux" "$DEBI_BASE_URL/initrd.gz"
  389. elif command_exists curl; then
  390. curl -O "$DEBI_BASE_URL/linux" -O "$DEBI_BASE_URL/initrd.gz"
  391. elif command_exists busybox; then
  392. busybox wget "$DEBI_BASE_URL/linux" "$DEBI_BASE_URL/initrd.gz"
  393. else
  394. echo_stderr 'Error: wget/curl/busybox not found.'
  395. exit 1
  396. fi
  397. gunzip initrd.gz
  398. echo preseed.cfg | cpio -H newc -o -A -F initrd
  399. gzip initrd
  400. if command_exists update-grub; then
  401. DEBI_GRUBCFG=/boot/grub/grub.cfg
  402. update-grub
  403. elif command_exists grub2-mkconfig; then
  404. DEBI_GRUBCFG=/boot/grub2/grub.cfg
  405. grub2-mkconfig -o "$DEBI_GRUBCFG"
  406. else
  407. echo_stderr 'Error: Command update-grub/grub2-mkconfig not found.'
  408. exit 1
  409. fi
  410. save_grubcfg="tee -a $DEBI_GRUBCFG"
  411. fi
  412. if [ "$DEBI_BOOT_PARTITION" = true ]; then
  413. DEBI_BOOT_DIR=/
  414. else
  415. DEBI_BOOT_DIR=/boot/
  416. fi
  417. DEBI_NEW_DIR="$DEBI_BOOT_DIR$DEBI_NEW"
  418. $save_grubcfg << EOF
  419. menuentry 'Debian Installer' --id debi {
  420. insmod part_msdos
  421. insmod part_gpt
  422. insmod ext2
  423. linux $DEBI_NEW_DIR/linux$DEBI_KERNEL_PARAMS
  424. initrd $DEBI_NEW_DIR/initrd.gz
  425. }
  426. EOF