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. code=1
  20. if command -v "$@" >/dev/null 2>&1; then
  21. code=0
  22. fi
  23. PATH="$_PATH"
  24. unset _PATH
  25. return $code
  26. }
  27. user="$(id -un 2>/dev/null || true)"
  28. sudo=''
  29. if [ "$user" != 'root' ]; then
  30. if command_exists sudo; then
  31. sudo='sudo'
  32. else
  33. exit 1
  34. fi
  35. fi
  36. while [ $# -gt 0 ]; do
  37. case $1 in
  38. --template)
  39. DEBI_TEMPLATE=$2
  40. shift
  41. ;;
  42. --hostname)
  43. DEBI_HOSTNAME=$2
  44. shift
  45. ;;
  46. --protocol)
  47. DEBI_PROTOCOL=$2
  48. shift
  49. ;;
  50. --mirror)
  51. DEBI_MIRROR=$2
  52. shift
  53. ;;
  54. --directory)
  55. DEBI_DIRECTORY=${2%/}
  56. shift
  57. ;;
  58. --suite)
  59. DEBI_SUITE=$2
  60. shift
  61. ;;
  62. --username)
  63. DEBI_USERNAME=$2
  64. shift
  65. ;;
  66. --password)
  67. DEBI_PASSWORD=$2
  68. shift
  69. ;;
  70. --timezone)
  71. DEBI_TIMEZONE=$2
  72. shift
  73. ;;
  74. --ntp-server)
  75. DEBI_NTP_SERVER=$2
  76. shift
  77. ;;
  78. --security-mirror)
  79. DEBI_SECURITY_MIRROR=$2
  80. shift
  81. ;;
  82. --upgrade)
  83. DEBI_UPGRADE=$2
  84. shift
  85. ;;
  86. --ip)
  87. DEBI_IP=$2
  88. shift
  89. ;;
  90. --netmask)
  91. DEBI_NETMASK=$2
  92. shift
  93. ;;
  94. --gateway)
  95. DEBI_GATEWAY=$2
  96. shift
  97. ;;
  98. --dns)
  99. DEBI_DNS=$2
  100. shift
  101. ;;
  102. --include)
  103. DEBI_INCLUDE=$2
  104. shift
  105. ;;
  106. --ssh-password)
  107. DEBI_SSH=true
  108. DEBI_SSH_PASSWORD=$2
  109. shift
  110. ;;
  111. --ssh-keys)
  112. DEBI_SSH=true
  113. DEBI_SSH_KEYS=$2
  114. shift
  115. ;;
  116. --filesystem)
  117. DEBI_FILESYSTEM=$2
  118. shift
  119. ;;
  120. --dry-run)
  121. DEBI_DRY_RUN=true
  122. ;;
  123. --disk-encryption)
  124. DEBI_DISK_ENCRYPTION="crypto"
  125. ;;
  126. --manual)
  127. DEBI_MANUAL=true
  128. ;;
  129. --architecture)
  130. DEBI_ARCHITECTURE=$2
  131. shift
  132. ;;
  133. --boot-partition)
  134. DEBI_BOOT_PARTITION=true
  135. ;;
  136. --poweroff)
  137. DEBI_POWEROFF=true
  138. ;;
  139. *)
  140. echo_stderr "Illegal option $1"
  141. exit 1
  142. esac
  143. shift
  144. done
  145. case "$DEBI_TEMPLATE" in
  146. china)
  147. DEBI_PROTOCOL=${DEBI_PROTOCOL:-https}
  148. DEBI_MIRROR=${DEBI_MIRROR:-chinanet.mirrors.ustc.edu.cn}
  149. DEBI_TIMEZONE=${DEBI_TIMEZONE:-Asia/Shanghai}
  150. DEBI_NTP_SERVER=${DEBI_NTP_SERVER:-cn.ntp.org.cn}
  151. DEBI_SECURITY_MIRROR=${DEBI_SECURITY_MIRROR:-true}
  152. DEBI_DNS=${DEBI_DNS:-156.154.70.5 156.154.71.5}
  153. ;;
  154. cloud)
  155. DEBI_PROTOCOL=${DEBI_PROTOCOL:-https}
  156. DEBI_MIRROR=${DEBI_MIRROR:-cdn-aws.deb.debian.org}
  157. DEBI_NTP_SERVER=${DEBI_NTP_SERVER:-time.google.com}
  158. DEBI_SECURITY_MIRROR=${DEBI_SECURITY_MIRROR:-true}
  159. esac
  160. DEBI_PROTOCOL=${DEBI_PROTOCOL:-http}
  161. DEBI_MIRROR=${DEBI_MIRROR:-deb.debian.org}
  162. DEBI_DIRECTORY=${DEBI_DIRECTORY:-/debian}
  163. if [ -z "$DEBI_ARCHITECTURE" ]; then
  164. if command_exists dpkg; then
  165. DEBI_ARCHITECTURE=$(dpkg --print-architecture)
  166. else
  167. DEBI_ARCHITECTURE=amd64
  168. fi
  169. fi
  170. DEBI_SUITE=${DEBI_SUITE:-stretch}
  171. DEBI_USERNAME=${DEBI_USERNAME:-debian}
  172. DEBI_TIMEZONE=${DEBI_TIMEZONE:-UTC}
  173. DEBI_NTP_SERVER=${DEBI_NTP_SERVER:-pool.ntp.org}
  174. DEBI_UPGRADE=${DEBI_UPGRADE:-full-upgrade}
  175. DEBI_DNS=${DEBI_DNS:-1.1.1.1 1.0.0.1}
  176. DEBI_FILESYSTEM=${DEBI_FILESYSTEM:-ext4}
  177. DEBI_DISK_ENCRYPTION=${DEBI_DISK_ENCRYPTION:-regular}
  178. if [ -z "$DEBI_SECURITY_MIRROR" ]; then
  179. DEBI_SECURITY_MIRROR=http://security.debian.org/debian-security
  180. else
  181. if [ "$DEBI_SECURITY_MIRROR" = true ]; then
  182. DEBI_SECURITY_MIRROR=$DEBI_PROTOCOL://$DEBI_MIRROR${DEBI_DIRECTORY%/*}/debian-security
  183. fi
  184. fi
  185. if [ "$DEBI_MANUAL" != true ]; then
  186. if [ -z "$DEBI_PASSWORD" ]; then
  187. DEBI_PASSWORD=$(mkpasswd -m sha-512)
  188. else
  189. DEBI_PASSWORD=$(mkpasswd -m sha-512 "$DEBI_PASSWORD")
  190. fi
  191. fi
  192. DEBI_TARGET="debian-$DEBI_SUITE"
  193. if [ "$DEBI_BOOT_PARTITION" = true ]; then
  194. DEBI_BOOT_DIRECTORY=/
  195. else
  196. DEBI_BOOT_DIRECTORY=/boot/
  197. fi
  198. DEBI_TARGET_PATH="$DEBI_BOOT_DIRECTORY$DEBI_TARGET"
  199. echo='cat'
  200. if [ "$DEBI_DRY_RUN" != true ]; then
  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