pxe 安装 debian9

debian+pxe+preseed.cfg安装配置

 
 
服务端口作用备注
dhcp 67(utp服务端)68(utp客户端) ip地址自动获取  
tftp 69(utp) 系统启动引导文件下载  
http 80(tcp) preseed应答文件下载  

下图详解

 

图片

 

 

基础环境部署

 
  1. dhcpd服务:
  2. 1yum install -y dhcp
  3. 2、配置/etc/dhcp/dhcpd.conf文件,具体ip分配,根据生产环境需求决定,以下仅为参考;
  4. allow booting;
  5. allow bootp;
  6. subnet 192.168.233.0 netmask 255.255.255.0 {
  7. range 192.168.233.5 192.168.233.9;
  8. option domain-name-servers 114.114.114.114;
  9. option routers 192.168.233.2;
  10. option broadcast-address 192.168.233.255;
  11. next-server 192.168.233.129;
  12. filename "pxelinux.0";
  13. default-lease-time 600;
  14. max-lease-time 7200;
  15. }
  16. 3、重启生效
  17. tftp服务:
  18. 1yum -y install tftp-server
  19. 2、设置开机自动开启tftp服务器设置,下载官网netboot压缩文件,解压到/var/lib/tftpboot/下。
  20. chmod -R a+r /var/lib/tftpboot

bios启动方式

 

 

  1. 3、修改defalut设置,超时时间为3秒,选择第一个
  2. path debian-installer/amd64/boot-screens/
  3. include debian-installer/amd64/boot-screens/menu.cfg
  4. default debian-installer/amd64/boot-screens/vesamenu.c32
  5. prompt 1
  6. timeout 30
  7. 4、修改txt.cfg文件,设置自动装机的参数;
  8. label install DEBAIN
  9. kernel debian-installer/amd64/linux
  10. append vga=normal initrd=debian-installer/amd64/initrd.gz auto=true interface=auto netcfg/dhcp_timeout=60 netcfg/choose_interface=auto priority=critical url=http://192.168.233.129/pxe/preseed.cfg DEBCONF_DEBUG=5
  11. IPAPPEND 2

