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