From 29864d1a0d4d8981a9f011e8768143daf3e6d72b Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Mon, 10 Sep 2012 17:48:23 -0500
Subject: LinHES-system: add_storage.py   new features - add ability to
 reconstruct /etc/fstab and dir structure based off the contents of
 /etc/storage.d

---
 abs/core/LinHES-system/PKGBUILD       |   8 +-
 abs/core/LinHES-system/add_storage.py | 205 ++++++++++++++++++++++++++++++++--
 2 files changed, 197 insertions(+), 16 deletions(-)

diff --git a/abs/core/LinHES-system/PKGBUILD b/abs/core/LinHES-system/PKGBUILD
index a954eab..d80bd5a 100755
--- a/abs/core/LinHES-system/PKGBUILD
+++ b/abs/core/LinHES-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-system
 pkgver=2
-pkgrel=85
+pkgrel=92
 arch=('i686' 'x86_64')
 MVDIR=$startdir/pkg/usr/LH
 BINDIR=$startdir/pkg/usr/bin
@@ -79,11 +79,11 @@ md5sums=('a941af97fe534731a507b1d57c0c2aed'
          '962a3e9eaba2d1466251b7ab0956705d'
          'd03aedeeeda0f5e9c1eb437411c88477'
          '94ce8f1d5085c0db7b267db8c2e9a3ff'
-         '16a2a3347961517fe7d95a2956a091a6'
-         '9c2294ccfd5359583497a6b03d918a27'
+         '6a56efa9abcf914ebe380178749cf7ce'
+         '0e7b910fe26e9096e5531cd38f331292'
          '859a80ddb4c234506379c605114c1343'
          '47e093e8cfe4b5b96602358e1f540832'
-         'e27c8172763e547f6a4fe6dc1c1acbf6'
+         '5e7c8bc96718174cdd48751e7d7ebf0d'
          'b8f0414427c5d9afdc32ceb3ae56f82a'
          '6519d209e151cdc39ae7e1091d189c3e'
          'd1cdf28fcb1c88f0e504905771dd6040'
diff --git a/abs/core/LinHES-system/add_storage.py b/abs/core/LinHES-system/add_storage.py
index 260df2a..eec497f 100644
--- a/abs/core/LinHES-system/add_storage.py
+++ b/abs/core/LinHES-system/add_storage.py
@@ -12,6 +12,9 @@ import commands
 import sys,os
 import random, string
 import ConfigParser
+from ConfigParser import SafeConfigParser
+import glob
+
 from MythTV import MythDB, MythBE, Recorded, MythError
 from socket import timeout, gethostname
 
@@ -71,6 +74,7 @@ class disk_device:
         self.in_use = self.get_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)
@@ -78,6 +82,9 @@ class disk_device:
     def set_mmount(self,flag):
         self.mmount = flag
 
+    def set_disk_num(self,num):
+        self.disk_num=num
+
     def get_is_device(self,device_props):
         return device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsDrive")
 
@@ -174,7 +181,7 @@ class disk_device:
 
     def find_fstype(self,moutpoint):
         fstype="xfs"
-        mp=['/myth', '/data/storage/disk0/media']
+        mp=['/myth', '/data/storage/disk0']
         for i in self.fs_map:
                 if i[1] in mp:
                     fstype = i[2]
@@ -214,7 +221,7 @@ class disk_device:
         return fstab
 
     def find_options_type(self,fstab):
-        mp=['/myth', '/data/storage/disk0/media']
+        mp=['/myth', '/data/storage/disk0']
         for i in fstab:
             split_line=i.split()
             try:
@@ -239,6 +246,7 @@ class disk_device:
 
         if bind:
             new_fstab_list=["/data/storage/disk0" , self.new_mount_point , "none" , "rw,bind", '0', '0']
+            uuid=self.find_uuid(self.block_partition)
         else:
         #determine options
 
@@ -266,12 +274,12 @@ class disk_device:
         f.close()
         return
 
-    def mount_disk(self,mount=True):
+    def mount_disk(self,no_mount=False):
         try:
             os.stat(self.new_mount_point)
         except:
             os.makedirs(self.new_mount_point)
-        if mount == True:
+        if no_mount == False:
             cmd = "mount %s" %self.new_mount_point
             runcmd(cmd)
         return
@@ -332,6 +340,7 @@ class disk_device:
         self.config.set('storage','mountpoint',self.new_mount_point)
         self.config.set('storage','shareable','True')
         self.config.set('storage','mmount',self.mmount)
