github.com/jlowellwofford/u-root@v1.0.0/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  )
    16  
    17  var (
    18  	outputFile = flag.String("o", "initramfs.cpio", "Initramfs output file")
    19  )
    20  
    21  func main() {
    22  	flag.Parse()
    23  
    24  	if flag.NArg() == 0 {
    25  		log.Fatalf("must specify at least one file to include in initramfs")
    26  	}
    27  	archiver := uroot.CPIOArchiver{
    28  		RecordFormat: cpio.Newc,
    29  	}
    30  
    31  	// Open the target initramfs file.
    32  	w, err := archiver.OpenWriter(*outputFile, "", "")
    33  	if err != nil {
    34  		log.Fatalf("failed to open cpio archive %q: %v", *outputFile, err)
    35  	}
    36  
    37  	files := uroot.NewArchiveFiles()
    38  	archive := uroot.ArchiveOpts{
    39  		ArchiveFiles:   files,
    40  		OutputFile:     w,
    41  		DefaultRecords: uroot.DefaultRamfs,
    42  	}
    43  	if err := uroot.ParseExtraFiles(archive.ArchiveFiles, flag.Args(), false); err != nil {
    44  		log.Fatalf("failed to parse file names %v: %v", flag.Args(), err)
    45  	}
    46  
    47  	if err := archive.Write(); err != nil {
    48  		log.Fatalf("failed to write archive %q: %v", *outputFile, err)
    49  	}
    50  }