github.com/andrewsun2898/u-root@v6.0.1-0.20200616011413-4b2895c1b815+incompatible/cmds/exp/modprobe/modprobe_linux.go (about)

     1  // Copyright 2013-2017 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  // modprobe - Add and remove modules from the Linux Kernel
     6  //
     7  // Synopsis:
     8  //     modprobe [-n] modulename [parameters...]
     9  //     modprobe [-n] -a modulename...
    10  //
    11  // Author:
    12  //     Roland Kammerer <dev.rck@gmail.com>
    13  package main
    14  
    15  import (
    16  	"flag"
    17  	"log"
    18  	"os"
    19  	"strings"
    20  
    21  	"github.com/u-root/u-root/pkg/kmodule"
    22  )
    23  
    24  const cmd = "modprobe [-an] modulename[s] [parameters...]"
    25  
    26  var (
    27  	dryRun     = flag.Bool("n", false, "Dry run")
    28  	all        = flag.Bool("a", false, "Insert all module names on the command line.")
    29  	verboseAll = flag.Bool("va", false, "Insert all module names on the command line.")
    30  	rootDir    = flag.String("d", "/", "Root directory for modules")
    31  	kernelVer  = flag.String("S", "", "Set kernel version instead of using uname")
    32  )
    33  
    34  func init() {
    35  	defUsage := flag.Usage
    36  	flag.Usage = func() {
    37  		os.Args[0] = cmd
    38  		defUsage()
    39  	}
    40  }
    41  
    42  func main() {
    43  	flag.Parse()
    44  
    45  	if flag.NArg() == 0 {
    46  		log.Println("Usage: ERROR: one module and optional module options.")
    47  		flag.Usage()
    48  		os.Exit(1)
    49  	}
    50  
    51  	opts := kmodule.ProbeOpts{
    52  		RootDir: *rootDir,
    53  		KVer:    *kernelVer,
    54  	}
    55  	if *dryRun {
    56  		log.Println("Unique dependencies in load order, already loaded ones get skipped:")
    57  		opts.DryRunCB = func(modPath string) {
    58  			log.Println(modPath)
    59  		}
    60  	}
    61  
    62  	// -va is just an alias for -a
    63  	*all = *all || *verboseAll
    64  	if *all {
    65  		modNames := flag.Args()
    66  		for _, modName := range modNames {
    67  			if err := kmodule.ProbeOptions(modName, "", opts); err != nil {
    68  				log.Printf("modprobe: Could not load module %q: %v", modName, err)
    69  			}
    70  		}
    71  		os.Exit(0)
    72  	}
    73  
    74  	modName := flag.Args()[0]
    75  	modOptions := strings.Join(flag.Args()[1:], " ")
    76  
    77  	if err := kmodule.ProbeOptions(modName, modOptions, opts); err != nil {
    78  		log.Fatalf("modprobe: Could not load module %q: %v", modName, err)
    79  	}
    80  }