netboot.sh 12 KB

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