summaryrefslogtreecommitdiffstats
path: root/src/com.gluster.storage.management.server.scripts
diff options
context:
space:
mode:
authorDhandapani <dhandapani@gluster.com>2011-04-11 12:54:32 +0530
committerDhandapani <dhandapani@gluster.com>2011-04-11 12:54:32 +0530
commit89211e6eb5996e1f18493d2a48696171b3693811 (patch)
tree27f4dcd8c9d8dd2430a1450f705a3b5af88bbec6 /src/com.gluster.storage.management.server.scripts
parentf66ee5b4693478be8608864aa174dfe8e94197f1 (diff)
parentc9f1c41af770a75160dd5f7dca572f6dedc8afec (diff)
Merge commit 'upstream/master'
Diffstat (limited to 'src/com.gluster.storage.management.server.scripts')
-rw-r--r--src/com.gluster.storage.management.server.scripts/src/common/Common.py9
-rw-r--r--src/com.gluster.storage.management.server.scripts/src/common/DiskUtils.py192
-rwxr-xr-x[-rw-r--r--]src/com.gluster.storage.management.server.scripts/src/nodes/CreateVolumeExportDirectory.py65
-rw-r--r--src/com.gluster.storage.management.server.scripts/src/nodes/XmlHandler.py2
4 files changed, 244 insertions, 24 deletions
diff --git a/src/com.gluster.storage.management.server.scripts/src/common/Common.py b/src/com.gluster.storage.management.server.scripts/src/common/Common.py
index 60f200fe..99c2f440 100644
--- a/src/com.gluster.storage.management.server.scripts/src/common/Common.py
+++ b/src/com.gluster.storage.management.server.scripts/src/common/Common.py
@@ -32,3 +32,12 @@ def log(priority, message=None):
else:
syslog.syslog(logPriority, logMessage)
return
+
+
+def stripEmptyLines(content):
+ ret = ""
+ for line in content.split("\n"):
+ if line.strip() != "":
+ ret += line
+ return ret
+
diff --git a/src/com.gluster.storage.management.server.scripts/src/common/DiskUtils.py b/src/com.gluster.storage.management.server.scripts/src/common/DiskUtils.py
new file mode 100644
index 00000000..bde12500
--- /dev/null
+++ b/src/com.gluster.storage.management.server.scripts/src/common/DiskUtils.py
@@ -0,0 +1,192 @@
+# Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com>
+# This file is part of Gluster Storage Platform.
+#
+# Gluster Storage Platform is free software; you can redistribute it
+# and/or modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 3 of
+# the License, or (at your option) any later version.
+#
+# Gluster Storage Platform is distributed in the hope that it will be
+# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see
+# <http://www.gnu.org/licenses/>.
+
+import os
+import glob
+import dbus
+
+import Globals
+from Utils import *
+
+ONE_MB_SIZE = 1048576
+
+
+def _stripDev(device):
+ if isString(device) and device.startswith("/dev/"):
+ return device[5:]
+ return device
+
+
+def _addDev(deviceName):
+ if isString(deviceName) and not deviceName.startswith("/dev/"):
+ return "/dev/" + deviceName
+ return deviceName
+
+
+def getDeviceName(device):
+ if type(device) == type([]):
+ nameList = []
+ for d in device:
+ nameList.append(_stripDev(d))
+ return nameList
+ return _stripDev(device)
+
+
+def getDevice(deviceName):
+ if isString(deviceName):
+ return _addDev(deviceName)
+ if type(deviceName) == type([]):
+ nameList = []
+ for d in deviceName:
+ nameList.append(_addDev(d))
+ return nameList
+ return _addDev(deviceName)
+
+
+def getDiskPartitionByUuid(uuid):
+ uuidFile = "/dev/disk/by-uuid/%s" % uuid
+ if os.path.exists(uuidFile):
+ return getDeviceName(os.path.realpath(uuidFile))
+ return None
+
+
+def getUuidByDiskPartition(device):
+ for uuidFile in glob.glob("/dev/disk/by-uuid/*"):
+ if os.path.realpath(uuidFile) == device:
+ return os.path.basename(uuidFile)
+ return None
+
+
+def getDiskPartitionUuid(partition):
+ log("WARNING: getDiskPartitionUuid() is deprecated by getUuidByDiskPartition()")
+ return getUuidByDiskPartition(partition)
+
+
+def getDiskPartitionByLabel(label):
+ ## TODO: Finding needs to be enhanced
+ labelFile = "/dev/disk/by-label/%s" % label
+ if os.path.exists(labelFile):
+ return getDeviceName(os.path.realpath(labelFile))
+ return None
+
+
+def getDeviceByLabel(label):
+ log("WARNING: getDeviceByLabel() is deprecated by getDiskPartitionByLabel()")
+ return getDiskPartitionByLabel(label)
+
+
+def getDiskPartitionLabel(device):
+ rv = runCommandFG(["sudo", "e2label", device], stdout=True)
+ if rv["Status"] == 0:
+ return rv["Stdout"].strip()
+ return False
+
+
+def getRootPartition(fsTabFile=Globals.FSTAB_FILE):
+ fsTabEntryList = readFsTab(fsTabFile)
+ for fsTabEntry in fsTabEntryList:
+ if fsTabEntry["MountPoint"] == "/":
+ if fsTabEntry["Device"].startswith("UUID="):
+ return getDiskPartitionByUuid(fsTabEntry["Device"].split("UUID=")[-1])
+ if fsTabEntry["Device"].startswith("LABEL="):
+ return getDiskPartitionByLabel(fsTabEntry["Device"].split("LABEL=")[-1])
+ return getDeviceName(fsTabEntry["Device"])
+ return None
+
+
+def getOsDisk():
+ log("WARNING: getOsDisk() is deprecated by getRootPartition()")
+ return getRootPartition()
+
+
+def getDiskList(diskDeviceList=None):
+ diskDeviceList = getDevice(diskDeviceList)
+ if isString(diskDeviceList):
+ diskDeviceList = [diskDeviceList]
+
+ dbusSystemBus = dbus.SystemBus()
+ halObj = dbusSystemBus.get_object("org.freedesktop.Hal",
+ "/org/freedesktop/Hal/Manager")
+ halManager = dbus.Interface(halObj, "org.freedesktop.Hal.Manager")
+ storageUdiList = halManager.FindDeviceByCapability("storage")
+
+ diskList = []
+ for udi in storageUdiList:
+ halDeviceObj = dbusSystemBus.get_object("org.freedesktop.Hal", udi)
+ halDevice = dbus.Interface(halDeviceObj,
+ "org.freedesktop.Hal.Device")
+ if halDevice.GetProperty("storage.drive_type") == "cdrom" or \
+ halDevice.GetProperty("block.is_volume"):
+ continue
+
+ disk = {}
+ disk["Device"] = str(halDevice.GetProperty('block.device'))
+ if diskDeviceList and disk["Device"] not in diskDeviceList:
+ continue
+ disk["Description"] = str(halDevice.GetProperty('storage.vendor')) + " " + str(halDevice.GetProperty('storage.model'))
+ if halDevice.GetProperty('storage.removable'):
+ disk["Size"] = long(halDevice.GetProperty('storage.removable.media_size'))
+ else:
+ disk["Size"] = long(halDevice.GetProperty('storage.size'))
+ disk["Interface"] = str(halDevice.GetProperty('storage.bus'))
+ disk["DriveType"] = str(halDevice.GetProperty('storage.drive_type'))
+ partitionList = []
+ partitionUdiList = halManager.FindDeviceStringMatch("info.parent", udi)
+ for partitionUdi in partitionUdiList:
+ partitionHalDeviceObj = dbusSystemBus.get_object("org.freedesktop.Hal",
+ partitionUdi)
+ partitionHalDevice = dbus.Interface(partitionHalDeviceObj,
+ "org.freedesktop.Hal.Device")
+ if not partitionHalDevice.GetProperty("block.is_volume"):
+ continue
+ partition = {}
+ partition["Device"] = str(partitionHalDevice.GetProperty('block.device'))
+ partition["Uuid"] = str(partitionHalDevice.GetProperty('volume.uuid'))
+ partition["Size"] = long(partitionHalDevice.GetProperty('volume.size'))
+ partition["Fstype"] = str(partitionHalDevice.GetProperty('volume.fstype'))
+ partition["Fsversion"] = str(partitionHalDevice.GetProperty('volume.fsversion'))
+ partition["Label"] = str(partitionHalDevice.GetProperty('volume.label'))
+ partition["Used"] = 0L
+ if partitionHalDevice.GetProperty("volume.is_mounted"):
+ rv = runCommandFG(["df", str(partitionHalDevice.GetProperty('volume.mount_point'))], stdout=True)
+ if rv["Status"] == 0:
+ try:
+ partition["Used"] = long(rv["Stdout"].split("\n")[1].split()[2])
+ except IndexError:
+ pass
+ except ValueError:
+ pass
+ partitionList.append(partition)
+ disk["Partitions"] = partitionList
+ diskList.append(disk)
+ return diskList
+
+
+def getMountPointByUuid(partitionUuid):
+ # check uuid in etc/fstab
+ try:
+ fstabEntries = open(Globals.FSTAB_FILE).readlines()
+ except IOError:
+ fstabEntries = []
+ found = False
+ for entry in fstabEntries:
+ entry = entry.strip()
+ if not entry:
+ continue
+ if entry.split()[0] == "UUID=" + partitionUuid:
+ return entry.split()[1]
+ return None
diff --git a/src/com.gluster.storage.management.server.scripts/src/nodes/CreateVolumeExportDirectory.py b/src/com.gluster.storage.management.server.scripts/src/nodes/CreateVolumeExportDirectory.py
index 611d9695..59dc3c19 100644..100755
--- a/src/com.gluster.storage.management.server.scripts/src/nodes/CreateVolumeExportDirectory.py
+++ b/src/com.gluster.storage.management.server.scripts/src/nodes/CreateVolumeExportDirectory.py
@@ -16,36 +16,55 @@
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
import os
+import sys
+import syslog
from XmlHandler import ResponseXml
-from optparse import OptionParser
+import DiskUtils
import Utils
+import Common
-def stripEmptyLines(content):
- ret = ""
- for line in content.split("\n"):
- if line.strip() != "":
- ret += line
- return ret
-
-def createDirectory(disk, volumename):
- dirname = "/export"
- if not os.path.isdir(dirname) or not os.path.isdir(disk):
- rs = ResponseXml()
- rs.appendTagRoute("code", 1)
- rs.appendTagRoute("message", "Disk is not mounted properly")
+def createDirectory(disk, volumeName):
+
+ # Retrieving disk uuid
+ diskUuid = DiskUtils.getUuidByDiskPartition(DiskUtils.getDevice(disk))
+
+ rs = ResponseXml()
+ if not diskUuid:
+ Common.log(syslog.LOG_ERR, "failed to find disk:%s uuid" % disk)
+ rs.appendTagRoute("status.code", "-1")
+ rs.appendTagRoute("status.message", "Error: Unable to find disk uuid")
+ return rs.toprettyxml()
+
+ # Retrieving disk mount point using disk uuid
+ diskMountPoint = DiskUtils.getMountPointByUuid(diskUuid)
+ if not os.path.exists(diskMountPoint):
+ Common.log(syslog.LOG_ERR, "failed to retrieve disk:%s mount point" % disk)
+ rs.appendTagRoute("status.code", "-1")
+ rs.appendTagRoute("status.message", "Error: Failed to retrieve disk details")
return rs.toprettyxml()
-
-
- if not os.path.isdir(dirname + "/" + disk + "/" + volumename + "/"):
- command = "mkdir " + volumename;
+
+ # creating volume directory under disk mount point
+ volumeDirectory = "%s/%s" % (diskMountPoint, volumeName)
+ if not os.path.exists(volumeDirectory):
+ command = ["sudo", "mkdir", volumeDirectory]
rv = Utils.runCommandFG(command, stdout=True, root=True)
- message = stripEmptyLines(rv["Stdout"])
+ message = Common.stripEmptyLines(rv["Stdout"])
if rv["Stderr"]:
- message += "Error: [" + stripEmptyLines(rv["Stderr"]) + "]"
- rs = ResponseXml()
+ error = Common.stripEmptyLines(rv["Stderr"])
+ message += "Error: [%s]" % (error)
+ Common.log(syslog.LOG_ERR, "failed to create volume directory %s, %s" % (volumeDirectory, error)
rs.appendTagRoute("status.code", rv["Status"])
rs.appendTagRoute("status.message", message)
return rs.toprettyxml()
-def main(disk, volumename):
- return createDirectory(disk, volumename) \ No newline at end of file
+def main():
+ if len(sys.argv) != 3:
+ print >> sys.stderr, "usage: %s <disk name> <volume name>" % sys.argv[0]
+ sys.exit(-1)
+
+ disk = sys.argv[1]
+ volumeName = sys.argv[2]
+ print createDirectory(disk, volumeName)
+ sys.exit(0)
+
+main()
diff --git a/src/com.gluster.storage.management.server.scripts/src/nodes/XmlHandler.py b/src/com.gluster.storage.management.server.scripts/src/nodes/XmlHandler.py
index d5a1fe19..72164ffb 100644
--- a/src/com.gluster.storage.management.server.scripts/src/nodes/XmlHandler.py
+++ b/src/com.gluster.storage.management.server.scripts/src/nodes/XmlHandler.py
@@ -343,4 +343,4 @@ def test():
networkInterfaces.appendChild(networkTag)
print rs.toprettyxml()
-test()
+#test()