2018-09-05 14:54:38 -04:00
|
|
|
package hcs
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
import (
|
2020-05-09 11:16:54 -04:00
|
|
|
"context"
|
2018-03-19 18:57:30 -04:00
|
|
|
"io"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/Microsoft/go-winio"
|
2020-05-09 11:16:54 -04:00
|
|
|
diskutil "github.com/Microsoft/go-winio/vhd"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sys/windows"
|
2018-03-19 18:57:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// makeOpenFiles calls winio.MakeOpenFile for each handle in a slice but closes all the handles
|
|
|
|
// if there is an error.
|
|
|
|
func makeOpenFiles(hs []syscall.Handle) (_ []io.ReadWriteCloser, err error) {
|
|
|
|
fs := make([]io.ReadWriteCloser, len(hs))
|
|
|
|
for i, h := range hs {
|
|
|
|
if h != syscall.Handle(0) {
|
|
|
|
if err == nil {
|
|
|
|
fs[i], err = winio.MakeOpenFile(h)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
syscall.Close(h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
for _, f := range fs {
|
|
|
|
if f != nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fs, nil
|
|
|
|
}
|
2020-05-09 11:16:54 -04:00
|
|
|
|
|
|
|
// creates a VHD formatted with NTFS of size `sizeGB` at the given `vhdPath`.
|
|
|
|
func CreateNTFSVHD(ctx context.Context, vhdPath string, sizeGB uint32) (err error) {
|
|
|
|
if err := diskutil.CreateVhdx(vhdPath, sizeGB, 1); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create VHD")
|
|
|
|
}
|
|
|
|
|
|
|
|
vhd, err := diskutil.OpenVirtualDisk(vhdPath, diskutil.VirtualDiskAccessNone, diskutil.OpenVirtualDiskFlagNone)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to open VHD")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err2 := windows.CloseHandle(windows.Handle(vhd))
|
|
|
|
if err == nil {
|
|
|
|
err = errors.Wrap(err2, "failed to close VHD")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := hcsFormatWritableLayerVhd(uintptr(vhd)); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to format VHD")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|