netboot.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. DEBNETB_COUNTRY=$2
  17. shift
  18. ;;
  19. -fqdn)
  20. DEBNETB_FQDN=$2
  21. shift
  22. ;;
  23. -proto)
  24. DEBNETB_PROTO=$2
  25. shift
  26. ;;
  27. -host)
  28. DEBNETB_HOST=$2
  29. shift
  30. ;;
  31. -dir)
  32. DEBNETB_DIR=${2%/}
  33. shift
  34. ;;
  35. -suite)
  36. DEBNETB_SUITE=$2
  37. shift
  38. ;;
  39. -u)
  40. DEBNETB_ADMIN=$2
  41. shift
  42. ;;
  43. -p)
  44. DEBNETB_PASSWD=$2
  45. shift
  46. ;;
  47. -tz)
  48. DEBNETB_TIME_ZONE=$2
  49. shift
  50. ;;
  51. -ntp)
  52. DEBNETB_NTP=$2
  53. shift
  54. ;;
  55. -s)
  56. DEBNETB_SECURITY=$2
  57. shift
  58. ;;
  59. -upgrade)
  60. DEBNETB_UPGRADE=$2
  61. shift
  62. ;;
  63. -ip)
  64. DEBNETB_IP_ADDR=$2
  65. shift
  66. ;;
  67. -cidr)
  68. DEBNETB_NETMASK=$2
  69. shift
  70. ;;
  71. -gw)
  72. DEBNETB_GATEWAY=$2
  73. shift
  74. ;;
  75. -ns)
  76. DEBNETB_DNS=$2
  77. shift
  78. ;;
  79. -add)
  80. DEBNETB_INCLUDE=$2
  81. shift
  82. ;;
  83. -ssh)
  84. DEBNETB_SSH_PASSWD=$2
  85. shift
  86. ;;
  87. -fs)
  88. DEBNETB_FILESYS=$2
  89. shift
  90. ;;
  91. -dry-run)
  92. DEBNETB_DRYRUN=true
  93. ;;
  94. -crypto)
  95. DEBNETB_DISKCRYPTO="crypto"
  96. ;;
  97. -manually)
  98. DEBNETB_MANUALLY=true
  99. ;;
  100. -arch)
  101. DEBNETB_ARCH=$2
  102. shift
  103. ;;
  104. -lvm)
  105. DEBNETB_ISLVM=true
  106. ;;
  107. *)
  108. echo "Illegal option $1"
  109. exit 1
  110. esac
  111. shift
  112. done
  113. case "$DEBNETB_COUNTRY" in
  114. CN)
  115. DEBNETB_PROTO=${DEBNETB_PROTO:-https}
  116. DEBNETB_HOST=${DEBNETB_HOST:-chinanet.mirrors.ustc.edu.cn}
  117. DEBNETB_TIME_ZONE=${DEBNETB_TIME_ZONE:-Asia/Shanghai}
  118. DEBNETB_NTP=${DEBNETB_NTP:-cn.ntp.org.cn}
  119. DEBNETB_SECURITY=${DEBNETB_SECURITY:-true}
  120. DEBNETB_DNS=${DEBNETB_DNS:-156.154.70.5 156.154.71.5}
  121. esac
  122. DEBNETB_COUNTRY=${DEBNETB_COUNTRY:-US}
  123. DEBNETB_PROTO=${DEBNETB_PROTO:-http}
  124. DEBNETB_HOST=${DEBNETB_HOST:-deb.debian.org}
  125. DEBNETB_DIR=${DEBNETB_DIR:-/debian}
  126. if [ -z "$DEBNETB_ARCH" ]; then
  127. DEBNETB_ARCH=$(dpkg --print-architecture)
  128. fi
  129. DEBNETB_SUITE=${DEBNETB_SUITE:-stretch}
  130. DEBNETB_ADMIN=${DEBNETB_ADMIN:-debian}
  131. DEBNETB_TIME_ZONE=${DEBNETB_TIME_ZONE:-UTC}
  132. DEBNETB_NTP=${DEBNETB_NTP:-pool.ntp.org}
  133. DEBNETB_UPGRADE=${DEBNETB_UPGRADE:-full-upgrade}
  134. DEBNETB_DNS=${DEBNETB_DNS:-8.8.8.8 8.8.4.4}
  135. DEBNETB_FILESYS=${DEBNETB_FILESYS:-ext4}
  136. DEBNETB_DISKCRYPTO=${DEBNETB_DISKCRYPTO:-regular}
  137. if [ -z "$DEBNETB_SECURITY" ]; then
  138. DEBNETB_SECURITY=http://security.debian.org/debian-security
  139. else
  140. if [ "$DEBNETB_SECURITY" = true ]; then
  141. DEBNETB_SECURITY=$DEBNETB_PROTO://$DEBNETB_HOST${DEBNETB_DIR%/*}/debian-security
  142. fi
  143. fi
  144. if [ "$DEBNETB_MANUALLY" != true ]; then
  145. if [ -z "$DEBNETB_PASSWD" ]; then
  146. DEBNETB_PASSWD=$(mkpasswd -m sha-512)
  147. else
  148. DEBNETB_PASSWD=$(mkpasswd -m sha-512 "$DEBNETB_PASSWD")
  149. fi
  150. fi
  151. if [ "$DEBNETB_DRYRUN" != true ]; then
  152. DEBNETB_BOOTNAME="debian-$DEBNETB_SUITE"
  153. if [ "$DEBNETB_ISLVM" = true ]; then
  154. DEBNETB_BOOTROOT=/
  155. else
  156. DEBNETB_BOOTROOT=/boot/
  157. fi
  158. DEBNETB_BOOT="/boot/$DEBNETB_BOOTNAME"
  159. DEBNETB_OUTPUTBOOT="$DEBNETB_BOOTROOT$DEBNETB_BOOTNAME"
  160. DEBNETB_URL=$DEBNETB_PROTO://$DEBNETB_HOST$DEBNETB_DIR/dists/$DEBNETB_SUITE/main/installer-$DEBNETB_ARCH/current/images/netboot/debian-installer/$DEBNETB_ARCH
  161. if type update-grub >/dev/null; then
  162. update-grub
  163. DEBNETB_GRUBCFG=/boot/grub/grub.cfg
  164. else
  165. DEBNETB_GRUBCFG=/boot/grub2/grub.cfg
  166. grub2-mkconfig –o "$DEBNETB_GRUBCFG"
  167. fi
  168. rm -fr "$DEBNETB_BOOT"
  169. mkdir -p "$DEBNETB_BOOT"
  170. cd "$DEBNETB_BOOT"
  171. fi
  172. cat >> preseed.cfg << EOF
  173. # COUNTRY: 1
  174. # IP_ADDR: 2
  175. # NETMASK: 2
  176. # GATEWAY: 2
  177. # DNS: 2
  178. # FQDN: 2
  179. # SSH_PASSWD: 2
  180. # PROTO: 3
  181. # HOST: 3
  182. # DIR: 3
  183. # SUITE: 3, 8
  184. # ADMIN: 4
  185. # PASSWD: 4
  186. # TIME_ZONE: 5
  187. # NTP: 5
  188. # FILESYS: 6
  189. # DISKCRYPTO: 6
  190. # SECURITY: 8
  191. # INCLUDE: 9
  192. # UPGRADE: 9
  193. # 1. Localization: COUNTRY
  194. d-i debian-installer/locale string en_US
  195. d-i debian-installer/language string en
  196. d-i debian-installer/country string {{-COUNTRY-}}
  197. d-i debian-installer/locale string en_US.UTF-8
  198. d-i keyboard-configuration/xkb-keymap select us
  199. # 2. Network configuration: IP_ADDR, NETMASK, GATEWAY, DNS, FQDN, SSH_PASSWD
  200. d-i netcfg/choose_interface select auto
  201. EOF
  202. if [ -n "$DEBNETB_IP_ADDR" ]; then
  203. echo "d-i netcfg/disable_autoconfig boolean true" >> preseed.cfg
  204. echo "d-i netcfg/get_ipaddress string $DEBNETB_IP_ADDR" >> preseed.cfg
  205. if [ -n "$DEBNETB_NETMASK" ]; then
  206. echo "d-i netcfg/get_netmask string $DEBNETB_NETMASK" >> preseed.cfg
  207. fi
  208. if [ -n "$DEBNETB_GATEWAY" ]; then
  209. echo "d-i netcfg/get_gateway string $DEBNETB_GATEWAY" >> preseed.cfg
  210. fi
  211. if [ -n "$DEBNETB_DNS" ]; then
  212. echo "d-i netcfg/get_nameservers string $DEBNETB_DNS" >> preseed.cfg
  213. fi
  214. echo "d-i netcfg/confirm_static boolean true" >> preseed.cfg
  215. fi
  216. cat >> preseed.cfg << EOF
  217. d-i netcfg/get_hostname string debian
  218. d-i netcfg/get_domain string
  219. EOF
  220. if [ -n "$DEBNETB_FQDN" ]; then
  221. echo "d-i netcfg/hostname string $DEBNETB_FQDN" >> preseed.cfg
  222. fi
  223. cat >> preseed.cfg << EOF
  224. d-i hw-detect/load_firmware boolean true
  225. EOF
  226. if [ -n "$DEBNETB_SSH_PASSWD" ]; then
  227. echo "d-i anna/choose_modules string network-console" >> preseed.cfg
  228. echo "d-i preseed/early_command string anna-install network-console" >> preseed.cfg
  229. echo "d-i network-console/password password $DEBNETB_SSH_PASSWD" >> preseed.cfg
  230. echo "d-i network-console/password-again password $DEBNETB_SSH_PASSWD" >> preseed.cfg
  231. echo "d-i network-console/start select Continue" >> preseed.cfg
  232. fi
  233. cat >> preseed.cfg << EOF
  234. # 3. Mirror settings: PROTO, HOST, DIR, SUITE
  235. d-i mirror/country string manual
  236. d-i mirror/protocol string {{-PROTO-}}
  237. d-i mirror/{{-PROTO-}}/hostname string {{-HOST-}}
  238. d-i mirror/{{-PROTO-}}/directory string {{-DIR-}}
  239. d-i mirror/{{-PROTO-}}/proxy string
  240. d-i mirror/suite string {{-SUITE-}}
  241. d-i mirror/udeb/suite string {{-SUITE-}}
  242. EOF
  243. if [ "$DEBNETB_MANUALLY" != true ]; then
  244. cat >> preseed.cfg << EOF
  245. # 4. Account setup: ADMIN, PASSWD
  246. d-i passwd/root-login boolean false
  247. d-i passwd/user-fullname string
  248. d-i passwd/username string {{-ADMIN-}}
  249. d-i passwd/user-password-crypted password {{-PASSWD-}}
  250. # 5. Clock and time zone setup: TIME_ZONE, NTP
  251. d-i clock-setup/utc boolean true
  252. d-i time/zone string {{-TIME_ZONE-}}
  253. d-i clock-setup/ntp boolean true
  254. d-i clock-setup/ntp-server string {{-NTP-}}
  255. EOF
  256. cat >> preseed.cfg << EOF
  257. # 6. Partitioning: FILESYS
  258. d-i partman-basicfilesystems/no_swap boolean false
  259. d-i partman/default_filesystem string {{-FILESYS-}}
  260. d-i partman-auto/method string {{-DISKCRYPTO-}}
  261. d-i partman-lvm/device_remove_lvm boolean true
  262. d-i partman-md/device_remove_md boolean true
  263. d-i partman-lvm/confirm boolean true
  264. d-i partman-lvm/confirm_nooverwrite boolean true
  265. EOF
  266. if [ "$DEBNETB_DISKCRYPTO" = "regular" ]; then
  267. cat >> preseed.cfg << EOF
  268. d-i partman-auto/expert_recipe string naive :: 0 1 -1 \$default_filesystem \$primary{ } \$bootable{ } method{ format } format{ } use_filesystem{ } \$default_filesystem{ } mountpoint{ / } .
  269. d-i partman-auto/choose_recipe select naive
  270. EOF
  271. fi
  272. cat >> preseed.cfg << EOF
  273. d-i partman-partitioning/confirm_write_new_label boolean true
  274. d-i partman/choose_partition select finish
  275. d-i partman/confirm boolean true
  276. d-i partman/confirm_nooverwrite boolean true
  277. d-i partman/mount_style select uuid
  278. EOF
  279. cat >> preseed.cfg << EOF
  280. # 7. Base system installation
  281. d-i base-installer/install-recommends boolean false
  282. # 8. Apt setup: SECURITY, SUITE
  283. d-i apt-setup/services-select multiselect updates
  284. d-i apt-setup/local0/repository string {{-SECURITY-}} {{-SUITE-}}/updates main
  285. d-i apt-setup/local0/source boolean true
  286. # 9. Package selection: INCLUDE, UPGRADE
  287. tasksel tasksel/first multiselect ssh-server
  288. EOF
  289. if [ -n "$DEBNETB_INCLUDE" ]; then
  290. echo "d-i pkgsel/include string $DEBNETB_INCLUDE" >> preseed.cfg
  291. fi
  292. cat >> preseed.cfg << EOF
  293. d-i pkgsel/upgrade select {{-UPGRADE-}}
  294. popularity-contest popularity-contest/participate boolean false
  295. # 10. Boot loader installation
  296. d-i grub-installer/only_debian boolean true
  297. d-i grub-installer/bootdev string default
  298. # 11. Finishing up the installation
  299. d-i finish-install/reboot_in_progress note
  300. EOF
  301. fi
  302. sed -i 's/{{-COUNTRY-}}/'"$DEBNETB_COUNTRY"'/g' preseed.cfg
  303. sed -i 's/{{-PROTO-}}/'"$DEBNETB_PROTO"'/g' preseed.cfg
  304. sed -i 's/{{-HOST-}}/'"$DEBNETB_HOST"'/g' preseed.cfg
  305. sed -i 's/{{-DIR-}}/'$(echo "$DEBNETB_DIR" | sed 's/\//\\\//g')'/g' preseed.cfg
  306. sed -i 's/{{-SUITE-}}/'"$DEBNETB_SUITE"'/g' preseed.cfg
  307. sed -i 's/{{-ADMIN-}}/'"$DEBNETB_ADMIN"'/g' preseed.cfg
  308. sed -i 's/{{-PASSWD-}}/'$(echo "$DEBNETB_PASSWD" | sed 's/\//\\\//g')'/g' preseed.cfg
  309. sed -i 's/{{-TIME_ZONE-}}/'$(echo "$DEBNETB_TIME_ZONE" | sed 's/\//\\\//g')'/g' preseed.cfg
  310. sed -i 's/{{-NTP-}}/'"$DEBNETB_NTP"'/g' preseed.cfg
  311. sed -i 's/{{-SECURITY-}}/'$(echo "$DEBNETB_SECURITY" | sed 's/\//\\\//g')'/g' preseed.cfg
  312. sed -i 's/{{-UPGRADE-}}/'"$DEBNETB_UPGRADE"'/g' preseed.cfg
  313. sed -i 's/{{-FILESYS-}}/'"$DEBNETB_FILESYS"'/g' preseed.cfg
  314. sed -i 's/{{-DISKCRYPTO-}}/'"$DEBNETB_DISKCRYPTO"'/g' preseed.cfg
  315. if [ "$DEBNETB_DRYRUN" != true ]; then
  316. wget "$DEBNETB_URL/linux" "$DEBNETB_URL/initrd.gz"
  317. gunzip initrd.gz
  318. echo preseed.cfg | cpio -H newc -o -A -F initrd
  319. gzip initrd
  320. cat >> "$DEBNETB_GRUBCFG" << EOF
  321. menuentry 'New Install' {
  322. insmod part_msdos
  323. insmod ext2
  324. set DEBNETB_root='(hd0,msdos1)'
  325. linux $DEBNETB_OUTPUTBOOT/linux
  326. initrd $DEBNETB_OUTPUTBOOT/initrd.gz
  327. }
  328. EOF
  329. fi