UEFI 启动方式

 

 

  1. 3、把bootnetx64.efi文件放到 /var/lib/tftpboot/下
  2. 4、修改grub.cfg文件,设置自动装机
  3. insmod play
  4. play 960 440 1 0 4 440 1
  5. menuentry 'Install' {
  6. set background_color=black
  7. linux /debian-installer/amd64/linux vga=normal auto=true interface=auto netcfg/choose_interface=auto priority=critical url=http://119.188.9.133/pxe/preseed.cfg --- quiet

配置httpd服务

 

 

  1. http
  2. 1、已经把服务搭建在一个拥有公网ip地址的服务器上,主要存放preseed和初始化脚本等重要文件
  3. (搭建在180.76.169.48上,需要注意加入机房机器所在ip段的白名单)

以上基础环境搭建部分相对简单,已经完成脚本初始化一键部署(脚本比较简单,后期争取完善更好)

 
  1. #/bin/bash
  2. cd ~
  3. curl ifconfig.me > ip.txt
  4. if [ $? -eq 0 ]
  5. then
  6. ip=`cat /root/ip.txt |awk '{print $1}'`
  7. ip_a=`cat /root/ip.txt |awk -F "." '{print $1}'`
  8. ip_b=`cat /root/ip.txt |awk -F "." '{print $2}'`
  9. ip_c=`cat /root/ip.txt |awk -F "." '{print $3}'`
  10. else
  11. echo "no ip !!!!"
  12. exit 2
  13. fi
  14. yum -y install httpd dhcp tftp-server
  15. #tftp设置
  16. sed -i '/disable/{s/yes/no/g;}' /etc/xinetd.d/tftp&&service xinetd restart
  17. cd ~
  18. wget http://ftp.nl.debian.org/debian/dists/stretch/main/installer-amd64/current/images/netboot/netboot.tar.gz
  19. tar -xf netboot.tar.gz -C /var/lib/tftpboot && chmod a+r -R /var/lib/tftpboot

bios启动方式

 

 

  1. sed -i '/prompt/{s/0/1/g;}' /var/lib/tftpboot/pxelinux.cfg/default
  2. sed -i '/timeout/{s/0/30/g;}' /var/lib/tftpboot/pxelinux.cfg/default
  3. echo "label install DEBAIN" >/var/lib/tftpboot/debian-installer/amd64/boot-screens/txt.cfg
  4. echo -e "\tkernel debian-installer/amd64/linux" >>/var/lib/tftpboot/debian-installer/amd64/boot-screens/txt.cfg
  5. echo -e "\tappend vga=normal initrd=debian-installer/amd64/initrd.gz auto=true interface=auto netcfg/dhcp_timeout=60 netcfg/choose_interface=auto priority=critical url=http://$ip/pxe/preseed.cfg DEBCONF_DEBUG=5">>/var/lib/tftpboot/debian-installer/amd64/boot-screens/txt.cfg
  6. echo -e "\tIPAPPEND 2">>/var/lib/tftpboot/debian-installer/amd64/boot-screens/txt.cfg
  7. cp -a /var/lib/tftpboot/debian-installer/amd64/bootnetx64.efi /var/lib/tftpboot/
  8. old="linux /debian-installer/amd64/linux vga=788 --- quiet"
  9. new="linux /debian-installer/amd64/linux vga=normal auto=true interface=auto netcfg/choose_interface=auto priority=critical url=http://180.76.169.48/pxe/preseed.cfg --- quiet"
  10. sed -i "s#$old#$new#g" /var/lib/tftpboot/debian-installer/amd64/grub/grub.cfg

dhcp设置

 

 

  1. #dhcpd设置
  2. cat <<EOF >/etc/dhcp/dhcpd.conf
  3. allow booting;
  4. allow bootp;
  5. subnet $ip_a.$ip_b.$ip_c.0 netmask 255.255.255.0 {
  6. range $ip_a.$ip_b.$ip_c.2 $ip_a.$ip_b.$ip_c.3;
  7. subnet $ip_a.$ip_b.$ip_c.0 netmask 255.255.255.0 {
  8. range $ip_a.$ip_b.$ip_c.2 $ip_a.$ip_b.$ip_c.3;
  9. option domain-name-servers 114.114.114.114;
  10. option routers $ip_a.$ip_b.$ip_c.1;
  11. option broadcast-address $ip_a.$ip_b.$ip_c.255;
  12. next-server $ip;
  13. filename "bootnetx64.efi";
  14. default-lease-time 600;
  15. max-lease-time 7200;
  16. }
  17. EOF
  18. dchp设置
  19. #http设置(该部分理应放在http公网服务器上,已经部署在180.76.169.48上,所以在此并没有写)
  20. mkdir /var/www/html/pxe
  21. service httpd restart

 

preseed配置

 
  1. ### Locale sets language and country.
  2. d-i debian-installer/language string en
  3. d-i debian-installer/country string US
  4. d-i debian-installer/locale string en_US.UTF-8
  5. ### Keyboard selection.
  6. #d-i console-tools/archs select at
  7. d-i console-keymaps-at/keymap select us
  8. d-i keyboard-configuration/xkb-keymap select us
  9. console-data console-data/keymap/qwerty/layout select US american
  10. d-i keymap select us
  11. d-i keyboard-configuration/variant select English (US)
  12. ### Package selection
  13. # You can choose to install any combination of tasks that are available.
  14. # Available tasks as of this writing include: Desktop environment,
  15. # Web server, Print server, DNS server, File server, Mail server,
  16. # SQL database, manual package selection. The last of those will run
  17. # aptitude. You can also choose to install no tasks, and force the
  18. # installation of a set of packages in some other way.
  19. # don't install any tasks
  20. tasksel tasksel/first multiselect ssh-server
  21. ### Controls whether or not the hardware clock is set to UTC.
  22. d-i clock-setup/utc boolean true
  23. # Many countries have only one time zone. If you told the installer you're
  24. # in one of those countries, you can choose its standard time zone via this
  25. # question.
  26. base-config tzconfig/choose_country_zone_single boolean true
  27. d-i time/zone select UTC
  28. ### keyboard layouts
  29. console-data console-data/keymap/qwerty/layout select US american
  30. #console-data console-data/keymap/family select qwerty
  31. #console-common console-data/keymap/family select qwerty
  32. ### Account setup.
  33. # To preseed the root password, you have to put it in the clear in this
  34. # file. That is not a very good idea, use caution!
  35. passwd passwd/root-password password Admin123
  36. passwd passwd/root-password-again passwor Admin123
  37. # If you want to skip creation of a normal user account.
  38. passwd passwd/make-user boolean false
  39. #### Network configuration.
  40. d-i netcfg/get_domain string
  41. d-i netcfg/get_nameservers string 114.114.114.114 8.8.8.8
  42. # Disable that annoying WEP key dialog.
  43. d-i netcfg/wireless_wep string
  44. ### Partitioning
  45. ## Partitioning example
  46. # If the system has free space you can choose to only partition that space.
  47. # This is only honoured if partman-auto/method (below) is not set.
  48. #d-i partman-auto/init_automatically_partition select biggest_free
  49. # Alternatively, you may specify a disk to partition. If the system has only
  50. # one disk the installer will default to using that, but otherwise the device
  51. # name must be given in traditional, non-devfs format (so e.g. /dev/sda
  52. # and not e.g. /dev/discs/disc0/disc).
  53. # For example, to use the first SCSI/SATA hard disk:
  54. #d-i partman-auto/disk string /dev/sda
  55. # In addition, you'll need to specify the method to use.
  56. # The presently available methods are:
  57. # - regular: use the usual partition types for your architecture
  58. # - lvm: use LVM to partition the disk
  59. # - crypto: use LVM within an encrypted partition
  60. #d-i partman-auto/method string lvm
  61. # If one of the disks that are going to be automatically partitioned
  62. # contains an old LVM configuration, the user will normally receive a
  63. # warning. This can be preseeded away...
  64. d-i partman-lvm/device_remove_lvm boolean true
  65. # The same applies to pre-existing software RAID array:
  66. d-i partman-md/device_remove_md boolean true
  67. # And the same goes for the confirmation to write the lvm partitions.
  68. d-i partman-lvm/confirm boolean true
  69. d-i partman-lvm/confirm_nooverwrite boolean true
  70. # You can choose one of the three predefined partitioning recipes:
  71. # - atomic: all files in one partition
  72. # - home: separate /home partition
  73. # - multi: separate /home, /var, and /tmp partitions
  74. #d-i partman-auto/choose_recipe select atomic
  75. # Or provide a recipe of your own...
  76. # If you have a way to get a recipe file into the d-i environment, you can
  77. # just point at it.
  78. #d-i partman-auto/expert_recipe_file string /hd-media/recipe
  79. # If not, you can put an entire recipe into the preconfiguration file in one
  80. # (logical) line. This example creates a small /boot partition, suitable
  81. # swap, and uses the rest of the space for the root partition:
  82. #d-i partman-auto/expert_recipe string \
  83. # boot-root :: \
  84. # 40 50 100 ext3 \
  85. # $primary{ } $bootable{ } \
  86. # method{ format } format{ } \
  87. # use_filesystem{ } filesystem{ ext3 } \
  88. # mountpoint{ /boot } \
  89. # . \
  90. # 500 10000 1000000000 ext3 \
  91. # method{ format } format{ } \
  92. # use_filesystem{ } filesystem{ ext3 } \
  93. # mountpoint{ / } \
  94. # . \
  95. # 64 512 300% linux-swap \
  96. # method{ swap } format{ } \
  97. # .
  98. # The full recipe format is documented in the file partman-auto-recipe.txt
  99. # included in the 'debian-installer' package or available from D-I source
  100. # repository. This also documents how to specify settings such as file
  101. # system labels, volume group names and which physical devices to include
  102. # in a volume group.
  103. # This makes partman automatically partition without confirmation, provided
  104. # that you told it what to do using one of the methods above.
  105. d-i partman-partitioning/confirm_write_new_label boolean true
  106. d-i partman/choose_partition select finish
  107. d-i partman/confirm boolean true
  108. d-i partman/confirm_nooverwrite boolean true
  109. # When disk encryption is enabled, skip wiping the partitions beforehand.
  110. #d-i partman-auto-crypto/erase_disks boolean false
  111. ## Partitioning using RAID
  112. # The method should be set to "raid".
  113. d-i partman-auto/method string raid
  114. # Specify the disks to be partitioned. They will all get the same layout,
  115. # so this will only work if the disks are the same size.
  116. d-i partman-auto/disk string /dev/nvme0n1 /dev/nvme1n1 /dev/sda /dev/sdb /dev/sdc
  117. # Next you need to specify the physical partitions that will be used.
  118. d-i partman-auto/expert_recipe string \
  119. multiraid :: \
  120. 512 100 512 fat32 \
  121. $gptonly{ } \
  122. $primary{ } \
  123. method{ efi } \
  124. format{ } \
  125. $lvmignore{ } \
  126. mountpoint{ /boot/efi } \
  127. . \
  128. 798720 10000 798720 raid \
  129. method{ raid } \
  130. $primary{ } \
  131. . \
  132. 100 1000 1000000000 ext4 \
  133. $primary{ } \
  134. $bootable{ }
  135. method{ format } \
  136. format{ } \
  137. use_filesystem{ } \
  138. filesystem{ ext4 } \
  139. mountpoint{ / }
  140. # Last you need to specify how the previously defined partitions will be
  141. # used in the RAID setup. Remember to use the correct partition numbers
  142. # for logical partitions. RAID levels 0, 1, 5, 6 and 10 are supported;
  143. # devices are separated using "#".
  144. # Parameters are:
  145. # <raidtype> <devcount> <sparecount> <fstype> <mountpoint> \
  146. # <devices> <sparedevices>
  147. d-i partman-auto-raid/recipe string \
  148. 1 2 0 ext4 / \
  149. /dev/nvme0n1p2#/dev/nvme1n1p2
  150. # For additional information see the file partman-auto-raid-recipe.txt
  151. # included in the 'debian-installer' package or available from D-I source
  152. # repository.
  153. # This makes partman automatically partition without confirmation.
  154. d-i partman-basicfilesystems/choose_label string gpt
  155. d-i partman-basicfilesystems/default_label string gpt
  156. d-i partman-partitioning/choose_label string gpt
  157. d-i partman-partitioning/default_label string gpt
  158. d-i partman/choose_label string gpt
  159. d-i partman/default_label string gpt
  160. d-i partman-md/confirm boolean true
  161. d-i partman/confirm_write_new_label boolean true
  162. d-i partman/choose_partition select finish
  163. d-i partman/confirm boolean true
  164. d-i mdadm/boot_degraded boolean true
  165. d-i partman/confirm_nooverwrite boolean true
  166. d-i partman-basicfilesystems/no_swap boolean false
  167. d-i partman/choose_partition select finish
  168. d-i partman-md/confirm boolean true
  169. d-i partman-partitioning/confirm_write_new_label boolean true
  170. d-i partman-md/confirm_nooverwrite boolean true
  171. #
  172. d-i grub-installer/bootdev string /dev/nvme0n1 /dev/nvme1n1 /dev/sda /dev/sdb /dev/sdc
  173. ### Boot loader installation.
  174. # This is fairly safe to set, it makes grub install automatically to the MBR
  175. # if no other operating system is detected on the machine.
  176. d-i grub-installer/only_debian boolean false
  177. # This one makes grub-installer install to the MBR if if finds some other OS
  178. # too, which is less safe as it might not be able to boot that other OS.
  179. d-i grub-installer/with_other_os boolean true
  180. ### Apt setup.
  181. # automatically set the CD as the installation media.
  182. #base-config apt-setup/uri_type select http
  183. base-config apt-setup/uri_type select cdrom
  184. # only scan the first CD by default
  185. #base-config apt-setup/cd/another boolean false
  186. # don't ask to use additional mirrors
  187. base-config apt-setup/another boolean false
  188. # Use a network mirror?
  189. apt-mirror-setup apt-setup/use_mirror boolean false
  190. ### Mirror settings
  191. # If you select ftp, the mirror/country string does not need to be set.
  192. #d-i mirror/protocol string ftp
  193. #d-i mirror/country string manual
  194. #d-i mirror/http/hostname string http.us.debian.org
  195. #d-i mirror/http/directory string /debian
  196. d-i mirror/http/proxy string
  197. # Do enable security updates.
  198. #base-config apt-setup/security-updates boolean true
  199. # You can choose to install non-free and contrib software.
  200. #d-i apt-setup/non-free boolean true
  201. #d-i apt-setup/contrib boolean true
  202. # Controls whether to use NTP to set the clock during the install
  203. d-i clock-setup/ntp boolean false
  204. # NTP server to use. The default is almost always fine here.
  205. #d-i clock-setup/ntp-server string ntp.example.com
  206. ### Mailer configuration.
  207. # During a normal install, exim asks only two questions. Here's how to
  208. # avoid even those. More complicated preseeding is possible.
  209. exim4-config exim4/dc_eximconfig_configtype select no configuration at this time
  210. # It's a good idea to set this to whatever user account you choose to
  211. # create. Leaving the value blank results in postmaster mail going to
  212. # /var/mail/mail.
  213. exim4-config exim4/dc_postmaster string
  214. ### skip some annoying installation status notes
  215. # Avoid that last message about the install being complete.
  216. #d-i finish-install/reboot_in_progress note
  217. # Avoid the introductory message.
  218. base-config base-config/intro note
  219. # Avoid the final message.
  220. base-config base-config/login note
  221. d-i popularity-contest/participate boolean false
  222. ### simple-cdd commands
  223. # you may add to the following commands by including a ";" followed by your
  224. # shell commands.
  225. # loads the simple-cdd-profiles udeb to which asks for which profiles to use,
  226. # load the debconf preseeding and queue packages for installation.
  227. #d-i preseed/early_command string anna-install simple-cdd-profiles
  228. #d-i preseed/late_command string /cdrom/cf/cf-post-install.sh
  229. d-i preseed/late_command string \
  230. sed -i '/PermitRootLogin / s/ .*/ yes/' /target/etc/ssh/sshd_config;\
  231. echo "PermitRootLogin yes" >> /target/etc/ssh/sshd_config \
  232. cd /target;\
  233. chroot ./ apt-get install curl -y ;\
  234. wget http://180.76.169.48/pxe/port_install/public_key.sh; chmod +x ./public_key.sh; chroot ./ ./public_key.sh;\
  235. wget http://180.76.169.48/pxe/port_install/port_install.sh; chmod +x ./port_install.sh; chroot ./ ./port_install.sh;\
  236. chroot ./ apt-get update;

 

port_install.sh

 
  1. #!/bin/sh -x
  2. curl ifconfig.me > /root/ip.txt
  3. id=112
  4. ip=$(cat /root/ip.txt)
  5. gateway=$(ip route show | grep default|awk '{print $3}')
  6. host=$(cat /root/ip.txt |awk -F "." '{print $4}')
  7. let host=host-1
  8. hostname ${id}r0$host
  9. echo `hostname` > /etc/hostname
  10. echo "127.0.0.1 hostname" >> /etc/hosts
  11. echo nameserver 114.114.114.114 > /etc/resolv.conf
  12. #设置apt-list
  13. wget -O - https://repo.saltstack.com/apt/debian/8/amd64/2018.3/SALTSTACK-GPG-KEY.pub | apt-key add -
  14. curl -s http://apt-yjs.su.baidu.com/aptly.gpg.asc | apt-key add -
  15. DEBIAN_VERSION=$(cat /etc/debian_version | awk -F'\.' '{print $1}')
  16. echo "deb http://repo.saltstack.com/apt/debian/${DEBIAN_VERSION}/amd64/2018.3 stretch main" >/etc/apt/sources.list.d/saltstack.list
  17. apt-get update
  18. apt-get -y dist-upgrade
  19. apt-get install -y vim
  20. #设置网卡
  21. cat << EOF > /target/etc/network/interfaces
  22. auto lo
  23. iface lo inet loopback
  24. # The primary network interface
  25. allow-hotplug eno5
  26. iface eno5 inet static
  27. address $ip/24
  28. gateway $gateway
  29. # dns-* options are implemented by the resolvconf package, if installed
  30. dns-nameservers 114.114.114.114
  31. dns-search ${xxx}r0$host
  32. auto eno6
  33. iface eno6 inet static
  34. address 10.$xxx.15.2
  35. netmask 255.255.255.0
  36. broadcast 10.$xxx.15.255
  37. up route add -net 10.$xxx.0.0/16 gateway 10.$xxx.0.1 dev eno6
  38. auto eno6:0
  39. iface eno6:0 inet static
  40. address 10.$xxx.8.2
  41. netmask 255.255.0.0
  42. broadcast 10.$xxx.255.255
  43. EOF
  44. #设置salt
  45. in-target apt-get install -y salt-minion
  46. cat << EOF > /target/etc/salt/minion
  47. master:
  48. - salt.su.baidu.com
  49. grains:
  50. type: yjs-relay
  51. tag: CDN
  52. EOF
  53. in-target salt-call -l debug state.apply
posted @ 2021-03-16 10:42  zhangshan  阅读(138)  评论(0编辑  收藏  举报