There is no need to use posix_fallocate or SYS_fallocate to shrink
the volume, ftruncate can do the work. qemu-img/kvm-img supports to
shrink the volume itself.
---
src/storage/storage_backend_fs.c | 7 +++++--
src/util/virstoragefile.c | 5 +++--
src/util/virstoragefile.h | 3 ++-
3 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c
index 78f5d53..cdfa66f 100644
--- a/src/storage/storage_backend_fs.c
+++ b/src/storage/storage_backend_fs.c
@@ -1245,13 +1245,16 @@ virStorageBackendFileSystemVolResize(virConnectPtr conn
ATTRIBUTE_UNUSED,
unsigned long long capacity,
unsigned int flags)
{
- virCheckFlags(VIR_STORAGE_VOL_RESIZE_ALLOCATE, -1);
+ virCheckFlags(VIR_STORAGE_VOL_RESIZE_ALLOCATE |
+ VIR_STORAGE_VOL_RESIZE_SHRINK, -1);
bool pre_allocate = flags & VIR_STORAGE_VOL_RESIZE_ALLOCATE;
+ bool shrink = flags & VIR_STORAGE_VOL_RESIZE_SHRINK;
if (vol->target.format == VIR_STORAGE_FILE_RAW) {
return virStorageFileResize(vol->target.path, capacity,
- vol->capacity, pre_allocate);
+ vol->capacity, pre_allocate,
+ shrink);
} else {
if (pre_allocate) {
virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index e42eb77..25614df 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -1044,7 +1044,8 @@ int
virStorageFileResize(const char *path,
unsigned long long capacity,
unsigned long long orig_capacity,
- bool pre_allocate)
+ bool pre_allocate,
+ bool shrink)
{
int fd = -1;
int ret = -1;
@@ -1057,7 +1058,7 @@ virStorageFileResize(const char *path,
goto cleanup;
}
- if (pre_allocate) {
+ if (pre_allocate && !shrink) {
#if HAVE_POSIX_FALLOCATE
if ((rc = posix_fallocate(fd, offset, len)) != 0) {
virReportSystemError(rc,
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index c195c9a..46fe9ba 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -92,7 +92,8 @@ void virStorageFileFreeMetadata(virStorageFileMetadataPtr meta);
int virStorageFileResize(const char *path,
unsigned long long capacity,
unsigned long long orig_capacity,
- bool pre_allocate);
+ bool pre_allocate,
+ bool shrink);
enum {
VIR_STORAGE_FILE_SHFS_NFS = (1 << 0),
--
1.8.1.4