netboot.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. $save_preseed << EOF
  215. d-i network-console/password-disabled boolean false
  216. d-i network-console/password password $DEBI_SSH_PASSWORD
  217. d-i network-console/password-again password $DEBI_SSH_PASSWORD
  218. EOF
  219. elif [ -n "$DEBI_SSH_KEYS" ]; then
  220. $save_preseed << EOF
  221. d-i network-console/password-disabled boolean true
  222. d-i network-console/authorized_keys_url string $DEBI_SSH_KEYS
  223. EOF
  224. fi
  225. echo 'd-i network-console/start select Continue' | $save_preseed
  226. fi
  227. DEBI_PROTOCOL=${DEBI_PROTOCOL:-http}
  228. DEBI_MIRROR=${DEBI_MIRROR:-deb.debian.org}
  229. DEBI_DIRECTORY=${DEBI_DIRECTORY:-/debian}
  230. $save_preseed << EOF
  231. # Mirror settings
  232. d-i mirror/country string manual
  233. d-i mirror/protocol string $DEBI_PROTOCOL
  234. d-i mirror/$DEBI_PROTOCOL/hostname string $DEBI_MIRROR
  235. d-i mirror/$DEBI_PROTOCOL/directory string $DEBI_DIRECTORY
  236. d-i mirror/$DEBI_PROTOCOL/proxy string
  237. d-i mirror/suite string $DEBI_SUITE
  238. d-i mirror/udeb/suite string $DEBI_SUITE
  239. EOF
  240. if [ "$DEBI_SKIP_USER" != true ]; then
  241. DEBI_USERNAME=${DEBI_USERNAME:-debian}
  242. if command_exists mkpasswd; then
  243. if [ -z "$DEBI_PASSWORD" ]; then
  244. DEBI_PASSWORD="$(mkpasswd -m sha512crypt)"
  245. else
  246. DEBI_PASSWORD="$(mkpasswd -m sha512crypt "$DEBI_PASSWORD")"
  247. fi
  248. elif command_exists busybox && busybox mkpasswd --help >/dev/null 2>&1; then
  249. if [ -z "$DEBI_PASSWORD" ]; then
  250. DEBI_PASSWORD="$(busybox mkpasswd -m sha512)"
  251. else
  252. DEBI_PASSWORD="$(busybox mkpasswd -m sha512 "$DEBI_PASSWORD")"
  253. fi
  254. elif command_exists python3; then
  255. if [ -z "$DEBI_PASSWORD" ]; then
  256. DEBI_PASSWORD="$(python3 -c 'import crypt, getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))')"
  257. else
  258. DEBI_PASSWORD="$(python3 -c "import crypt; print(crypt.crypt(\"$DEBI_PASSWORD\", crypt.mksalt(crypt.METHOD_SHA512)))")"
  259. fi
  260. else
  261. DEBI_CLEARTEXT=true
  262. if [ -z "$DEBI_PASSWORD" ]; then
  263. printf 'Password: '
  264. read_secret DEBI_PASSWORD
  265. fi
  266. fi
  267. $save_preseed << EOF
  268. # Account setup
  269. EOF
  270. if [ "$DEBI_USERNAME" = root ]; then
  271. $save_preseed << EOF
  272. d-i passwd/root-login boolean true
  273. d-i passwd/make-user boolean false
  274. EOF
  275. if [ "$DEBI_CLEARTEXT" = true ]; then
  276. $save_preseed << EOF
  277. d-i passwd/root-password password $DEBI_PASSWORD
  278. d-i passwd/root-password-again password $DEBI_PASSWORD
  279. EOF
  280. else
  281. echo "d-i passwd/root-password-crypted password $DEBI_PASSWORD" | $save_preseed
  282. fi
  283. else
  284. $save_preseed << EOF
  285. d-i passwd/root-login boolean false
  286. d-i passwd/make-user boolean true
  287. d-i passwd/user-fullname string
  288. d-i passwd/username string $DEBI_USERNAME
  289. EOF
  290. if [ "$DEBI_CLEARTEXT" = true ]; then
  291. $save_preseed << EOF
  292. d-i passwd/user-password password $DEBI_PASSWORD
  293. d-i passwd/user-password-again password $DEBI_PASSWORD
  294. EOF
  295. else
  296. echo "d-i passwd/user-password-crypted password $DEBI_PASSWORD" | $save_preseed
  297. fi
  298. fi
  299. fi
  300. DEBI_TIMEZONE=${DEBI_TIMEZONE:-UTC}
  301. DEBI_NTP=${DEBI_NTP:-0.debian.pool.ntp.org}
  302. $save_preseed << EOF
  303. # Clock and time zone setup
  304. d-i clock-setup/utc boolean true
  305. d-i time/zone string $DEBI_TIMEZONE
  306. d-i clock-setup/ntp boolean true
  307. d-i clock-setup/ntp-server string $DEBI_NTP
  308. EOF
  309. if [ "$DEBI_SKIP_PART" != true ]; then
  310. DEBI_FS=${DEBI_FS:-ext4}
  311. DEBI_PART=${DEBI_PART:-regular}
  312. $save_preseed << EOF
  313. # Partitioning
  314. EOF
  315. if [ -n "$DEBI_DISK" ]; then
  316. echo "d-i partman-auto/disk string $DEBI_DISK" | $save_preseed
  317. fi
  318. $save_preseed << EOF
  319. d-i partman-auto/method string $DEBI_PART
  320. d-i partman-lvm/device_remove_lvm boolean true
  321. d-i partman-md/device_remove_md boolean true
  322. d-i partman-lvm/confirm boolean true
  323. d-i partman-lvm/confirm_nooverwrite boolean true
  324. EOF
  325. if [ "$DEBI_PART" = "regular" ]; then
  326. $save_preseed << EOF
  327. d-i partman/default_filesystem string $DEBI_FS
  328. d-i partman-auto/expert_recipe string naive :: 0 1 -1 \$default_filesystem \$primary{ } \$bootable{ } method{ format } format{ } use_filesystem{ } \$default_filesystem{ } mountpoint{ / } .
  329. d-i partman-auto/choose_recipe select naive
  330. d-i partman-basicfilesystems/no_swap boolean false
  331. EOF
  332. fi
  333. $save_preseed << EOF
  334. d-i partman-partitioning/confirm_write_new_label boolean true
  335. d-i partman/choose_partition select finish
  336. d-i partman/confirm boolean true
  337. d-i partman/confirm_nooverwrite boolean true
  338. d-i partman/mount_style select uuid
  339. EOF
  340. fi
  341. $save_preseed << EOF
  342. # Base system installation
  343. d-i base-installer/install-recommends boolean false
  344. EOF
  345. if [ -n "$DEBI_KERNEL" ]; then
  346. echo "d-i base-installer/kernel/image string $DEBI_KERNEL" | $save_preseed
  347. fi
  348. if [ -z "$DEBI_SECURITY" ]; then
  349. DEBI_SECURITY=http://security.debian.org/debian-security
  350. else
  351. if [ "$DEBI_SECURITY" = true ]; then
  352. DEBI_SECURITY=$DEBI_PROTOCOL://$DEBI_MIRROR${DEBI_DIRECTORY%/*}/debian-security
  353. fi
  354. fi
  355. $save_preseed << EOF
  356. # Apt setup
  357. d-i apt-setup/services-select multiselect updates, backports
  358. d-i apt-setup/local0/repository string $DEBI_SECURITY $DEBI_SUITE/updates main
  359. d-i apt-setup/local0/source boolean true
  360. EOF
  361. DEBI_UPGRADE=${DEBI_UPGRADE:-full-upgrade}
  362. $save_preseed << EOF
  363. # Package selection
  364. tasksel tasksel/first multiselect ssh-server
  365. EOF
  366. if [ -n "$DEBI_INSTALL" ]; then
  367. echo "d-i pkgsel/include string $DEBI_INSTALL" | $save_preseed
  368. fi
  369. $save_preseed << EOF
  370. d-i pkgsel/upgrade select $DEBI_UPGRADE
  371. popularity-contest popularity-contest/participate boolean false
  372. # Boot loader installation
  373. d-i grub-installer/only_debian boolean true
  374. d-i grub-installer/bootdev string default
  375. EOF
  376. if [ -n "$DEBI_KERNEL_PARAMS" ]; then
  377. echo "d-i debian-installer/add-kernel-opts string$DEBI_KERNEL_PARAMS" | $save_preseed
  378. fi
  379. if [ "$DEBI_USERNAME" = root ]; then
  380. $save_preseed << 'EOF'
  381. d-i preseed/late_command string \
  382. in-target sed -ri 's/^#?PermitRootLogin .+/PermitRootLogin yes/' /etc/ssh/sshd_config
  383. EOF
  384. fi
  385. $save_preseed << EOF
  386. # Finishing up the installation
  387. d-i finish-install/reboot_in_progress note
  388. EOF
  389. if [ "$DEBI_POWEROFF" = true ]; then
  390. echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  391. fi
  392. save_grubcfg="cat"
  393. if [ "$DEBI_DRY_RUN" != true ]; then
  394. if [ -z "$DEBI_ARCH" ]; then
  395. if command_exists dpkg; then
  396. DEBI_ARCH="$(dpkg --print-architecture)"
  397. else
  398. DEBI_ARCH=amd64
  399. fi
  400. fi
  401. DEBI_BASE_URL="$DEBI_PROTOCOL://$DEBI_MIRROR$DEBI_DIRECTORY/dists/$DEBI_SUITE/main/installer-$DEBI_ARCH/current/images/netboot/debian-installer/$DEBI_ARCH"
  402. if command_exists wget; then
  403. wget "$DEBI_BASE_URL/linux" "$DEBI_BASE_URL/initrd.gz"
  404. elif command_exists curl; then
  405. curl -O "$DEBI_BASE_URL/linux" -O "$DEBI_BASE_URL/initrd.gz"
  406. elif command_exists busybox; then
  407. busybox wget "$DEBI_BASE_URL/linux" "$DEBI_BASE_URL/initrd.gz"
  408. else
  409. echo_stderr 'Error: wget/curl/busybox not found.'
  410. exit 1
  411. fi
  412. gunzip initrd.gz
  413. echo preseed.cfg | cpio -H newc -o -A -F initrd
  414. gzip initrd
  415. if command_exists update-grub; then
  416. DEBI_GRUBCFG=/boot/grub/grub.cfg
  417. update-grub
  418. elif command_exists grub2-mkconfig; then
  419. DEBI_GRUBCFG=/boot/grub2/grub.cfg
  420. grub2-mkconfig -o "$DEBI_GRUBCFG"
  421. else
  422. echo_stderr 'Error: Command update-grub/grub2-mkconfig not found.'
  423. exit 1
  424. fi
  425. save_grubcfg="tee -a $DEBI_GRUBCFG"
  426. fi
  427. if [ "$DEBI_BOOT_PARTITION" = true ]; then
  428. DEBI_BOOT_DIR=/
  429. else
  430. DEBI_BOOT_DIR=/boot/
  431. fi
  432. DEBI_NEW_DIR="$DEBI_BOOT_DIR$DEBI_NEW"
  433. $save_grubcfg << EOF
  434. menuentry 'Debian Installer' --id debi {
  435. insmod part_msdos
  436. insmod part_gpt
  437. insmod ext2
  438. linux $DEBI_NEW_DIR/linux$DEBI_KERNEL_PARAMS
  439. initrd $DEBI_NEW_DIR/initrd.gz
  440. }
  441. EOF