Skip to content

Commit ee6beea

Browse files
authored
Merge pull request #162 from jsafrane/block-snapshot
Fixed block volume snapshots
2 parents 49cddb8 + b2ad7fd commit ee6beea

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

pkg/hostpath/controllerserver.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
170170
switch volumeSource.Type.(type) {
171171
case *csi.VolumeContentSource_Snapshot:
172172
if snapshot := volumeSource.GetSnapshot(); snapshot != nil {
173-
err = loadFromSnapshot(capacity, snapshot.GetSnapshotId(), path)
173+
err = loadFromSnapshot(capacity, snapshot.GetSnapshotId(), path, requestedAccessType)
174174
vol.ParentSnapID = snapshot.GetSnapshotId()
175175
}
176176
case *csi.VolumeContentSource_Volume:
@@ -282,7 +282,7 @@ func (cs *controllerServer) ListVolumes(ctx context.Context, req *csi.ListVolume
282282

283283
// getSnapshotPath returns the full path to where the snapshot is stored
284284
func getSnapshotPath(snapshotId string) string {
285-
return filepath.Join(dataRoot, fmt.Sprintf("%s.tgz", snapshotId))
285+
return filepath.Join(dataRoot, fmt.Sprintf("%s.snap", snapshotId))
286286
}
287287

288288
// CreateSnapshot uses tar command to create snapshot for hostpath volume. The tar command can quickly create
@@ -331,16 +331,17 @@ func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
331331
creationTime := ptypes.TimestampNow()
332332
volPath := hostPathVolume.VolPath
333333
file := getSnapshotPath(snapshotID)
334-
args := []string{}
334+
335+
var cmd []string
335336
if hostPathVolume.VolAccessType == blockAccess {
336337
glog.V(4).Infof("Creating snapshot of Raw Block Mode Volume")
337-
args = []string{"czf", file, volPath}
338+
cmd = []string{"cp", volPath, file}
338339
} else {
339340
glog.V(4).Infof("Creating snapshot of Filsystem Mode Volume")
340-
args = []string{"czf", file, "-C", volPath, "."}
341+
cmd = []string{"tar", "czf", file, "-C", volPath, "."}
341342
}
342343
executor := utilexec.New()
343-
out, err := executor.Command("tar", args...).CombinedOutput()
344+
out, err := executor.Command(cmd[0], cmd[1:]...).CombinedOutput()
344345
if err != nil {
345346
return nil, status.Errorf(codes.Internal, "failed create snapshot: %v: %s", err, out)
346347
}

pkg/hostpath/hostpath.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func hostPathIsEmpty(p string) (bool, error) {
278278
}
279279

280280
// loadFromSnapshot populates the given destPath with data from the snapshotID
281-
func loadFromSnapshot(size int64, snapshotId, destPath string) error {
281+
func loadFromSnapshot(size int64, snapshotId, destPath string, mode accessType) error {
282282
snapshot, ok := hostPathVolumeSnapshots[snapshotId]
283283
if !ok {
284284
return status.Errorf(codes.NotFound, "cannot find snapshot %v", snapshotId)
@@ -290,9 +290,18 @@ func loadFromSnapshot(size int64, snapshotId, destPath string) error {
290290
return status.Errorf(codes.InvalidArgument, "snapshot %v size %v is greater than requested volume size %v", snapshotId, snapshot.SizeBytes, size)
291291
}
292292
snapshotPath := snapshot.Path
293-
args := []string{"zxvf", snapshotPath, "-C", destPath}
293+
294+
var cmd []string
295+
switch mode {
296+
case mountAccess:
297+
cmd = []string{"tar", "zxvf", snapshotPath, "-C", destPath}
298+
case blockAccess:
299+
cmd = []string{"dd", "if=" + snapshotPath, "of=" + destPath}
300+
default:
301+
return status.Errorf(codes.InvalidArgument, "unknown accessType: %d", mode)
302+
}
294303
executor := utilexec.New()
295-
out, err := executor.Command("tar", args...).CombinedOutput()
304+
out, err := executor.Command(cmd[0], cmd[1:]...).CombinedOutput()
296305
if err != nil {
297306
return status.Errorf(codes.Internal, "failed pre-populate data from snapshot %v: %v: %s", snapshotId, err, out)
298307
}

0 commit comments

Comments
 (0)