github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/boot/initrd.go (about)

     1  // Copyright 2020 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 boot
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  	"strings"
    11  
    12  	"github.com/u-root/u-root/pkg/uio"
    13  )
    14  
    15  // CatInitrds concatenates initrds on first ReadAt call from a list of
    16  // io.ReaderAts, pads them to a 512 byte boundary.
    17  func CatInitrds(initrds ...io.ReaderAt) io.ReaderAt {
    18  	var names []string
    19  	for _, initrd := range initrds {
    20  		names = append(names, stringer(initrd))
    21  	}
    22  
    23  	return uio.NewLazyOpenerAt(strings.Join(names, ","), func() (io.ReaderAt, error) {
    24  		buf := new(bytes.Buffer)
    25  		for i, ireader := range initrds {
    26  			size, err := buf.ReadFrom(uio.Reader(ireader))
    27  			if err != nil {
    28  				return nil, err
    29  			}
    30  			// Don't pad the ending or an already aligned file.
    31  			if i != len(initrds)-1 && size%512 != 0 {
    32  				padding := make([]byte, 512-(size%512))
    33  				buf.Write(padding)
    34  			}
    35  		}
    36  		// Buffer doesn't implement ReadAt, so wrap in NewReader
    37  		return bytes.NewReader(buf.Bytes()), nil
    38  	})
    39  }