netboot.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #!/bin/sh
  2. # Copyright 2018 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 -ex
  13. while [ $# -gt 0 ]; do
  14. case $1 in
  15. -c)
  16. COUNTRY=$2
  17. shift
  18. ;;
  19. -fqdn)
  20. FQDN=$2
  21. shift
  22. ;;
  23. -proto)
  24. PROTO=$2
  25. shift
  26. ;;
  27. -host)
  28. HOST=$2
  29. shift
  30. ;;
  31. -dir)
  32. DIR=${2%/}
  33. shift
  34. ;;
  35. -suite)
  36. SUITE=$2
  37. shift
  38. ;;
  39. -u)
  40. ADMIN=$2
  41. shift
  42. ;;
  43. -p)
  44. PASSWD=$2
  45. shift
  46. ;;
  47. -tz)
  48. TIME_ZONE=$2
  49. shift
  50. ;;
  51. -ntp)
  52. NTP=$2
  53. shift
  54. ;;
  55. -s)
  56. SECURITY=$2
  57. shift
  58. ;;
  59. -upgrade)
  60. UPGRADE=$2
  61. shift
  62. ;;
  63. -ip)
  64. IP_ADDR=$2
  65. shift
  66. ;;
  67. -cidr)
  68. NETMASK=$2
  69. shift
  70. ;;
  71. -gw)
  72. GATEWAY=$2
  73. shift
  74. ;;
  75. -ns)
  76. DNS=$2
  77. shift
  78. ;;
  79. -add)
  80. INCLUDE=$2
  81. shift
  82. ;;
  83. -ssh)
  84. SSH_PASSWD=$2
  85. shift
  86. ;;
  87. *)
  88. echo "Illegal option $1"
  89. exit 1
  90. esac
  91. shift
  92. done
  93. case "$COUNTRY" in
  94. CN)
  95. PROTO=${PROTO:-https}
  96. HOST=${HOST:-chinanet.mirrors.ustc.edu.cn}
  97. TIME_ZONE=${TIME_ZONE:-Asia/Shanghai}
  98. NTP=${NTP:-ntp1.aliyun.com}
  99. SECURITY=${SECURITY:-true}
  100. DNS=${DNS:-1.2.4.8 119.29.29.29}
  101. esac
  102. COUNTRY=${COUNTRY:-US}
  103. PROTO=${PROTO:-https}
  104. HOST=${HOST:-dpvctowv9b08b.cloudfront.net}
  105. DIR=${DIR:-/debian}
  106. ARCH=$(dpkg --print-architecture)
  107. SUITE=${SUITE:-stretch}
  108. ADMIN=${ADMIN:-ubuntu}
  109. TIME_ZONE=${TIME_ZONE:-UTC}
  110. NTP=${NTP:-time.google.com}
  111. UPGRADE=${UPGRADE:-full-upgrade}
  112. DNS=${DNS:-1.1.1.1 156.154.70.5 8.8.8.8}
  113. if [ -z "$SECURITY" ]; then
  114. SECURITY=https://dpvctowv9b08b.cloudfront.net/debian-security
  115. else
  116. if [ "$SECURITY" = true ]; then
  117. SECURITY=$PROTO://$HOST${DIR%/*}/debian-security
  118. fi
  119. fi
  120. if [ -z "$PASSWD" ]; then
  121. PASSWD=$(mkpasswd -m sha-512)
  122. else
  123. PASSWD=$(mkpasswd -m sha-512 "$PASSWD")
  124. fi
  125. BOOT=/boot/debian-$SUITE
  126. URL=$PROTO://$HOST$DIR/dists/$SUITE/main/installer-$ARCH/current/images/netboot/debian-installer/$ARCH
  127. update-grub
  128. rm -fr "$BOOT"
  129. mkdir -p "$BOOT"
  130. cd "$BOOT"
  131. cat >> preseed.cfg << EOF
  132. # COUNTRY: 1
  133. # IP_ADDR: 2
  134. # NETMASK: 2
  135. # GATEWAY: 2
  136. # DNS: 2
  137. # FQDN: 2
  138. # SSH_PASSWD: 2
  139. # PROTO: 3
  140. # HOST: 3
  141. # DIR: 3
  142. # SUITE: 3, 8
  143. # ADMIN: 4
  144. # PASSWD: 4
  145. # TIME_ZONE: 5
  146. # NTP: 5
  147. # SECURITY: 8
  148. # INCLUDE: 9
  149. # UPGRADE: 9
  150. # 1. Localization: COUNTRY
  151. d-i debian-installer/locale string en_US
  152. d-i debian-installer/language string en
  153. d-i debian-installer/country string {{-COUNTRY-}}
  154. d-i debian-installer/locale string en_US.UTF-8
  155. d-i keyboard-configuration/xkb-keymap select us
  156. # 2. Network configuration: IP_ADDR, NETMASK, GATEWAY, DNS, FQDN, SSH_PASSWD
  157. d-i netcfg/choose_interface select auto
  158. EOF
  159. if [ -n "$IP_ADDR" ]; then
  160. echo "d-i netcfg/disable_autoconfig boolean true" >> preseed.cfg
  161. echo "d-i netcfg/get_ipaddress string $IP_ADDR" >> preseed.cfg
  162. if [ -n "$NETMASK" ]; then
  163. echo "d-i netcfg/get_netmask string $NETMASK" >> preseed.cfg
  164. fi
  165. if [ -n "$GATEWAY" ]; then
  166. echo "d-i netcfg/get_gateway string $GATEWAY" >> preseed.cfg
  167. fi
  168. if [ -n "$DNS" ]; then
  169. echo "d-i netcfg/get_nameservers string $DNS" >> preseed.cfg
  170. fi
  171. echo "d-i netcfg/confirm_static boolean true" >> preseed.cfg
  172. fi
  173. cat >> preseed.cfg << EOF
  174. d-i netcfg/get_hostname string localhost
  175. d-i netcfg/get_domain string localdomain
  176. EOF
  177. if [ -n "$FQDN" ]; then
  178. echo "d-i netcfg/hostname string $FQDN" >> preseed.cfg
  179. fi
  180. cat >> preseed.cfg << EOF
  181. d-i hw-detect/load_firmware boolean true
  182. EOF
  183. if [ -n "$SSH_PASSWD" ]; then
  184. echo "d-i anna/choose_modules string network-console" >> preseed.cfg
  185. echo "d-i preseed/early_command string anna-install network-console" >> preseed.cfg
  186. echo "d-i network-console/password password $SSH_PASSWD" >> preseed.cfg
  187. echo "d-i network-console/password-again password $SSH_PASSWD" >> preseed.cfg
  188. echo "d-i network-console/start select Continue" >> preseed.cfg
  189. fi
  190. cat >> preseed.cfg << EOF
  191. # 3. Mirror settings: PROTO, HOST, DIR, SUITE
  192. d-i mirror/country string manual
  193. d-i mirror/protocol string {{-PROTO-}}
  194. d-i mirror/{{-PROTO-}}/hostname string {{-HOST-}}
  195. d-i mirror/{{-PROTO-}}/directory string {{-DIR-}}
  196. d-i mirror/{{-PROTO-}}/proxy string
  197. d-i mirror/suite string {{-SUITE-}}
  198. d-i mirror/udeb/suite string {{-SUITE-}}
  199. # 4. Account setup: ADMIN, PASSWD
  200. d-i passwd/root-login boolean false
  201. d-i passwd/user-fullname string
  202. d-i passwd/username string {{-ADMIN-}}
  203. d-i passwd/user-password-crypted password {{-PASSWD-}}
  204. # 5. Clock and time zone setup: TIME_ZONE, NTP
  205. d-i clock-setup/utc boolean true
  206. d-i time/zone string {{-TIME_ZONE-}}
  207. d-i clock-setup/ntp boolean true
  208. d-i clock-setup/ntp-server string {{-NTP-}}
  209. # 6. Partitioning
  210. d-i partman-basicfilesystems/no_swap boolean false
  211. d-i partman-auto/method string regular
  212. d-i partman-lvm/device_remove_lvm boolean true
  213. d-i partman-md/device_remove_md boolean true
  214. d-i partman-lvm/confirm boolean true
  215. d-i partman-lvm/confirm_nooverwrite boolean true
  216. d-i partman-auto/expert_recipe string naive :: 0 1 -1 ext4 $primary{ } $bootable{ } method{ format } format{ } use_filesystem{ } filesystem{ ext4 } mountpoint{ / } .
  217. d-i partman-auto/choose_recipe select naive
  218. d-i partman-partitioning/confirm_write_new_label boolean true
  219. d-i partman/choose_partition select finish
  220. d-i partman/confirm boolean true
  221. d-i partman/confirm_nooverwrite boolean true
  222. d-i partman/mount_style select uuid
  223. # 7. Base system installation
  224. d-i base-installer/install-recommends boolean false
  225. # 8. Apt setup: SECURITY, SUITE
  226. d-i apt-setup/services-select multiselect updates
  227. d-i apt-setup/local0/repository string {{-SECURITY-}} {{-SUITE-}}/updates main
  228. d-i apt-setup/local0/source boolean true
  229. # 9. Package selection: INCLUDE, UPGRADE
  230. tasksel tasksel/first multiselect ssh-server
  231. EOF
  232. if [ -n "$INCLUDE" ]; then
  233. echo "d-i pkgsel/include string $INCLUDE" >> preseed.cfg
  234. fi
  235. cat >> preseed.cfg << EOF
  236. d-i pkgsel/upgrade select {{-UPGRADE-}}
  237. popularity-contest popularity-contest/participate boolean false
  238. # 10. Boot loader installation
  239. d-i grub-installer/only_debian boolean true
  240. d-i grub-installer/bootdev string default
  241. # 11. Finishing up the installation
  242. d-i finish-install/reboot_in_progress note
  243. EOF
  244. sed -i 's/{{-COUNTRY-}}/'"$COUNTRY"'/g' preseed.cfg
  245. sed -i 's/{{-PROTO-}}/'"$PROTO"'/g' preseed.cfg
  246. sed -i 's/{{-HOST-}}/'"$HOST"'/g' preseed.cfg
  247. sed -i 's/{{-DIR-}}/'$(echo "$DIR" | sed 's/\//\\\//g')'/g' preseed.cfg
  248. sed -i 's/{{-SUITE-}}/'"$SUITE"'/g' preseed.cfg
  249. sed -i 's/{{-ADMIN-}}/'"$ADMIN"'/g' preseed.cfg
  250. sed -i 's/{{-PASSWD-}}/'$(echo "$PASSWD" | sed 's/\//\\\//g')'/g' preseed.cfg
  251. sed -i 's/{{-TIME_ZONE-}}/'$(echo "$TIME_ZONE" | sed 's/\//\\\//g')'/g' preseed.cfg
  252. sed -i 's/{{-NTP-}}/'"$NTP"'/g' preseed.cfg
  253. sed -i 's/{{-SECURITY-}}/'$(echo "$SECURITY" | sed 's/\//\\\//g')'/g' preseed.cfg
  254. sed -i 's/{{-UPGRADE-}}/'"$UPGRADE"'/g' preseed.cfg
  255. wget "$URL/linux" "$URL/initrd.gz"
  256. gunzip initrd.gz
  257. echo preseed.cfg | cpio -H newc -o -A -F initrd
  258. gzip initrd
  259. cat >> ../grub/grub.cfg << EOF
  260. menuentry 'New Install' {
  261. insmod part_msdos
  262. insmod ext2
  263. set root='(hd0,msdos1)'
  264. linux $BOOT/linux
  265. initrd $BOOT/initrd.gz
  266. }
  267. EOF