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. --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. echo_stderr "Error: No such preset $DEBI_PRESET"
  169. exit 1
  170. esac
  171. DEBI_SUITE=${DEBI_SUITE:-stretch}
  172. save_preseed=cat
  173. if [ "$DEBI_DRY_RUN" != true ]; then
  174. user="$(id -un 2>/dev/null || true)"
  175. if [ "$user" != root ]; then
  176. echo_stderr 'Error: Require root.'
  177. exit 1
  178. fi
  179. DEBI_NEW="debian-$DEBI_SUITE"
  180. DEBI_NEW_DIR="/boot/$DEBI_NEW"
  181. rm -rf "$DEBI_NEW_DIR"
  182. mkdir -p "$DEBI_NEW_DIR"
  183. save_preseed="tee -a $DEBI_NEW_DIR/preseed.cfg"
  184. fi
  185. $save_preseed << EOF
  186. # Localization
  187. d-i debian-installer/locale string en_US.UTF-8
  188. d-i keyboard-configuration/xkb-keymap select us
  189. # Network configuration
  190. d-i netcfg/choose_interface select auto
  191. EOF
  192. DEBI_NS=${DEBI_NS:-1.1.1.1 1.0.0.1}
  193. if [ -n "$DEBI_IP" ]; then
  194. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  195. echo "d-i netcfg/get_ipaddress string $DEBI_IP" | $save_preseed
  196. if [ -n "$DEBI_NETMASK" ]; then
  197. echo "d-i netcfg/get_netmask string $DEBI_NETMASK" | $save_preseed
  198. fi
  199. if [ -n "$DEBI_GATEWAY" ]; then
  200. echo "d-i netcfg/get_gateway string $DEBI_GATEWAY" | $save_preseed
  201. fi
  202. if [ -n "$DEBI_NS" ]; then
  203. echo "d-i netcfg/get_nameservers string $DEBI_NS" | $save_preseed
  204. fi
  205. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  206. fi
  207. $save_preseed << EOF
  208. d-i netcfg/get_hostname string debian
  209. d-i netcfg/get_domain string
  210. EOF
  211. if [ -n "$DEBI_HOSTNAME" ]; then
  212. echo "d-i netcfg/hostname string $DEBI_HOSTNAME" | $save_preseed
  213. fi
  214. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  215. if [ "$DEBI_SSH" = true ]; then
  216. $save_preseed << EOF
  217. # Network console
  218. d-i anna/choose_modules string network-console
  219. d-i preseed/early_command string anna-install network-console
  220. EOF
  221. if [ -n "$DEBI_SSH_PASSWORD" ]; then
  222. echo "d-i network-console/password password $DEBI_SSH_PASSWORD" | $save_preseed
  223. echo "d-i network-console/password-again password $DEBI_SSH_PASSWORD" | $save_preseed
  224. fi
  225. if [ -n "$DEBI_SSH_KEYS" ]; then
  226. echo "d-i network-console/authorized_keys_url string $DEBI_SSH_KEYS" | $save_preseed
  227. fi
  228. echo 'd-i network-console/start select Continue' | $save_preseed
  229. fi
  230. DEBI_PROTOCOL=${DEBI_PROTOCOL:-http}
  231. DEBI_MIRROR=${DEBI_MIRROR:-deb.debian.org}
  232. DEBI_DIRECTORY=${DEBI_DIRECTORY:-/debian}
  233. $save_preseed << EOF
  234. # Mirror settings
  235. d-i mirror/country string manual
  236. d-i mirror/protocol string $DEBI_PROTOCOL
  237. d-i mirror/$DEBI_PROTOCOL/hostname string $DEBI_MIRROR
  238. d-i mirror/$DEBI_PROTOCOL/directory string $DEBI_DIRECTORY
  239. d-i mirror/$DEBI_PROTOCOL/proxy string
  240. d-i mirror/suite string $DEBI_SUITE
  241. d-i mirror/udeb/suite string $DEBI_SUITE
  242. EOF
  243. if [ "$DEBI_SKIP_USER" != true ]; then
  244. DEBI_USERNAME=${DEBI_USERNAME:-debian}
  245. if command_exists mkpasswd; then
  246. if [ -z "$DEBI_PASSWORD" ]; then
  247. DEBI_PASSWORD="$(mkpasswd -m sha-512)"
  248. else
  249. DEBI_PASSWORD="$(mkpasswd -m sha-512 "$DEBI_PASSWORD")"
  250. fi
  251. elif command_exists python3; then
  252. if [ -z "$DEBI_PASSWORD" ]; then
  253. DEBI_PASSWORD="$(python3 -c 'import crypt, getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))')"
  254. else
  255. DEBI_PASSWORD="$(python3 -c "import crypt; print(crypt.crypt(\"$DEBI_PASSWORD\", crypt.mksalt(crypt.METHOD_SHA512)))")"
  256. fi
  257. else
  258. DEBI_CLEARTEXT=true
  259. if [ -z "$DEBI_PASSWORD" ]; then
  260. printf 'Password: '
  261. read_secret DEBI_PASSWORD
  262. fi
  263. fi
  264. $save_preseed << EOF
  265. # Account setup
  266. EOF
  267. if [ "$DEBI_USERNAME" = root ]; then
  268. echo 'd-i passwd/make-user boolean false' | $save_preseed
  269. if [ "$DEBI_CLEARTEXT" = true ]; then
  270. $save_preseed << EOF
  271. d-i passwd/root-password password $DEBI_PASSWORD
  272. d-i passwd/root-password-again password $DEBI_PASSWORD
  273. EOF
  274. else
  275. echo "d-i passwd/root-password-crypted password $DEBI_PASSWORD" | $save_preseed
  276. fi
  277. else
  278. $save_preseed << EOF
  279. d-i passwd/root-login boolean false
  280. d-i passwd/user-fullname string
  281. d-i passwd/username string $DEBI_USERNAME
  282. EOF
  283. if [ "$DEBI_CLEARTEXT" = true ]; then
  284. $save_preseed << EOF
  285. d-i passwd/user-password password $DEBI_PASSWORD
  286. d-i passwd/user-password-again password $DEBI_PASSWORD
  287. EOF
  288. else
  289. echo "d-i passwd/user-password-crypted password $DEBI_PASSWORD"
  290. fi
  291. fi
  292. fi
  293. DEBI_TIMEZONE=${DEBI_TIMEZONE:-UTC}
  294. DEBI_NTP=${DEBI_NTP:-pool.ntp.org}
  295. $save_preseed << EOF
  296. # Clock and time zone setup
  297. d-i clock-setup/utc boolean true
  298. d-i time/zone string $DEBI_TIMEZONE
  299. d-i clock-setup/ntp boolean true
  300. d-i clock-setup/ntp-server string $DEBI_NTP
  301. EOF
  302. if [ "$DEBI_SKIP_PART" != true ]; then
  303. DEBI_FS=${DEBI_FS:-ext4}
  304. DEBI_PART=${DEBI_PART:-regular}
  305. $save_preseed << EOF
  306. # Partitioning
  307. EOF
  308. if [ -n "$DEBI_DISK" ]; then
  309. echo "d-i partman-auto/disk string $DEBI_DISK" | $save_preseed
  310. fi
  311. $save_preseed << EOF
  312. d-i partman-auto/method string $DEBI_PART
  313. d-i partman-lvm/device_remove_lvm boolean true
  314. d-i partman-md/device_remove_md boolean true
  315. d-i partman-lvm/confirm boolean true
  316. d-i partman-lvm/confirm_nooverwrite boolean true
  317. EOF
  318. if [ "$DEBI_PART" = "regular" ]; then
  319. $save_preseed << EOF
  320. d-i partman/default_filesystem string $DEBI_FS
  321. d-i partman-auto/expert_recipe string naive :: 0 1 -1 \$default_filesystem \$primary{ } \$bootable{ } method{ format } format{ } use_filesystem{ } \$default_filesystem{ } mountpoint{ / } .
  322. d-i partman-auto/choose_recipe select naive
  323. d-i partman-basicfilesystems/no_swap boolean false
  324. EOF
  325. fi
  326. $save_preseed << EOF
  327. d-i partman-partitioning/confirm_write_new_label boolean true
  328. d-i partman/choose_partition select finish
  329. d-i partman/confirm boolean true
  330. d-i partman/confirm_nooverwrite boolean true
  331. d-i partman/mount_style select uuid
  332. EOF
  333. fi
  334. $save_preseed << EOF
  335. # Base system installation
  336. d-i base-installer/install-recommends boolean false
  337. EOF
  338. if [ -z "$DEBI_SECURITY" ]; then
  339. DEBI_SECURITY=http://security.debian.org/debian-security
  340. else
  341. if [ "$DEBI_SECURITY" = true ]; then
  342. DEBI_SECURITY=$DEBI_PROTOCOL://$DEBI_MIRROR${DEBI_DIRECTORY%/*}/debian-security
  343. fi
  344. fi
  345. $save_preseed << EOF
  346. # Apt setup
  347. d-i apt-setup/services-select multiselect updates, backports
  348. d-i apt-setup/local0/repository string $DEBI_SECURITY $DEBI_SUITE/updates main
  349. d-i apt-setup/local0/source boolean true
  350. EOF
  351. DEBI_UPGRADE=${DEBI_UPGRADE:-full-upgrade}
  352. $save_preseed << EOF
  353. # Package selection
  354. tasksel tasksel/first multiselect ssh-server
  355. EOF
  356. if [ -n "$DEBI_INSTALL" ]; then
  357. echo "d-i pkgsel/include string $DEBI_INSTALL" | $save_preseed
  358. fi
  359. $save_preseed << EOF
  360. d-i pkgsel/upgrade select $DEBI_UPGRADE
  361. popularity-contest popularity-contest/participate boolean false
  362. # Boot loader installation
  363. d-i grub-installer/only_debian boolean true
  364. d-i grub-installer/bootdev string default
  365. # Finishing up the installation
  366. d-i finish-install/reboot_in_progress note
  367. EOF
  368. if [ "$DEBI_POWEROFF" = true ]; then
  369. echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  370. fi
  371. save_grubcfg=cat
  372. if [ "$DEBI_DRY_RUN" != true ]; then
  373. if [ -z "$DEBI_ARCH" ]; then
  374. if command_exists dpkg; then
  375. DEBI_ARCH="$(dpkg --print-architecture)"
  376. else
  377. DEBI_ARCH=amd64
  378. fi
  379. fi
  380. DEBI_BASE_URL="$DEBI_PROTOCOL://$DEBI_MIRROR$DEBI_DIRECTORY/dists/$DEBI_SUITE/main/installer-$DEBI_ARCH/current/images/netboot/debian-installer/$DEBI_ARCH"
  381. if command_exists wget; then
  382. wget -P "$DEBI_NEW_DIR" "$DEBI_BASE_URL/linux" "$DEBI_BASE_URL/initrd.gz"
  383. elif command_exists curl; then
  384. curl "$DEBI_BASE_URL/linux" -o "$DEBI_NEW_DIR/linux" "$DEBI_BASE_URL/initrd.gz" -o "$DEBI_NEW_DIR/initrd.gz"
  385. else
  386. echo_stderr 'Error: wget/curl not found.'
  387. exit 1
  388. fi
  389. gunzip initrd.gz
  390. echo preseed.cfg | cpio -H newc -o -A -F initrd
  391. gzip initrd
  392. if command_exists update-grub; then
  393. DEBI_GRUBCFG=/boot/grub/grub.cfg
  394. update-grub
  395. elif command_exists grub2-mkconfig; then
  396. DEBI_GRUBCFG=/boot/grub2/grub.cfg
  397. grub2-mkconfig -o "$DEBI_GRUBCFG"
  398. else
  399. echo_stderr 'Error: Command update-grub/grub2-mkconfig not found.'
  400. exit 1
  401. fi
  402. save_grubcfg="tee -a $DEBI_GRUBCFG"
  403. fi
  404. if [ "$DEBI_BOOT_PARTITION" = true ]; then
  405. DEBI_BOOT_DIR=/
  406. else
  407. DEBI_BOOT_DIR=/boot/
  408. fi
  409. DEBI_NEW_DIR="$DEBI_BOOT_DIR$DEBI_NEW"
  410. DEBI_PARTITION_TYPE=${DEBI_PARTITION_TYPE:-msdos}
  411. $save_grubcfg << EOF
  412. menuentry 'Debian Installer' --id debi {
  413. insmod part_$DEBI_PARTITION_TYPE
  414. insmod ext2
  415. set root='(hd0,${DEBI_PARTITION_TYPE}1)'
  416. linux $DEBI_NEW_DIR/linux
  417. initrd $DEBI_NEW_DIR/initrd.gz
  418. }
  419. EOF