github.com/system-transparency/u-root@v6.0.1-0.20190919065413-ed07a650de4c+incompatible/integration/testcmd/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  
    12  	"golang.org/x/sys/unix"
    13  
    14  	"github.com/u-root/u-root/pkg/cmdline"
    15  	"github.com/u-root/u-root/pkg/sh"
    16  )
    17  
    18  // Mount a vfat volume and kexec the kernel within.
    19  func main() {
    20  	if err := os.MkdirAll("/testdata", 0755); err != nil {
    21  		log.Fatal(err)
    22  	}
    23  	if os.Getenv("UROOT_USE_9P") == "1" {
    24  		sh.RunOrDie("mount", "-t", "9p", "tmpdir", "/testdata")
    25  	} else {
    26  		sh.RunOrDie("mount", "-r", "-t", "vfat", "/dev/sda1", "/testdata")
    27  	}
    28  
    29  	// Get and increment the counter.
    30  	kExecCounter, ok := cmdline.Flag("kexeccounter")
    31  	if !ok {
    32  		kExecCounter = "0"
    33  	}
    34  	fmt.Printf("KEXECCOUNTER=%s\n", kExecCounter)
    35  
    36  	if kExecCounter == "0" {
    37  		cmdLine := cmdline.FullCmdLine() + " kexeccounter=1"
    38  		log.Print("cmdline: ", cmdLine)
    39  		sh.RunOrDie("kexec",
    40  			"-i", "/testdata/initramfs.cpio",
    41  			"-c", cmdLine,
    42  			"/testdata/kernel")
    43  	} else {
    44  		unix.Reboot(unix.LINUX_REBOOT_CMD_POWER_OFF)
    45  	}
    46  }