netboot.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/bin/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|--country)
  16. COUNTRY=$2
  17. shift
  18. ;;
  19. -h|--hostname)
  20. HOST=$2
  21. shift
  22. ;;
  23. -x|--transport|--protocol)
  24. TRANSPORT=$2
  25. shift
  26. ;;
  27. -m|--mirror|--host)
  28. MIRROR=$2
  29. shift
  30. ;;
  31. -d|--dir|--path|--directory)
  32. DIRECTORY=${2%/}
  33. shift
  34. ;;
  35. -r|--suite|--release)
  36. SUITE=$2
  37. shift
  38. ;;
  39. -u|--user|--username)
  40. USERNAME=$2
  41. shift
  42. ;;
  43. -p|--pass|--password)
  44. PASSWORD=$2
  45. shift
  46. ;;
  47. -z|--time|--zone|--timezone)
  48. TIMEZONE=$2
  49. shift
  50. ;;
  51. -t|--ntp|--ntpserver)
  52. NTPSERVER=$2
  53. shift
  54. ;;
  55. -s|--security)
  56. SECURITY=$2
  57. shift
  58. ;;
  59. -l|--link|--linked|--linking)
  60. LINKED=true
  61. ;;
  62. -g|--upgrade)
  63. UPGRADE=$2
  64. shift
  65. ;;
  66. --addr|--ipaddr)
  67. IPADDR=$2
  68. shift
  69. ;;
  70. --mask|--netmask)
  71. NETMASK=$2
  72. shift
  73. ;;
  74. --gate|--gateway)
  75. GATEWAY=$2
  76. shift
  77. ;;
  78. --dns|--dnserver|--dnsserver)
  79. DNS=$2
  80. shift
  81. ;;
  82. --incl|--include)
  83. INCLUDE=$2
  84. shift
  85. ;;
  86. *)
  87. echo "Illegal option $1"
  88. exit 1
  89. esac
  90. shift
  91. done
  92. case "$COUNTRY" in
  93. CN)
  94. TRANSPORT=${TRANSPORT:-https}
  95. MIRROR=${MIRROR:-chinanet.mirrors.ustc.edu.cn}
  96. TIMEZONE=${TIMEZONE:-Asia/Shanghai}
  97. NTPSERVER=${NTPSERVER:-ntp1.aliyun.com}
  98. LINKED=${LINKED:-true}
  99. esac
  100. COUNTRY=${COUNTRY:-US}
  101. HOST=${HOST:-debian}
  102. TRANSPORT=${TRANSPORT:-http}
  103. MIRROR=${MIRROR:-deb.debian.org}
  104. DIRECTORY=${DIRECTORY:-/debian}
  105. ARCH=$(dpkg --print-architecture)
  106. SUITE=${SUITE:-stretch}
  107. USERNAME=${USERNAME:-debian}
  108. TIMEZONE=${TIMEZONE:-UTC}
  109. NTPSERVER=${NTPSERVER:-pool.ntp.org}
  110. UPGRADE=${UPGRADE:-full-upgrade}
  111. LINKED=${LINKED:-false}
  112. if [ -z "$SECURITY" ]; then
  113. if $LINKED; then
  114. SECURITY=$TRANSPORT://$MIRROR${DIRECTORY%/*}/debian-security
  115. else
  116. SECURITY=http://security.debian.org/debian-security
  117. fi
  118. fi
  119. if [ -z "$PASSWORD" ]; then
  120. PASSWORD=$(mkpasswd -m sha-512)
  121. else
  122. PASSWORD=$(mkpasswd -m sha-512 "$PASSWORD")
  123. fi
  124. BOOT=/boot/debian-$SUITE
  125. URL=$TRANSPORT://$MIRROR$DIRECTORY/dists/$SUITE/main/installer-$ARCH/current/images/netboot/debian-installer/$ARCH
  126. update-grub
  127. rm -fr "$BOOT"
  128. mkdir -p "$BOOT"
  129. cd "$BOOT"
  130. cat >> preseed.cfg << EOF
  131. # COUNTRY: 1
  132. # HOST: 2
  133. # TRANSPORT: 3
  134. # MIRROR: 3
  135. # DIRECTORY: 3
  136. # SUITE: 3, 8
  137. # USERNAME: 4
  138. # PASSWORD: 4
  139. # TIMEZONE: 5
  140. # NTPSERVER: 5
  141. # SECURITY: 8
  142. # UPGRADE: 9
  143. # 1. Localization: COUNTRY
  144. d-i debian-installer/locale string en_US
  145. d-i debian-installer/language string en
  146. d-i debian-installer/country string {{-COUNTRY-}}
  147. d-i debian-installer/locale string en_US.UTF-8
  148. d-i keyboard-configuration/xkb-keymap select us
  149. # 2. Network configuration: HOST
  150. d-i netcfg/choose_interface select auto
  151. EOF
  152. if [ -n "$IPADDR" ]; then
  153. echo "d-i netcfg/disable_autoconfig boolean true" >> preseed.cfg
  154. echo "d-i netcfg/get_ipaddress string $IPADDR" >> preseed.cfg
  155. if [ -n "$NETMASK" ]; then
  156. echo "d-i netcfg/get_netmask string $NETMASK" >> preseed.cfg
  157. fi
  158. if [ -n "$GATEWAY" ]; then
  159. echo "d-i netcfg/get_gateway string $GATEWAY" >> preseed.cfg
  160. fi
  161. if [ -n "$DNS" ]; then
  162. echo "d-i netcfg/get_nameservers string $DNS" >> preseed.cfg
  163. fi
  164. echo "d-i netcfg/confirm_static boolean true" >> preseed.cfg
  165. fi
  166. cat >> preseed.cfg << EOF
  167. d-i netcfg/get_hostname string unassigned-hostname
  168. d-i netcfg/get_domain string unassigned-domain
  169. d-i netcfg/hostname string {{-HOST-}}
  170. d-i hw-detect/load_firmware boolean true
  171. # 3. Mirror settings: TRANSPORT, MIRROR, DIRECTORY, SUITE
  172. d-i mirror/country string manual
  173. d-i mirror/protocol string {{-TRANSPORT-}}
  174. d-i mirror/{{-TRANSPORT-}}/hostname string {{-MIRROR-}}
  175. d-i mirror/{{-TRANSPORT-}}/directory string {{-DIRECTORY-}}
  176. d-i mirror/{{-TRANSPORT-}}/proxy string
  177. d-i mirror/suite string {{-SUITE-}}
  178. d-i mirror/udeb/suite string {{-SUITE-}}
  179. # 4. Account setup: USERNAME, PASSWORD
  180. d-i passwd/root-login boolean false
  181. d-i passwd/user-fullname string
  182. d-i passwd/username string {{-USERNAME-}}
  183. d-i passwd/user-password-crypted password {{-PASSWORD-}}
  184. # 5. Clock and time zone setup: TIMEZONE, NTPSERVER
  185. d-i clock-setup/utc boolean true
  186. d-i time/zone string {{-TIMEZONE-}}
  187. d-i clock-setup/ntp boolean true
  188. d-i clock-setup/ntp-server string {{-NTPSERVER-}}
  189. # 6. Partitioning
  190. d-i partman-basicfilesystems/no_swap boolean false
  191. d-i partman-auto/method string regular
  192. d-i partman-lvm/device_remove_lvm boolean true
  193. d-i partman-md/device_remove_md boolean true
  194. d-i partman-lvm/confirm boolean true
  195. d-i partman-lvm/confirm_nooverwrite boolean true
  196. d-i partman-auto/expert_recipe string naive :: 0 1 -1 ext4 $primary{ } $bootable{ } method{ format } format{ } use_filesystem{ } filesystem{ ext4 } mountpoint{ / } .
  197. d-i partman-auto/choose_recipe select naive
  198. d-i partman-partitioning/confirm_write_new_label boolean true
  199. d-i partman/choose_partition select finish
  200. d-i partman/confirm boolean true
  201. d-i partman/confirm_nooverwrite boolean true
  202. d-i partman/mount_style select uuid
  203. # 7. Base system installation
  204. d-i base-installer/install-recommends boolean false
  205. # 8. Apt setup: SECURITY, SUITE
  206. d-i apt-setup/services-select multiselect updates
  207. d-i apt-setup/local0/repository string {{-SECURITY-}} {{-SUITE-}}/updates main
  208. d-i apt-setup/local0/source boolean true
  209. # 9. Package selection: TASKS, UPGRADE
  210. tasksel tasksel/first multiselect ssh-server
  211. EOF
  212. if [ -n "$INCLUDE" ]; then
  213. echo "d-i pkgsel/include string $INCLUDE" >> preseed.cfg
  214. fi
  215. cat >> preseed.cfg << EOF
  216. d-i pkgsel/upgrade select {{-UPGRADE-}}
  217. popularity-contest popularity-contest/participate boolean false
  218. # 10. Boot loader installation
  219. d-i grub-installer/only_debian boolean true
  220. d-i grub-installer/bootdev string default
  221. EOF
  222. sed -i 's/{{-COUNTRY-}}/'"$COUNTRY"'/g' preseed.cfg
  223. sed -i 's/{{-HOST-}}/'"$HOST"'/g' preseed.cfg
  224. sed -i 's/{{-TRANSPORT-}}/'"$TRANSPORT"'/g' preseed.cfg
  225. sed -i 's/{{-MIRROR-}}/'"$MIRROR"'/g' preseed.cfg
  226. sed -i 's/{{-DIRECTORY-}}/'$(echo "$DIRECTORY" | sed 's/\//\\\//g')'/g' preseed.cfg
  227. sed -i 's/{{-SUITE-}}/'"$SUITE"'/g' preseed.cfg
  228. sed -i 's/{{-USERNAME-}}/'"$USERNAME"'/g' preseed.cfg
  229. sed -i 's/{{-PASSWORD-}}/'$(echo "$PASSWORD" | sed 's/\//\\\//g')'/g' preseed.cfg
  230. sed -i 's/{{-TIMEZONE-}}/'$(echo "$TIMEZONE" | sed 's/\//\\\//g')'/g' preseed.cfg
  231. sed -i 's/{{-NTPSERVER-}}/'"$NTPSERVER"'/g' preseed.cfg
  232. sed -i 's/{{-SECURITY-}}/'$(echo "$SECURITY" | sed 's/\//\\\//g')'/g' preseed.cfg
  233. sed -i 's/{{-UPGRADE-}}/'"$UPGRADE"'/g' preseed.cfg
  234. wget "$URL/linux" "$URL/initrd.gz"
  235. gunzip initrd.gz
  236. echo preseed.cfg | cpio -H newc -o -A -F initrd
  237. gzip initrd
  238. cat >> ../grub/grub.cfg << EOF
  239. menuentry 'New Install' {
  240. insmod part_msdos
  241. insmod ext2
  242. set root='(hd0,msdos1)'
  243. linux $BOOT/linux
  244. initrd $BOOT/initrd.gz
  245. }
  246. EOF