diff options
Diffstat (limited to 'linhes/linhes-system/add_storage.py')
-rwxr-xr-x | linhes/linhes-system/add_storage.py | 102 |
1 files changed, 60 insertions, 42 deletions
diff --git a/linhes/linhes-system/add_storage.py b/linhes/linhes-system/add_storage.py index 9f2e61c..e210bf1 100755 --- a/linhes/linhes-system/add_storage.py +++ b/linhes/linhes-system/add_storage.py @@ -5,17 +5,17 @@ #Disks that are mounted, in fstab, size < 5000 bytes, optical or #have already been seen will not be presented as an option. # -# Version 2.0.0 +# Version 2.0.1 import dbus import pickle import subprocess import sys,os,re -import random, string +import random,string import configparser -from configparser import SafeConfigParser +from configparser import ConfigParser import glob - +import logging from MythTV import MythDB, MythBE, Recorded, MythError from socket import timeout, gethostname @@ -50,19 +50,18 @@ class disk_device: self.block_path = block_dev.Get('org.freedesktop.UDisks2.Block', 'Device', dbus_interface='org.freedesktop.DBus.Properties') self.block_path = bytearray(self.block_path).replace(b'\x00', b'').decode('utf-8') - print("Device:", self.block_path) + logging.info("Device: %s", self.block_path) self.read_only = self.get_is_readonly(block_dev) - print("ReadOnly:", self.read_only) + logging.info("ReadOnly: %s", self.read_only) self.device_file_path = self.get_device_file_path(block_dev) - print("Device File Path:", self.device_file_path) + logging.info("Device File Path: %s", self.device_file_path) self.device_id = block_dev.Get('org.freedesktop.UDisks2.Block', 'Id', dbus_interface='org.freedesktop.DBus.Properties') - print("Device Id:", self.device_id) + logging.info("Device Id: %s", self.device_id) self.is_device = self.get_is_device() - print("Is Device:", self.is_device) - + logging.info("Is Device: %s", self.is_device) self.drive = block_dev.Get('org.freedesktop.UDisks2.Block', 'Drive', dbus_interface='org.freedesktop.DBus.Properties') - print("Drive:", self.drive) + logging.info("Drive: %s", self.drive) drive_dev = bus.get_object("org.freedesktop.UDisks2", self.drive) self.storage_dir = storage_dir @@ -71,15 +70,15 @@ class disk_device: self.fs_map = self.get_fsmap() self.vendor = drive_dev.Get('org.freedesktop.UDisks2.Drive', 'Vendor', dbus_interface='org.freedesktop.DBus.Properties') - print("Vendor:", self.vendor) + logging.info("Vendor: %s", self.vendor) self.model = drive_dev.Get('org.freedesktop.UDisks2.Drive', 'Model', dbus_interface='org.freedesktop.DBus.Properties') - print("Model:", self.model) + logging.info("Model: %s", self.model) self.device_size = drive_dev.Get('org.freedesktop.UDisks2.Drive', 'Size', dbus_interface='org.freedesktop.DBus.Properties') - print("Drive Size:", self.device_size) + logging.info("Drive Size: %s", self.device_size) self.serial_number = self.get_serial_number(drive_dev) - print("Serial:", self.serial_number) + logging.info("Serial: %s", self.serial_number) self.is_optical = self.get_is_optical_disc(drive_dev) - print("Is Optical:", self.is_optical) + logging.info("Is Optical: %s", self.is_optical) self.mmount = False self.dir_sg = False @@ -88,30 +87,36 @@ class disk_device: self.f = block_dev.Get('org.freedesktop.UDisks2.Filesystem', 'MountPoints', dbus_interface='org.freedesktop.DBus.Properties') self.is_mounted = True self.f[0] = bytearray(self.f[0]).replace(b'\x00', b'').decode('utf-8') - print("MountPoints:", self.f[0]) + logging.info("MountPoints: %s", self.f[0]) except: self.f = [''] self.is_mounted = False - print("MountPoints:", self.f[0]) - print("Is Mounted:", self.is_mounted) + logging.info("MountPoints: %s", self.f[0]) + logging.info("Is Mounted: %s", self.is_mounted) try: self.partition_size = block_dev.Get('org.freedesktop.UDisks2.Partition', 'Size', dbus_interface='org.freedesktop.DBus.Properties') - print("Partition Size:", self.partition_size) - self.block_partition = block_dev.Get('org.freedesktop.UDisks2.Partition', 'Number', dbus_interface='org.freedesktop.DBus.Properties') - print("Partition Number:", self.block_partition) except: - pass + self.partition_size = 0 + logging.info("Partition Size: %s", self.partition_size) + + self.set_partition("1") + logging.info("Block Partition: %s", self.block_partition) self.in_use = self.get_in_use() - print("In Use:", self.in_use) + logging.info("In Use: %s", self.in_use) self.uuid='' self.new_mount_point='' self.disk_num='' - def set_partition(self,partition): - self.block_partition = "%s%s" %(self.block_path,partition) + if self.is_device: + if 'nvme' in self.block_path: + self.block_partition = "%sp%s" %(self.block_path,partition) + else: + self.block_partition = "%s%s" %(self.block_path,partition) + else: + self.block_partition = self.block_path def set_mmount(self,flag): self.mmount = flag @@ -142,7 +147,7 @@ class disk_device: return True def get_is_device(self): - match = re.search(r'part\d+$', self.device_id) + match = re.search(r'part\d+$', self.device_file_path) if match is None: return True else: @@ -160,7 +165,7 @@ class disk_device: serial_number = drive_dev.Get('org.freedesktop.UDisks2.Drive', 'Serial', dbus_interface='org.freedesktop.DBus.Properties') random_string = os.urandom(5) if serial_number == '': - serial_number = "".join( [random.choice(string.letters) for i in range(6)] ) + serial_number = "".join( [random.choice(string.ascii_letters) for i in range(6)] ) serial_number = "NSW%s" %serial_number return serial_number @@ -292,7 +297,7 @@ class disk_device: new_options = self.find_options_type(fstab)[0] #find blkid - #self.block_partition="%s1" %self.block_path + self.block_partition="%s1" %self.block_path uuid=self.find_uuid(self.block_partition) #construct new line @@ -396,13 +401,12 @@ class disk_device: self.config.set('storage','storage_groups',self.dir_sg) self.config.set('storage','disk_num',self.disk_num) - filename="%s_%s.conf" %(self.model.replace(' ',''), self.serial_number.replace(' ','')) configfile="/etc/storage.d/%s" %filename print(" %s" %configfile) - with open(configfile, 'wb') as configfile: + with open(configfile, 'w') as configfile: self.config.write(configfile) return @@ -430,7 +434,7 @@ def runcmd(cmd): pass else: cmd = "echo "+cmd - #print cmd + #print(cmd) cmdout = subprocess.getstatusoutput(cmd) #logging.debug(" %s", cmdout) return cmdout @@ -444,7 +448,7 @@ def scan_system(): current_drive_list=[] for dev in ud_manager.GetManagedObjects().items(): if dev[0].startswith("/org/freedesktop/UDisks2/block_devices"): - print('\n' + dev[0]) + logging.info(dev[0]) drive = disk_device(dev[0],storage_dir) if drive.is_device and drive.device_size > 5000 and not drive.is_optical : current_drive_list.append(drive) @@ -472,16 +476,16 @@ def search_for_match(system_drive,known_drive_list): match_drive=False for y in known_drive_list: if system_drive.serial_number.startswith("NSW"): - #print "Match_test: hash" - system_drive_hash = "%s_%s_%s" %(system_drive.model,system_drive.parition_size,system_drive.device_file_path) - y_drive_hash = "%s_%s_%s" %(y.model,y.parition_size,y.device_file_path) + #print("Match_test: hash") + system_drive_hash = "%s_%s_%s" %(system_drive.model,system_drive.partition_size,system_drive.device_file_path) + y_drive_hash = "%s_%s_%s" %(y.model,y.partition_size,y.device_file_path) if system_drive_hash == y_drive_hash : match_drive = True print("\n* No serial number was found, matched using hash method: %s" %system_drive.model) break elif y.serial_number == system_drive.serial_number: - #print "Match_test: serial number" + #print("Match_test: serial number") match_drive=True break @@ -579,11 +583,15 @@ def remove_pickle(): pass def last_disk_num(): - parser = SafeConfigParser() + parser = ConfigParser() num_list = [] for conf_file in glob.glob('%s/*.conf' %storage_dir): parser.read(conf_file) - disk_num = parser.get('storage', 'disk_num') + try: + disk_num = parser.get('storage', 'disk_num') + except: + print("\nSkipping " + conf_file + "is missing disk_num.") + continue num_list.append(int(disk_num)) num_list.sort() try: @@ -804,7 +812,11 @@ def reconstruct_storagegroups(): for conf_file in glob.glob('%s/*.conf' %storage_dir): parser = SafeConfigParser() parser.read(conf_file) - mount_point = parser.get('storage', 'mountpoint') + try: + mount_point = parser.get('storage', 'mountpoint') + except: + print("\nSkipping: " + conf_file + " is missing mountpoint") + continue mmount = parser.getboolean('storage', 'mmount') try: removed = parser.getboolean('storage', 'removed') @@ -1061,7 +1073,7 @@ class reconstruct_path: self.config.set('storage','disk_num',self.disk_num) print(" %s" %self.conf_file) - with open(self.conf_file, 'wb') as self.conf_file: + with open(self.conf_file, 'w') as self.conf_file: self.config.write(self.conf_file) return @@ -1162,6 +1174,12 @@ if __name__ == "__main__": if not os.geteuid()==0: sys.exit("\nRoot access is required to run this program.\n") + logging.basicConfig(filename='/var/log/add_storage.log', filemode='w', + format='%(asctime)s - %(levelname)s - %(message)s', + datefmt='%y-%m-%d %H:%M:%S') + logger = logging.getLogger() + logger.setLevel(logging.INFO) + if "--help" in sys.argv or "-h" in sys.argv: usage() @@ -1171,7 +1189,7 @@ if __name__ == "__main__": if "--no_mount" in sys.argv : no_mount = True - if "--no_destruction" or "-nd" in sys.argv: + if "--no_destruction" in sys.argv: destruction = False if "--new_init" in sys.argv : |