github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/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/dhclient" 31 "github.com/u-root/u-root/pkg/netboot" 32 "github.com/u-root/u-root/pkg/urlfetch" 33 ) 34 35 var ( 36 ifName = "^e.*" 37 noLoad = flag.Bool("no-load", false, "get DHCP response, but don't load the kernel") 38 dryRun = flag.Bool("dry-run", false, "download kernel, but don't kexec it") 39 verbose = flag.Bool("v", false, "Verbose output") 40 ) 41 42 const ( 43 dhcpTimeout = 5 * time.Second 44 dhcpTries = 3 45 ) 46 47 // Netboot boots all interfaces matched by the regex in ifaceNames. 48 func Netboot(ifaceNames string) error { 49 filteredIfs, err := dhclient.Interfaces(ifaceNames) 50 if err != nil { 51 return err 52 } 53 54 ctx, cancel := context.WithTimeout(context.Background(), (1<<dhcpTries)*dhcpTimeout) 55 defer cancel() 56 57 c := dhclient.Config{ 58 Timeout: dhcpTimeout, 59 Retries: dhcpTries, 60 } 61 if *verbose { 62 c.LogLevel = dhclient.LogSummary 63 } 64 r := dhclient.SendRequests(ctx, filteredIfs, true, true, c) 65 66 for { 67 select { 68 case <-ctx.Done(): 69 return ctx.Err() 70 71 case result, ok := <-r: 72 if !ok { 73 log.Printf("Configured all interfaces.") 74 return fmt.Errorf("nothing bootable found") 75 } 76 if result.Err != nil { 77 continue 78 } 79 80 if err := result.Lease.Configure(); err != nil { 81 log.Printf("Failed to configure lease %s: %v", result.Lease, err) 82 // Boot further regardless of lease configuration result. 83 // 84 // If lease failed, fall back to use locally configured 85 // ip/ipv6 address. 86 } 87 img, err := netboot.BootImage(urlfetch.DefaultSchemes, result.Lease) 88 if err != nil { 89 log.Printf("Failed to boot lease %v: %v", result.Lease, err) 90 continue 91 } 92 93 // Cancel other DHCP requests in flight. 94 cancel() 95 log.Printf("Got configuration: %s", img) 96 97 if *noLoad { 98 return nil 99 } 100 if err := img.Load(*dryRun); err != nil { 101 return fmt.Errorf("kexec load of %v failed: %v", img, err) 102 } 103 if *dryRun { 104 return nil 105 } 106 if err := boot.Execute(); err != nil { 107 return fmt.Errorf("kexec of %v failed: %v", img, err) 108 } 109 110 // Kexec should either return an error or not return. 111 panic("unreachable") 112 } 113 } 114 } 115 116 func main() { 117 flag.Parse() 118 if len(flag.Args()) > 1 { 119 log.Fatalf("Only one regexp-style argument is allowed, e.g.: " + ifName) 120 } 121 122 if len(flag.Args()) > 0 { 123 ifName = flag.Args()[0] 124 } 125 126 if err := Netboot(ifName); err != nil { 127 log.Fatal(err) 128 } 129 }