[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[et-mgmt-tools] [PATCH] Provide a common block device size utility for Solaris & Linux



# HG changeset patch
# User john levon sun com
# Date 1228484251 28800
# Node ID 89668fd908b8ee8dc8fd54d89888037d8845e8d2
# Parent  862a53870d43aaa9b65d7afadaed999ca4349fea
Provide a common block device size utility for Solaris & Linux.

Signed-off-by: John Danielson <john danielson sun com>

diff --git a/virtinst/CloneManager.py b/virtinst/CloneManager.py
--- a/virtinst/CloneManager.py
+++ b/virtinst/CloneManager.py
@@ -24,7 +24,6 @@ import logging
 import logging
 import urlgrabber.progress as progress
 import util
-import commands
 import libvirt
 import Guest
 from VirtualDisk import VirtualDisk
@@ -397,13 +396,7 @@ class CloneDesign(object):
         for i in lst:
             mode = os.stat(i)[stat.ST_MODE]
             if stat.S_ISBLK(mode):
-                dummy, msg = commands.getstatusoutput('fdisk -s %s' % i)
-                # check
-                if msg.isdigit() == False:
-                    lines = msg.splitlines()
-                    # retry eg. for the GPT disk
-                    msg = lines[len(lines)-1]
-                size.append(int(msg) * 1024)
+                size.append(util.blkdev_size(i))
                 typ.append(False)
             elif stat.S_ISREG(mode):
                 size.append(os.path.getsize(i))
@@ -457,13 +450,7 @@ class CloneDesign(object):
                 continue
             mode = os.stat(i)[stat.ST_MODE]
             if stat.S_ISBLK(mode):
-                dummy, msg = commands.getstatusoutput('fdisk -s %s' % i)
-                # check
-                if msg.isdigit() == False:
-                    lines = msg.splitlines()
-                    # retry eg. for the GPT disk
-                    msg = lines[len(lines)-1]
-                size.append(int(msg) * 1024)
+                size.append(util.blkdev_size(i))
                 typ.append(False)
             elif stat.S_ISREG(mode):
                 size.append(os.path.getsize(i))
diff --git a/virtinst/util.py b/virtinst/util.py
--- a/virtinst/util.py
+++ b/virtinst/util.py
@@ -257,6 +257,15 @@ def xml_escape(str):
     str = str.replace("<", "&lt;")
     str = str.replace(">", "&gt;")
     return str
+ 
+def blkdev_size(path):
+    """Return the size of the block device.  We can't use os.stat() as
+    that returns zero on many platforms."""
+    fd = os.open(path, os.O_RDONLY)
+    # os.SEEK_END is not present on all systems
+    size = os.lseek(fd, 0, 2)
+    os.close(fd)
+    return size
 
 def compareMAC(p, q):
     """Compare two MAC addresses"""


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]