netboot.sh 8.0 KB

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