netboot.sh 9.2 KB

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