Skip to content

Module for listing, mounting and unmounting media devices

License

Notifications You must be signed in to change notification settings

vallentin/mount.py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mount.py

mount.py is a simple, small and self-contained Python only module, for listing, mounting and unmounting media devices on Linux.

mount.py is developed and tested using Python 3.5.1. It was initially developed for and tested on a Raspberry Pi Model B+ running Raspbian GNU/Linux.

Advice: It isn't optimal, but I would highly advice to mount and unmount devices when working with them, or at least just frequently unmount devices if they haven't been used for some time. As forgetting to unmount a device, before pulling it out can result in a corrupt device. The same goes for when manually selecting a directory name when mounting. Don't try and mount multiple devices to the same directory, it can as well corrupt and wipe the connected device(s).

Install

Installation is easily done through the terminal.

Using git

$ git clone https://github.com/MrVallentin/mount.py
$ sudo python mount.py/setup.py install
$ rm -rf mount.py

Using curl & unzip

$ curl -o mount.py.zip -L https://github.com/MrVallentin/mount.py/archive/master.zip
$ unzip mount.py.zip
$ sudo python mount.py-master/setup.py install
$ rm -rf mount.py-master
$ rm -rf mount.py.zip

Using wget & unzip

$ wget https://github.com/MrVallentin/mount.py/archive/master.zip -O mount.py.zip
$ unzip mount.py.zip
$ sudo python mount.py-master/setup.py install
$ rm -rf mount.py-master
$ rm -rf mount.py.zip
Uninstall

Download the module using any of the above, and then do the following:

$ sudo python mount.py/setup.py install --record files.txt
$ cat mount.py/files.txt | sudo xargs rm -rf

Testing

After installation checking whether mount.py is working, can simply be done by doing:

$ sudo python -m mount

The output is the same as the List USB Devices example.

How does it work?

Internally mount.py relies on os.system() calls, which in turn some require superuser access.

When listing media devices mount.py queries and parses /proc/partitions. When getting partitions fdisk is queried. Last for not least mkdir and mount is used for mounting, follow by umount used for unmounting.

Examples

API Reference

The following references and examples are based on mount.py being imported using from mount import *.

list_media_devices()

Returns a list containing all the device paths, for media devices/block devices (suchs as USB flash drives).

Example:

Tested with 2 USBs connected.

>>> list_media_devices()

['/dev/sda', '/dev/sdb']


```python
get_device_name(device)

Returns the device name from the device path.

>>> get_device_name("/dev/sda")

'sda'


```python
get_device_block_path(device)

Returns the system block device path.

>>> get_device_block_path("/dev/sda")

'/sys/block/sda'

get_device_block_path("sda") '/sys/block/sda'


```python
get_media_path(device)

Returns the media path, where files on the device can be found after mounting.

>>> get_media_path("/dev/sda")

/media/sda

get_media_path("sda") /media/sda


```python
get_partition(device)

Returns the partition path.

Requires superuser.

>>> get_partition("/dev/sda")

/dev/sda1


```python
is_mounted(device)

Returns True or False whether a device is mounted or not.

mount(device, name=None)

Mounts a specific device to a directory named name in /media. If the directory in /media doesn't exist, then it is created.

If name is None then name is equivalent to name = get_device_name(device).

mount() can safely be called no matter whether a device is already mounted or not.

Requires superuser.

>>> mount("/dev/sda")

```python
unmount(device, name=None)

Unmounts a specific device from the directory named name in /media.

If name is None then name is equivalent to name = get_device_name(device).

unmount() can safely be called no matter whether a device is already mounted or not.

Requires superuser.

>>> unmount("/dev/sda")

```python
is_removable(device)

Returns True or False whether a device is removable or not.

get_size(device)

Returns the total storage space in bytes.

Example:

Tested with a 16 GB USB connected.

>>> "%.2f" % (get_size("/dev/sda") / 1024 ** 3)

'14.84'


```python
get_model(device)

Returns the model name.

get_vendor(device)

Returns the vendor name.

Examples

Example: List USB Devices

The following example, is the equivalent of doing sudo python -m mount in the terminal.

import os
from mount import *

devices = list_media_devices()

for device in devices:
	name = get_device_name(device)
	path = get_media_path(device)
	
	print("Drive:", get_device_name(device))
	
	mount(device)
	
	print("Mounted:", "Yes" if is_mounted(device) else "No")
	print("Removable:", "Yes" if is_removable(device) else "No")
	print("Size:", get_size(device), "bytes")
	print("Size:", "%.2f" % (get_size(device) / 1024 ** 3), "GB")
	print("Model:", get_model(device))
	print("Vendor:", get_vendor(device))
	print()

Tested using a 16 GB USB, the above prints:

Drive: sda
Mounted: Yes
Removable: Yes
Size: 15931539456 bytes
Size: 14.84 GB
Model: SD/MMC
Vendor: Generic-

Example: List Files on USB Devices

import os
from mount import *

def list_files(root, indent=1):
	for filename in os.listdir(root):
		path = os.path.join(root, filename)
		
		if os.path.isfile(path):
			print("-" * indent, filename)
		elif os.path.isdir(path):
			print("+" * indent, filename)
			list_files(path, indent + 1)

devices = list_media_devices()

for device in devices:
	print("Drive:", get_device_name(device))
	
	mount(device)
	
	if is_mounted(device):
		print("Files:")
		list_files(get_media_path(device))
	
	unmount(device)
	
	print()

Using a USB with a few test files on, the above prints:

Drive: sda
Files:
- file.txt
+ folder
-- file in folder.txt
++ subfolder
--- file in subfolder.txt

Example: Write to USB

import os
from mount import *

devices = list_media_devices()

device = None
for check_device in devices:
	# Compare model, vendor, size, etc
	# to find the target drive.
	
	# In this example, we're just
	# gonna just the first.
	device = check_device
	break

if device:
	path = get_media_path(device)
	
	mount(device)
	
	if is_mounted(device):
		with open(path + "/hello-world.txt", "w") as f:
			f.write("Hello World")
	
	unmount(device)

Example: Read from USB

import os
from mount import *

devices = list_media_devices()

device = None
for check_device in devices:
	# Compare model, vendor, size, etc
	# to find the target drive.
	
	# In this example, we're just
	# gonna just the first.
	device = check_device
	break

if device:
	path = get_media_path(device)
	
	mount(device)
	
	if is_mounted(device):
		with open(path + "/hello-world.txt", "r") as f:
			print(f.read())
	
	unmount(device)

Targeting the same USB, and having executed the Write to USB example prior to executing this example. Then the above prints:

Hello World

Example: Detect USB Connections

TO DO

Reporting Bugs & Requests

Feel free to use the issue tracker, for reporting bugs, submitting patches or requesting features.

Before submitting bugs, make sure that you're using the latest version of mount.py.

License

This module is shared under the MIT license, and is therefore free to use, share, distribute and modify. See LICENSE for more details.

About

Module for listing, mounting and unmounting media devices

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages