github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/containers/utils/boot-creator/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"path"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  
     9  	"io"
    10  	"os"
    11  
    12  	unikos "github.com/solo-io/unik/pkg/os"
    13  	"github.com/pborman/uuid"
    14  )
    15  
    16  const staticFileDir = "/tmp/staticfiles"
    17  
    18  func main() {
    19  	log.SetLevel(log.DebugLevel)
    20  	buildcontextdir := flag.String("d", "/opt/vol", "build context. relative volume names are relative to that")
    21  	kernelInContext := flag.String("p", "program.bin", "kernel binary name.")
    22  	usePartitionTables := flag.Bool("part", true, "indicates whether or not to use partition tables and install grub")
    23  	args := flag.String("a", "", "arguments to kernel")
    24  	out := flag.String("o", "", "base name of output file")
    25  
    26  	flag.Parse()
    27  
    28  	kernelFile := path.Join(*buildcontextdir, *kernelInContext)
    29  	imgFile := path.Join(*buildcontextdir, "boot.image."+uuid.New())
    30  	defer os.Remove(imgFile)
    31  
    32  	log.WithFields(log.Fields{"kernelFile": kernelFile, "args": *args, "imgFile": imgFile, "usePartitionTables": *usePartitionTables}).Debug("calling CreateBootImageWithSize")
    33  
    34  	s1, err := unikos.DirSize(*buildcontextdir)
    35  	if err != nil {
    36  		log.Fatal(err)
    37  	}
    38  	s2 := float64(s1) * 1.1
    39  	size := ((int64(s2) >> 20) + 20)
    40  
    41  	if err := unikos.CopyDir(*buildcontextdir, staticFileDir); err != nil {
    42  		log.Fatal(err)
    43  	}
    44  
    45  	//no need to copy twice
    46  	os.Remove(path.Join(staticFileDir, *kernelInContext))
    47  
    48  	if err := unikos.CreateBootImageWithSize(imgFile, unikos.MegaBytes(size), kernelFile, staticFileDir, *args, *usePartitionTables); err != nil {
    49  		log.Fatal(err)
    50  	}
    51  
    52  	src, err := os.Open(imgFile)
    53  	if err != nil {
    54  		log.Fatal("failed to open produced image file "+imgFile, err)
    55  	}
    56  	outFile := path.Join(*buildcontextdir, *out)
    57  	dst, err := os.OpenFile(outFile, os.O_RDWR, 0)
    58  	if err != nil {
    59  		log.Fatal("failed to open target output file "+outFile, err)
    60  	}
    61  	n, err := io.Copy(dst, src)
    62  	if err != nil {
    63  		log.Fatal("failed copying produced image file to target output file", err)
    64  	}
    65  	log.Infof("wrote %d bytes to disk", n)
    66  }