github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/persist/manager.go (about) 1 // Copyright (c) 2019 Huawei Corporation 2 // Copyright (c) 2020 Intel Corporation 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 // 6 7 package persist 8 9 import ( 10 "fmt" 11 12 exp "github.com/kata-containers/runtime/virtcontainers/experimental" 13 persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" 14 "github.com/kata-containers/runtime/virtcontainers/persist/fs" 15 "github.com/kata-containers/runtime/virtcontainers/pkg/rootless" 16 ) 17 18 type initFunc (func() (persistapi.PersistDriver, error)) 19 20 const ( 21 RootFSName = "fs" 22 RootlessFSName = "rootlessfs" 23 ) 24 25 var ( 26 // NewStoreFeature is an experimental feature 27 NewStoreFeature = exp.Feature{ 28 Name: "newstore", 29 Description: "This is a new storage driver which reorganized disk data structures, it has to be an experimental feature since it breaks backward compatibility.", 30 ExpRelease: "2.0", 31 } 32 expErr error 33 supportedDrivers = map[string]initFunc{ 34 35 RootFSName: fs.Init, 36 RootlessFSName: fs.RootlessInit, 37 } 38 mockTesting = false 39 ) 40 41 func init() { 42 expErr = exp.Register(NewStoreFeature) 43 } 44 45 func EnableMockTesting() { 46 mockTesting = true 47 } 48 49 // GetDriver returns new PersistDriver according to driver name 50 func GetDriverByName(name string) (persistapi.PersistDriver, error) { 51 if expErr != nil { 52 return nil, expErr 53 } 54 55 if f, ok := supportedDrivers[name]; ok { 56 return f() 57 } 58 59 return nil, fmt.Errorf("failed to get storage driver %q", name) 60 } 61 62 // GetDriver returns new PersistDriver according to current needs. 63 // For example, a rootless FS driver is returned if the process is running 64 // as unprivileged process. 65 func GetDriver() (persistapi.PersistDriver, error) { 66 if expErr != nil { 67 return nil, expErr 68 } 69 70 if mockTesting { 71 return fs.MockFSInit() 72 } 73 74 if rootless.IsRootless() { 75 if f, ok := supportedDrivers[RootlessFSName]; ok { 76 return f() 77 } 78 } 79 80 if f, ok := supportedDrivers[RootFSName]; ok { 81 return f() 82 } 83 84 return nil, fmt.Errorf("Could not find a FS driver") 85 }