无忧启动论坛

标题: ipxe菜单 内嵌实现自动判断服务器,外网内网全协议自动判断启动 [打印本页]

作者: rose4cat    时间: 2021-12-15 14:32
标题: ipxe菜单 内嵌实现自动判断服务器,外网内网全协议自动判断启动
本帖最后由 rose4cat 于 2021-12-22 18:22 编辑

支持内网外网启动 兼容bios efi平台, http nfs ftp tftp ...
服务器上目录结构/主目录
\ipxe\ipxe.pxe undionly.kpxe snponly.efi,ipxe.efi...
\ipxe\bootstrap\default
\ipxe\bootstrap\mac\*.ipxe
\ipxe\bootstrap\uuid\*.ipxe
\ipxe\bootstrap\chip\*.ipxe
\ipxe\bootstrap\pci\*.ipxe
\ipxe\bootstrap\bus\*.ipxe

代码实现:

  1. #!ipxe

  2. :code_begin #  Note 2021 by zulinfi
  3. prompt --key 0x02 --timeout 100 Press Ctrl-B for the iPXE shell...${filename} && shell ||

  4. # 全变量设置 web site:  ddns aaa.vicp.net bbb.vicp.net ccc.eicp.net and port map web: 80>88 ftp: 21>2121
  5. set pxeFileName ipxe/bootstrap/default
  6. isset ${default_webServerPort} || set default_webServerPort 80
  7. isset ${default_ftpServerpath} || set default_ftpServerpath 21
  8. isset ${default_nfsServerpath} || set default_nfsServerpath /volume2/pxe

  9. isset ${diyWebServerPort} || set diyWebServerPort 88
  10. isset ${diyFtpServerPort} || set diyFtpServerPort 2121
  11. isset ${diyNfsServerpath} || set diyNfsServerpath /volume1/pxe

  12. set totalWanSvr:int32 1 #外网服务器个数
  13. set web_pxe_server0${totalWanSvr} aaa.vicp.net && inc totalWanSvr ||
  14. set web_pxe_server0${totalWanSvr} bbb.vicp.net && inc totalWanSvr ||
  15. set web_pxe_server0${totalWanSvr} ccc.eicp.net #&& inc totalWanSvr ||

  16. isset ${diy_lanPxeServer} || set diy_lanPxeServer 192.168.1.8 #备选服务器ip
  17. # set 66:ipv4 192.168.1.8
  18. set lanSvr_idx:int32 1
  19. set wanSvr_idx:int32 1
  20. # set keep-san 1

  21. :retry_dhcp
  22. ifopen
  23. isset ${ip} || dhcp ||
  24. # buildarch:  i386 = bios , x86_64 efi = 64-bit
  25. # platform:  pcbios, efi
  26. # iseq ${buildarch} x86_64 && set arch x86_64 || set arch i386 ||
  27. iseq ${platform} efi && set platformStart efi_start || set platformStart pcbios_start ||
  28. cpuid --ext 29         && set arch x86_64         || set arch i386 ||
  29. cpuid 6 && set pae yes ||
  30. cpuid --ecx 5 && set vmx yes ||
  31. cpuid --ext --ecx 2 && set svm yes ||
  32. isset ${vmx} || isset ${svm} && set hw_virt yes ||

  33. :set_Escape_char
  34. set _:hex 2d         && set _ ${_:string} # ANSI "-" screen sequence - 2d
  35. set colon:hex 3a        && set colon ${colon:string} # ANSI ":" screen sequence - 3a
  36. set mark:hex 23         && set mark ${mark:string} # ANSI "#" screen sequence - 23
  37. set space:hex 20:20     && set space ${space:string} # ANSI " " screen sequence - 20
  38. set lf:hex 0a           && set lf ${lf:string} # ANSI "lf:" screen sequence - 0a
  39. set cr:hex 0d           && set cr ${cr:string} # ANSI "cr:" screen sequence - 0d
  40. set cls:hex 1b:5b:4a    && set cls ${cls:string} # ANSI clear screen sequence - "^[[J"

  41. # 显示常用变量开关
  42. # clear debug_timeout || # isset ${debug_timeout} || set debug_timeout 9000 #调试开关
  43. isset ${menu-timeout} || set menu-timeout 3000
  44. isset ${visableSystemVal} || set visableSystemVal:int32 0
  45. # Note 确定网络启动服务器,并设置网络启动传输协议,本着先内网dhcp服务器->自定服务器->外网服务器, 协议本着先http协议->nfs->ftp->tftp
  46. isset ${debug_timeout} && prompt --key 0x02 --timeout ${debug_timeout} filename: ${filename} ready get next-server and boot protocol. ||
  47. isset ${boot_url} && goto pxeServer_defaultMenu ||
  48. isset ${boot_server} && goto setBootURL ||
  49. prompt --key 0x17 --timeout 500 Press Ctrl-W from WWW boot... && goto tryWanBootProtocol ||
  50. isset ${next-server} && initrd --name default --timeout 1000 tftp://${next-server}/${pxeFileName} && goto setLanBootProtocol ||
  51. isset ${netX/gateway} && initrd --name default --timeout 1000 tftp://${netX/gateway}/${pxeFileName} && set next-server ${netX/gateway} && goto setLanBootProtocol ||
  52. isset ${diy_lanPxeServer} && initrd --name default --timeout 1000 tftp://${diy_lanPxeServer}/${pxeFileName} && set next-server ${diy_lanPxeServer} && goto setLanBootProtocol ||
  53. isset ${next-server} || goto tryWanBootProtocol ||
  54. :setLanBootProtocol
  55. initrd --name default --timeout 2000 http://${next-server}${colon}${default_webServerPort}/${pxeFileName} && set boot_protocol http && set server_port ${colon}${default_webServerPort} && goto setBootURL ||
  56. initrd --name default --timeout 2000 nfs://${next-server}${default_nfsServerpath}/${pxeFileName} && set boot_protocol nfs && set server_port ${default_nfsServerpath} && goto setBootURL ||
  57. initrd --name default --timeout 2000 ftp://${next-server}${colon}${default_ftpServerpath}/${pxeFileName} && set boot_protocol ftp && set server_port ${colon}${default_ftpServerpath} && goto setBootURL ||
  58. initrd --name default --timeout 1000 tftp://${next-server}/${pxeFileName} && set boot_protocol tftp && clear server_port && goto setBootURL ||
  59. iseq ${lanSvr_idx} 2 && goto tryWanBootProtocol || inc lanSvr_idx || # 如果以上全部失败则变更默认端口重试一次
  60. iseq ${default_webServerPort} 80                         && set default_webServerPort ${diyWebServerPort} || set default_webServerPort 80 ||
  61. iseq ${default_ftpServerpath} 21                         && set default_ftpServerpath ${diyFtpServerPort} || set default_ftpServerpath 21 ||
  62. iseq ${default_nfsServerpath} /volume2/pxe         && set default_nfsServerpath ${diyNfsServerpath} || set default_nfsServerpath /volume2/pxe ||
  63. goto setLanBootProtocol ||

  64. :tryWanBootProtocol
  65. set default_webServerPort 80 ||
  66. set default_ftpServerpath 21 ||
  67. set default_nfsServerpath /volume2/pxe ||
  68. :setWanBootProtocol  #本地无启动文件则远程尝试Web启动,网络传输协议 http>ftp>nfs
  69. isset ${wanSvr_idx} || set wanSvr_idx:int32 1 ||
  70. initrd --timeout 2000 http://${web_pxe_server0${wanSvr_idx}}${colon}${default_webServerPort}/${pxeFileName} && set boot_server ${web_pxe_server0${wanSvr_idx}} && set wanBoot y && set boot_protocol http && set server_port ${colon}${default_webServerPort} && goto setBootURL ||
  71. initrd --timeout 2000 ftp://${web_pxe_server0${wanSvr_idx}}${colon}${default_ftpServerpath}/${pxeFileName} && set boot_server ${web_pxe_server0${wanSvr_idx}} && set wanBoot y && set boot_protocol  ftp && set server_port ${colon}${default_ftpServerpath} && goto setBootURL ||
  72. initrd --timeout 2000 nfs://${web_pxe_server0${wanSvr_idx}}${default_nfsServerpath}/${pxeFileName} && set boot_server ${web_pxe_server0${wanSvr_idx}} && set wanBoot y && set boot_protocol  ftp && set server_port ${default_nfsServerpath} && goto setBootURL ||
  73. iseq ${wanSvr_idx} ${totalWanSvr} && goto diyServerPort ||
  74. isset ${debug_timeout} && prompt --timeout ${debug_timeout} wanSvr_idx: ${wanSvr_idx} ${space} totalWanSvr: ${totalWanSvr} ||
  75. inc wanSvr_idx 1 && goto setWanBootProtocol ||

  76. :diyServerPort
  77. iseq ${diyPortTryed} y && goto noAnyServer || set diyPortTryed y ||
  78. clear wanSvr_idx
  79. iseq ${default_webServerPort} 80 && set default_webServerPort ${diyWebServerPort} || set default_webServerPort 80 ||
  80. iseq ${default_ftpServerpath} 21 && set default_ftpServerpath ${diyFtpServerPort} || set default_ftpServerpath 21 ||
  81. iseq ${default_nfsServerpath} /volume2/pxe && set default_nfsServerpath ${diyNfsServerpath} || set default_nfsServerpath /volume2/pxe ||
  82. goto setWanBootProtocol ||

  83. :noAnyServer
  84. isset ${debug_timeout} && prompt --key 0x02 --timeout ${debug_timeout} Error! No Cloud server Online. Server use gateway. Press Ctrl-B for the iPXE shell. && shell ||
  85. set boot_protocol tftp && clear server_port ||
  86. set next-server ${netX/gateway} ||
  87. set boot_server ${netX/gateway} ||

  88. :setBootURL #确定设置网络启动传输协议
  89. iseq ${wanBoot} y && set prefixHostname wanPC || set prefixHostname lanPC ||
  90. iseq ${wanBoot} y || set boot_server ${next-server} ||
  91. set boot_url ${boot_protocol}://${boot_server}${server_port} ||
  92. :code_end

  93. :code_begin #  Note 取本机iP地址未位数关赋值本地名称

  94. :start
  95. set hostIP ${netX/ip} ||
  96. set returnMenuPoint getLastIpend && goto Function_getLastIp ||
  97. :getLastIpend
  98. set hostname ${prefixHostname}${Function_getLastIp} ||
  99. :code_end

  100. :code_begin #  Note 启动后自动取服务器上的如下菜单顺序尝试加载启动

  101. isset ${debug_timeout} && prompt --timeout ${debug_timeout} filename: ${filename} begin loading order .ipxe by hostname\uuid\mac\bus\default ||
  102. ##############ipxe/bootstrap/文件夹下依次先后:###############
  103. #               1.->hostname-${hostname}.ipxe                                 #
  104. #               2.->uuid-${uuid}.ipxe                                         #
  105. #               3.->mac-${mac:hexhyp}.ipxe                                    #
  106. #               4.->bus-${busid:hexhyp}.ipxe                                  #
  107. #               5.->default                                                                        #
  108. #################default文件为最后通用菜单###################
  109. set boot_ipxe_url ${boot_url}/ipxe # set basedir
  110. set bootstrap_url ${boot_ipxe_url}/bootstrap
  111. set defaultMenuFile ${bootstrap_url}/default
  112. isset ${ServerMenuLoaded} && goto pxeServer_defaultMenu || #如果加载过服务器上的default菜单文件,则跳过再次加载机器特征ipxe与default菜单
  113. set ServerMenuLoaded y ||
  114. # if hostname DHCP variable is set and script is present
  115. chain --autofree --timeout 2000 ${bootstrap_url}/hostname/${hostname}.ipxe ||
  116. # if uuid SMBIOS variable is set and script is present
  117. chain --autofree --timeout 2000 ${bootstrap_url}/uuid/${uuid}.ipxe ||
  118. # Boot <bootstrap_url>/mac/01-02-03-04-05-06.ipxe if script is present 这儿可以设置静态ip地址mac绑定
  119. chain --autofree --timeout 2000 ${bootstrap_url}/mac/${mac:hexhyp}.ipxe ||
  120. # Boot <bootstrap_url>/bus/01-80-86-10-0e.ipxe if PCI Intel adapter is present and script is present
  121. chain --autofree --timeout 2000 ${bootstrap_url}/bus/${busid:hexhyp}.ipxe ||
  122. # Boot ${bootstrap_url}/pci/8086100e.ipxe if one type  PCI Intel adapter is present and script is present
  123. # chain --autofree --name default --timeout 2000 ${bootstrap_url}/pci/${pci/${busloc}.0.2}${pci/${busloc}.2.2}.ipxe ||
  124. # Boot ${bootstrap_url}/chip/82541pi.ipxe if one type of PCI Intel adapter is present and script is present
  125. # chain --autofree --name default --timeout 2000 ${bootstrap_url}/chip/${chip}.ipxe ||
  126. imgfree
  127. chain --replace --timeout 2000 ${bootstrap_url}/default && imgstat && prompt begin to exit svrDefault && exit || goto serverNotReady ||
  128. # --replace chain加载的映像/脚本即为最终映像,完成后退出系统。不加此选项类似call命令,结束chain调用后从此映像/脚本断点后面继续执行。
  129. :serverNotReady
  130. isset ${debug_timeout} && prompt --timeout ${debug_timeout} Not found bootfile or server Not Ready ! Press any key enter system menu.  ||
  131. clear ServerMenuLoaded && goto Function_topMenuShow || #设置已尝试机器特征菜单 失败后只显示本地相关功能菜单

  132. # 内建菜单结束 外置菜单default开始
  133. :pxeServer_defaultMenu
  134. isset ${debug_timeout} && prompt --timeout ${debug_timeout} Ready ! Press any key enter system.  ||
  135. isset ${debug_timeout} && prompt --key 0x02 --timeout ${debug_timeout} Begin load bootServer ipxemenu and set var Press Ctrl-B for the iPXE shell. && shell ||
  136. :code_end

  137. #chain --autofree ${bootstrap_url}/default && exit || 以下代码略
  138. #chain --autofree ${bootstrap_url}/init.ipxe && exit ||
