netboot.sh 12 KB

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