github.hscsec.cn/u-root/u-root@v7.0.0+incompatible/cmds/boot/pxeboot/pxeboot.go (about) 1 // Copyright 2017-2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Command pxeboot implements PXE-based booting. 6 // 7 // pxeboot combines a DHCP client with a TFTP/HTTP client to download files as 8 // well as pxelinux and iPXE configuration file parsing. 9 // 10 // PXE-based booting requests a DHCP lease, and looks at the BootFileName and 11 // ServerName options (which may be embedded in the original BOOTP message, or 12 // as option codes) to find something to boot. 13 // 14 // This BootFileName may point to 15 // 16 // - an iPXE script beginning with #!ipxe 17 // 18 // - a pxelinux.0, in which case we will ignore the pxelinux and try to parse 19 // pxelinux.cfg/<files> 20 package main 21 22 import ( 23 "context" 24 "flag" 25 "fmt" 26 "log" 27 "time" 28 29 "github.com/u-root/u-root/pkg/boot" 30 "github.com/u-root/u-root/pkg/boot/bootcmd" 31 "github.com/u-root/u-root/pkg/boot/menu" 32 "github.com/u-root/u-root/pkg/boot/netboot" 33 "github.com/u-root/u-root/pkg/curl" 34 "github.com/u-root/u-root/pkg/dhclient" 35 "github.com/u-root/u-root/pkg/ulog" 36 ) 37 38 var ( 39 ifName = "^e.*" 40 noLoad = flag.Bool("no-load", false, "get DHCP response, print chosen boot configuration, but do not download + exec it") 41 noExec = flag.Bool("no-exec", false, "download boot configuration, but do not exec it") 42 noNetConfig = flag.Bool("no-net-config", false, "get DHCP response, but do not apply the network config it to the kernel interface") 43 verbose = flag.Bool("v", false, "Verbose output") 44 ) 45 46 const ( 47 dhcpTimeout = 5 * time.Second 48 dhcpTries = 3 49 ) 50 51 // NetbootImages requests DHCP on every ifaceNames interface, and parses 52 // netboot images from the DHCP leases. Returns bootable OSes. 53 func NetbootImages(ifaceNames string) ([]boot.OSImage, error) { 54 filteredIfs, err := dhclient.Interfaces(ifaceNames) 55 if err != nil { 56 return nil, err 57 } 58 59 ctx, cancel := context.WithTimeout(context.Background(), (1<<dhcpTries)*dhcpTimeout) 60 defer cancel() 61 62 c := dhclient.Config{ 63 Timeout: dhcpTimeout, 64 Retries: dhcpTries, 65 } 66 if *verbose { 67 c.LogLevel = dhclient.LogSummary 68 } 69 r := dhclient.SendRequests(ctx, filteredIfs, true, true, c, 30*time.Second) 70 71 for { 72 select { 73 case <-ctx.Done(): 74 return nil, ctx.Err() 75 76 case result, ok := <-r: 77 if !ok { 78 return nil, fmt.Errorf("nothing bootable found, all interfaces are configured or timed out") 79 } 80 iname := result.Interface.Attrs().Name 81 if result.Err != nil { 82 log.Printf("Could not configure %s for %s: %v", iname, result.Protocol, result.Err) 83 continue 84 } 85 86 if *noNetConfig { 87 log.Printf("Skipping configuring %s with lease %s", iname, result.Lease) 88 } else if err := result.Lease.Configure(); err != nil { 89 log.Printf("Failed to configure lease %s: %v", result.Lease, err) 90 // Boot further regardless of lease configuration result. 91 // 92 // If lease failed, fall back to use locally configured 93 // ip/ipv6 address. 94 } 95 96 // Don't use the other context, as it's for the DHCP timeout. 97 imgs, err := netboot.BootImages(context.Background(), ulog.Log, curl.DefaultSchemes, result.Lease) 98 if err != nil { 99 log.Printf("Failed to boot lease %v: %v", result.Lease, err) 100 continue 101 } 102 return imgs, nil 103 } 104 } 105 } 106 107 func main() { 108 flag.Parse() 109 if len(flag.Args()) > 1 { 110 log.Fatalf("Only one regexp-style argument is allowed, e.g.: " + ifName) 111 } 112 if len(flag.Args()) > 0 { 113 ifName = flag.Args()[0] 114 } 115 116 images, err := NetbootImages(ifName) 117 if err != nil { 118 log.Printf("Netboot failed: %v", err) 119 } 120 121 menuEntries := menu.OSImages(*verbose, images...) 122 menuEntries = append(menuEntries, menu.Reboot{}) 123 menuEntries = append(menuEntries, menu.StartShell{}) 124 125 // Boot does not return. 126 bootcmd.ShowMenuAndBoot(menuEntries, nil, *noLoad, *noExec) 127 }