gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/cmds/core/init/init.go (about)

     1  // Copyright 2012-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  // init is u-root's standard userspace init process.
     6  //
     7  // init is intended to be the first process run by the kernel when it boots up.
     8  // init does some basic initialization (mount file systems, turn on loopback)
     9  // and then tries to execute, in order, /inito, a uinit (either in /bin, /bbin,
    10  // or /ubin), and then a shell (/bin/defaultsh and /bin/sh).
    11  package main
    12  
    13  import (
    14  	"flag"
    15  	"fmt"
    16  	"log"
    17  	"os/exec"
    18  
    19  	"github.com/u-root/u-root/pkg/libinit"
    20  )
    21  
    22  // initCmds has all the bits needed to continue
    23  // the init process after some initial setup.
    24  type initCmds struct {
    25  	cmds []*exec.Cmd
    26  }
    27  
    28  var (
    29  	verbose = flag.Bool("v", false, "print all build commands")
    30  	test    = flag.Bool("test", false, "Test mode: don't try to set control tty")
    31  	debug   = func(string, ...interface{}) {}
    32  )
    33  
    34  func main() {
    35  	flag.Parse()
    36  
    37  	log.Printf("Welcome to u-root!")
    38  	fmt.Println(`                              _`)
    39  	fmt.Println(`   _   _      _ __ ___   ___ | |_`)
    40  	fmt.Println(`  | | | |____| '__/ _ \ / _ \| __|`)
    41  	fmt.Println(`  | |_| |____| | | (_) | (_) | |_`)
    42  	fmt.Println(`   \__,_|    |_|  \___/ \___/ \__|`)
    43  	fmt.Println()
    44  
    45  	log.SetPrefix("init: ")
    46  
    47  	if *verbose {
    48  		debug = log.Printf
    49  	}
    50  
    51  	// Before entering an interactive shell, decrease the loglevel because
    52  	// spamming non-critical logs onto the shell frustrates users. The logs
    53  	// are still accessible through kernel logs buffers (on most kernels).
    54  	quiet()
    55  
    56  	libinit.SetEnv()
    57  	libinit.CreateRootfs()
    58  	libinit.NetInit()
    59  
    60  	// osInitGo wraps all the kernel-specific (i.e. non-portable) stuff.
    61  	// It returns an initCmds struct derived from kernel-specific information
    62  	// to be used in the rest of init.
    63  	ic := osInitGo()
    64  
    65  	// Start background build.
    66  	if isBgBuildEnabled() {
    67  		go startBgBuild()
    68  	}
    69  
    70  	cmdCount := libinit.RunCommands(debug, ic.cmds...)
    71  	if cmdCount == 0 {
    72  		log.Printf("No suitable executable found in %v", ic.cmds)
    73  	}
    74  
    75  	// We need to reap all children before exiting.
    76  	log.Printf("Waiting for orphaned children")
    77  	libinit.WaitOrphans()
    78  	log.Printf("All commands exited")
    79  	log.Printf("Syncing filesystems")
    80  	if err := quiesce(); err != nil {
    81  		log.Printf("%v", err)
    82  	}
    83  	log.Printf("Exiting...")
    84  }