netboot.sh 9.3 KB

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