From a8048501b4ba5e52ef943aed017e655024f010a0 Mon Sep 17 00:00:00 2001 From: timothy Date: Wed, 23 Nov 2011 17:46:39 +0530 Subject: Enhanced format_device.py Removed gluster_provision_block_wrapper.py Added format_device_background.py --- .../src/backend/format_device.py | 75 +++++++----- .../src/backend/format_device_background.py | 128 +++++++++++++++++++++ .../src/backend/gluster_provision_block_wrapper.py | 101 ---------------- 3 files changed, 173 insertions(+), 131 deletions(-) create mode 100755 src/com.gluster.storage.management.gateway.scripts/src/backend/format_device_background.py delete mode 100755 src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_provision_block_wrapper.py (limited to 'src') diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/format_device.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/format_device.py index 8ae00260..5cdfd1c9 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/format_device.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/format_device.py @@ -14,7 +14,6 @@ if not p2 in sys.path: import Globals import Utils import DiskUtils -from optparse import OptionParser def main(): @@ -22,23 +21,49 @@ def main(): sys.stderr.write("format device unsupported") sys.exit(1) - parser = OptionParser() - parser.add_option("-t", "--type", action="store", type="string", dest="fstype") - (options, args) = parser.parse_args() - - if len(args) != 1: - sys.stderr.write("usage: %s [-t FSTYPE] DEVICE_NAME\n" % os.path.basename(sys.argv[0])) + if len(sys.argv) != 4: + sys.stderr.write("usage: %s FSTYPE MOUNT_POINT DEVICE_NAME\n" % os.path.basename(sys.argv[0])) sys.exit(-1) - device = DiskUtils.getDevice(args[0]) - deviceFormatLockFile = Utils.getDeviceFormatLockFile(device) - deviceFormatStatusFile = Utils.getDeviceFormatStatusFile(device) - deviceFormatOutputFile = Utils.getDeviceFormatOutputFile(device) + fsType = sys.argv[1] + mountPoint = sys.argv[2] + device = DiskUtils.getDevice(sys.argv[3]) if DiskUtils.isDataDiskPartitionFormatted(device): - sys.stderr.write("Device already formatted\n") + Utils.log("device %s already formatted" % device) + sys.stderr.write("device %s already formatted\n" % sys.argv[3]) sys.exit(2) + if os.path.exists(mountPoint): + if not os.path.isdir(mountPoint): + Utils.log("mount point %s exists but not a directory" % mountPoint) + sys.stderr.write("mount point %s exists but not a directory" % mountPoint) + sys.exit(3) + procMounts = Utils.readFile("/proc/mounts") + if procMounts.find(" %s " % mountPoint) != -1: + Utils.log("mount point %s already has a mount" % mountPoint) + sys.stderr.write("mount point %s already has a mount\n" % mountPoint) + sys.exit(4) + if procMounts.find(" %s/" % mountPoint) != -1: + Utils.log("mount point %s has a submount" % mountPoint) + sys.stderr.write("mount point %s has a submount\n" % mountPoint) + sys.exit(5) + else: + status = Utils.runCommand("mkdir -p %s" % mountPoint, output=True, root=True) + if status["Status"] != 0: + Utils.log("failed to create mount point %s" % mountPoint) + sys.stderr.write("failed to create mount point %s\n" % mountPoint) + sys.exit(6) + + if fsType not in Utils.getFileSystemType(): + Utils.log("invalid file system type %s" % fsType) + sys.stderr.write("invalid file system type %s\n" % fsType) + sys.exit(7) + + deviceFormatLockFile = Utils.getDeviceFormatLockFile(device) + deviceFormatStatusFile = Utils.getDeviceFormatStatusFile(device) + deviceFormatOutputFile = Utils.getDeviceFormatOutputFile(device) + if os.path.exists(deviceFormatStatusFile): Utils.log("format status file %s exists" % deviceFormatStatusFile) line = Utils.readFile(deviceFormatStatusFile) @@ -46,29 +71,19 @@ def main(): sys.stderr.write("failed to read format status file %s\n" % deviceFormatStatusFile) sys.exit(-2) if line.strip().upper() == "COMPLETED": - sys.stderr.write("Device already formatted\n") - sys.exit(3) + sys.stderr.write("Device %s already formatted\n" % sys.argv[3]) + sys.exit(8) else: - sys.stderr.write("Device format already running\n") - sys.exit(4) + sys.stderr.write("Formatting device %s already running\n" % sys.argv[3]) + sys.exit(9) if os.path.exists(deviceFormatLockFile): Utils.log("lock file %s exists" % deviceFormatLockFile) - sys.stderr.write("Device format already running\n") - sys.exit(5) - - if options.fstype: - command = ["%s/gluster_provision_block_wrapper.py" % p1, "-t", "%s" % (options.fstype), "%s" % (device)] - else: - command = ["%s/gluster_provision_block_wrapper.py" % p1, "%s" % (device)] + sys.stderr.write("Formatting device %s already running\n" % sys.argv[3]) + sys.exit(10) - try: - pid = os.fork() - except OSError, e: - Utils.log("failed to fork a child process: %s" % str(e)) - sys.exit(6) - if pid == 0: - os.execv(command[0], command) + command = ["%s/format_device_background.py" % p1, fsType, mountPoint, sys.argv[3]] + Utils.runCommandBG(command) sys.exit(0) diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/format_device_background.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/format_device_background.py new file mode 100755 index 00000000..a804a59c --- /dev/null +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/format_device_background.py @@ -0,0 +1,128 @@ +#!/usr/bin/python +# Copyright (C) 2011 Gluster, Inc. +# This file is part of Gluster Storage Platform. +# + +import os +import sys +p1 = os.path.abspath(os.path.dirname(sys.argv[0])) +p2 = "%s/common" % os.path.dirname(p1) +if not p1 in sys.path: + sys.path.append(p1) +if not p2 in sys.path: + sys.path.append(p2) +import Utils +import FsTabUtils +import DiskUtils + +def writeStatus(deviceFormatStatusFile, message): + try: + fp = open(deviceFormatStatusFile, "w") + fp.write(message) + fp.close() + except IOError, e: + Utils.log("Failed to update log file %s: %s" % (deviceFormatStatusFile, str(e))) + return False + return True + +def main(): + if len(sys.argv) != 4: + sys.stderr.write("usage: %s FSTYPE MOUNT_POINT DEVICE_NAME\n" % os.path.basename(sys.argv[0])) + sys.exit(-1) + + fsType = sys.argv[1] + mountPoint = sys.argv[2] + device = DiskUtils.getDevice(sys.argv[3]) + + deviceFormatLockFile = Utils.getDeviceFormatLockFile(device) + deviceFormatStatusFile = Utils.getDeviceFormatStatusFile(device) + deviceFormatOutputFile = Utils.getDeviceFormatOutputFile(device) + + if os.path.exists(deviceFormatStatusFile): + Utils.log("device format status file %s exists" % deviceFormatStatusFile) + sys.exit(1) + + if os.path.exists(deviceFormatLockFile): + Utils.log("device format lock file %s exists" % deviceFormatLockFile) + sys.exit(2) + + try: + fp = open(deviceFormatLockFile, "w") + fp.close() + except OSError, e: + Utils.log("failed to create lock file %s: %s" % (deviceFormatLockFile, str(e))) + writeStatus(deviceFormatStatusFile, "Lock file creation failed\n") + sys.exit(-2) + + try: + fptr = open(deviceFormatOutputFile, 'w') + except IOError, e: + Utils.log("failed to create output file %s" % deviceFormatOutputFile) + writeStatus(deviceFormatStatusFile, "Output file creation failed\n") + Utils.removeFile(deviceFormatLockFile) + sys.exit(-3) + + if fsType in ['ext3', 'ext4', 'ext4dev']: + command = "/sbin/mkfs.%s -F -I 512 %s" % (fsType, device) + elif fsType == "xfs": + command = "/sbin/mkfs.%s -f -i size=512 %s" % (fsType, device) + else: + command = "/sbin/mkfs.%s %s" % (fsType, device) + + status = Utils.runCommand(command, output=True, root=True) + if status["Status"] != 0: + Utils.removeFile(deviceFormatOutputFile) + Utils.removeFile(deviceFormatLockFile) + writeStatus(deviceFormatStatusFile, "Device format failed\n") + sys.exit(3) + + if Utils.runCommand("udevadm trigger") != 0: + Utils.log("failed running udevadm trigger") + + if Utils.runCommand("/usr/bin/lshal") != 0: + Utils.log("failed running /usr/bin/lshal") + + deviceUuid = DiskUtils.getUuidByDiskPartition(device) + if not deviceUuid: + Utils.removeFile(deviceFormatOutputFile) + Utils.removeFile(deviceFormatLockFile) + Utils.log("UUID not found after device %s formatted" % device) + writeStatus(deviceFormatStatusFile, "UUID not found after device %s formatted\n" % sys.argv[3]) + sys.exit(4) + + if DiskUtils.isDataDiskPartitionFormatted(device): + Utils.removeFile(deviceFormatOutputFile) + Utils.removeFile(deviceFormatLockFile) + Utils.log("UUID device %s already has an entry in fstab" % device) + writeStatus(deviceFormatStatusFile, "UUID device %s already has an entry in fstab\n" % sys.argv[3]) + sys.exit(5) + + newFsTabEntry = {"Device" : "UUID=%s" % deviceUuid, + "MountPoint" : mountPoint, + "FsType" : fsType, + "Options" : "defaults", + "DumpOption" : "0", + "fsckOrder" : "2"} + if fsType in ['ext3', 'ext4', 'ext4dev']: + newFsTabEntry["Options"] = "defaults,user_xattr" + if not FsTabUtils.addFsTabEntry(newFsTabEntry): + Utils.removeFile(deviceFormatOutputFile) + Utils.removeFile(deviceFormatLockFile) + writeStatus(deviceFormatStatusFile, "failed to update fstab") + sys.exit(6) + + status = Utils.runCommand("mount %s" % mountPoint, output=True, root=True) + if status["Status"] != 0: + Utils.removeFile(deviceFormatOutputFile) + Utils.removeFile(deviceFormatLockFile) + Utils.log("Mounting device %s on %s failed" % (device, mountPoint)) + writeStatus(deviceFormatStatusFile, "Mounting device %s on %s failed\n" % (sys.argv[3], mountPoint)) + sys.exit(7) + + writeStatus(deviceFormatStatusFile, "Completed\n") + Utils.removeFile(deviceFormatOutputFile) + Utils.removeFile(deviceFormatLockFile) + sys.exit(0) + +if __name__ == "__main__": + main() diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_provision_block_wrapper.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_provision_block_wrapper.py deleted file mode 100755 index a2827ea2..00000000 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/gluster_provision_block_wrapper.py +++ /dev/null @@ -1,101 +0,0 @@ -#!/usr/bin/python -# Copyright (C) 2011 Gluster, Inc. -# This file is part of Gluster Storage Platform. -# - -import os -import sys -p1 = os.path.abspath(os.path.dirname(sys.argv[0])) -p2 = "%s/common" % os.path.dirname(p1) -if not p1 in sys.path: - sys.path.append(p1) -if not p2 in sys.path: - sys.path.append(p2) -import subprocess -import Utils -from optparse import OptionParser - -def writeStatus(deviceFormatStatusFile, message): - try: - fp = open(deviceFormatStatusFile, "w") - fp.write(message) - fp.close() - except IOError, e: - Utils.log("Failed to update log file %s: %s" % (deviceFormatStatusFile, str(e))) - return False - return True - - -def main(): - parser = OptionParser() - parser.add_option("-t", "--type", action="store", type="string", dest="fstype") - (options, args) = parser.parse_args() - - if len(args) != 1: - sys.stderr.write("usage: %s [-t FSTYPE] DEVICE" % os.path.basename(sys.argv[0])) - sys.exit(-1) - - device = args[0] - deviceFormatLockFile = Utils.getDeviceFormatLockFile(device) - deviceFormatStatusFile = Utils.getDeviceFormatStatusFile(device) - deviceFormatOutputFile = Utils.getDeviceFormatOutputFile(device) - - if os.path.exists(deviceFormatStatusFile): - Utils.log("device format status file %s exists" % deviceFormatStatusFile) - sys.exit(1) - - if os.path.exists(deviceFormatLockFile): - Utils.log("device format lock file %s exists" % deviceFormatLockFile) - sys.exit(2) - - try: - fp = open(deviceFormatLockFile, "w") - fp.close() - except OSError, e: - Utils.log("failed to create lock file %s: %s" % (deviceFormatLockFile, str(e))) - writeStatus(deviceFormatStatusFile, "Lock file creation failed\n") - sys.exit(3) - - try: - fptr = open(deviceFormatOutputFile, 'w') - except IOError, e: - Utils.log("failed to create output file %s" % deviceFormatOutputFile) - writeStatus(deviceFormatStatusFile, "Output file creation failed\n") - Utils.removeFile(deviceFormatLockFile) - sys.exit(4) - - if options.fstype: - command = "gluster-provision-block -t %s %s" % (options.fstype, device) - else: - command = "gluster-provision-block %s" % (device) - - process = Utils.runCommandBG(command, - stdinFileObj=subprocess.PIPE, - stdoutFileObj=fptr, - stderrFileObj=subprocess.PIPE) - if process: - status = process.wait() - else: - Utils.removeFile(deviceFormatOutputFile) - Utils.removeFile(deviceFormatLockFile) - writeStatus(deviceFormatStatusFile, "Device format failed\n") - sys.exit(5) - - if status != 0: - Utils.removeFile(deviceFormatOutputFile) - Utils.removeFile(deviceFormatLockFile) - writeStatus(deviceFormatStatusFile, "Device format failed\n") - sys.exit(6) - - if Utils.runCommand("/sbin/udevtrigger") != 0: - Utils.log("failed running /sbin/udevtrigger") - - if Utils.runCommand("/usr/bin/lshal") != 0: - Utils.log("failed running /usr/bin/lshal") - writeStatus(deviceFormatStatusFile, "Completed\n") - Utils.removeFile(deviceFormatOutputFile) - Utils.removeFile(deviceFormatLockFile) - sys.exit(0) - -if __name__ == "__main__": - main() -- cgit