github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/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  	"net"
    28  	"strings"
    29  	"time"
    30  
    31  	"github.com/mvdan/u-root-coreutils/pkg/boot"
    32  	"github.com/mvdan/u-root-coreutils/pkg/boot/bootcmd"
    33  	"github.com/mvdan/u-root-coreutils/pkg/boot/menu"
    34  	"github.com/mvdan/u-root-coreutils/pkg/boot/netboot"
    35  	"github.com/mvdan/u-root-coreutils/pkg/curl"
    36  	"github.com/mvdan/u-root-coreutils/pkg/dhclient"
    37  	"github.com/mvdan/u-root-coreutils/pkg/sh"
    38  	"github.com/mvdan/u-root-coreutils/pkg/ulog"
    39  
    40  	"github.com/insomniacslk/dhcp/dhcpv4"
    41  )
    42  
    43  var (
    44  	ifName      = "^e.*"
    45  	noLoad      = flag.Bool("no-load", false, "get DHCP response, print chosen boot configuration, but do not download + exec it")
    46  	noExec      = flag.Bool("no-exec", false, "download boot configuration, but do not exec it")
    47  	noNetConfig = flag.Bool("no-net-config", false, "get DHCP response, but do not apply the network config it to the kernel interface")
    48  	skipBonded  = flag.Bool("skip-bonded", false, "Skip NICs that have already been added to a bond")
    49  	verbose     = flag.Bool("v", false, "Verbose output")
    50  	ipv4        = flag.Bool("ipv4", true, "use IPV4")
    51  	ipv6        = flag.Bool("ipv6", true, "use IPV6")
    52  	cmdAppend   = flag.String("cmd", "", "Kernel command to append for each image")
    53  	bootfile    = flag.String("file", "", "Boot file name (default tftp) or full URI to use instead of DHCP.")
    54  	server      = flag.String("server", "0.0.0.0", "Server IP (Requires -file for effect)")
    55  )
    56  
    57  const (
    58  	dhcpTimeout = 5 * time.Second
    59  	dhcpTries   = 3
    60  )
    61  
    62  // NetbootImages requests DHCP on every ifaceNames interface, and parses
    63  // netboot images from the DHCP leases. Returns bootable OSes.
    64  func NetbootImages(ifaceNames string) ([]boot.OSImage, error) {
    65  	filteredIfs, err := dhclient.Interfaces(ifaceNames)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	if *skipBonded {
    71  		filteredIfs = dhclient.FilterBondedInterfaces(filteredIfs, *verbose)
    72  	}
    73  
    74  	ctx, cancel := context.WithTimeout(context.Background(), (1<<dhcpTries)*dhcpTimeout)
    75  	defer cancel()
    76  
    77  	c := dhclient.Config{
    78  		Timeout: dhcpTimeout,
    79  		Retries: dhcpTries,
    80  	}
    81  	if *verbose {
    82  		c.LogLevel = dhclient.LogSummary
    83  	}
    84  	r := dhclient.SendRequests(ctx, filteredIfs, *ipv4, *ipv6, c, 30*time.Second)
    85  
    86  	for {
    87  		select {
    88  		case <-ctx.Done():
    89  			return nil, ctx.Err()
    90  
    91  		case result, ok := <-r:
    92  			if !ok {
    93  				return nil, fmt.Errorf("nothing bootable found, all interfaces are configured or timed out")
    94  			}
    95  			iname := result.Interface.Attrs().Name
    96  			if result.Err != nil {
    97  				log.Printf("Could not configure %s for %s: %v", iname, result.Protocol, result.Err)
    98  				continue
    99  			}
   100  
   101  			if *noNetConfig {
   102  				log.Printf("Skipping configuring %s with lease %s", iname, result.Lease)
   103  			} else if err := result.Lease.Configure(); err != nil {
   104  				log.Printf("Failed to configure lease %s: %v", result.Lease, err)
   105  				// Boot further regardless of lease configuration result.
   106  				//
   107  				// If lease failed, fall back to use locally configured
   108  				// ip/ipv6 address.
   109  			}
   110  
   111  			// Don't use the other context, as it's for the DHCP timeout.
   112  			imgs, err := netboot.BootImages(context.Background(), ulog.Log, curl.DefaultSchemes, result.Lease)
   113  			if err != nil {
   114  				log.Printf("Failed to boot lease %v: %v", result.Lease, err)
   115  				continue
   116  			}
   117  
   118  			return imgs, nil
   119  		}
   120  	}
   121  }
   122  
   123  func newManualLease() (dhclient.Lease, error) {
   124  	filteredIfs, err := dhclient.Interfaces(ifName)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	d, err := dhcpv4.New()
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  
   134  	d.BootFileName = *bootfile
   135  	d.ServerIPAddr = net.ParseIP(*server)
   136  
   137  	return dhclient.NewPacket4(filteredIfs[0], d), nil
   138  }
   139  
   140  func dumpNetDebugInfo() {
   141  	log.Println("Dump debug info of network status")
   142  	commands := []string{"ip link", "ip addr", "ip route show table all", "ip -6 route show table all", "ip neigh"}
   143  	for _, cmd := range commands {
   144  		cmds := strings.Split(cmd, " ")
   145  		name := cmds[0]
   146  		args := cmds[1:]
   147  		sh.RunWithLogs(name, args...)
   148  	}
   149  }
   150  
   151  func main() {
   152  	flag.Parse()
   153  	if len(flag.Args()) > 1 {
   154  		log.Fatalf("Only one regexp-style argument is allowed, e.g.: " + ifName)
   155  	}
   156  	if len(flag.Args()) > 0 {
   157  		ifName = flag.Args()[0]
   158  	}
   159  
   160  	var images []boot.OSImage
   161  	var err error
   162  	if *bootfile == "" {
   163  		images, err = NetbootImages(ifName)
   164  		if err != nil {
   165  			dumpNetDebugInfo()
   166  		}
   167  	} else {
   168  		log.Printf("Skipping DHCP for manual target..")
   169  		var l dhclient.Lease
   170  		l, err = newManualLease()
   171  		if err == nil {
   172  			images, err = netboot.BootImages(context.Background(), ulog.Log, curl.DefaultSchemes, l)
   173  		}
   174  	}
   175  
   176  	if err != nil {
   177  		log.Printf("Netboot failed: %v", err)
   178  	}
   179  
   180  	for _, img := range images {
   181  		img.Edit(func(cmdline string) string {
   182  			return cmdline + " " + *cmdAppend
   183  		})
   184  	}
   185  
   186  	menuEntries := menu.OSImages(*verbose, images...)
   187  	menuEntries = append(menuEntries, menu.Reboot{})
   188  	menuEntries = append(menuEntries, menu.StartShell{})
   189  
   190  	// Boot does not return.
   191  	bootcmd.ShowMenuAndBoot(menuEntries, nil, *noLoad, *noExec)
   192  }