netboot.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. *)
  83. echo "Illegal option $1"
  84. exit 1
  85. esac
  86. shift
  87. done
  88. case "$COUNTRY" in
  89. CN)
  90. TRANSPORT=${TRANSPORT:-https}
  91. MIRROR=${MIRROR:-chinanet.mirrors.ustc.edu.cn}
  92. TIMEZONE=${TIMEZONE:-Asia/Shanghai}
  93. NTPSERVER=${NTPSERVER:-ntp1.aliyun.com}
  94. LINKED=${LINKED:-true}
  95. esac
  96. COUNTRY=${COUNTRY:-US}
  97. HOST=${HOST:-debian}
  98. TRANSPORT=${TRANSPORT:-http}
  99. MIRROR=${MIRROR:-deb.debian.org}
  100. DIRECTORY=${DIRECTORY:-/debian}
  101. ARCH=$(dpkg --print-architecture)
  102. SUITE=${SUITE:-stretch}
  103. USERNAME=${USERNAME:-debian}
  104. TIMEZONE=${TIMEZONE:-UTC}
  105. NTPSERVER=${NTPSERVER:-pool.ntp.org}
  106. UPGRADE=${UPGRADE:-full-upgrade}
  107. LINKED=${LINKED:-false}
  108. if [ -z "$SECURITY" ]; then
  109. if $LINKED; then
  110. SECURITY=$TRANSPORT://$MIRROR${DIRECTORY%/*}/debian-security
  111. else
  112. SECURITY=http://security.debian.org/debian-security
  113. fi
  114. fi
  115. if [ -z "$PASSWORD" ]; then
  116. PASSWORD=$(mkpasswd -m sha-512)
  117. else
  118. PASSWORD=$(mkpasswd -m sha-512 "$PASSWORD")
  119. fi
  120. BOOT=/boot/debian-$SUITE
  121. URL=$TRANSPORT://$MIRROR$DIRECTORY/dists/$SUITE/main/installer-$ARCH/current/images/netboot/debian-installer/$ARCH
  122. update-grub
  123. rm -fr "$BOOT"
  124. mkdir -p "$BOOT"
  125. cd "$BOOT"
  126. cat >> preseed.cfg << EOF
  127. # COUNTRY: 1
  128. # HOST: 2
  129. # TRANSPORT: 3
  130. # MIRROR: 3
  131. # DIRECTORY: 3
  132. # SUITE: 3, 8
  133. # USERNAME: 4
  134. # PASSWORD: 4
  135. # TIMEZONE: 5
  136. # NTPSERVER: 5
  137. # SECURITY: 8
  138. # UPGRADE: 9
  139. # 1. Localization: COUNTRY
  140. d-i debian-installer/locale string en_US
  141. d-i debian-installer/language string en
  142. d-i debian-installer/country string {{-COUNTRY-}}
  143. d-i debian-installer/locale string en_US.UTF-8
  144. d-i keyboard-configuration/xkb-keymap select us
  145. # 2. Network configuration: HOST
  146. d-i netcfg/choose_interface select auto
  147. EOF
  148. if [ -n "$IPADDR" ]; then
  149. echo "d-i netcfg/disable_autoconfig boolean true" >> preseed.cfg
  150. echo "d-i netcfg/get_ipaddress string $IPADDR" >> preseed.cfg
  151. if [ -n "$NETMASK" ]; then
  152. echo "d-i netcfg/get_netmask string $NETMASK" >> preseed.cfg
  153. fi
  154. if [ -n "$GATEWAY" ]; then
  155. echo "d-i netcfg/get_gateway string $GATEWAY" >> preseed.cfg
  156. fi
  157. if [ -n "$DNS" ]; then
  158. echo "d-i netcfg/get_nameservers string $DNS" >> preseed.cfg
  159. fi
  160. echo "d-i netcfg/confirm_static boolean true" >> preseed.cfg
  161. fi
  162. cat >> preseed.cfg << EOF
  163. d-i netcfg/get_hostname string unassigned-hostname
  164. d-i netcfg/get_domain string unassigned-domain
  165. d-i netcfg/hostname string {{-HOST-}}
  166. d-i hw-detect/load_firmware boolean true
  167. # 3. Mirror settings: TRANSPORT, MIRROR, DIRECTORY, SUITE
  168. d-i mirror/country string manual
  169. d-i mirror/protocol string {{-TRANSPORT-}}
  170. d-i mirror/{{-TRANSPORT-}}/hostname string {{-MIRROR-}}
  171. d-i mirror/{{-TRANSPORT-}}/directory string {{-DIRECTORY-}}
  172. d-i mirror/{{-TRANSPORT-}}/proxy string
  173. d-i mirror/suite string {{-SUITE-}}
  174. d-i mirror/udeb/suite string {{-SUITE-}}
  175. # 4. Account setup: USERNAME, PASSWORD
  176. d-i passwd/root-login boolean false
  177. d-i passwd/user-fullname string
  178. d-i passwd/username string {{-USERNAME-}}
  179. d-i passwd/user-password-crypted password {{-PASSWORD-}}
  180. # 5. Clock and time zone setup: TIMEZONE, NTPSERVER
  181. d-i clock-setup/utc boolean true
  182. d-i time/zone string {{-TIMEZONE-}}
  183. d-i clock-setup/ntp boolean true
  184. d-i clock-setup/ntp-server string {{-NTPSERVER-}}
  185. # 6. Partitioning
  186. d-i partman-basicfilesystems/no_swap boolean false
  187. d-i partman-auto/method string regular
  188. d-i partman-lvm/device_remove_lvm boolean true
  189. d-i partman-md/device_remove_md boolean true
  190. d-i partman-lvm/confirm boolean true
  191. d-i partman-lvm/confirm_nooverwrite boolean true
  192. d-i partman-auto/expert_recipe string naive :: 0 1 -1 ext4 $primary{ } $bootable{ } method{ format } format{ } use_filesystem{ } filesystem{ ext4 } mountpoint{ / } .
  193. d-i partman-auto/choose_recipe select naive
  194. d-i partman-partitioning/confirm_write_new_label boolean true
  195. d-i partman/choose_partition select finish
  196. d-i partman/confirm boolean true
  197. d-i partman/confirm_nooverwrite boolean true
  198. d-i partman/mount_style select uuid
  199. # 7. Base system installation
  200. d-i base-installer/install-recommends boolean false
  201. # 8. Apt setup: SECURITY, SUITE
  202. d-i apt-setup/services-select multiselect updates
  203. d-i apt-setup/local0/repository string {{-SECURITY-}} {{-SUITE-}}/updates main
  204. d-i apt-setup/local0/source boolean true
  205. # 9. Package selection: TASKS, UPGRADE
  206. tasksel tasksel/first multiselect ssh-server
  207. d-i pkgsel/upgrade select {{-UPGRADE-}}
  208. popularity-contest popularity-contest/participate boolean false
  209. # 10. Boot loader installation
  210. d-i grub-installer/only_debian boolean true
  211. d-i grub-installer/bootdev string default
  212. EOF
  213. sed -i 's/{{-COUNTRY-}}/'"$COUNTRY"'/g' preseed.cfg
  214. sed -i 's/{{-HOST-}}/'"$HOST"'/g' preseed.cfg
  215. sed -i 's/{{-TRANSPORT-}}/'"$TRANSPORT"'/g' preseed.cfg
  216. sed -i 's/{{-MIRROR-}}/'"$MIRROR"'/g' preseed.cfg
  217. sed -i 's/{{-DIRECTORY-}}/'$(echo "$DIRECTORY" | sed 's/\//\\\//g')'/g' preseed.cfg
  218. sed -i 's/{{-SUITE-}}/'"$SUITE"'/g' preseed.cfg
  219. sed -i 's/{{-USERNAME-}}/'"$USERNAME"'/g' preseed.cfg
  220. sed -i 's/{{-PASSWORD-}}/'$(echo "$PASSWORD" | sed 's/\//\\\//g')'/g' preseed.cfg
  221. sed -i 's/{{-TIMEZONE-}}/'$(echo "$TIMEZONE" | sed 's/\//\\\//g')'/g' preseed.cfg
  222. sed -i 's/{{-NTPSERVER-}}/'"$NTPSERVER"'/g' preseed.cfg
  223. sed -i 's/{{-SECURITY-}}/'$(echo "$SECURITY" | sed 's/\//\\\//g')'/g' preseed.cfg
  224. sed -i 's/{{-UPGRADE-}}/'"$UPGRADE"'/g' preseed.cfg
  225. wget "$URL/linux" "$URL/initrd.gz"
  226. gunzip initrd.gz
  227. echo preseed.cfg | cpio -H newc -o -A -F initrd
  228. gzip initrd
  229. cat >> ../grub/grub.cfg << EOF
  230. menuentry 'New Install' {
  231. insmod part_msdos
  232. insmod ext2
  233. set root='(hd0,msdos1)'
  234. linux $BOOT/linux
  235. initrd $BOOT/initrd.gz
  236. }
  237. EOF