无忧启动论坛

 找回密码
 注册
搜索
系统gho:最纯净好用系统下载站广告联系 微信:wuyouceo QQ:184822951
查看: 8114|回复: 21
打印 上一主题 下一主题

[原创] ipxe菜单 内嵌实现自动判断服务器,外网内网全协议自动判断启动

[复制链接]
跳转到指定楼层
1#
发表于 2021-12-15 14:32:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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 ||
复制代码
2#
发表于 2021-12-15 14:47:35 | 只看该作者
收藏了备用!
回复

使用道具 举报

3#
发表于 2021-12-15 15:14:30 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

4#
发表于 2021-12-15 15:38:36 | 只看该作者
本帖最后由 bfgxp 于 2021-12-15 15:52 编辑

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

使用道具 举报

5#
发表于 2021-12-15 16:30:48 | 只看该作者
感觉直接exit  还是慢了点
回复

使用道具 举报

6#
发表于 2021-12-15 17:18:38 | 只看该作者
谢谢分享呀
回复

使用道具 举报

7#
发表于 2021-12-15 17:54:37 | 只看该作者
谢谢分享。
回复

使用道具 举报

8#
发表于 2021-12-15 22:19:57 | 只看该作者

谢谢分享
回复

使用道具 举报

9#
发表于 2021-12-31 01:01:32 | 只看该作者
请楼主搞个模板出来给大家下载,小白们可以依葫芦画瓢。
回复

使用道具 举报

10#
发表于 2021-12-31 13:12:36 | 只看该作者
启动竟然也要这么多的代码!
回复

使用道具 举报

11#
发表于 2022-2-26 09:41:44 | 只看该作者
自动判断,自动识别就是好。
回复

使用道具 举报

12#
发表于 2022-3-8 00:03:37 | 只看该作者
想再了解一下用互联网网启的技术

点评

也想了解。一起学习一下。  发表于 2022-3-8 22:01
回复

使用道具 举报

13#
发表于 2022-8-27 12:06:57 | 只看该作者
感谢分享
回复

使用道具 举报

14#
发表于 2022-8-27 13:45:39 | 只看该作者
支持一下,一个菜单这么多,佩服。我是全部都分开的,一个个的小菜单,对应相应的场景
回复

使用道具 举报

15#
发表于 2022-8-28 10:35:28 | 只看该作者

收藏了备用!
回复

使用道具 举报

16#
发表于 2023-9-19 22:49:01 | 只看该作者
想实现外网的网启
回复

使用道具 举报

17#
发表于 2024-1-31 18:40:04 | 只看该作者
学习了
回复

使用道具 举报

18#
发表于 2024-1-31 18:40:26 | 只看该作者

学习了
回复

使用道具 举报

19#
发表于 2024-2-5 11:18:47 | 只看该作者
回复

使用道具 举报

20#
发表于 2024-2-5 11:18:59 | 只看该作者
回复

使用道具 举报

21#
发表于 2024-2-20 14:49:29 | 只看该作者
谢谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1 )

闽公网安备 35020302032614号

GMT+8, 2024-4-29 21:22

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表