复制代码

作者: fd1688    时间: 2021-12-15 14:47
收藏了备用!
作者: 马路大    时间: 2021-12-15 15:14
提示: 作者被禁止或删除 内容自动屏蔽
作者: bfgxp    时间: 2021-12-15 15:38
本帖最后由 bfgxp 于 2021-12-15 15:52 编辑

向楼主请教个问题,ipxe的菜单,如何书写命令来启动本地磁盘现有系统。
或者是说默认从硬盘启动。已经找到了答案,直接exit

作者: xiaoy    时间: 2021-12-15 16:30
感觉直接exit  还是慢了点

作者: lanyanlei9507    时间: 2021-12-15 17:18
谢谢分享呀
作者: 旁观者清    时间: 2021-12-15 17:54
谢谢分享。
作者: test2333    时间: 2021-12-15 22:19

谢谢分享
作者: tigerhuang    时间: 2021-12-31 01:01
请楼主搞个模板出来给大家下载,小白们可以依葫芦画瓢。
作者: juyz    时间: 2021-12-31 13:12
启动竟然也要这么多的代码!
作者: lusir401    时间: 2022-2-26 09:41
自动判断,自动识别就是好。
作者: 2012droopy    时间: 2022-3-8 00:03
想再了解一下用互联网网启的技术
作者: mlc8675    时间: 2022-8-27 12:06
感谢分享
作者: 友联电脑    时间: 2022-8-27 13:45
支持一下,一个菜单这么多,佩服。我是全部都分开的,一个个的小菜单,对应相应的场景
作者: mlc8675    时间: 2022-8-28 10:35

收藏了备用!
作者: xcj138    时间: 2023-9-19 22:49
想实现外网的网启
作者: Duane98    时间: 2024-1-31 18:40
学习了
作者: Duane98    时间: 2024-1-31 18:40

学习了
作者: Duane98    时间: 2024-2-5 11:18

作者: Duane98    时间: 2024-2-5 11:18

作者: wang1126    时间: 2024-2-20 14:49
谢谢分享




欢迎光临 无忧启动论坛 (http://bbs.c3.wuyou.net/) Powered by Discuz! X3.3