netboot.sh 6.9 KB

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