From 66f9118ec4eab7ec68ff3270536937ef99ba5932 Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Sat, 17 Dec 2022 18:13:07 -0500
Subject: linhes-system: add_storage.py: first updates for udisks2

---
 linhes/linhes-system/PKGBUILD       |   6 +-
 linhes/linhes-system/add_storage.py | 166 ++++++++++++++++++++----------------
 2 files changed, 97 insertions(+), 75 deletions(-)

diff --git a/linhes/linhes-system/PKGBUILD b/linhes/linhes-system/PKGBUILD
index 42492c9..20286a3 100755
--- a/linhes/linhes-system/PKGBUILD
+++ b/linhes/linhes-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=linhes-system
 pkgver=9.0.0
-pkgrel=9
+pkgrel=10
 arch=('x86_64')
 #install=$pkgname.install
 pkgdesc="Everything that makes LinHES a system"
@@ -19,14 +19,14 @@ source=($binfiles
     '10-monitor.conf' 'x11vnc.override.conf'
     'system-sudo.rules' 'linhes-profile.sh'
     'lh_system_start.sh.desktop')
-sha256sums=('96f67b5428debb7dac909893c56a7637bf6545c068732822981d4080125c53d9'
+sha256sums=('d4a4f7ba3445c9f2fb20da0b3cff6fcb2bd6ff845153c10a4a8fa66a8b5faf68'
             'b0e8fe4d04e1f779d52a28156489fb51efc13e173efccba2d6d458044bf35904'
             '4ab36bbabf0964f666278c225f4c2d41f4277acb42e9023163fa3a9599282466'
             '632eb5073219b86667a361e567ad766fbd122f27dc5e775bba9983c417686aa9'
             '11168c9cd3b117decaab6bc665c183b4aab917cf0a976bce4c1b5e4686a27bc9'
             'ae34515e144830f424d3bd3f6b1b446892d62beed20bca6f0fb19b0bbb779f27'
             '23358a7bff4968eccd469613b81b1415c2ae0ebe77f14f74426697333e4d88d7'
-            'a664ca7d13373f5fe6eb7747939624d7342039f04a19c06d602e83f89b30de16'
+            '5698b2899ecb8a9cce610aecb4deaa17078cee37b0f6d8e262e360b20f27efe3'
             '91bdec992bb2c933e15625c181f2195c402060b879168ebf35944cb064c904b9'
             'cedd34e448d3d45ab0f9fff40c115401f45bdea5b99215d0aff514a759ae2b7c'
             '76f023c0cde7fea269234f1b29c32b117b91769217d4b1b8a3922daceb25f9f8'
diff --git a/linhes/linhes-system/add_storage.py b/linhes/linhes-system/add_storage.py
index 041f612..9f2e61c 100755
--- a/linhes/linhes-system/add_storage.py
+++ b/linhes/linhes-system/add_storage.py
@@ -5,12 +5,12 @@
 #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
 
 import dbus
 import pickle
 import subprocess
-import sys,os
+import sys,os,re
 import random, string
 import configparser
 from configparser import SafeConfigParser
@@ -46,35 +46,70 @@ for key in list(SG_MAP.keys()):
 
 class disk_device:
     def  __init__(self,device,storage_dir):
-        device_obj = bus.get_object("org.freedesktop.UDisks", device)
-        device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
+        block_dev = bus.get_object("org.freedesktop.UDisks2", 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)
+        self.read_only = self.get_is_readonly(block_dev)
+        print("ReadOnly:", self.read_only)
+        self.device_file_path = self.get_device_file_path(block_dev)
+        print("Device File Path:", 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)
+        self.is_device = self.get_is_device()
+        print("Is Device:", self.is_device)
+
+
+        self.drive = block_dev.Get('org.freedesktop.UDisks2.Block', 'Drive', dbus_interface='org.freedesktop.DBus.Properties')
+        print("Drive:", self.drive)
+        drive_dev = bus.get_object("org.freedesktop.UDisks2", self.drive)
+
         self.storage_dir = storage_dir
         self.top_mount_dir = "/data/storage"
         self.config = configparser.RawConfigParser()
         self.fs_map = self.get_fsmap()
-        self.is_device = self.get_is_device(device_props)
-        self.vendor = self.get_vendor(device_props)
-        self.model = self.get_model(device_props)
+
+        self.vendor = drive_dev.Get('org.freedesktop.UDisks2.Drive', 'Vendor', dbus_interface='org.freedesktop.DBus.Properties')
+        print("Vendor:", self.vendor)
+        self.model = drive_dev.Get('org.freedesktop.UDisks2.Drive', 'Model', dbus_interface='org.freedesktop.DBus.Properties')
+        print("Model:", 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)
+        self.serial_number = self.get_serial_number(drive_dev)
+        print("Serial:", self.serial_number)
+        self.is_optical = self.get_is_optical_disc(drive_dev)
+        print("Is Optical:", self.is_optical)
 
         self.mmount = False
         self.dir_sg = False
-        self.f = self.get_mounts(device_props)
-        self.is_mounted = self.get_is_mounted(device_props)
-
-        self.parition_size = self.get_partition_size(device_props)
-        self.device_size = self.get_device_size(device_props)
-        self.serial_number = self.get_serial_number(device_props)
-        self.read_only = self.get_is_readonly(device_props)
-        self.is_optical = self.get_is_optical_disc(device_props)
-        self.connection = self.get_connections(device_props)
-        self.block_path = self.get_device_file(device_props)
-        self.device_file_path = self.get_device_file_path(device_props)
-        self.block_partition="%s1" %self.block_path
+
+        try:
+            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])
+        except:
+            self.f = ['']
+            self.is_mounted = False
+            print("MountPoints:", self.f[0])
+        print("Is Mounted:", 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.in_use = self.get_in_use()
+        print("In Use:", 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)
 
@@ -87,26 +122,32 @@ class disk_device:
     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")
-
-    def get_vendor(self,device_props):
-        return device_props.Get('org.freedesktop.UDisks.Device', "DriveVendor")
-
-    def get_model(self,device_props):
-        return device_props.Get('org.freedesktop.UDisks.Device', "DriveModel")
-
-    def get_mounts(self,device_props):
-        return device_props.Get('org.freedesktop.UDisks.Device', "DeviceMountPaths")
-
-    def get_is_mounted(self,device_props):
-       return device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsMounted")
-
     def get_name(self):
         filename="%s_%s" %(self.model.replace(' ',''),
                                 self.serial_number.replace(' ',''))
         return filename
 
+    def get_is_readonly(self,block_dev):
+        readonly = block_dev.Get('org.freedesktop.UDisks2.Block', 'ReadOnly', dbus_interface='org.freedesktop.DBus.Properties')
+        if readonly == 0:
+            return False
+        else:
+            return True
+
+    def get_is_optical_disc(self,drive_dev):
+        optical = drive_dev.Get('org.freedesktop.UDisks2.Drive', 'Optical', dbus_interface='org.freedesktop.DBus.Properties')
+        if optical == 0:
+            return False
+        else:
+            return True
+
+    def get_is_device(self):
+        match = re.search(r'part\d+$', self.device_id)
+        if match is None:
+            return True
+        else:
+            return False
+
     def get_in_use(self):
         in_use = False
         for i in self.fs_map:
@@ -115,43 +156,24 @@ class disk_device:
                 break
         return in_use
 
-    def get_partition_size(self,device_props):
-        return device_props.Get('org.freedesktop.UDisks.Device', "PartitionSize")
-
-    def get_device_size(self,device_props):
-        return device_props.Get('org.freedesktop.UDisks.Device', "DeviceSize")
-
-
-    def get_serial_number(self,device_props):
-        serial_number = device_props.Get('org.freedesktop.UDisks.Device', "DriveSerial")
+    def get_serial_number(self,drive_dev):
+        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 = "NSW%s" %serial_number
         return serial_number
 
-    def get_is_readonly(self,device_props):
-        return device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsReadOnly")
-
-    def get_is_optical_disc(self,device_props):
-        return device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsOpticalDisc")
-
-    def get_connections(self,device_props):
-       return device_props.Get('org.freedesktop.UDisks.Device', "DriveConnectionInterface")
-
-    def get_device_file(self,device_props):
-       return  device_props.Get('org.freedesktop.UDisks.Device', "DeviceFile")
-
-    def get_device_file_path(self,device_props):
-        path = device_props.Get('org.freedesktop.UDisks.Device', "DeviceFileByPath")
+    def get_device_file_path(self,block_dev):
+        paths = block_dev.Get('org.freedesktop.UDisks2.Block', 'Symlinks', dbus_interface='org.freedesktop.DBus.Properties')
         try:
-            path = path[0]
+            for path in paths:
+                path = bytearray(path).replace(b'\x00', b'').decode('utf-8')
+                if path.startswith('/dev/disk/by-path'):
+                    return path
         except:
             path = "None"
 
-        return path
-
-
     def partition_disk(self):
         print("    Creating new partition table")
         cmd = "parted -s -a optimal %s mklabel gpt" %self.block_path
@@ -417,15 +439,15 @@ def runcmd(cmd):
 
 
 def scan_system():
-
-    ud_manager_obj = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
-    ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')
+    ud_manager_obj = bus.get_object("org.freedesktop.UDisks2", "/org/freedesktop/UDisks2")
+    ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.DBus.ObjectManager')
     current_drive_list=[]
-    for dev in ud_manager.EnumerateDevices():
-        drive = disk_device(dev,storage_dir)
-
-        if drive.is_device and drive.device_size > 5000 and not drive.is_optical :
-            current_drive_list.append(drive)
+    for dev in ud_manager.GetManagedObjects().items():
+        if dev[0].startswith("/org/freedesktop/UDisks2/block_devices"):
+            print('\n' + 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)
     return current_drive_list
 
 def read_known_list():
@@ -706,8 +728,8 @@ def main(scan_only, destruction, no_mount, install_call, dir_sg):
                     i.add_sg(DB,host,SG_MAP)
 
                 print("-----")
-            cmd = "systemconfig.py -m fileshare"
-            runcmd(cmd)
+            #cmd = "systemconfig.py -m fileshare"
+            #runcmd(cmd)
         #i.add_sg(DB,host,SG_MAP)
 
 def myth_main(no_mount,install_call,dir_sg):
@@ -1149,7 +1171,7 @@ if __name__ == "__main__":
     if "--no_mount" in sys.argv :
         no_mount = True
 
-    if "--no_destruction" in sys.argv:
+    if "--no_destruction" or "-nd" in sys.argv:
         destruction = False
 
     if "--new_init" in sys.argv :
-- 
cgit v0.12

