netboot.sh 8.3 KB

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