Added Linux mount and unmount functions

* If not running with root priviledges then udevil is used.
This commit is contained in:
2Shirt 2020-01-06 20:58:46 -07:00
parent c135d686df
commit b0b0b612a1
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
2 changed files with 40 additions and 25 deletions

View file

@ -128,19 +128,6 @@ def is_valid_path(path_obj, path_type):
return valid_path
def mount(mount_source, mount_point, read_write=False):
"""Mount mount_source on mount_point."""
os.makedirs(mount_point, exist_ok=True)
cmd = [
'mount',
mount_source,
mount_point,
'-o',
'rw' if read_write else 'ro',
]
run_program(cmd)
def prep_device(dev_path, label, use_mbr=False, indent=2):
"""Format device in preparation for applying the WizardKit components
@ -250,12 +237,6 @@ def show_selections(args, sources, ufd_dev, ufd_sources):
print_standard(' ')
def unmount(mount_point):
"""Unmount mount_point."""
cmd = ['umount', mount_point]
run_program(cmd)
def update_boot_entries(boot_entries, boot_files, iso_label, ufd_label):
"""Update boot files for UFD usage"""
configs = []

View file

@ -62,6 +62,27 @@ def make_temp_file():
return pathlib.Path(proc.stdout.strip())
def mount(source, mount_point=None, read_write=False):
"""Mount source (on mount_point if provided).
NOTE: If not running_as_root() then udevil will be used.
"""
cmd = [
'mount',
'-o', 'rw' if read_write else 'ro',
source,
]
if not running_as_root():
cmd.insert(0, 'udevil')
if mount_point:
cmd.append(mount_point)
# Run mount command
proc = run_program(cmd, check=False)
if not proc.returncode == 0:
raise RuntimeError(f'Failed to mount: {source} on {mount_point}')
def mount_volumes(device_path=None, read_write=False, scan_corestorage=False):
"""Mount all detected volumes, returns list.
@ -117,12 +138,7 @@ def mount_volumes(device_path=None, read_write=False, scan_corestorage=False):
# Attempt to mount volume
if not already_mounted:
cmd = [
'udevil',
'mount',
'-o', 'rw' if read_write else 'ro',
vol.path,
]
mount(vol.path, read_write=read_write)
proc = run_program(cmd, check=False)
if proc.returncode:
result += 'Failed to mount'
@ -202,5 +218,23 @@ def scan_corestorage_container(container, timeout=300):
return inner_volumes
def unmount(source_or_mountpoint):
"""Unmount source_or_mountpoint.
NOTE: If not running_as_root() then udevil will be used.
"""
cmd = [
'umount',
source_or_mountpoint,
]
if not running_as_root():
cmd.insert(0, 'udevil')
# Run unmount command
proc = run_program(cmd, check=False)
if not proc.returncode == 0:
raise RuntimeError(f'Failed to unmount: {source_or_mountpoint}')
if __name__ == '__main__':
print("This file is not meant to be called directly.")