github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/tools/mkinitramfs/main.go (about)

     1  // Copyright 2015-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  // mkinitramfs creates a u-root initramfs given the list of files on the
     6  // command line.
     7  package mkinitramfs
     8  
     9  import (
    10  	"flag"
    11  	"log"
    12  	"os"
    13  
    14  	"github.com/u-root/u-root/pkg/uroot"
    15  	"github.com/u-root/u-root/pkg/uroot/initramfs"
    16  )
    17  
    18  var (
    19  	outputFile = flag.String("o", "initramfs.cpio", "Initramfs output file")
    20  )
    21  
    22  func main() {
    23  	flag.Parse()
    24  
    25  	if flag.NArg() == 0 {
    26  		log.Fatalf("must specify at least one file to include in initramfs")
    27  	}
    28  	logger := log.New(os.Stderr, "", log.LstdFlags)
    29  
    30  	// Open the target initramfs file.
    31  	w, err := initramfs.CPIO.OpenWriter(logger, *outputFile)
    32  	if err != nil {
    33  		log.Fatalf("failed to open cpio archive %q: %v", *outputFile, err)
    34  	}
    35  
    36  	files := initramfs.NewFiles()
    37  	archive := &initramfs.Opts{
    38  		Files:       files,
    39  		OutputFile:  w,
    40  		BaseArchive: uroot.DefaultRamfs().Reader(),
    41  	}
    42  	if err := uroot.ParseExtraFiles(logger, archive.Files, flag.Args(), false); err != nil {
    43  		log.Fatalf("failed to parse file names %v: %v", flag.Args(), err)
    44  	}
    45  
    46  	if err := initramfs.Write(archive); err != nil {
    47  		log.Fatalf("failed to write archive %q: %v", *outputFile, err)
    48  	}
    49  }