netboot.sh 12 KB

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