netboot.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. USERNAME=$2
  41. shift
  42. ;;
  43. -p)
  44. PASSWD=$2
  45. shift
  46. ;;
  47. -tz)
  48. TIMEZONE=$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. *)
  84. echo "Illegal option $1"
  85. exit 1
  86. esac
  87. shift
  88. done
  89. case "$COUNTRY" in
  90. CN)
  91. PROTO=${PROTO:-https}
  92. HOST=${HOST:-chinanet.mirrors.ustc.edu.cn}
  93. TIMEZONE=${TIMEZONE:-Asia/Shanghai}
  94. NTP=${NTP:-ntp1.aliyun.com}
  95. SECURITY=${SECURITY:-true}
  96. esac
  97. COUNTRY=${COUNTRY:-US}
  98. FQDN=${FQDN:-localhost.localdomain}
  99. PROTO=${PROTO:-https}
  100. HOST=${HOST:-dpvctowv9b08b.cloudfront.net}
  101. DIR=${DIR:-/debian}
  102. ARCH=$(dpkg --print-architecture)
  103. SUITE=${SUITE:-stretch}
  104. USERNAME=${USERNAME:-ubuntu}
  105. TIMEZONE=${TIMEZONE:-UTC}
  106. NTP=${NTP:-time.google.com}
  107. UPGRADE=${UPGRADE:-full-upgrade}
  108. DNS=${DNS:-1.1.1.1 156.154.70.5 8.8.8.8}
  109. if [ -z "$SECURITY" ]; then
  110. SECURITY=https://dpvctowv9b08b.cloudfront.net/debian-security
  111. else
  112. if [ "$SECURITY" = true ]; then
  113. SECURITY=$PROTO://$HOST${DIR%/*}/debian-security
  114. fi
  115. fi
  116. if [ -z "$PASSWD" ]; then
  117. PASSWD=$(mkpasswd -m sha-512)
  118. else
  119. PASSWD=$(mkpasswd -m sha-512 "$PASSWD")
  120. fi
  121. BOOT=/boot/debian-$SUITE
  122. URL=$PROTO://$HOST$DIR/dists/$SUITE/main/installer-$ARCH/current/images/netboot/debian-installer/$ARCH
  123. update-grub
  124. rm -fr "$BOOT"
  125. mkdir -p "$BOOT"
  126. cd "$BOOT"
  127. cat >> preseed.cfg << EOF
  128. # COUNTRY: 1
  129. # FQDN: 2
  130. # PROTO: 3
  131. # HOST: 3
  132. # DIR: 3
  133. # SUITE: 3, 8
  134. # USERNAME: 4
  135. # PASSWD: 4
  136. # TIMEZONE: 5
  137. # NTP: 5
  138. # SECURITY: 8
  139. # UPGRADE: 9
  140. # 1. Localization: COUNTRY
  141. d-i debian-installer/locale string en_US
  142. d-i debian-installer/language string en
  143. d-i debian-installer/country string {{-COUNTRY-}}
  144. d-i debian-installer/locale string en_US.UTF-8
  145. d-i keyboard-configuration/xkb-keymap select us
  146. # 2. Network configuration: FQDN
  147. d-i netcfg/choose_interface select auto
  148. EOF
  149. if [ -n "$IP_ADDR" ]; then
  150. echo "d-i netcfg/disable_autoconfig boolean true" >> preseed.cfg
  151. echo "d-i netcfg/get_ipaddress string $IP_ADDR" >> preseed.cfg
  152. if [ -n "$NETMASK" ]; then
  153. echo "d-i netcfg/get_netmask string $NETMASK" >> preseed.cfg
  154. fi
  155. if [ -n "$GATEWAY" ]; then
  156. echo "d-i netcfg/get_gateway string $GATEWAY" >> preseed.cfg
  157. fi
  158. if [ -n "$DNS" ]; then
  159. echo "d-i netcfg/get_nameservers string $DNS" >> preseed.cfg
  160. fi
  161. echo "d-i netcfg/confirm_static boolean true" >> preseed.cfg
  162. fi
  163. cat >> preseed.cfg << EOF
  164. d-i netcfg/get_hostname string unassigned-hostname
  165. d-i netcfg/get_domain string unassigned-domain
  166. d-i netcfg/hostname string {{-FQDN-}}
  167. d-i hw-detect/load_firmware boolean true
  168. # 3. Mirror settings: PROTO, HOST, DIR, SUITE
  169. d-i mirror/country string manual
  170. d-i mirror/protocol string {{-PROTO-}}
  171. d-i mirror/{{-PROTO-}}/hostname string {{-HOST-}}
  172. d-i mirror/{{-PROTO-}}/directory string {{-DIR-}}
  173. d-i mirror/{{-PROTO-}}/proxy string
  174. d-i mirror/suite string {{-SUITE-}}
  175. d-i mirror/udeb/suite string {{-SUITE-}}
  176. # 4. Account setup: USERNAME, PASSWD
  177. d-i passwd/root-login boolean false
  178. d-i passwd/user-fullname string
  179. d-i passwd/username string {{-USERNAME-}}
  180. d-i passwd/user-password-crypted password {{-PASSWD-}}
  181. # 5. Clock and time zone setup: TIMEZONE, NTP
  182. d-i clock-setup/utc boolean true
  183. d-i time/zone string {{-TIMEZONE-}}
  184. d-i clock-setup/ntp boolean true
  185. d-i clock-setup/ntp-server string {{-NTP-}}
  186. # 6. Partitioning
  187. d-i partman-basicfilesystems/no_swap boolean false
  188. d-i partman-auto/method string regular
  189. d-i partman-lvm/device_remove_lvm boolean true
  190. d-i partman-md/device_remove_md boolean true
  191. d-i partman-lvm/confirm boolean true
  192. d-i partman-lvm/confirm_nooverwrite boolean true
  193. d-i partman-auto/expert_recipe string naive :: 0 1 -1 ext4 $primary{ } $bootable{ } method{ format } format{ } use_filesystem{ } filesystem{ ext4 } mountpoint{ / } .
  194. d-i partman-auto/choose_recipe select naive
  195. d-i partman-partitioning/confirm_write_new_label boolean true
  196. d-i partman/choose_partition select finish
  197. d-i partman/confirm boolean true
  198. d-i partman/confirm_nooverwrite boolean true
  199. d-i partman/mount_style select uuid
  200. # 7. Base system installation
  201. d-i base-installer/install-recommends boolean false
  202. # 8. Apt setup: SECURITY, SUITE
  203. d-i apt-setup/services-select multiselect updates
  204. d-i apt-setup/local0/repository string {{-SECURITY-}} {{-SUITE-}}/updates main
  205. d-i apt-setup/local0/source boolean true
  206. # 9. Package selection: TASKS, UPGRADE
  207. tasksel tasksel/first multiselect ssh-server
  208. EOF
  209. if [ -n "$INCLUDE" ]; then
  210. echo "d-i pkgsel/include string $INCLUDE" >> preseed.cfg
  211. fi
  212. cat >> preseed.cfg << EOF
  213. d-i pkgsel/upgrade select {{-UPGRADE-}}
  214. popularity-contest popularity-contest/participate boolean false
  215. # 10. Boot loader installation
  216. d-i grub-installer/only_debian boolean true
  217. d-i grub-installer/bootdev string default
  218. EOF
  219. sed -i 's/{{-COUNTRY-}}/'"$COUNTRY"'/g' preseed.cfg
  220. sed -i 's/{{-FQDN-}}/'"$FQDN"'/g' preseed.cfg
  221. sed -i 's/{{-PROTO-}}/'"$PROTO"'/g' preseed.cfg
  222. sed -i 's/{{-HOST-}}/'"$HOST"'/g' preseed.cfg
  223. sed -i 's/{{-DIR-}}/'$(echo "$DIR" | sed 's/\//\\\//g')'/g' preseed.cfg
  224. sed -i 's/{{-SUITE-}}/'"$SUITE"'/g' preseed.cfg
  225. sed -i 's/{{-USERNAME-}}/'"$USERNAME"'/g' preseed.cfg
  226. sed -i 's/{{-PASSWD-}}/'$(echo "$PASSWD" | sed 's/\//\\\//g')'/g' preseed.cfg
  227. sed -i 's/{{-TIMEZONE-}}/'$(echo "$TIMEZONE" | sed 's/\//\\\//g')'/g' preseed.cfg
  228. sed -i 's/{{-NTP-}}/'"$NTP"'/g' preseed.cfg
  229. sed -i 's/{{-SECURITY-}}/'$(echo "$SECURITY" | sed 's/\//\\\//g')'/g' preseed.cfg
  230. sed -i 's/{{-UPGRADE-}}/'"$UPGRADE"'/g' preseed.cfg
  231. wget "$URL/linux" "$URL/initrd.gz"
  232. gunzip initrd.gz
  233. echo preseed.cfg | cpio -H newc -o -A -F initrd
  234. gzip initrd
  235. cat >> ../grub/grub.cfg << EOF
  236. menuentry 'New Install' {
  237. insmod part_msdos
  238. insmod ext2
  239. set root='(hd0,msdos1)'
  240. linux $BOOT/linux
  241. initrd $BOOT/initrd.gz
  242. }
  243. EOF