netboot.sh 13 KB

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