[Libguestfs] [PATCH] LocalCopy: Use blockdev to get the size of block devices

Matthew Booth mbooth at redhat.com
Tue Apr 6 14:04:00 UTC 2010


LocalCopy was previously only using stat to determine the size of a source disk.
If that source disk was a block device it would return 4k. Amongst other
problems, this resulted in disks which would not import into RHEV.
---
 lib/Sys/VirtV2V/Transfer/LocalCopy.pm |   32 +++++++++++++++++++++++++++++++-
 1 files changed, 31 insertions(+), 1 deletions(-)

diff --git a/lib/Sys/VirtV2V/Transfer/LocalCopy.pm b/lib/Sys/VirtV2V/Transfer/LocalCopy.pm
index 814bf4a..093e78f 100644
--- a/lib/Sys/VirtV2V/Transfer/LocalCopy.pm
+++ b/lib/Sys/VirtV2V/Transfer/LocalCopy.pm
@@ -17,6 +17,7 @@
 
 package Sys::VirtV2V::Transfer::LocalCopy;
 
+use POSIX;
 use File::Spec;
 use File::stat;
 
@@ -79,7 +80,36 @@ sub transfer
                                 path => $path,
                                 error => $!)));
 
-    my $vol = $target->create_volume($name, $st->size);
+    my $size;
+
+    # If it's a block device, use the output of blockdev command
+    if (S_ISBLK($st->mode)) {
+        my $blockdev;
+        open($blockdev, '-|', 'blockdev', '--getsize64', $path)
+            or die("Unable to execute blockdev: $!");
+
+        while (<$blockdev>) {
+            if (defined($size)) {
+                my $error = "blockdev returned multiple output lines:\n$size\n";
+                $error .= $_;
+                while(<$blockdev>) {
+                    $error .= $_;
+                }
+                die($error);
+            }
+            chomp;
+            $size = $_;
+        }
+
+        close($blockdev) or die("blockdev returned an error: $size");
+    }
+
+    # Otherwise use the size of the file directly
+    else {
+        $size = $st->size;
+    }
+
+    my $vol = $target->create_volume($name, $size);
     $vol->open();
 
     for (;;) {
-- 
1.6.6.1




More information about the Libguestfs mailing list