Skip to content

Commit 0b6c26c

Browse files
authored
feat(glance): support nfs as backend storage (#25240)
1 parent 696bd7b commit 0b6c26c

16 files changed

Lines changed: 288 additions & 23 deletions

File tree

pkg/apis/image/consts.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ const (
5151

5252
LocalFilePrefix = "file://"
5353
S3Prefix = "s3://"
54+
NfsPrefix = "nfs://"
55+
56+
NfsSubDirName = "images"
5457

5558
IMAGE_STORAGE_DRIVER_LOCAL = "local"
5659
IMAGE_STORAGE_DRIVER_S3 = "s3"
60+
IMAGE_STORAGE_DRIVER_NFS = "nfs"
5761

5862
// image properties
5963
IMAGE_OS_ARCH = "os_arch"

pkg/compute/hostdrivers/esxi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (self *SESXiHostDriver) CheckAndSetCacheImage(ctx context.Context, userCred
7979
hostCacheImage := models.StoragecachedimageManager.GetStoragecachedimage(storageCache.GetId(), cacheImage.GetId())
8080
if hostCacheImage == nil {
8181
zone, _ := host.GetZone()
82-
srcHostCacheImage, err = cacheImage.ChooseSourceStoragecacheInRange(api.HOST_TYPE_ESXI, []string{host.Id},
82+
srcHostCacheImage, err = cacheImage.ChooseSourceStoragecacheInRange([]string{api.HOST_TYPE_ESXI}, []string{host.Id},
8383
[]interface{}{zone, host.GetCloudprovider()})
8484
if err != nil {
8585
return err

pkg/compute/hostdrivers/kvm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (self *SKVMHostDriver) CheckAndSetCacheImage(ctx context.Context, userCred
220220
if srcHost != nil {
221221
rangeObjs = append(rangeObjs, srcHost)
222222
}
223-
srcHostCacheImage, err := cacheImage.ChooseSourceStoragecacheInRange(api.HOST_TYPE_HYPERVISOR, []string{host.Id}, rangeObjs)
223+
srcHostCacheImage, err := cacheImage.ChooseSourceStoragecacheInRange([]string{api.HOST_TYPE_HYPERVISOR, api.HOST_TYPE_CONTAINER}, []string{host.Id}, rangeObjs)
224224
if err != nil {
225225
return errors.Wrapf(err, "Choose source storagecache")
226226
}

pkg/compute/models/cachedimages.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ func (cachedImage *SCachedimage) addRefCount() {
611611
}
612612
}
613613

614-
func (cachedImage *SCachedimage) ChooseSourceStoragecacheInRange(hostType string, excludes []string, rangeObjs []interface{}) (*SStoragecachedimage, error) {
614+
func (cachedImage *SCachedimage) ChooseSourceStoragecacheInRange(hostType []string, excludes []string, rangeObjs []interface{}) (*SStoragecachedimage, error) {
615615
storageCachedImage := StoragecachedimageManager.Query().SubQuery()
616616
storage := StorageManager.Query().SubQuery()
617617
hostStorage := HoststorageManager.Query().SubQuery()
@@ -635,7 +635,7 @@ func (cachedImage *SCachedimage) ChooseSourceStoragecacheInRange(hostType string
635635
q = q.Filter(sqlchemy.NotIn(host.Field("id"), excludes))
636636
}
637637
if len(hostType) > 0 {
638-
q = q.Filter(sqlchemy.Equals(host.Field("host_type"), hostType))
638+
q = q.Filter(sqlchemy.In(host.Field("host_type"), hostType))
639639
}
640640

641641
for _, rangeObj := range rangeObjs {

pkg/hostman/storageman/imagecache_local.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ func (l *SLocalImageCache) prepare(ctx context.Context, input api.CacheImageInpu
211211

212212
l.remoteFile = remotefile.NewRemoteFile(ctx, url,
213213
l.GetPath(), false, input.Checksum, -1, nil, l.GetTmpPath(), input.SrcUrl)
214+
if l.Manager.GetStorageType() == api.STORAGE_NFS {
215+
l.remoteFile.NfsSetTargetStorageId(l.Manager.GetStorageId(), l.Manager.GetStoragePath())
216+
}
214217
return false, nil
215218
}
216219

pkg/hostman/storageman/imagecachemanager_base.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type IImageCacheManger interface {
5353

5454
IsLocal() bool
5555
GetStorageType() string
56+
GetStorageId() string
57+
GetStoragePath() string
5658

5759
// for diskhandler
5860
PrefetchImageCache(ctx context.Context, data interface{}) (jsonutils.JSONObject, error)

pkg/hostman/storageman/imagecachemanager_local.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ func (c *SLocalImageCacheManager) GetStorageType() string {
7575
return c.storage.StorageType()
7676
}
7777

78+
func (c *SLocalImageCacheManager) GetStorageId() string {
79+
if c.storage == nil {
80+
return ""
81+
}
82+
return c.storage.GetId()
83+
}
84+
85+
func (c *SLocalImageCacheManager) GetStoragePath() string {
86+
if c.storage == nil {
87+
return ""
88+
}
89+
return c.storage.GetPath()
90+
}
91+
7892
func (c *SLocalImageCacheManager) loadCache(ctx context.Context) {
7993
if len(c.cachePath) == 0 {
8094
return

pkg/hostman/storageman/imagecachemanager_lvm.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ func (c *SLVMImageCacheManager) GetStorageType() string {
6565
return c.storage.StorageType()
6666
}
6767

68+
func (s *SLVMImageCacheManager) GetStorageId() string {
69+
return s.storage.GetId()
70+
}
71+
72+
func (s *SLVMImageCacheManager) GetStoragePath() string {
73+
return s.storage.GetPath()
74+
}
75+
6876
func (c *SLVMImageCacheManager) Lvmlockd() bool {
6977
return c.lvmlockd
7078
}

pkg/hostman/storageman/imagecachemanager_rbd.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ func (c *SRbdImageCacheManager) GetStorageType() string {
118118
return c.storage.StorageType()
119119
}
120120

121+
func (c *SRbdImageCacheManager) GetStorageId() string {
122+
return c.storage.GetId()
123+
}
124+
125+
func (c *SRbdImageCacheManager) GetStoragePath() string {
126+
return c.storage.GetPath()
127+
}
128+
121129
func (c *SRbdImageCacheManager) GetPath() string {
122130
return c.Pool
123131
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2019 Yunion
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package remotefile
16+
17+
import (
18+
"os"
19+
"path"
20+
21+
"yunion.io/x/log"
22+
)
23+
24+
type NfsRemoteFileInfo struct {
25+
NfsImagePath string `json:"nfs_image_path"`
26+
}
27+
28+
func (i *NfsRemoteFileInfo) nfsLinkImage(targetPath, storagePath string) error {
29+
srcPath := path.Join(storagePath, i.NfsImagePath)
30+
log.Infof("NfsRemoteFileInfo start link %s to %s", srcPath, targetPath)
31+
return os.Link(srcPath, targetPath)
32+
}

0 commit comments

Comments
 (0)