netboot.sh 13 KB

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