Skip to content

Commit

Permalink
core: Preallocate blank block volumes (kubevirt#1559)
Browse files Browse the repository at this point in the history
Importer pod needs to be started for blank block volumes and it needs to
handle the case.

Signed-off-by: Tomasz Baranski <tbaransk@redhat.com>
  • Loading branch information
tomob authored and Tomasz Baranski committed Feb 23, 2021
1 parent de9eec6 commit f452cfc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
13 changes: 10 additions & 3 deletions cmd/cdi-importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,16 @@ func main() {
klog.Warningf("Available space less than requested size, creating blank image sized to available space: %s.\n", minSizeQuantity.String())
}

quantityWithFSOverhead := importer.GetUsableSpace(filesystemOverhead, minSizeQuantity.Value())
klog.Infof("Space adjusted for filesystem overhead: %d.\n", quantityWithFSOverhead)
err := image.CreateBlankImage(common.ImporterWritePath, *resource.NewScaledQuantity(quantityWithFSOverhead, 0), preallocation)
var err error
if volumeMode == v1.PersistentVolumeFilesystem {
quantityWithFSOverhead := importer.GetUsableSpace(filesystemOverhead, minSizeQuantity.Value())
klog.Infof("Space adjusted for filesystem overhead: %d.\n", quantityWithFSOverhead)
err = image.CreateBlankImage(common.ImporterWritePath, *resource.NewScaledQuantity(quantityWithFSOverhead, 0), preallocation)
} else if volumeMode == v1.PersistentVolumeBlock && preallocation {
klog.V(1).Info("Preallocating blank block volume")
err = image.PreallocateBlankBlock(common.WriteBlockPath, minSizeQuantity)
}

if err != nil {
klog.Errorf("%+v", err)
message := fmt.Sprintf("Unable to create blank image: %+v", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/import-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (r *ImportReconciler) Reconcile(req reconcile.Request) (reconcile.Result, e
// In case this is a request to create a blank disk on a block device, we do not create a pod.
// we just mark the DV as successful
volumeMode := getVolumeMode(pvc)
if volumeMode == corev1.PersistentVolumeBlock && pvc.GetAnnotations()[AnnSource] == SourceNone {
if volumeMode == corev1.PersistentVolumeBlock && pvc.GetAnnotations()[AnnSource] == SourceNone && pvc.GetAnnotations()[AnnPreallocationRequested] != "true" {
log.V(1).Info("attempting to create blank disk for block mode, this is a no-op, marking pvc with pod-phase succeeded")
if pvc.GetAnnotations() == nil {
pvc.SetAnnotations(make(map[string]string, 0))
Expand Down
14 changes: 14 additions & 0 deletions pkg/image/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,17 @@ func (o *qemuOperations) CreateBlankImage(dest string, size resource.Quantity, p

return nil
}

// PreallocateBlankBlock writes requested amount of zeros to block device mounted at dest
func PreallocateBlankBlock(dest string, size resource.Quantity) error {
klog.V(3).Infof("block volume size is %s", size.String())

args := []string{"if=/dev/zero", "of=" + dest, "bs=" + convertQuantityToQemuSize(size), "count=1"}
_, err := qemuExecFunction(nil, nil, "dd", args...)

if err != nil {
return errors.Wrap(err, fmt.Sprintf("Could not preallocate blank block volume at %s with size %s", dest, size.String()))
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/importer/data-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (dp *DataProcessor) resize() (ProcessingPhase, error) {
if dp.requestImageSize != "" && size < int64(0) {
var err error
klog.V(3).Infoln("Resizing image")
shouldPreallocate, err = ResizeImage(dp.dataFile, dp.requestImageSize, dp.availableSpace)
shouldPreallocate, err = ResizeImage(dp.dataFile, dp.requestImageSize, dp.getUsableSpace())
if err != nil {
return ProcessingPhaseError, errors.Wrap(err, "Resize of image failed")
}
Expand Down
7 changes: 7 additions & 0 deletions tests/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,5 +933,12 @@ var _ = Describe("Preallocation", func() {
Entry("Blank image", true, func() *cdiv1.DataVolume {
return utils.NewDataVolumeForBlankRawImage("import-dv", "100Mi")
}),
Entry("Blank block DataVolume", true, func() *cdiv1.DataVolume {
if !f.IsBlockVolumeStorageClassAvailable() {
Skip("Storage Class for block volume is not available")
}

return utils.NewDataVolumeForBlankRawImageBlock("import-dv", "100Mi", f.BlockSCName)
}),
)
})

0 comments on commit f452cfc

Please sign in to comment.