...

Source file src/github.com/Microsoft/hcsshim/internal/hcs/utils.go

Documentation: github.com/Microsoft/hcsshim/internal/hcs

     1  //go:build windows
     2  
     3  package hcs
     4  
     5  import (
     6  	"context"
     7  	"io"
     8  	"syscall"
     9  
    10  	"github.com/Microsoft/go-winio"
    11  	diskutil "github.com/Microsoft/go-winio/vhd"
    12  	"github.com/Microsoft/hcsshim/computestorage"
    13  	"github.com/pkg/errors"
    14  	"golang.org/x/sys/windows"
    15  )
    16  
    17  // makeOpenFiles calls winio.MakeOpenFile for each handle in a slice but closes all the handles
    18  // if there is an error.
    19  func makeOpenFiles(hs []syscall.Handle) (_ []io.ReadWriteCloser, err error) {
    20  	fs := make([]io.ReadWriteCloser, len(hs))
    21  	for i, h := range hs {
    22  		if h != syscall.Handle(0) {
    23  			if err == nil {
    24  				fs[i], err = winio.MakeOpenFile(h)
    25  			}
    26  			if err != nil {
    27  				syscall.Close(h)
    28  			}
    29  		}
    30  	}
    31  	if err != nil {
    32  		for _, f := range fs {
    33  			if f != nil {
    34  				f.Close()
    35  			}
    36  		}
    37  		return nil, err
    38  	}
    39  	return fs, nil
    40  }
    41  
    42  // CreateNTFSVHD creates a VHD formatted with NTFS of size `sizeGB` at the given `vhdPath`.
    43  func CreateNTFSVHD(ctx context.Context, vhdPath string, sizeGB uint32) (err error) {
    44  	if err := diskutil.CreateVhdx(vhdPath, sizeGB, 1); err != nil {
    45  		return errors.Wrap(err, "failed to create VHD")
    46  	}
    47  
    48  	vhd, err := diskutil.OpenVirtualDisk(vhdPath, diskutil.VirtualDiskAccessNone, diskutil.OpenVirtualDiskFlagNone)
    49  	if err != nil {
    50  		return errors.Wrap(err, "failed to open VHD")
    51  	}
    52  	defer func() {
    53  		err2 := windows.CloseHandle(windows.Handle(vhd))
    54  		if err == nil {
    55  			err = errors.Wrap(err2, "failed to close VHD")
    56  		}
    57  	}()
    58  
    59  	if err := computestorage.FormatWritableLayerVhd(ctx, windows.Handle(vhd)); err != nil {
    60  		return errors.Wrap(err, "failed to format VHD")
    61  	}
    62  
    63  	return nil
    64  }
    65  

View as plain text