netboot.sh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #!/usr/bin/env sh
  2. # Copyright 2018-present 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. --template)
  16. DEBI_TEMPLATE=$2
  17. shift
  18. ;;
  19. --hostname)
  20. DEBI_HOSTNAME=$2
  21. shift
  22. ;;
  23. --protocol)
  24. DEBI_PROTOCOL=$2
  25. shift
  26. ;;
  27. --mirror)
  28. DEBI_MIRROR=$2
  29. shift
  30. ;;
  31. --directory)
  32. DEBI_DIRECTORY=${2%/}
  33. shift
  34. ;;
  35. --suite)
  36. DEBI_SUITE=$2
  37. shift
  38. ;;
  39. --username)
  40. DEBI_USERNAME=$2
  41. shift
  42. ;;
  43. --password)
  44. DEBI_PASSWORD=$2
  45. shift
  46. ;;
  47. --timezone)
  48. DEBI_TIMEZONE=$2
  49. shift
  50. ;;
  51. --ntp-server)
  52. DEBI_NTP_SERVER=$2
  53. shift
  54. ;;
  55. --security-mirror)
  56. DEBI_SECURITY_MIRROR=$2
  57. shift
  58. ;;
  59. --upgrade)
  60. DEBI_UPGRADE=$2
  61. shift
  62. ;;
  63. --ip)
  64. DEBI_IP=$2
  65. shift
  66. ;;
  67. --netmask)
  68. DEBI_NETMASK=$2
  69. shift
  70. ;;
  71. --gateway)
  72. DEBI_GATEWAY=$2
  73. shift
  74. ;;
  75. --dns)
  76. DEBI_DNS=$2
  77. shift
  78. ;;
  79. --include)
  80. DEBI_INCLUDE=$2
  81. shift
  82. ;;
  83. --ssh-password)
  84. DEBI_SSH=true
  85. DEBI_SSH_PASSWD=$2
  86. shift
  87. ;;
  88. --ssh-keys)
  89. DEBI_SSH=true
  90. DEBI_SSH_KEYS=$2
  91. shift
  92. ;;
  93. --filesystem)
  94. DEBI_FILESYSTEM=$2
  95. shift
  96. ;;
  97. --dry-run)
  98. DEBI_DRY_RUN=true
  99. ;;
  100. --disk-encryption)
  101. DEBI_DISK_ENCRYPTION="crypto"
  102. ;;
  103. --manual)
  104. DEBI_MANUAL=true
  105. ;;
  106. --architecture)
  107. DEBI_ARCHITECTURE=$2
  108. shift
  109. ;;
  110. --boot-partition)
  111. DEBI_BOOT_PARTITION=true
  112. ;;
  113. *)
  114. echo "Illegal option $1"
  115. exit 1
  116. esac
  117. shift
  118. done
  119. case "$DEBI_TEMPLATE" in
  120. china)
  121. DEBI_PROTOCOL=${DEBI_PROTOCOL:-https}
  122. DEBI_MIRROR=${DEBI_MIRROR:-chinanet.mirrors.ustc.edu.cn}
  123. DEBI_TIMEZONE=${DEBI_TIMEZONE:-Asia/Shanghai}
  124. DEBI_NTP_SERVER=${DEBI_NTP_SERVER:-cn.ntp.org.cn}
  125. DEBI_SECURITY_MIRROR=${DEBI_SECURITY_MIRROR:-true}
  126. DEBI_DNS=${DEBI_DNS:-156.154.70.5 156.154.71.5}
  127. vps)
  128. DEBI_PROTOCOL=${DEBI_PROTOCOL:-https}
  129. DEBI_MIRROR=${DEBI_MIRROR:-cdn-aws.deb.debian.org}
  130. DEBI_NTP_SERVER=${DEBI_NTP_SERVER:-time.google.com}
  131. DEBI_SECURITY_MIRROR=${DEBI_SECURITY_MIRROR:-true}
  132. esac
  133. DEBI_PROTOCOL=${DEBI_PROTOCOL:-http}
  134. DEBI_MIRROR=${DEBI_MIRROR:-deb.debian.org}
  135. DEBI_DIRECTORY=${DEBI_DIRECTORY:-/debian}
  136. if [ -z "$DEBI_ARCHITECTURE" ]; then
  137. DEBI_ARCHITECTURE=$(dpkg --print-architecture)
  138. fi
  139. DEBI_SUITE=${DEBI_SUITE:-stretch}
  140. DEBI_USERNAME=${DEBI_USERNAME:-debian}
  141. DEBI_TIMEZONE=${DEBI_TIMEZONE:-UTC}
  142. DEBI_NTP_SERVER=${DEBI_NTP_SERVER:-pool.ntp.org}
  143. DEBI_UPGRADE=${DEBI_UPGRADE:-full-upgrade}
  144. DEBI_DNS=${DEBI_DNS:-1.1.1.1 1.0.0.1}
  145. DEBI_FILESYSTEM=${DEBI_FILESYSTEM:-ext4}
  146. DEBI_DISK_ENCRYPTION=${DEBI_DISK_ENCRYPTION:-regular}
  147. if [ -z "$DEBI_SECURITY_MIRROR" ]; then
  148. DEBI_SECURITY_MIRROR=http://security.debian.org/debian-security
  149. else
  150. if [ "$DEBI_SECURITY_MIRROR" = true ]; then
  151. DEBI_SECURITY_MIRROR=$DEBI_PROTOCOL://$DEBI_MIRROR${DEBI_DIRECTORY%/*}/debian-security
  152. fi
  153. fi
  154. if [ "$DEBI_MANUAL" != true ]; then
  155. if [ -z "$DEBI_PASSWORD" ]; then
  156. DEBI_PASSWORD=$(mkpasswd -m sha-512)
  157. else
  158. DEBI_PASSWORD=$(mkpasswd -m sha-512 "$DEBI_PASSWORD")
  159. fi
  160. fi
  161. if [ "$DEBI_DRY_RUN" != true ]; then
  162. DEBI_TARGET="debian-$DEBI_SUITE"
  163. if [ "$DEBI_BOOT_PARTITION" = true ]; then
  164. DEBI_BOOT_DIRECTORY=/
  165. else
  166. DEBI_BOOT_DIRECTORY=/boot/
  167. fi
  168. DEBI_WORKDIR="/boot/$DEBI_TARGET"
  169. DEBI_TARGET_PATH="$DEBI_BOOT_DIRECTORY$DEBI_TARGET"
  170. DEBI_BASE_URL=$DEBI_PROTOCOL://$DEBI_MIRROR$DEBI_DIRECTORY/dists/$DEBI_SUITE/main/installer-$DEBI_ARCHITECTURE/current/images/netboot/debian-installer/$DEBI_ARCHITECTURE
  171. if type update-grub >/dev/null; then
  172. update-grub
  173. DEBI_GRUB_CONFIG=/boot/grub/grub.cfg
  174. else
  175. DEBI_GRUB_CONFIG=/boot/grub2/grub.cfg
  176. grub2-mkconfig > "$DEBI_GRUB_CONFIG"
  177. fi
  178. rm -fr "$DEBI_WORKDIR"
  179. mkdir -p "$DEBI_WORKDIR"
  180. cd "$DEBI_WORKDIR"
  181. fi
  182. cat >> preseed.cfg << EOF
  183. # Localization
  184. d-i debian-installer/locale string en_US.UTF-8
  185. d-i keyboard-configuration/xkb-keymap select us
  186. # Network configuration
  187. d-i netcfg/choose_interface select auto
  188. EOF
  189. if [ -n "$DEBI_IP" ]; then
  190. echo "d-i netcfg/disable_autoconfig boolean true" >> preseed.cfg
  191. echo "d-i netcfg/get_ipaddress string $DEBI_IP" >> preseed.cfg
  192. if [ -n "$DEBI_NETMASK" ]; then
  193. echo "d-i netcfg/get_netmask string $DEBI_NETMASK" >> preseed.cfg
  194. fi
  195. if [ -n "$DEBI_GATEWAY" ]; then
  196. echo "d-i netcfg/get_gateway string $DEBI_GATEWAY" >> preseed.cfg
  197. fi
  198. if [ -n "$DEBI_DNS" ]; then
  199. echo "d-i netcfg/get_nameservers string $DEBI_DNS" >> preseed.cfg
  200. fi
  201. echo "d-i netcfg/confirm_static boolean true" >> preseed.cfg
  202. fi
  203. cat >> preseed.cfg << EOF
  204. d-i netcfg/get_hostname string debian
  205. d-i netcfg/get_domain string
  206. EOF
  207. if [ -n "$DEBI_HOSTNAME" ]; then
  208. echo "d-i netcfg/hostname string $DEBI_HOSTNAME" >> preseed.cfg
  209. fi
  210. cat >> preseed.cfg << EOF
  211. d-i hw-detect/load_firmware boolean true
  212. EOF
  213. if [ "$DEBI_SSH" = true ]; then
  214. echo "d-i anna/choose_modules string network-console" >> preseed.cfg
  215. echo "d-i preseed/early_command string anna-install network-console" >> preseed.cfg
  216. if [ -n "$DEBI_SSH_PASSWORD" ]; then
  217. echo "d-i network-console/password password $DEBI_SSH_PASSWORD" >> preseed.cfg
  218. echo "d-i network-console/password-again password $DEBI_SSH_PASSWORD" >> preseed.cfg
  219. fi
  220. if [ -n "$DEBI_SSH_KEYS" ]; then
  221. echo "d-i network-console/authorized_keys_url string $DEBI_SSH_KEYS" >> preseed.cfg
  222. fi
  223. echo "d-i network-console/start select Continue" >> preseed.cfg
  224. fi
  225. cat >> preseed.cfg << EOF
  226. # Mirror settings
  227. d-i mirror/country string manual
  228. d-i mirror/protocol string {{-PROTOCOL-}}
  229. d-i mirror/{{-PROTOCOL-}}/hostname string {{-MIRROR-}}
  230. d-i mirror/{{-PROTOCOL-}}/directory string {{-DIRECTORY-}}
  231. d-i mirror/{{-PROTOCOL-}}/proxy string
  232. d-i mirror/suite string {{-SUITE-}}
  233. d-i mirror/udeb/suite string {{-SUITE-}}
  234. # Clock and time zone setup
  235. d-i clock-setup/utc boolean true
  236. d-i time/zone string {{-TIMEZONE-}}
  237. d-i clock-setup/ntp boolean true
  238. d-i clock-setup/ntp-server string {{-NTP_SERVER-}}
  239. EOF
  240. if [ "$DEBI_MANUAL" != true ]; then
  241. cat >> preseed.cfg << EOF
  242. # User account setup
  243. d-i passwd/root-login boolean false
  244. d-i passwd/user-fullname string
  245. d-i passwd/username string {{-USERNAME-}}
  246. d-i passwd/user-password-crypted password {{-PASSWORD-}}
  247. # Disk partitioning
  248. d-i partman-basicfilesystems/no_swap boolean false
  249. d-i partman/default_filesystem string {{-FILESYSTEM-}}
  250. d-i partman-auto/method string {{-DISK_ENCRYPTION-}}
  251. d-i partman-lvm/device_remove_lvm boolean true
  252. d-i partman-md/device_remove_md boolean true
  253. d-i partman-lvm/confirm boolean true
  254. d-i partman-lvm/confirm_nooverwrite boolean true
  255. EOF
  256. if [ "$DEBI_DISK_ENCRYPTION" = "regular" ]; then
  257. cat >> preseed.cfg << EOF
  258. d-i partman-auto/expert_recipe string naive :: 0 1 -1 \$default_filesystem \$primary{ } \$bootable{ } method{ format } format{ } use_filesystem{ } \$default_filesystem{ } mountpoint{ / } .
  259. d-i partman-auto/choose_recipe select naive
  260. EOF
  261. fi
  262. cat >> preseed.cfg << EOF
  263. d-i partman-partitioning/confirm_write_new_label boolean true
  264. d-i partman/choose_partition select finish
  265. d-i partman/confirm boolean true
  266. d-i partman/confirm_nooverwrite boolean true
  267. d-i partman/mount_style select uuid
  268. EOF
  269. cat >> preseed.cfg << EOF
  270. # Base system installation
  271. d-i base-installer/install-recommends boolean false
  272. # Apt setup
  273. d-i apt-setup/services-select multiselect updates
  274. d-i apt-setup/local0/repository string {{-SECURITY_MIRROR-}} {{-SUITE-}}/updates main
  275. d-i apt-setup/local0/source boolean true
  276. # Package selection
  277. tasksel tasksel/first multiselect ssh-server
  278. EOF
  279. if [ -n "$DEBI_INCLUDE" ]; then
  280. echo "d-i pkgsel/include string $DEBI_INCLUDE" >> preseed.cfg
  281. fi
  282. cat >> preseed.cfg << EOF
  283. d-i pkgsel/upgrade select {{-UPGRADE-}}
  284. popularity-contest popularity-contest/participate boolean false
  285. # Boot loader installation
  286. d-i grub-installer/only_debian boolean true
  287. d-i grub-installer/bootdev string default
  288. # Finishing up the installation
  289. d-i finish-install/reboot_in_progress note
  290. EOF
  291. fi
  292. sed -i 's/{{-PROTOCOL-}}/'"$DEBI_PROTOCOL"'/g' preseed.cfg
  293. sed -i 's/{{-MIRROR-}}/'"$DEBI_MIRROR"'/g' preseed.cfg
  294. sed -i 's/{{-DIRECTORY-}}/'$(echo "$DEBI_DIRECTORY" | sed 's/\//\\\//g')'/g' preseed.cfg
  295. sed -i 's/{{-SUITE-}}/'"$DEBI_SUITE"'/g' preseed.cfg
  296. sed -i 's/{{-USERNAME-}}/'"$DEBI_USERNAME"'/g' preseed.cfg
  297. sed -i 's/{{-PASSWORD-}}/'$(echo "$DEBI_PASSWORD" | sed 's/\//\\\//g')'/g' preseed.cfg
  298. sed -i 's/{{-TIMEZONE-}}/'$(echo "$DEBI_TIMEZONE" | sed 's/\//\\\//g')'/g' preseed.cfg
  299. sed -i 's/{{-NTP_SERVER-}}/'"$DEBI_NTP_SERVER"'/g' preseed.cfg
  300. sed -i 's/{{-SECURITY_MIRROR-}}/'$(echo "$DEBI_SECURITY_MIRROR" | sed 's/\//\\\//g')'/g' preseed.cfg
  301. sed -i 's/{{-UPGRADE-}}/'"$DEBI_UPGRADE"'/g' preseed.cfg
  302. sed -i 's/{{-FILESYSTEM-}}/'"$DEBI_FILESYS"'/g' preseed.cfg
  303. sed -i 's/{{-DISK_ENCRYPTION-}}/'"$DEBI_DISK_ENCRYPTION"'/g' preseed.cfg
  304. if [ "$DEBI_DRYRUN" != true ]; then
  305. wget "$DEBI_BASE_URL/linux" "$DEBI_BASE_URL/initrd.gz"
  306. gunzip initrd.gz
  307. echo preseed.cfg | cpio -H newc -o -A -F initrd
  308. gzip initrd
  309. cat >> "$DEBI_GRUB_CONFIG" << EOF
  310. menuentry 'Debian Installer' --id debi {
  311. insmod part_msdos
  312. insmod ext2
  313. set root='(hd0,msdos1)'
  314. linux $DEBI_TARGET_PATH/linux
  315. initrd $DEBI_TARGET_PATH/initrd.gz
  316. }
  317. EOF
  318. fi