netboot.sh 6.0 KB

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