netboot.sh 9.4 KB

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