netboot.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #!/usr/bin/env sh
  2. # Copyright 2018-present Brent, Yang Bohan
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. set -e
  13. echo_stderr() {
  14. echo "$@" 1>&2
  15. }
  16. command_exists() {
  17. _PATH="$PATH"
  18. PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin"
  19. set +e
  20. command -v "$@" >/dev/null 2>&1
  21. command_exit_status=$?
  22. set -e
  23. PATH="$_PATH"
  24. unset _PATH
  25. return $command_exit_status
  26. }
  27. while [ $# -gt 0 ]; do
  28. case $1 in
  29. --template)
  30. DEBI_TEMPLATE=$2
  31. shift
  32. ;;
  33. --hostname)
  34. DEBI_HOSTNAME=$2
  35. shift
  36. ;;
  37. --protocol)
  38. DEBI_PROTOCOL=$2
  39. shift
  40. ;;
  41. --mirror)
  42. DEBI_MIRROR=$2
  43. shift
  44. ;;
  45. --directory)
  46. DEBI_DIRECTORY=${2%/}
  47. shift
  48. ;;
  49. --suite)
  50. DEBI_SUITE=$2
  51. shift
  52. ;;
  53. --username)
  54. DEBI_USERNAME=$2
  55. shift
  56. ;;
  57. --password)
  58. DEBI_PASSWORD=$2
  59. shift
  60. ;;
  61. --timezone)
  62. DEBI_TIMEZONE=$2
  63. shift
  64. ;;
  65. --ntp-server)
  66. DEBI_NTP_SERVER=$2
  67. shift
  68. ;;
  69. --security-mirror)
  70. DEBI_SECURITY_MIRROR=$2
  71. shift
  72. ;;
  73. --upgrade)
  74. DEBI_UPGRADE=$2
  75. shift
  76. ;;
  77. --ip)
  78. DEBI_IP=$2
  79. shift
  80. ;;
  81. --netmask)
  82. DEBI_NETMASK=$2
  83. shift
  84. ;;
  85. --gateway)
  86. DEBI_GATEWAY=$2
  87. shift
  88. ;;
  89. --dns)
  90. DEBI_DNS=$2
  91. shift
  92. ;;
  93. --include)
  94. DEBI_INCLUDE=$2
  95. shift
  96. ;;
  97. --ssh-password)
  98. DEBI_SSH=true
  99. DEBI_SSH_PASSWORD=$2
  100. shift
  101. ;;
  102. --ssh-keys)
  103. DEBI_SSH=true
  104. DEBI_SSH_KEYS=$2
  105. shift
  106. ;;
  107. --filesystem)
  108. DEBI_FILESYSTEM=$2
  109. shift
  110. ;;
  111. --dry-run)
  112. DEBI_DRY_RUN=true
  113. ;;
  114. --disk-encryption)
  115. DEBI_DISK_ENCRYPTION="crypto"
  116. ;;
  117. --manual)
  118. DEBI_MANUAL=true
  119. ;;
  120. --architecture)
  121. DEBI_ARCHITECTURE=$2
  122. shift
  123. ;;
  124. --boot-partition)
  125. DEBI_BOOT_PARTITION=true
  126. ;;
  127. --poweroff)
  128. DEBI_POWEROFF=true
  129. ;;
  130. *)
  131. echo_stderr "Illegal option $1"
  132. exit 1
  133. esac
  134. shift
  135. done
  136. case "$DEBI_TEMPLATE" in
  137. china)
  138. DEBI_PROTOCOL=${DEBI_PROTOCOL:-https}
  139. DEBI_MIRROR=${DEBI_MIRROR:-chinanet.mirrors.ustc.edu.cn}
  140. DEBI_TIMEZONE=${DEBI_TIMEZONE:-Asia/Shanghai}
  141. DEBI_NTP_SERVER=${DEBI_NTP_SERVER:-cn.ntp.org.cn}
  142. DEBI_SECURITY_MIRROR=${DEBI_SECURITY_MIRROR:-true}
  143. DEBI_DNS=${DEBI_DNS:-156.154.70.5 156.154.71.5}
  144. ;;
  145. cloud)
  146. DEBI_PROTOCOL=${DEBI_PROTOCOL:-https}
  147. DEBI_MIRROR=${DEBI_MIRROR:-cdn-aws.deb.debian.org}
  148. DEBI_NTP_SERVER=${DEBI_NTP_SERVER:-time.google.com}
  149. DEBI_SECURITY_MIRROR=${DEBI_SECURITY_MIRROR:-true}
  150. esac
  151. DEBI_PROTOCOL=${DEBI_PROTOCOL:-http}
  152. DEBI_MIRROR=${DEBI_MIRROR:-deb.debian.org}
  153. DEBI_DIRECTORY=${DEBI_DIRECTORY:-/debian}
  154. if [ -z "$DEBI_ARCHITECTURE" ]; then
  155. if command_exists dpkg; then
  156. DEBI_ARCHITECTURE=$(dpkg --print-architecture)
  157. else
  158. DEBI_ARCHITECTURE=amd64
  159. fi
  160. fi
  161. DEBI_SUITE=${DEBI_SUITE:-stretch}
  162. DEBI_USERNAME=${DEBI_USERNAME:-debian}
  163. DEBI_TIMEZONE=${DEBI_TIMEZONE:-UTC}
  164. DEBI_NTP_SERVER=${DEBI_NTP_SERVER:-pool.ntp.org}
  165. DEBI_UPGRADE=${DEBI_UPGRADE:-full-upgrade}
  166. DEBI_DNS=${DEBI_DNS:-1.1.1.1 1.0.0.1}
  167. DEBI_FILESYSTEM=${DEBI_FILESYSTEM:-ext4}
  168. DEBI_DISK_ENCRYPTION=${DEBI_DISK_ENCRYPTION:-regular}
  169. if [ -z "$DEBI_SECURITY_MIRROR" ]; then
  170. DEBI_SECURITY_MIRROR=http://security.debian.org/debian-security
  171. else
  172. if [ "$DEBI_SECURITY_MIRROR" = true ]; then
  173. DEBI_SECURITY_MIRROR=$DEBI_PROTOCOL://$DEBI_MIRROR${DEBI_DIRECTORY%/*}/debian-security
  174. fi
  175. fi
  176. if [ "$DEBI_MANUAL" != true ]; then
  177. if [ -z "$DEBI_PASSWORD" ]; then
  178. DEBI_PASSWORD=$(mkpasswd -m sha-512)
  179. else
  180. DEBI_PASSWORD=$(mkpasswd -m sha-512 "$DEBI_PASSWORD")
  181. fi
  182. fi
  183. DEBI_TARGET="debian-$DEBI_SUITE"
  184. if [ "$DEBI_BOOT_PARTITION" = true ]; then
  185. DEBI_BOOT_DIRECTORY=/
  186. else
  187. DEBI_BOOT_DIRECTORY=/boot/
  188. fi
  189. DEBI_TARGET_PATH="$DEBI_BOOT_DIRECTORY$DEBI_TARGET"
  190. echo='cat'
  191. if [ "$DEBI_DRY_RUN" != true ]; then
  192. user="$(id -un 2>/dev/null || true)"
  193. sudo=''
  194. if [ "$user" != 'root' ]; then
  195. if command_exists sudo; then
  196. sudo='sudo'
  197. else
  198. exit 1
  199. fi
  200. fi
  201. DEBI_WORKDIR="/boot/$DEBI_TARGET"
  202. DEBI_BASE_URL=$DEBI_PROTOCOL://$DEBI_MIRROR$DEBI_DIRECTORY/dists/$DEBI_SUITE/main/installer-$DEBI_ARCHITECTURE/current/images/netboot/debian-installer/$DEBI_ARCHITECTURE
  203. if command_exists update-grub; then
  204. $sudo update-grub
  205. DEBI_GRUB_CONFIG=/boot/grub/grub.cfg
  206. else
  207. DEBI_GRUB_CONFIG=/boot/grub2/grub.cfg
  208. $sudo grub2-mkconfig -o "$DEBI_GRUB_CONFIG"
  209. fi
  210. $sudo rm -rf "$DEBI_WORKDIR"
  211. $sudo mkdir -p "$DEBI_WORKDIR"
  212. cd "$DEBI_WORKDIR"
  213. $sudo rm -f preseed.cfg
  214. echo="$sudo tee -a preseed.cfg"
  215. fi
  216. $echo << EOF
  217. # Localization
  218. d-i debian-installer/locale string en_US.UTF-8
  219. d-i keyboard-configuration/xkb-keymap select us
  220. # Network configuration
  221. d-i netcfg/choose_interface select auto
  222. EOF
  223. if [ -n "$DEBI_IP" ]; then
  224. echo "d-i netcfg/disable_autoconfig boolean true" | $echo
  225. echo "d-i netcfg/get_ipaddress string $DEBI_IP" | $echo
  226. if [ -n "$DEBI_NETMASK" ]; then
  227. echo "d-i netcfg/get_netmask string $DEBI_NETMASK" | $echo
  228. fi
  229. if [ -n "$DEBI_GATEWAY" ]; then
  230. echo "d-i netcfg/get_gateway string $DEBI_GATEWAY" | $echo
  231. fi
  232. if [ -n "$DEBI_DNS" ]; then
  233. echo "d-i netcfg/get_nameservers string $DEBI_DNS" | $echo
  234. fi
  235. echo "d-i netcfg/confirm_static boolean true" | $echo
  236. fi
  237. $echo << EOF
  238. d-i netcfg/get_hostname string debian
  239. d-i netcfg/get_domain string
  240. EOF
  241. if [ -n "$DEBI_HOSTNAME" ]; then
  242. echo "d-i netcfg/hostname string $DEBI_HOSTNAME" | $echo
  243. fi
  244. echo "d-i hw-detect/load_firmware boolean true" | $echo
  245. if [ "$DEBI_SSH" = true ]; then
  246. $echo << EOF
  247. # Network console
  248. d-i anna/choose_modules string network-console
  249. d-i preseed/early_command string anna-install network-console
  250. EOF
  251. if [ -n "$DEBI_SSH_PASSWORD" ]; then
  252. echo "d-i network-console/password password $DEBI_SSH_PASSWORD" | $echo
  253. echo "d-i network-console/password-again password $DEBI_SSH_PASSWORD" | $echo
  254. fi
  255. if [ -n "$DEBI_SSH_KEYS" ]; then
  256. echo "d-i network-console/authorized_keys_url string $DEBI_SSH_KEYS" | $echo
  257. fi
  258. echo "d-i network-console/start select Continue" | $echo
  259. fi
  260. $echo << EOF
  261. # Mirror settings
  262. d-i mirror/country string manual
  263. d-i mirror/protocol string $DEBI_PROTOCOL
  264. d-i mirror/$DEBI_PROTOCOL/hostname string $DEBI_MIRROR
  265. d-i mirror/$DEBI_PROTOCOL/directory string $DEBI_DIRECTORY
  266. d-i mirror/$DEBI_PROTOCOL/proxy string
  267. d-i mirror/suite string $DEBI_SUITE
  268. d-i mirror/udeb/suite string $DEBI_SUITE
  269. # Clock and time zone setup
  270. d-i clock-setup/utc boolean true
  271. d-i time/zone string $DEBI_TIMEZONE
  272. d-i clock-setup/ntp boolean true
  273. d-i clock-setup/ntp-server string $DEBI_NTP_SERVER
  274. EOF
  275. if [ "$DEBI_MANUAL" != true ]; then
  276. $echo << EOF
  277. # Account setup
  278. d-i passwd/root-login boolean false
  279. d-i passwd/user-fullname string
  280. d-i passwd/username string $DEBI_USERNAME
  281. d-i passwd/user-password-crypted password $DEBI_PASSWORD
  282. # Partitioning
  283. d-i partman/default_filesystem string $DEBI_FILESYSTEM
  284. d-i partman-auto/method string $DEBI_DISK_ENCRYPTION
  285. d-i partman-lvm/device_remove_lvm boolean true
  286. d-i partman-md/device_remove_md boolean true
  287. d-i partman-lvm/confirm boolean true
  288. d-i partman-lvm/confirm_nooverwrite boolean true
  289. EOF
  290. if [ "$DEBI_DISK_ENCRYPTION" = "regular" ]; then
  291. $echo << EOF
  292. d-i partman-auto/expert_recipe string naive :: 0 1 -1 \$default_filesystem \$primary{ } \$bootable{ } method{ format } format{ } use_filesystem{ } \$default_filesystem{ } mountpoint{ / } .
  293. d-i partman-auto/choose_recipe select naive
  294. d-i partman-basicfilesystems/no_swap boolean false
  295. EOF
  296. fi
  297. $echo << EOF
  298. d-i partman-partitioning/confirm_write_new_label boolean true
  299. d-i partman/choose_partition select finish
  300. d-i partman/confirm boolean true
  301. d-i partman/confirm_nooverwrite boolean true
  302. d-i partman/mount_style select uuid
  303. # Base system installation
  304. d-i base-installer/install-recommends boolean false
  305. # Apt setup
  306. d-i apt-setup/services-select multiselect updates, backports
  307. d-i apt-setup/local0/repository string $DEBI_SECURITY_MIRROR $DEBI_SUITE/updates main
  308. d-i apt-setup/local0/source boolean true
  309. # Package selection
  310. tasksel tasksel/first multiselect ssh-server
  311. EOF
  312. if [ -n "$DEBI_INCLUDE" ]; then
  313. echo "d-i pkgsel/include string $DEBI_INCLUDE" | $echo
  314. fi
  315. $echo << EOF
  316. d-i pkgsel/upgrade select $DEBI_UPGRADE
  317. popularity-contest popularity-contest/participate boolean false
  318. # Boot loader installation
  319. d-i grub-installer/only_debian boolean true
  320. d-i grub-installer/bootdev string default
  321. # Finishing up the installation
  322. d-i finish-install/reboot_in_progress note
  323. EOF
  324. if [ "$DEBI_POWEROFF" = true ]; then
  325. echo 'd-i debian-installer/exit/poweroff boolean true' | $echo
  326. fi
  327. fi
  328. echo2='cat'
  329. if [ "$DEBI_DRY_RUN" != true ]; then
  330. if command_exists wget; then
  331. $sudo wget "$DEBI_BASE_URL/linux" "$DEBI_BASE_URL/initrd.gz"
  332. elif command_exists curl; then
  333. $sudo curl -O "$DEBI_BASE_URL/linux" -O "$DEBI_BASE_URL/initrd.gz"
  334. else
  335. echo_stderr 'wget/curl not found'
  336. exit 1
  337. fi
  338. $sudo gunzip initrd.gz
  339. echo preseed.cfg | $sudo cpio -H newc -o -A -F initrd
  340. $sudo gzip initrd
  341. echo2="$sudo tee -a $DEBI_GRUB_CONFIG"
  342. fi
  343. $echo2 << EOF
  344. menuentry 'Debian Installer' --id debi {
  345. insmod part_msdos
  346. insmod ext2
  347. set root='(hd0,msdos1)'
  348. linux $DEBI_TARGET_PATH/linux
  349. initrd $DEBI_TARGET_PATH/initrd.gz
  350. }
  351. EOF