Created
May 15, 2020 17:01
-
-
Save rodnaxel/e7cbf244214832762a19892ff0be2e18 to your computer and use it in GitHub Desktop.
The script uset to mount device by Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
def cmd(args: list, as_root=False): | |
args = args if not as_root else ['root'].extend(args) | |
print(args) | |
result = subprocess.run(args, | |
capture_output=True, | |
shell=False, | |
encoding='utf8') | |
return result.returncode, result.stdout | |
def show(filter='label'): | |
''' This function show available device ''' | |
_, r = cmd(['ls', '/dev/disk/by-label']) | |
return r.split() | |
def mount(label: str): | |
# Find device | |
err, output = cmd(['blkid', '--label', label]) | |
if err != 0: | |
raise FileNotFoundError("Device not found") | |
device = output.strip() | |
# Create mount point | |
username = cmd(['whoami'])[-1].strip() | |
mount_point = '/media/{}/{}'.format(username, label) | |
if not os.path.exists(mount_point): | |
cmd(('mkdir', '-p', mount_point), as_root=True) | |
print('Create mount point: {}'.format(mount_point)) | |
# Mount device to mount point | |
#mount -t ntfs-3g /dev/sda5 /media/tech/Recovery -o umask=0,nls=utf8 | |
err, _ = cmd(('mount', '-t', 'ntfs-3g', '-o', 'umask=0,nls=utf8', device, mount_point), as_root=True) | |
print('Mount return code: ', err) | |
def umount(label: str): | |
device = cmd(['blkid', '--label', label])[-1].strip() | |
err, _ = cmd(('sudo', 'umount', device)) | |
print("umount return code:", err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment