From 606efac3fe96b05de7993506d2347605aa7c1a2b Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Tue, 4 Jun 2019 20:09:56 -0600 Subject: [PATCH] Updated mounting sections --- .bin/Scripts/functions/data.py | 51 ++++++++++++++++++++++++++------ .bin/Scripts/mount-backup-shares | 3 -- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/.bin/Scripts/functions/data.py b/.bin/Scripts/functions/data.py index fa0bb20c..c359ab6c 100644 --- a/.bin/Scripts/functions/data.py +++ b/.bin/Scripts/functions/data.py @@ -151,12 +151,16 @@ def is_valid_wim_file(item): def get_mounted_volumes(): """Get mounted volumes, returns dict.""" cmd = [ - 'findmnt', '-J', '-b', '-i', - '-t', ( + 'findmnt', + '--list', + '--json', + '--bytes', + '--invert', + '--types', ( 'autofs,binfmt_misc,bpf,cgroup,cgroup2,configfs,debugfs,devpts,' 'devtmpfs,hugetlbfs,mqueue,proc,pstore,securityfs,sysfs,tmpfs' ), - '-o', 'SOURCE,TARGET,FSTYPE,LABEL,SIZE,AVAIL,USED'] + '--output', 'SOURCE,TARGET,FSTYPE,LABEL,SIZE,AVAIL,USED'] json_data = get_json_from_command(cmd) mounted_volumes = [] for item in json_data.get('filesystems', []): @@ -195,6 +199,8 @@ def mount_volumes( volumes.update({child['name']: child}) for grandchild in child.get('children', []): volumes.update({grandchild['name']: grandchild}) + for great_grandchild in grandchild.get('children', []): + volumes.update({great_grandchild['name']: great_grandchild}) # Get list of mounted volumes mounted_volumes = get_mounted_volumes() @@ -237,6 +243,7 @@ def mount_volumes( if vol_data['show_data']['data'] == 'Failed to mount': vol_data['mount_point'] = None else: + fstype = vol_data.get('fstype', 'UNKNOWN FS') size_used = human_readable_size( mounted_volumes[vol_path]['used'], decimals=1, @@ -250,8 +257,9 @@ def mount_volumes( vol_data['mount_point'] = mounted_volumes[vol_path]['target'] vol_data['show_data']['data'] = 'Mounted on {}'.format( mounted_volumes[vol_path]['target']) - vol_data['show_data']['data'] = '{:40} ({} used, {} free)'.format( + vol_data['show_data']['data'] = '{:40} ({}, {} used, {} free)'.format( vol_data['show_data']['data'], + fstype, size_used, size_avail) @@ -285,6 +293,14 @@ def mount_backup_shares(read_write=False): def mount_network_share(server, read_write=False): """Mount a network share defined by server.""" + uid = '1000' + + # Get UID + cmd = ['id', '--user', 'tech'] + result = run_program(cmd, check=False, encoding='utf-8', errors='ignore') + if result.stdout.strip().isnumeric(): + uid = result.stdout.strip() + if read_write: username = server['RW-User'] password = server['RW-Pass'] @@ -300,18 +316,35 @@ def mount_network_share(server, read_write=False): error = r'Failed to mount \\{Name}\{Share} ({IP})'.format(**server) success = 'Mounted {Name}'.format(**server) elif psutil.LINUX: + # Make mountpoint cmd = [ 'sudo', 'mkdir', '-p', '/Backups/{Name}'.format(**server)] run_program(cmd) + + # Set mount options + cmd_options = [ + # Assuming GID matches UID + 'gid={}'.format(uid), + 'uid={}'.format(uid), + ] + cmd_options.append('rw' if read_write else 'ro') + cmd_options.append('username={}'.format(username)) + if password: + cmd_options.append('password={}'.format(password)) + else: + # Skip password check + cmd_options.append('guest') + + # Set mount command cmd = [ 'sudo', 'mount', - '//{IP}/{Share}'.format(**server), + '//{IP}/{Share}'.format(**server).replace('\\', '/'), '/Backups/{Name}'.format(**server), - '-o', '{}username={},password={}'.format( - '' if read_write else 'ro,', - username, - password)] + '-o', ','.join(cmd_options), + ] + + # Set result messages warning = 'Failed to mount /Backups/{Name}, {IP} unreachable.'.format( **server) error = 'Failed to mount /Backups/{Name}'.format(**server) diff --git a/.bin/Scripts/mount-backup-shares b/.bin/Scripts/mount-backup-shares index 6a1e88d7..0d8b7fd3 100755 --- a/.bin/Scripts/mount-backup-shares +++ b/.bin/Scripts/mount-backup-shares @@ -16,9 +16,6 @@ if __name__ == '__main__': # Prep clear_screen() - # Connect - connect_to_network() - # Mount if is_connected(): mount_backup_shares(read_write=True)