netboot.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #!/usr/bin/env 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. -fs)
  88. FILESYS=$2
  89. shift
  90. ;;
  91. -dry-run)
  92. DRYRUN=true
  93. ;;
  94. -crypto)
  95. DISKCRYPTO="crypto"
  96. ;;
  97. -manually)
  98. MANUALLY=true
  99. ;;
  100. *)
  101. echo "Illegal option $1"
  102. exit 1
  103. esac
  104. shift
  105. done
  106. case "$COUNTRY" in
  107. CN)
  108. PROTO=${PROTO:-https}
  109. HOST=${HOST:-chinanet.mirrors.ustc.edu.cn}
  110. TIME_ZONE=${TIME_ZONE:-Asia/Shanghai}
  111. NTP=${NTP:-cn.ntp.org.cn}
  112. SECURITY=${SECURITY:-true}
  113. DNS=${DNS:-156.154.70.5 156.154.71.5}
  114. esac
  115. COUNTRY=${COUNTRY:-US}
  116. PROTO=${PROTO:-http}
  117. HOST=${HOST:-deb.debian.org}
  118. DIR=${DIR:-/debian}
  119. ARCH=$(dpkg --print-architecture)
  120. SUITE=${SUITE:-stretch}
  121. ADMIN=${ADMIN:-debian}
  122. TIME_ZONE=${TIME_ZONE:-UTC}
  123. NTP=${NTP:-pool.ntp.org}
  124. UPGRADE=${UPGRADE:-full-upgrade}
  125. DNS=${DNS:-8.8.8.8 8.8.4.4}
  126. FILESYS=${FILESYS:-ext4}
  127. DISKCRYPTO=${DISKCRYPTO:-regular}
  128. if [ -z "$SECURITY" ]; then
  129. SECURITY=http://security.debian.org/debian-security
  130. else
  131. if [ "$SECURITY" = true ]; then
  132. SECURITY=$PROTO://$HOST${DIR%/*}/debian-security
  133. fi
  134. fi
  135. if [ -z "$PASSWD" ] && [ "$MANUALLY" != true ]; then
  136. PASSWD=$(mkpasswd -m sha-512)
  137. else
  138. PASSWD=$(mkpasswd -m sha-512 "$PASSWD")
  139. fi
  140. if [ "$DRYRUN" != true ]; then
  141. BOOT=/boot/debian-$SUITE
  142. URL=$PROTO://$HOST$DIR/dists/$SUITE/main/installer-$ARCH/current/images/netboot/debian-installer/$ARCH
  143. update-grub
  144. rm -fr "$BOOT"
  145. mkdir -p "$BOOT"
  146. cd "$BOOT"
  147. fi
  148. cat >> preseed.cfg << EOF
  149. # COUNTRY: 1
  150. # IP_ADDR: 2
  151. # NETMASK: 2
  152. # GATEWAY: 2
  153. # DNS: 2
  154. # FQDN: 2
  155. # SSH_PASSWD: 2
  156. # PROTO: 3
  157. # HOST: 3
  158. # DIR: 3
  159. # SUITE: 3, 8
  160. # ADMIN: 4
  161. # PASSWD: 4
  162. # TIME_ZONE: 5
  163. # NTP: 5
  164. # FILESYS: 6
  165. # DISKCRYPTO: 6
  166. # SECURITY: 8
  167. # INCLUDE: 9
  168. # UPGRADE: 9
  169. # 1. Localization: COUNTRY
  170. d-i debian-installer/locale string en_US
  171. d-i debian-installer/language string en
  172. d-i debian-installer/country string {{-COUNTRY-}}
  173. d-i debian-installer/locale string en_US.UTF-8
  174. d-i keyboard-configuration/xkb-keymap select us
  175. # 2. Network configuration: IP_ADDR, NETMASK, GATEWAY, DNS, FQDN, SSH_PASSWD
  176. d-i netcfg/choose_interface select auto
  177. EOF
  178. if [ -n "$IP_ADDR" ]; then
  179. echo "d-i netcfg/disable_autoconfig boolean true" >> preseed.cfg
  180. echo "d-i netcfg/get_ipaddress string $IP_ADDR" >> preseed.cfg
  181. if [ -n "$NETMASK" ]; then
  182. echo "d-i netcfg/get_netmask string $NETMASK" >> preseed.cfg
  183. fi
  184. if [ -n "$GATEWAY" ]; then
  185. echo "d-i netcfg/get_gateway string $GATEWAY" >> preseed.cfg
  186. fi
  187. if [ -n "$DNS" ]; then
  188. echo "d-i netcfg/get_nameservers string $DNS" >> preseed.cfg
  189. fi
  190. echo "d-i netcfg/confirm_static boolean true" >> preseed.cfg
  191. fi
  192. cat >> preseed.cfg << EOF
  193. d-i netcfg/get_hostname string debian
  194. d-i netcfg/get_domain string
  195. EOF
  196. if [ -n "$FQDN" ]; then
  197. echo "d-i netcfg/hostname string $FQDN" >> preseed.cfg
  198. fi
  199. cat >> preseed.cfg << EOF
  200. d-i hw-detect/load_firmware boolean true
  201. EOF
  202. if [ -n "$SSH_PASSWD" ]; then
  203. echo "d-i anna/choose_modules string network-console" >> preseed.cfg
  204. echo "d-i preseed/early_command string anna-install network-console" >> preseed.cfg
  205. echo "d-i network-console/password password $SSH_PASSWD" >> preseed.cfg
  206. echo "d-i network-console/password-again password $SSH_PASSWD" >> preseed.cfg
  207. echo "d-i network-console/start select Continue" >> preseed.cfg
  208. fi
  209. cat >> preseed.cfg << EOF
  210. # 3. Mirror settings: PROTO, HOST, DIR, SUITE
  211. d-i mirror/country string manual
  212. d-i mirror/protocol string {{-PROTO-}}
  213. d-i mirror/{{-PROTO-}}/hostname string {{-HOST-}}
  214. d-i mirror/{{-PROTO-}}/directory string {{-DIR-}}
  215. d-i mirror/{{-PROTO-}}/proxy string
  216. d-i mirror/suite string {{-SUITE-}}
  217. d-i mirror/udeb/suite string {{-SUITE-}}
  218. EOF
  219. if [ "$MANUALLY" != true ]; then
  220. cat >> preseed.cfg << EOF
  221. # 4. Account setup: ADMIN, PASSWD
  222. d-i passwd/root-login boolean false
  223. d-i passwd/user-fullname string
  224. d-i passwd/username string {{-ADMIN-}}
  225. d-i passwd/user-password-crypted password {{-PASSWD-}}
  226. # 5. Clock and time zone setup: TIME_ZONE, NTP
  227. d-i clock-setup/utc boolean true
  228. d-i time/zone string {{-TIME_ZONE-}}
  229. d-i clock-setup/ntp boolean true
  230. d-i clock-setup/ntp-server string {{-NTP-}}
  231. EOF
  232. cat >> preseed.cfg << EOF
  233. # 6. Partitioning: FILESYS
  234. d-i partman-basicfilesystems/no_swap boolean false
  235. d-i partman/default_filesystem string {{-FILESYS-}}
  236. d-i partman-auto/method string {{-DISKCRYPTO-}}
  237. d-i partman-lvm/device_remove_lvm boolean true
  238. d-i partman-md/device_remove_md boolean true
  239. d-i partman-lvm/confirm boolean true
  240. d-i partman-lvm/confirm_nooverwrite boolean true
  241. EOF
  242. if [ "$DISKCRYPTO" = "regular" ]; then
  243. cat >> preseed.cfg << EOF
  244. d-i partman-auto/expert_recipe string naive :: 0 1 -1 \$default_filesystem \$primary{ } \$bootable{ } method{ format } format{ } use_filesystem{ } \$default_filesystem{ } mountpoint{ / } .
  245. d-i partman-auto/choose_recipe select naive
  246. EOF
  247. fi
  248. cat >> preseed.cfg << EOF
  249. d-i partman-partitioning/confirm_write_new_label boolean true
  250. d-i partman/choose_partition select finish
  251. d-i partman/confirm boolean true
  252. d-i partman/confirm_nooverwrite boolean true
  253. d-i partman/mount_style select uuid
  254. EOF
  255. cat >> preseed.cfg << EOF
  256. # 7. Base system installation
  257. d-i base-installer/install-recommends boolean false
  258. # 8. Apt setup: SECURITY, SUITE
  259. d-i apt-setup/services-select multiselect updates
  260. d-i apt-setup/local0/repository string {{-SECURITY-}} {{-SUITE-}}/updates main
  261. d-i apt-setup/local0/source boolean true
  262. # 9. Package selection: INCLUDE, UPGRADE
  263. tasksel tasksel/first multiselect ssh-server
  264. EOF
  265. if [ -n "$INCLUDE" ]; then
  266. echo "d-i pkgsel/include string $INCLUDE" >> preseed.cfg
  267. fi
  268. cat >> preseed.cfg << EOF
  269. d-i pkgsel/upgrade select {{-UPGRADE-}}
  270. popularity-contest popularity-contest/participate boolean false
  271. # 10. Boot loader installation
  272. d-i grub-installer/only_debian boolean true
  273. d-i grub-installer/bootdev string default
  274. # 11. Finishing up the installation
  275. d-i finish-install/reboot_in_progress note
  276. EOF
  277. fi
  278. sed -i 's/{{-COUNTRY-}}/'"$COUNTRY"'/g' preseed.cfg
  279. sed -i 's/{{-PROTO-}}/'"$PROTO"'/g' preseed.cfg
  280. sed -i 's/{{-HOST-}}/'"$HOST"'/g' preseed.cfg
  281. sed -i 's/{{-DIR-}}/'$(echo "$DIR" | sed 's/\//\\\//g')'/g' preseed.cfg
  282. sed -i 's/{{-SUITE-}}/'"$SUITE"'/g' preseed.cfg
  283. sed -i 's/{{-ADMIN-}}/'"$ADMIN"'/g' preseed.cfg
  284. sed -i 's/{{-PASSWD-}}/'$(echo "$PASSWD" | sed 's/\//\\\//g')'/g' preseed.cfg
  285. sed -i 's/{{-TIME_ZONE-}}/'$(echo "$TIME_ZONE" | sed 's/\//\\\//g')'/g' preseed.cfg
  286. sed -i 's/{{-NTP-}}/'"$NTP"'/g' preseed.cfg
  287. sed -i 's/{{-SECURITY-}}/'$(echo "$SECURITY" | sed 's/\//\\\//g')'/g' preseed.cfg
  288. sed -i 's/{{-UPGRADE-}}/'"$UPGRADE"'/g' preseed.cfg
  289. sed -i 's/{{-FILESYS-}}/'"$FILESYS"'/g' preseed.cfg
  290. sed -i 's/{{-DISKCRYPTO-}}/'"$DISKCRYPTO"'/g' preseed.cfg
  291. if [ "$DRYRUN" != true ]; then
  292. wget "$URL/linux" "$URL/initrd.gz"
  293. gunzip initrd.gz
  294. echo preseed.cfg | cpio -H newc -o -A -F initrd
  295. gzip initrd
  296. cat >> ../grub/grub.cfg << EOF
  297. menuentry 'New Install' {
  298. insmod part_msdos
  299. insmod ext2
  300. set root='(hd0,msdos1)'
  301. linux $BOOT/linux
  302. initrd $BOOT/initrd.gz
  303. }
  304. EOF
  305. fi