netboot.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. #!/usr/bin/env sh
  2. # Copyright 2018-present Bohan Yang (Brent)
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. #
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. #
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. set -e
  18. echo_stderr() {
  19. echo "$@" 1>&2
  20. }
  21. command_exists() {
  22. command -v "$@" >/dev/null 2>&1
  23. }
  24. read_secret()
  25. {
  26. stty -echo
  27. trap 'stty echo' EXIT
  28. read "$@"
  29. stty echo
  30. trap - EXIT
  31. echo
  32. }
  33. while [ $# -gt 0 ]; do
  34. case $1 in
  35. --preset)
  36. DEBI_PRESET=$2
  37. shift
  38. ;;
  39. --ip)
  40. DEBI_IP=$2
  41. shift
  42. ;;
  43. --netmask)
  44. DEBI_NETMASK=$2
  45. shift
  46. ;;
  47. --gateway)
  48. DEBI_GATEWAY=$2
  49. shift
  50. ;;
  51. --ns)
  52. DEBI_NS=$2
  53. shift
  54. ;;
  55. --hostname)
  56. DEBI_HOSTNAME=$2
  57. shift
  58. ;;
  59. --ethn)
  60. DEBI_KERNEL_PARAMS=' net.ifnames=0 biosdevname=0'
  61. --ssh-password)
  62. DEBI_SSH=true
  63. DEBI_SSH_PASSWORD=$2
  64. shift
  65. ;;
  66. --ssh-keys)
  67. DEBI_SSH=true
  68. DEBI_SSH_KEYS=$2
  69. shift
  70. ;;
  71. --protocol)
  72. DEBI_PROTOCOL=$2
  73. shift
  74. ;;
  75. --mirror)
  76. DEBI_MIRROR=$2
  77. shift
  78. ;;
  79. --directory)
  80. DEBI_DIRECTORY=${2%/}
  81. shift
  82. ;;
  83. --suite)
  84. DEBI_SUITE=$2
  85. shift
  86. ;;
  87. --skip-user)
  88. DEBI_SKIP_USER=true
  89. ;;
  90. --username)
  91. DEBI_USERNAME=$2
  92. shift
  93. ;;
  94. --password)
  95. DEBI_PASSWORD=$2
  96. shift
  97. ;;
  98. --timezone)
  99. DEBI_TIMEZONE=$2
  100. shift
  101. ;;
  102. --ntp)
  103. DEBI_NTP=$2
  104. shift
  105. ;;
  106. --skip-part)
  107. DEBI_SKIP_PART=true
  108. ;;
  109. --disk)
  110. DEBI_DISK=$2
  111. shift
  112. ;;
  113. --part)
  114. DEBI_PART=$2
  115. shift
  116. ;;
  117. --fs)
  118. DEBI_FS=$2
  119. shift
  120. ;;
  121. --security)
  122. DEBI_SECURITY=$2
  123. shift
  124. ;;
  125. --install)
  126. DEBI_INSTALL=$2
  127. shift
  128. ;;
  129. --upgrade)
  130. DEBI_UPGRADE=$2
  131. shift
  132. ;;
  133. --poweroff)
  134. DEBI_POWEROFF=true
  135. ;;
  136. --arch)
  137. DEBI_ARCH=$2
  138. shift
  139. ;;
  140. --boot-partition)
  141. DEBI_BOOT_PARTITION=true
  142. ;;
  143. --dry-run)
  144. DEBI_DRY_RUN=true
  145. ;;
  146. *)
  147. echo_stderr "Error: Illegal option $1"
  148. exit 1
  149. esac
  150. shift
  151. done
  152. if [ -n "$DEBI_PRESET" ]; then
  153. case "$DEBI_PRESET" in
  154. china)
  155. DEBI_NS=${DEBI_NS:-156.154.70.5 156.154.71.5}
  156. DEBI_PROTOCOL=${DEBI_PROTOCOL:-https}
  157. DEBI_MIRROR=${DEBI_MIRROR:-mirrors.aliyun.com}
  158. DEBI_TIMEZONE=${DEBI_TIMEZONE:-Asia/Shanghai}
  159. DEBI_NTP=${DEBI_NTP:-cn.ntp.org.cn}
  160. DEBI_SECURITY=${DEBI_SECURITY:-true}
  161. ;;
  162. cloud)
  163. DEBI_PROTOCOL=${DEBI_PROTOCOL:-https}
  164. DEBI_MIRROR=${DEBI_MIRROR:-cdn-aws.deb.debian.org}
  165. DEBI_SECURITY=${DEBI_SECURITY:-true}
  166. ;;
  167. *)
  168. echo_stderr "Error: No such preset $DEBI_PRESET"
  169. exit 1
  170. esac
  171. fi
  172. DEBI_SUITE=${DEBI_SUITE:-stable}
  173. DEBI_NEW="debian-$DEBI_SUITE"
  174. DEBI_NEW_DIR="/boot/$DEBI_NEW"
  175. save_preseed=cat
  176. if [ "$DEBI_DRY_RUN" != true ]; then
  177. user="$(id -un 2>/dev/null || true)"
  178. if [ "$user" != root ]; then
  179. echo_stderr 'Error: Require root.'
  180. exit 1
  181. fi
  182. rm -rf "$DEBI_NEW_DIR"
  183. mkdir -p "$DEBI_NEW_DIR"
  184. cd "$DEBI_NEW_DIR"
  185. save_preseed='tee -a preseed.cfg'
  186. fi
  187. $save_preseed << EOF
  188. # Localization
  189. d-i debian-installer/locale string en_US.UTF-8
  190. d-i keyboard-configuration/xkb-keymap select us
  191. # Network configuration
  192. d-i netcfg/choose_interface select auto
  193. EOF
  194. DEBI_NS=${DEBI_NS:-8.8.8.8 8.8.4.4}
  195. if [ -n "$DEBI_IP" ]; then
  196. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  197. echo "d-i netcfg/get_ipaddress string $DEBI_IP" | $save_preseed
  198. if [ -n "$DEBI_NETMASK" ]; then
  199. echo "d-i netcfg/get_netmask string $DEBI_NETMASK" | $save_preseed
  200. fi
  201. if [ -n "$DEBI_GATEWAY" ]; then
  202. echo "d-i netcfg/get_gateway string $DEBI_GATEWAY" | $save_preseed
  203. fi
  204. if [ -n "$DEBI_NS" ]; then
  205. echo "d-i netcfg/get_nameservers string $DEBI_NS" | $save_preseed
  206. fi
  207. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  208. fi
  209. $save_preseed << EOF
  210. d-i netcfg/get_hostname string debian
  211. d-i netcfg/get_domain string
  212. EOF
  213. if [ -n "$DEBI_HOSTNAME" ]; then
  214. echo "d-i netcfg/hostname string $DEBI_HOSTNAME" | $save_preseed
  215. fi
  216. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  217. if [ "$DEBI_SSH" = true ]; then
  218. $save_preseed << EOF
  219. # Network console
  220. d-i anna/choose_modules string network-console
  221. d-i preseed/early_command string anna-install network-console
  222. EOF
  223. if [ -n "$DEBI_SSH_PASSWORD" ]; then
  224. echo "d-i network-console/password password $DEBI_SSH_PASSWORD" | $save_preseed
  225. echo "d-i network-console/password-again password $DEBI_SSH_PASSWORD" | $save_preseed
  226. fi
  227. if [ -n "$DEBI_SSH_KEYS" ]; then
  228. echo "d-i network-console/authorized_keys_url string $DEBI_SSH_KEYS" | $save_preseed
  229. fi
  230. echo 'd-i network-console/start select Continue' | $save_preseed
  231. fi
  232. DEBI_PROTOCOL=${DEBI_PROTOCOL:-http}
  233. DEBI_MIRROR=${DEBI_MIRROR:-deb.debian.org}
  234. DEBI_DIRECTORY=${DEBI_DIRECTORY:-/debian}
  235. $save_preseed << EOF
  236. # Mirror settings
  237. d-i mirror/country string manual
  238. d-i mirror/protocol string $DEBI_PROTOCOL
  239. d-i mirror/$DEBI_PROTOCOL/hostname string $DEBI_MIRROR
  240. d-i mirror/$DEBI_PROTOCOL/directory string $DEBI_DIRECTORY
  241. d-i mirror/$DEBI_PROTOCOL/proxy string
  242. d-i mirror/suite string $DEBI_SUITE
  243. d-i mirror/udeb/suite string $DEBI_SUITE
  244. EOF
  245. if [ "$DEBI_SKIP_USER" != true ]; then
  246. DEBI_USERNAME=${DEBI_USERNAME:-debian}
  247. if command_exists mkpasswd; then
  248. if [ -z "$DEBI_PASSWORD" ]; then
  249. DEBI_PASSWORD="$(mkpasswd -m sha-512)"
  250. else
  251. DEBI_PASSWORD="$(mkpasswd -m sha-512 "$DEBI_PASSWORD")"
  252. fi
  253. elif command_exists python3; then
  254. if [ -z "$DEBI_PASSWORD" ]; then
  255. DEBI_PASSWORD="$(python3 -c 'import crypt, getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))')"
  256. else
  257. DEBI_PASSWORD="$(python3 -c "import crypt; print(crypt.crypt(\"$DEBI_PASSWORD\", crypt.mksalt(crypt.METHOD_SHA512)))")"
  258. fi
  259. else
  260. DEBI_CLEARTEXT=true
  261. if [ -z "$DEBI_PASSWORD" ]; then
  262. printf 'Password: '
  263. read_secret DEBI_PASSWORD
  264. fi
  265. fi
  266. $save_preseed << EOF
  267. # Account setup
  268. EOF
  269. if [ "$DEBI_USERNAME" = root ]; then
  270. echo 'd-i passwd/make-user boolean false' | $save_preseed
  271. if [ "$DEBI_CLEARTEXT" = true ]; then
  272. $save_preseed << EOF
  273. d-i passwd/root-password password $DEBI_PASSWORD
  274. d-i passwd/root-password-again password $DEBI_PASSWORD
  275. EOF
  276. else
  277. echo "d-i passwd/root-password-crypted password $DEBI_PASSWORD" | $save_preseed
  278. fi
  279. else
  280. $save_preseed << EOF
  281. d-i passwd/root-login boolean false
  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 [ -z "$DEBI_SECURITY" ]; then
  341. DEBI_SECURITY=http://security.debian.org/debian-security
  342. else
  343. if [ "$DEBI_SECURITY" = true ]; then
  344. DEBI_SECURITY=$DEBI_PROTOCOL://$DEBI_MIRROR${DEBI_DIRECTORY%/*}/debian-security
  345. fi
  346. fi
  347. $save_preseed << EOF
  348. # Apt setup
  349. d-i apt-setup/services-select multiselect updates, backports
  350. d-i apt-setup/local0/repository string $DEBI_SECURITY $DEBI_SUITE/updates main
  351. d-i apt-setup/local0/source boolean true
  352. EOF
  353. DEBI_UPGRADE=${DEBI_UPGRADE:-full-upgrade}
  354. $save_preseed << EOF
  355. # Package selection
  356. tasksel tasksel/first multiselect ssh-server
  357. EOF
  358. if [ -n "$DEBI_INSTALL" ]; then
  359. echo "d-i pkgsel/include string $DEBI_INSTALL" | $save_preseed
  360. fi
  361. $save_preseed << EOF
  362. d-i pkgsel/upgrade select $DEBI_UPGRADE
  363. popularity-contest popularity-contest/participate boolean false
  364. # Boot loader installation
  365. d-i grub-installer/only_debian boolean true
  366. d-i grub-installer/bootdev string default
  367. # Finishing up the installation
  368. d-i finish-install/reboot_in_progress note
  369. EOF
  370. if [ "$DEBI_POWEROFF" = true ]; then
  371. echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  372. fi
  373. save_grubcfg=cat
  374. if [ "$DEBI_DRY_RUN" != true ]; then
  375. if [ -z "$DEBI_ARCH" ]; then
  376. if command_exists dpkg; then
  377. DEBI_ARCH="$(dpkg --print-architecture)"
  378. else
  379. DEBI_ARCH=amd64
  380. fi
  381. fi
  382. DEBI_BASE_URL="$DEBI_PROTOCOL://$DEBI_MIRROR$DEBI_DIRECTORY/dists/$DEBI_SUITE/main/installer-$DEBI_ARCH/current/images/netboot/debian-installer/$DEBI_ARCH"
  383. if command_exists wget; then
  384. wget "$DEBI_BASE_URL/linux" "$DEBI_BASE_URL/initrd.gz"
  385. elif command_exists curl; then
  386. curl -O "$DEBI_BASE_URL/linux" -O "$DEBI_BASE_URL/initrd.gz"
  387. else
  388. echo_stderr 'Error: wget/curl not found.'
  389. exit 1
  390. fi
  391. gunzip initrd.gz
  392. echo preseed.cfg | cpio -H newc -o -A -F initrd
  393. gzip initrd
  394. if command_exists update-grub; then
  395. DEBI_GRUBCFG=/boot/grub/grub.cfg
  396. update-grub
  397. elif command_exists grub2-mkconfig; then
  398. DEBI_GRUBCFG=/boot/grub2/grub.cfg
  399. grub2-mkconfig -o "$DEBI_GRUBCFG"
  400. else
  401. echo_stderr 'Error: Command update-grub/grub2-mkconfig not found.'
  402. exit 1
  403. fi
  404. save_grubcfg="tee -a $DEBI_GRUBCFG"
  405. fi
  406. if [ "$DEBI_BOOT_PARTITION" = true ]; then
  407. DEBI_BOOT_DIR=/
  408. else
  409. DEBI_BOOT_DIR=/boot/
  410. fi
  411. DEBI_NEW_DIR="$DEBI_BOOT_DIR$DEBI_NEW"
  412. $save_grubcfg << EOF
  413. menuentry 'Debian Installer' --id debi {
  414. insmod part_msdos
  415. insmod part_gpt
  416. insmod ext2
  417. linux $DEBI_NEW_DIR/linux$DEBI_KERNEL_PARAMS
  418. initrd $DEBI_NEW_DIR/initrd.gz
  419. }
  420. EOF