-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmount.py
143 lines (103 loc) · 3.49 KB
/
mount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# Author: Christian Vallentin <[email protected]>
# Website: http://vallentinsource.com
# Repository: https://github.com/MrVallentin/mount.py
#
# Date Created: March 25, 2016
# Last Modified: March 27, 2016
#
# Developed and tested using Python 3.5.1
import os
def list_media_devices():
# If the major number is 8, that indicates it to be a disk device.
#
# The minor number is the partitions on the same device:
# - 0 means the entire disk
# - 1 is the primary
# - 2 is extended
# - 5 is logical partitions
# The maximum number of partitions is 15.
#
# Use `$ sudo fdisk -l` and `$ sudo sfdisk -l /dev/sda` for more information.
with open("/proc/partitions", "r") as f:
devices = []
for line in f.readlines()[2:]: # skip header lines
words = [ word.strip() for word in line.split() ]
minor_number = int(words[1])
device_name = words[3]
if (minor_number % 16) == 0:
path = "/sys/class/block/" + device_name
if os.path.islink(path):
if os.path.realpath(path).find("/usb") > 0:
devices.append("/dev/" + device_name)
return devices
def get_device_name(device):
return os.path.basename(device)
def get_device_block_path(device):
return "/sys/block/%s" % get_device_name(device)
def get_media_path(device):
return "/media/" + get_device_name(device)
def get_partition(device):
os.system("fdisk -l %s > output" % device)
with open("output", "r") as f:
data = f.read()
return data.split("\n")[-2].split()[0].strip()
def is_mounted(device):
return os.path.ismount(get_media_path(device))
def mount_partition(partition, name="usb"):
path = get_media_path(name)
if not is_mounted(path):
os.system("mkdir -p " + path)
os.system("mount %s %s" % (partition, path))
def unmount_partition(name="usb"):
path = get_media_path(name)
if is_mounted(path):
os.system("umount " + path)
#os.system("rm -rf " + path)
def mount(device, name=None):
if not name:
name = get_device_name(device)
mount_partition(get_partition(device), name)
def unmount(device, name=None):
if not name:
name = get_device_name(device)
unmount_partition(name)
def is_removable(device):
path = get_device_block_path(device) + "/removable"
if os.path.exists(path):
with open(path, "r") as f:
return f.read().strip() == "1"
return None
def get_size(device):
path = get_device_block_path(device) + "/size"
if os.path.exists(path):
with open(path, "r") as f:
# Multiply by 512, as Linux sectors are always considered to be 512 bytes long
# Resource: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/linux/types.h?id=v4.4-rc6#n121
return int(f.read().strip()) * 512
return -1
def get_model(device):
path = get_device_block_path(device) + "/device/model"
if os.path.exists(path):
with open(path, "r") as f:
return f.read().strip()
return None
def get_vendor(device):
path = get_device_block_path(device) + "/device/vendor"
if os.path.exists(path):
with open(path, "r") as f:
return f.read().strip()
return None
if __name__ == "__main__":
devices = list_media_devices()
for device in devices:
mount(device)
print("Drive:", get_device_name(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(" ")
unmount(device)