github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+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 ipv4 = flag.Bool("ipv4", true, "use IPV4") 45 ipv6 = flag.Bool("ipv6", true, "use IPV6") 46 ) 47 48 const ( 49 dhcpTimeout = 5 * time.Second 50 dhcpTries = 3 51 ) 52 53 // NetbootImages requests DHCP on every ifaceNames interface, and parses 54 // netboot images from the DHCP leases. Returns bootable OSes. 55 func NetbootImages(ifaceNames string) ([]boot.OSImage, error) { 56 filteredIfs, err := dhclient.Interfaces(ifaceNames) 57 if err != nil { 58 return nil, err 59 } 60 61 ctx, cancel := context.WithTimeout(context.Background(), (1<<dhcpTries)*dhcpTimeout) 62 defer cancel() 63 64 c := dhclient.Config{ 65 Timeout: dhcpTimeout, 66 Retries: dhcpTries, 67 } 68 if *verbose { 69 c.LogLevel = dhclient.LogSummary 70 } 71 r := dhclient.SendRequests(ctx, filteredIfs, *ipv4, *ipv6, c, 30*time.Second) 72 73 for { 74 select { 75 case <-ctx.Done(): 76 return nil, ctx.Err() 77 78 case result, ok := <-r: 79 if !ok { 80 return nil, fmt.Errorf("nothing bootable found, all interfaces are configured or timed out") 81 } 82 iname := result.Interface.Attrs().Name 83 if result.Err != nil { 84 log.Printf("Could not configure %s for %s: %v", iname, result.Protocol, result.Err) 85 continue 86 } 87 88 if *noNetConfig { 89 log.Printf("Skipping configuring %s with lease %s", iname, result.Lease) 90 } else if err := result.Lease.Configure(); err != nil { 91 log.Printf("Failed to configure lease %s: %v", result.Lease, err) 92 // Boot further regardless of lease configuration result. 93 // 94 // If lease failed, fall back to use locally configured 95 // ip/ipv6 address. 96 } 97 98 // Don't use the other context, as it's for the DHCP timeout. 99 imgs, err := netboot.BootImages(context.Background(), ulog.Log, curl.DefaultSchemes, result.Lease) 100 if err != nil { 101 log.Printf("Failed to boot lease %v: %v", result.Lease, err) 102 continue 103 } 104 return imgs, nil 105 } 106 } 107 } 108 109 func main() { 110 flag.Parse() 111 if len(flag.Args()) > 1 { 112 log.Fatalf("Only one regexp-style argument is allowed, e.g.: " + ifName) 113 } 114 if len(flag.Args()) > 0 { 115 ifName = flag.Args()[0] 116 } 117 118 images, err := NetbootImages(ifName) 119 if err != nil { 120 log.Printf("Netboot failed: %v", err) 121 } 122 123 menuEntries := menu.OSImages(*verbose, images...) 124 menuEntries = append(menuEntries, menu.Reboot{}) 125 menuEntries = append(menuEntries, menu.StartShell{}) 126 127 // Boot does not return. 128 bootcmd.ShowMenuAndBoot(menuEntries, nil, *noLoad, *noExec) 129 }