[PATCH v3 3/8] virsh: Track if vol-upload or vol-download work over a block device

Michal Privoznik mprivozn at redhat.com
Mon Aug 24 11:50:16 UTC 2020


We can't use virFileInData() with block devices, but we can
emulate being in data section all the time (vol-upload case).
Alternatively, we can't just lseek() beyond EOF with block
devices to create a hole, we will have to write zeroes
(vol-download case).  But to decide we need to know if the FD we
are reading data from / writing data to is a block device. Store
this information in _virshStreamCallbackData.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
Reviewed-by: Peter Krempa <pkrempa at redhat.com>
---
 tools/virsh-util.h   |  1 +
 tools/virsh-volume.c | 14 ++++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/tools/virsh-util.h b/tools/virsh-util.h
index 72653d9735..9ef28cfe0a 100644
--- a/tools/virsh-util.h
+++ b/tools/virsh-util.h
@@ -72,6 +72,7 @@ typedef virshStreamCallbackData *virshStreamCallbackDataPtr;
 struct _virshStreamCallbackData {
     vshControl *ctl;
     int fd;
+    bool isBlock;
 };
 
 int
diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c
index d29db6c38d..374bde4318 100644
--- a/tools/virsh-volume.c
+++ b/tools/virsh-volume.c
@@ -676,6 +676,7 @@ cmdVolUpload(vshControl *ctl, const vshCmd *cmd)
     virshControlPtr priv = ctl->privData;
     unsigned int flags = 0;
     virshStreamCallbackData cbData;
+    struct stat sb;
 
     if (vshCommandOptULongLong(ctl, cmd, "offset", &offset) < 0)
         return false;
@@ -694,8 +695,14 @@ cmdVolUpload(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
     }
 
+    if (fstat(fd, &sb) < 0) {
+        vshError(ctl, _("unable to stat %s"), file);
+        goto cleanup;
+    }
+
     cbData.ctl = ctl;
     cbData.fd = fd;
+    cbData.isBlock = !!S_ISBLK(sb.st_mode);
 
     if (vshCommandOptBool(cmd, "sparse"))
         flags |= VIR_STORAGE_VOL_UPLOAD_SPARSE_STREAM;
@@ -792,6 +799,7 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
     virshControlPtr priv = ctl->privData;
     virshStreamCallbackData cbData;
     unsigned int flags = 0;
+    struct stat sb;
 
     if (vshCommandOptULongLong(ctl, cmd, "offset", &offset) < 0)
         return false;
@@ -818,8 +826,14 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
         created = true;
     }
 
+    if (fstat(fd, &sb) < 0) {
+        vshError(ctl, _("unable to stat %s"), file);
+        goto cleanup;
+    }
+
     cbData.ctl = ctl;
     cbData.fd = fd;
+    cbData.isBlock = !!S_ISBLK(sb.st_mode);
 
     if (!(st = virStreamNew(priv->conn, 0))) {
         vshError(ctl, _("cannot create a new stream"));
-- 
2.26.2




More information about the libvir-list mailing list