netboot.sh 9.7 KB

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