package snapshot
import (
"context"
"fmt"
"path/filepath"
"github.com/containerd/containerd/snapshots"
"github.com/openeuler/Conch/internal/snapshot/common"
)
type Kind uint8
const (
KindUnknown Kind = iota
KindRW
KindSnapshot
KindImage
)
type SnapshotConfig struct {
Rootfs string
MemDir string
VmDir string
RootDir string
MemSize int64
pmemFiles []string
Labels map[string]string
}
func (c *SnapshotConfig) PmemFiles() []string {
result := make([]string, 0, len(c.pmemFiles))
for _, name := range c.pmemFiles {
result = append(result, filepath.Join(c.Rootfs, name))
}
return result
}
func (c *SnapshotConfig) SnapshotMemFile() string {
return filepath.Join(c.MemDir, common.MemFileName)
}
func (c *SnapshotConfig) InitrdFile() string {
return filepath.Join(c.VmDir, common.VmInitrdRelativePath)
}
func (c *SnapshotConfig) KernelFile() string {
return filepath.Join(c.VmDir, common.VmKernelRelativePath)
}
func (c *SnapshotConfig) SnapDir() string {
return filepath.Join(c.MemDir, c.RootDir)
}
func (c *SnapshotConfig) initDefaults() {
if c.MemSize <= 0 {
c.MemSize = common.MemFileDefaultSize
}
if c.RootDir == "" {
c.RootDir = "/conch/snapshot"
}
if c.pmemFiles == nil {
c.pmemFiles = make([]string, 0)
}
if c.Labels == nil {
c.Labels = make(map[string]string)
}
}
func (c *SnapshotConfig) createLabels() {
c.Labels[common.SnapshotLabel] = "true"
c.Labels[common.SnapshotLabelMemSize] = fmt.Sprintf("%d", c.MemSize)
c.Labels[common.SnapshotLabelRootfs] = c.Rootfs
c.Labels[common.SnapshotLabelSnapshotDir] = c.RootDir
}
type Opt func(info *SnapshotConfig) error
type ParentSnapshotIDs struct {
Rootfs string
Mem string
VM string
}
func Prepare(ctx context.Context, namespace, key string, parents ParentSnapshotIDs, opts ...Opt) (*SnapshotConfig, error) {
if gServer.snt == nil {
return nil, fmt.Errorf("server not init")
}
return gServer.Prepare(ctx, namespace, key, parents, opts...)
}
func AcquireView(ctx context.Context, namespace, key string, parents ParentSnapshotIDs, opts ...Opt) (*SnapshotConfig, error) {
if gServer.snt == nil {
return nil, fmt.Errorf("server not init")
}
return gServer.AcquireView(ctx, namespace, key, parents, opts...)
}
func AcquireResumeWorkspace(ctx context.Context, namespace, key string, parents ParentSnapshotIDs, cid uint32, socketPath string, opts ...Opt) (*SnapshotConfig, error) {
if gServer.snt == nil {
return nil, fmt.Errorf("server not init")
}
return gServer.AcquireResumeWorkspace(ctx, namespace, key, parents, cid, socketPath, opts...)
}
func ResolveParentSnapshotIDs(namespace, rootfs string) (ParentSnapshotIDs, error) {
if gServer.snt == nil {
return ParentSnapshotIDs{}, fmt.Errorf("server not init")
}
return gServer.ResolveParentSnapshotIDs(namespace, rootfs)
}
func ResolveImageParentSnapshotIDs(namespace, rootfs string) (ParentSnapshotIDs, error) {
if gServer.snt == nil {
return ParentSnapshotIDs{}, fmt.Errorf("server not init")
}
return gServer.ResolveImageParentSnapshotIDs(namespace, rootfs)
}
func Commit(ctx context.Context, namespace, snapshotID, key string, opts ...Opt) error {
if gServer.snt == nil {
return fmt.Errorf("server not init")
}
return gServer.Commit(ctx, namespace, snapshotID, key, opts...)
}
func Remove(ctx context.Context, namespace, key string) error {
if gServer.snt == nil {
return fmt.Errorf("server not init")
}
return gServer.Remove(ctx, namespace, key)
}
func CleanupAllViews() {
gServer.CleanupAllViews()
}
func Close() error {
if gServer.snt == nil {
return nil
}
return gServer.Close()
}
func Stat(ctx context.Context, namespace, key string) (snapshots.Info, error) {
if gServer.snt == nil {
return snapshots.Info{}, fmt.Errorf("server not init")
}
return gServer.snt.Stat(ctx, namespace, key)
}