Add support for custom wcow and lcow sandbox scratch sizes

Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
This commit is contained in:
Justin Terry (VM) 2020-01-15 14:10:13 -08:00 committed by Justin Terry (SF)
parent 431cfd86e7
commit bb8af922bb
2 changed files with 59 additions and 7 deletions

View File

@ -57,6 +57,10 @@ func init() {
}) })
} }
const (
rootfsSizeLabel = "containerd.io/snapshot/io.microsoft.container.storage.rootfs.size-gb"
)
type snapshotter struct { type snapshotter struct {
root string root string
ms *storage.MetaStore ms *storage.MetaStore
@ -321,7 +325,21 @@ func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
return nil, err return nil, err
} }
scratchSource, err := s.openOrCreateScratch(ctx) var snapshotInfo snapshots.Info
for _, o := range opts {
o(&snapshotInfo)
}
var sizeGB int
if sizeGBstr, ok := snapshotInfo.Labels[rootfsSizeLabel]; ok {
i32, err := strconv.ParseInt(sizeGBstr, 10, 32)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse annotation %q=%q", rootfsSizeLabel, sizeGBstr)
}
sizeGB = int(i32)
}
scratchSource, err := s.openOrCreateScratch(ctx, sizeGB)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -352,19 +370,24 @@ func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
return s.mounts(newSnapshot), nil return s.mounts(newSnapshot), nil
} }
func (s *snapshotter) openOrCreateScratch(ctx context.Context) (_ *os.File, err error) { func (s *snapshotter) openOrCreateScratch(ctx context.Context, sizeGB int) (_ *os.File, err error) {
// Create the scratch.vhdx cache file if it doesn't already exit. // Create the scratch.vhdx cache file if it doesn't already exit.
s.scratchLock.Lock() s.scratchLock.Lock()
defer s.scratchLock.Unlock() defer s.scratchLock.Unlock()
scratchFinalPath := filepath.Join(s.root, "scratch.vhdx") vhdFileName := "scratch.vhdx"
if sizeGB > 0 {
vhdFileName = fmt.Sprintf("scratch_%d.vhdx", sizeGB)
}
scratchFinalPath := filepath.Join(s.root, vhdFileName)
scratchSource, err := os.OpenFile(scratchFinalPath, os.O_RDONLY, 0700) scratchSource, err := os.OpenFile(scratchFinalPath, os.O_RDONLY, 0700)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return nil, errors.Wrap(err, "failed to open scratch.vhdx for read") return nil, errors.Wrapf(err, "failed to open vhd %s for read", vhdFileName)
} }
log.G(ctx).Debug("scratch.vhdx not found, creating a new one") log.G(ctx).Debugf("vhd %s not found, creating a new one", vhdFileName)
// Golang logic for ioutil.TempFile without the file creation // Golang logic for ioutil.TempFile without the file creation
r := uint32(time.Now().UnixNano() + int64(os.Getpid())) r := uint32(time.Now().UnixNano() + int64(os.Getpid()))
@ -380,7 +403,12 @@ func (s *snapshotter) openOrCreateScratch(ctx context.Context) (_ *os.File, err
LogFormat: runhcs.JSON, LogFormat: runhcs.JSON,
Owner: "containerd", Owner: "containerd",
} }
if err := rhcs.CreateScratch(ctx, scratchTempPath); err != nil {
opt := runhcs.CreateScratchOpts{
SizeGB: sizeGB,
}
if err := rhcs.CreateScratchWithOpts(ctx, scratchTempPath, &opt); err != nil {
_ = os.Remove(scratchTempPath) _ = os.Remove(scratchTempPath)
return nil, errors.Wrapf(err, "failed to create '%s' temp file", scratchTempName) return nil, errors.Wrapf(err, "failed to create '%s' temp file", scratchTempName)
} }

View File

@ -23,6 +23,7 @@ import (
"encoding/json" "encoding/json"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
winfs "github.com/Microsoft/go-winio/pkg/fs" winfs "github.com/Microsoft/go-winio/pkg/fs"
@ -48,6 +49,10 @@ func init() {
}) })
} }
const (
rootfsSizeLabel = "containerd.io/snapshot/io.microsoft.container.storage.rootfs.size-gb"
)
type snapshotter struct { type snapshotter struct {
root string root string
info hcsshim.DriverInfo info hcsshim.DriverInfo
@ -332,7 +337,26 @@ func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
return nil, errors.Wrap(err, "failed to create sandbox layer") return nil, errors.Wrap(err, "failed to create sandbox layer")
} }
// TODO(darrenstahlmsft): Allow changing sandbox size var snapshotInfo snapshots.Info
for _, o := range opts {
o(&snapshotInfo)
}
var sizeGB int
if sizeGBstr, ok := snapshotInfo.Labels[rootfsSizeLabel]; ok {
i32, err := strconv.ParseInt(sizeGBstr, 10, 32)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse annotation %q=%q", rootfsSizeLabel, sizeGBstr)
}
sizeGB = int(i32)
}
if sizeGB > 0 {
const gbToByte = 1024 * 1024 * 1024
if err := hcsshim.ExpandSandboxSize(s.info, newSnapshot.ID, uint64(gbToByte*sizeGB)); err != nil {
return nil, errors.Wrapf(err, "failed to expand scratch size to %d GB", sizeGB)
}
}
} }
if err := t.Commit(); err != nil { if err := t.Commit(); err != nil {