-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyDisk.py
More file actions
165 lines (138 loc) · 5.57 KB
/
Copy pathMyDisk.py
File metadata and controls
165 lines (138 loc) · 5.57 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import json
import ctypes
import os
from subprocess import STDOUT, PIPE, Popen, check_output, CalledProcessError
import com
class prog:
umount = ['umount']
lsblk = ['lsblk', '--json', '--noheadings', '-o', 'name,size,model,serial,fstype,label']
rsync = ['rsync', '--recursive', '--compress-level=9', '--human-readable', '--progress', '--no-perms', '--no-owner', '--no-group', '--no-times', '--ignore-existing', '--exclude-from=/etc/rsync_exclude.conf']
cp = ['cp', '/media/cw/Drew/Live_USB/scripts/rsync_exclude.conf', '/etc/rsync_exclude.conf']
ntfs = ['lowntfs-3g', '-o', 'windows_names,ignore_case']
cifs = ['mount.cifs', '-o', 'username=root,password=cw8400', '//nas/data', '/media/data']
findd = ['find', '', '-type', 'd']
findf = ['find', '', '-type', 'f']
mkdir = ['mkdir','-p', '']
ddr = ['ddrescue','--cluster-size=1024', '--skip-size=128s,1M', '--reopen-on-error', '--force', '--verbose', '', '']
# ddr = ['ddrescue', '','']
# class container for Ignore lists
# TODO set up regex for these to avoid adding loop0 loop1...loopX
class ignore:
filesystems = ['iso9660', 'squashfs', 'crypto_LUKS', None, 'swap']
devices = ['sr0', 'sr1', 'loop0']
sn = ['C860008AE288B0B109030049']
# List Filesystem Tree topdown
def GetTree(path='/mnt'):
prog.findd[1]=path
find = Popen(prog.findd, stdout=PIPE, stderr=PIPE)
out, err = find.communicate()
return([s.strip() for s in out.splitlines()])
def Rescue(oldfile, newfile):
prog.ddr[6] = com.shellQoute(oldfile)
prog.ddr[7] = com.shellQoute(newfile)
# ddr = Popen(prog.ddr, stdout=PIPE, stderr=PIPE, shell=True)
# out, err = ddr.communicate()
# print("{}".format(" ".join(prog.ddr)))
return("{}".format(" ".join(prog.ddr)))
# print("ddrescue stdout = {}".format(out))
# print("ddrescue stderr = {}".format(err))
def GetFiles(path):
prog.findf[1]=path
find = Popen(prog.findf, stdout=PIPE, stderr=PIPE)
out, err = find.communicate()
return([s.strip() for s in out.splitlines()])
def SetTree(path):
prog.mkdir[2] = com.shellQoute(path)
try:
mkdir = check_output(prog.mkdir, stderr=PIPE, shell=True)
except CalledProcessError as e:
print("mkdir returned the following error: {}".format(str(e.output)))
# Print block file systems
def listFileSystems():
lsblk = Popen(prog.lsblk, stdout=PIPE, stderr=PIPE)
out, err = lsblk.communicate()
# print(out)
try:
decoded = json.loads(out.decode("utf-8"))
# Access data
for x in decoded['blockdevices']:
if x['name'] not in ignore.devices and x['serial'] not in ignore.sn: # Display valid disks with a SN
print(com.color.HEADER+"Drive: "+com.color.OKGREEN+"/dev/"+x['name']+com.color.END)
print(com.color.HEADER+"Size: "+com.color.WARNING+x['size']+com.color.END)
if x['model'] is not None:
print(com.color.HEADER+"Model: "+com.color.END+x['model'])
if x['serial'] is not None:
print(com.color.HEADER+"Serial: "+com.color.END+x['serial'])
print("")
for c in x['children']:
if c['fstype'] not in ignore.filesystems:
print('\t'+com.color.UNDERLINE+'Partition:'+com.color.END)
print("\t"+com.color.HEADER+"Name: "+com.color.OKGREEN+"/dev/"+c['name']+com.color.END)
if c['label'] is not None:
print("\t"+com.color.HEADER+"Label: "+com.color.END+c['label'])
if c['fstype'] is not None:
print("\t"+com.color.HEADER+"Type: "+com.color.END+c['fstype'])
else:
print("\t"+com.color.HEADER+"Type: "+com.color.FAIL+"UNKNOWN"+com.color.END)
if c['size'] is not None:
print("\t"+com.color.HEADER+"Size: "+com.color.WARNING+c['size']+com.color.END)
else:
print("\t"+com.color.HEADER+"Size: "+com.color.FAIL+"UNKNOWN"+com.color.END)
print("")
print("") # add a blank line at the end of each group as some values may not print
except (ValueError, KeyError, TypeError):
print(color.FAIL+"There was a problem parsing the JavaScript Object Notation (JSON)"+com.color.END)
print(ValueError.msg)
exit(1)
_libc = ctypes.cdll.LoadLibrary("libc.so.6")
# Simple wrapper around mount(2) and umount(2).
# Not thoroughly tested, and not very talkative.
class FLAGS:
def __new__(self):
raise NotImplementedError("This class is non-instantiable.")
def flag_bits(count):
flag = 1
for i in range(count):
yield flag
flag <<= 1
(
MS_RDONLY, # 0
MS_NOSUID, # 1
MS_NODEV, # 2
MS_NOEXEC, # 3
MS_SYNCHRONOUS, # 4
MS_REMOUNT, # 5
MS_MANDLOCK, # 6
MS_DIRSYNC, # 7
_, _, # SKIP 8, 9
MS_NOATIME, # 10
MS_NODIRATIME, # 11
MS_BIND, # 12
MS_MOVE, # 13
MS_REC, # 14
MS_SILENT, # 15
MS_POSIXACL, # 16
MS_UNBINDABLE, # 17
MS_PRIVATE, # 18
MS_SLAVE, # 19
MS_SHARED, # 20
MS_RELATIME, # 21
MS_KERNMOUNT, # 22
MS_I_VERSION, # 23
MS_STRICTATIME, # 24
_, _, _, _, _, # SKIP 25-29
MS_ACTIVE, # 30
MS_NOUSER, # 31
) = flag_bits(32)
del flag_bits, _
MS_MGC_VAL = 0xc0ed0000
MS_MGC_MSK = 0xffff0000
def mount(source, target, fstype, flags=0, data=None):
flags = (flags & FLAGS.MS_MGC_MSK) | FLAGS.MS_MGC_VAL
result = _libc.mount(ctypes.c_char_p(source), ctypes.c_char_p(target), ctypes.c_char_p(fstype), flags, ctypes.c_char_p(data) if data is not None else 0)
if result != 0:
raise OSError(ctypes.get_errno())
def umount(target):
result = _libc.umount(ctypes.c_char_p(target))
if result != 0:
raise OSError(ctypes.get_errno())