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