Skip to content

Commit

Permalink
Add support for custom wcow and lcow sandbox scratch sizes
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Terry (VM) <[email protected]>
  • Loading branch information
jterry75 authored and fahedouch committed Aug 7, 2020
1 parent 613523b commit 87f7eff
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
40 changes: 34 additions & 6 deletions snapshots/lcow/lcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func init() {
})
}

const (
rootfsSizeLabel = "containerd.io/snapshot/io.microsoft.container.storage.rootfs.size-gb"
)

type snapshotter struct {
root string
ms *storage.MetaStore
Expand Down Expand Up @@ -321,7 +325,21 @@ func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
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 {
return nil, err
}
Expand Down Expand Up @@ -352,19 +370,24 @@ func (s *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
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.
s.scratchLock.Lock()
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)
if err != nil {
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
r := uint32(time.Now().UnixNano() + int64(os.Getpid()))
Expand All @@ -380,7 +403,12 @@ func (s *snapshotter) openOrCreateScratch(ctx context.Context) (_ *os.File, err
LogFormat: runhcs.JSON,
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)
return nil, errors.Wrapf(err, "failed to create '%s' temp file", scratchTempName)
}
Expand Down
26 changes: 25 additions & 1 deletion snapshots/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"strconv"
"strings"

winfs "github.com/Microsoft/go-winio/pkg/fs"
Expand All @@ -48,6 +49,10 @@ func init() {
})
}

const (
rootfsSizeLabel = "containerd.io/snapshot/io.microsoft.container.storage.rootfs.size-gb"
)

type snapshotter struct {
root string
info hcsshim.DriverInfo
Expand Down Expand Up @@ -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")
}

// 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 {
Expand Down

0 comments on commit 87f7eff

Please sign in to comment.