gopkg.in/hugelgupf/u-root.v9@v9.0.0-20180831063832-3f6f1057f09b/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  
    13  	"github.com/u-root/u-root/pkg/cpio"
    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  	archiver := initramfs.CPIOArchiver{
    29  		RecordFormat: cpio.Newc,
    30  	}
    31  
    32  	// Open the target initramfs file.
    33  	w, err := archiver.OpenWriter(*outputFile, "", "")
    34  	if err != nil {
    35  		log.Fatalf("failed to open cpio archive %q: %v", *outputFile, err)
    36  	}
    37  
    38  	files := initramfs.NewFiles()
    39  	archive := &initramfs.Opts{
    40  		Files:          files,
    41  		OutputFile:     w,
    42  		DefaultRecords: uroot.DefaultRamfs,
    43  	}
    44  	if err := uroot.ParseExtraFiles(archive.Files, flag.Args(), false); err != nil {
    45  		log.Fatalf("failed to parse file names %v: %v", flag.Args(), err)
    46  	}
    47  
    48  	if err := initramfs.Write(archive); err != nil {
    49  		log.Fatalf("failed to write archive %q: %v", *outputFile, err)
    50  	}
    51  }