github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/init/init_linux.go (about)

     1  // Copyright 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  package main
     6  
     7  import (
     8  	"log"
     9  	"os"
    10  	"path/filepath"
    11  	"strconv"
    12  	"strings"
    13  	"syscall"
    14  
    15  	"github.com/u-root/u-root/pkg/cmdline"
    16  	"github.com/u-root/u-root/pkg/kmodule"
    17  )
    18  
    19  // installModules installs kernel modules (.ko files) from /lib/modules.
    20  // Useful for modules that need to be loaded for boot (ie a network
    21  // driver needed for netboot)
    22  func installModules() {
    23  	modulePattern := "/lib/modules/*.ko"
    24  	files, err := filepath.Glob(modulePattern)
    25  	if err != nil {
    26  		log.Printf("installModules: %v", err)
    27  		return
    28  	}
    29  	if len(files) == 0 {
    30  		// Since it is common for users to not have modules, no need to error or
    31  		// print if there are none to install
    32  		return
    33  	}
    34  
    35  	for _, filename := range files {
    36  		f, err := os.Open(filename)
    37  		if err != nil {
    38  			log.Printf("installModules: can't open %q: %v", filename, err)
    39  			continue
    40  		}
    41  		// Module flags are passed to the command line in the form modulename.flag=val
    42  		// And must be passed to FileInit as flag=val to be installed properly
    43  		moduleName := strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))
    44  		flags := cmdline.FlagsForModule(moduleName)
    45  		err = kmodule.FileInit(f, flags, 0)
    46  		f.Close()
    47  		if err != nil {
    48  			log.Printf("installModules: can't install %q: %v", filename, err)
    49  		}
    50  	}
    51  }
    52  
    53  func init() {
    54  	osInitGo = runOSInitGo
    55  }
    56  
    57  func runOSInitGo() {
    58  	// Install modules before exec-ing into user mode below
    59  	installModules()
    60  
    61  	// systemd is "special". If we are supposed to run systemd, we're
    62  	// going to exec, and if we're going to exec, we're done here.
    63  	// systemd uber alles.
    64  	initFlags := cmdline.GetInitFlagMap()
    65  
    66  	// systemd gets upset when it discovers it isn't really process 1, so
    67  	// we can't start it in its own namespace. I just love systemd.
    68  	systemd, present := initFlags["systemd"]
    69  	systemdEnabled, boolErr := strconv.ParseBool(systemd)
    70  	if present && boolErr == nil && systemdEnabled {
    71  		if err := syscall.Exec("/inito", []string{"/inito"}, os.Environ()); err != nil {
    72  			log.Printf("Lucky you, systemd failed: %v", err)
    73  		}
    74  	}
    75  }