gopkg.in/hugelgupf/u-root.v2@v2.0.0-20180831055005-3f8fdb0ce09d/integration/testdata/kexec/uinit/kexec.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  	"fmt"
     9  	"log"
    10  	"os"
    11  	"os/exec"
    12  
    13  	"github.com/u-root/u-root/pkg/cmdline"
    14  )
    15  
    16  func sh(arg0 string, args ...string) {
    17  	cmd := exec.Command(arg0, args...)
    18  	cmd.Stdin = os.Stdin
    19  	cmd.Stdout = os.Stdout
    20  	cmd.Stderr = os.Stderr
    21  	if err := cmd.Run(); err != nil {
    22  		log.Fatal(err)
    23  	}
    24  }
    25  
    26  // Mount a vfat volume and kexec the kernel within.
    27  func main() {
    28  	sh("mkdir", "/testdata")
    29  	sh("mount", "-r", "-t", "vfat", "/dev/sda1", "/testdata")
    30  
    31  	// Get and increment the counter.
    32  	kExecCounter, ok := cmdline.Flag("kexeccounter")
    33  	if !ok {
    34  		kExecCounter = "0"
    35  	}
    36  	fmt.Printf("KEXECCOUNTER=%s\n", kExecCounter)
    37  
    38  	if kExecCounter == "0" {
    39  		cmdLine := cmdline.FullCmdLine() + " kexeccounter=1"
    40  		log.Print("cmdline: ", cmdLine)
    41  		sh("kexec",
    42  			"-i", "/testdata/initramfs.cpio",
    43  			"-c", cmdLine,
    44  			"/testdata/bzImage")
    45  	}
    46  }