+        self.config.set('storage','disk_num',self.disk_num)
 
 
         filename="%s_%s.conf" %(self.model.replace(' ',''),
@@ -343,13 +352,16 @@ class disk_device:
             self.config.write(configfile)
         return
 
+    def symlink_disk(self):
+        print "    Creating symlink for disk number"
+        disk_ln="%s/disk%s" %(self.top_mount_dir,self.disk_num)
+        cmd = "ln -s %s %s" %(self.new_mount_point,disk_ln)
+        print cmd
+        runcmd(cmd)
+
     def symlink(self):
         pass
         print "    Creating symlink"
-        #cmd = "ln -s %s %s/myth " %(self.new_mount_point, self.top_mount_dir)
-        #runcmd(cmd)
-        #cmd = "ln -s %s %s/disk0 " %(self.new_mount_point, self.top_mount_dir)
-        #runcmd(cmd)
         cmd = "ln -s %s/media /myth " %(self.new_mount_point)
         runcmd(cmd)
 
@@ -460,7 +472,15 @@ def remove_pickle():
     except:
         pass
 
-
+def last_disk_num():
+    parser = SafeConfigParser()
+    num_list = []
+    for conf_file in glob.glob('%s/*.conf' %storage_dir):
+        parser.read(conf_file)
+        disk_num = parser.get('storage', 'disk_num')
+        num_list.append(int(disk_num))
+        num_list.sort()
+    return num_list[-1]
 #--------------------------------------------
 
 def main(scan_only, destruction, no_mount, install_call , dir_fe_only):
@@ -523,12 +543,18 @@ def main(scan_only, destruction, no_mount, install_call , dir_fe_only):
 
 
     #save new list to disk_device
-    write_known_drive_list(system_drive_list)
+    #write_known_drive_list(system_drive_list)
+    #disk_num = last_disk_num()
+
+
     if len(process_list) > 0:
         print "\n    Adding storage: \n"
         if prompt_to_continue() == True:
+            write_known_drive_list(system_drive_list)
+            disk_num = last_disk_num()
             for i in process_list:
                 print "    Drive: %s" %(i.get_name())
+                disk_num = disk_num + 1
                 if destruction == True:
                     i.partition_disk()
                     i.format_disk()
@@ -538,8 +564,10 @@ def main(scan_only, destruction, no_mount, install_call , dir_fe_only):
                     if dir_fe_only != True:
                         i.mkdirs(FS_LIST_BE)
                     i.mkdirs(FS_LIST_FE)
+                i.set_disk_num(disk_num)
                 i.write_config()
 
+                i.symlink_disk()
                 #always create the FE groups(video)..do not need to create Be
                 if dir_fe_only != True:
                     i.add_sg(DB,host,SG_MAP_BE)
@@ -600,6 +628,7 @@ def myth_main(no_mount,install_call,dir_fe_only):
     print "    Drive: %s" %(i.get_name())
     i.set_mmount(True)
     i.set_partition("7")
+    i.set_disk_num(0)
     i.add_fstab(True)
     #if not install_call:
     i.mount_disk(no_mount)
@@ -612,6 +641,155 @@ def myth_main(no_mount,install_call,dir_fe_only):
 
     i.symlink()
 
+class reconstruct_path:
+    def  __init__(self,conf_file):
+        self.conf_file = conf_file
+        parser = SafeConfigParser()
+        parser.read(self.conf_file)
+
+
+        self.uuid = parser.get('storage', 'uuid')
+        self.mount_point = parser.get('storage', 'mountpoint')
+        self.shareable = parser.get('storage', 'shareable')
+        self.myth_mount = parser.get('storage', 'mmount')
+        self.bind = self.myth_mount
+        self.disk_num = parser.get('storage', 'disk_num')
+        self.top_mount_dir = os.path.dirname(self.mount_point)
+
+    def get_conf(self):
+       return self.conf_file
+
+    def get_uuid(self):
+        return self.uuid
+
+    def get_mount_point(self):
+        return self.mount_point
+
+    def get_shareable(self):
+        return self.shareable
+
+    def get_is_myth_mount(self):
+        return self.myth_mount
+
+    def get_disk_num(self):
+        return self.disk_num
+
+    def create_mount_point(self):
+        try:
+            os.stat(self.mount_point)
+        except:
+            os.makedirs(self.mount_point)
+
+    def find_options_type(self,fstab):
+        mp=['/myth', '/data/storage/disk0']
+        for i in fstab:
+            split_line=i.split()
+            try:
+                if split_line[1] in mp:
+                    options = split_line[3]
+                    break
+                else:
+                    options = "defaults"
+                    mount_point = i
+            except:
+                options = "defaults"
+        return options,i
+
+    def read_fstab(self):
+        f = open('/etc/fstab', 'r')
+        fstab=f.readlines()
+        f.close()
+        return fstab
+
+    def check_in_fstab(self,fstab,check_path):
+
+        for line in fstab:
+            if line.find(check_path) > -1:
+                return True
+        return False
+
+    def append_fstab(self,line):
+        new_fstab_line='\t'.join(line)
+        new_fstab_line="%s\n" %new_fstab_line
+
+        f = open('/etc/fstab', 'a')
+        #f = open('/tmp/fstab', 'a')
+        f.write(new_fstab_line)
+        f.write("\n")
+        f.close()
+
+    def symlink(self):
+        print "    Creating symlink"
+        if not os.path.exists("/myth"):
+            cmd = "ln -s %s/media /myth " %(self.mount_point)
+            runcmd(cmd)
+        else:
+            print "    Skipping symlink, path already present"
+
+    def symlink_disk(self):
+        print "    Creating symlink for disk number"
+        disk_ln="%s/disk%s" %(self.top_mount_dir,self.disk_num)
+        cmd = "ln -s %s %s" %(self.mount_point,disk_ln)
+        runcmd(cmd)
+
+
+    def add_fstab(self):
+        new_fstab_list=['UUID=', 'mount_point', 'auto', 'defaults', '0', '1']
+        fstab=self.read_fstab()
+
+        if self.bind == "True":
+            self.symlink()
+
+        if self.check_in_fstab(fstab,self.uuid) == True:
+            print "    Found storage in fstab, will not add it"
+        else:
+            print "    Adding storage to fstab"
+            if self.bind == "True" :
+                print "    Bind mount"
+                new_fstab_list=["/data/storage/disk0" , self.mount_point , "none" , "rw,bind", '0', '0']
+                if self.check_in_fstab(fstab,self.mount_point) == False:
+                    self.append_fstab(new_fstab_list)
+                else:
+                    print "    Found bind storage in fstab, will not add it"
+
+            else:
+                #construct new line
+                new_options = self.find_options_type(fstab)[0]
+                new_fstab_list[0]="UUID=%s" %self.uuid
+                new_fstab_list[1]=self.mount_point
+                new_fstab_list[3]=new_options
+                self.append_fstab(new_fstab_list)
+
+    def mount_disk(self,no_mount=False):
+        try:
+            os.stat(self.mount_point)
+        except:
+            os.makedirs(self.mount_point)
+        if no_mount == False :
+            cmd = "mount %s" %self.mount_point
+            runcmd(cmd)
+        return
+
+def reconstruct_mounts(no_mount):
+    print "Recreating devices based on contents of /etc/storage.d"
+    for conf_file in glob.glob('%s/*.conf' %storage_dir):
+        print "\n"
+        cf = reconstruct_path(conf_file)
+
+        #print cf.get_conf()
+        #print cf.get_uuid()
+        print "    %s" %cf.get_mount_point()
+        #print cf.get_shareable()
+        #print cf.get_is_myth_mount()
+        #print cf.get_disk_num()
+
+        cf.create_mount_point()
+        cf.add_fstab()
+        cf.symlink_disk()
+        cf.mount_disk(no_mount)
+
+    pass
+
 
 def usage():
     help='''\n
@@ -647,7 +825,7 @@ if __name__ == "__main__":
     destruction = True
     install_call = False
     dir_fe_only = False
-
+    reconstruct = False
     try:
         os.remove("/tmp/scan_report")
     except:
@@ -677,10 +855,13 @@ if __name__ == "__main__":
     if "--fe_only" in sys.argv:
         dir_fe_only = True
 
-
+    if "--reconstruct" in sys.argv:
+        reconstruct = True
 
     if "--double_myth" in sys.argv:
         myth_main(no_mount,install_call,dir_fe_only)
+    elif reconstruct == True:
+        reconstruct_mounts(no_mount)
     else:
         main(scan_only,destruction,no_mount, install_call, dir_fe_only)
 
-- 
cgit v0.12

