netboot.sh 5.5 KB

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