-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_gen_rep.py
167 lines (123 loc) · 6.2 KB
/
image_gen_rep.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import pytsk3
import os
import gzip
import itertools
import threading
import time
import sys
from tqdm import tqdm
import hashlib
import subprocess
import datetime
from jinja2 import Template
import re
import getpass
def get_info(volume_loc):
disk = volume_loc
parted_command = ['sudo', 'parted', disk, 'print']
grep_command = ['grep', 'Partition Table']
awk_command = ['awk', '{print $3}']
parted_output = subprocess.run(parted_command, capture_output=True, text=True, check=True).stdout
grep_output = subprocess.run(grep_command, input=parted_output, capture_output=True, text=True, check=True).stdout
partition_table = subprocess.run(awk_command, input=grep_output, capture_output=True, text=True, check=True).stdout.strip()
print(partition_table)
if(partition_table == "mbr" or partition_table =="msdos"):
output = subprocess.run(['sudo', 'fdisk', '-l', disk], capture_output=True, text=True).stdout
total_used_bytes = 0
pattern = r'^/dev/(\S+)\s+\*?\s+(\d+)\s+(\d+)\s+(\d+)\s+([\d.]+\w?)\s+([a-zA-Z0-9]*)\s+(.*?)\s*$'
partitions = []
lines = output.splitlines()
for line in lines:
match = re.match(pattern, line)
if match:
name = match.group(1)
start = int(match.group(2))
end = int(match.group(3))
sectors = int(match.group(4))
size = match.group(5)
id = match.group(6)
type = match.group(7)
partitions.append({
'name': name,
'start': start,
'end': end,
'sectors': sectors,
'size': size,
'id': id,
'type': type
})
partition_information = []
for part in partitions:
partition_information.append(f'<b>Partition {part["name"]}:</b><br>')
partition_information.append(f'<b>Start:</b> {part["start"]}<br>')
partition_information.append(f'<b>End:</b> {part["end"]}<br>')
partition_information.append(f'<b>Sectors:</b> {part["sectors"]}<br>')
partition_information.append(f'<b>Size:</b> {part["size"]}<br>')
partition_information.append(f'<b>ID:</b> {part["id"]}<br>')
partition_information.append(f'<b>Type:</b> {part["type"]}<br>')
partition_information.append('<br>')
# nname = String(["name"])
nname = part["name"]
if not nname.startswith("/dev/"):
nname = "/dev/"+nname
print(nname)
df_output = subprocess.run(['df', '-B1', nname], capture_output=True, text=True).stdout.strip()
used_amount = df_output.splitlines()[1].split()[2]
partition_information.append(f'<b>Memory Used (in bytes):</b><br>{used_amount}<br><br>')
total_used_bytes += int(used_amount)
partition_information = '\n'.join(partition_information)
else:
total_used_bytes=0
# disk = '/dev/nvme0n1'
output = subprocess.run(['sudo', 'fdisk', '-l', disk], capture_output=True, text=True).stdout
pattern = r'^(/dev/.*?p\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+([\d.]+\w?)\s+(.*?)$'
partitions = []
lines = output.splitlines()
for line in lines:
match = re.match(pattern, line)
if match:
name = match.group(1)
start = int(match.group(2))
end = int(match.group(3))
sectors = int(match.group(4))
size = match.group(5)
type = match.group(6)
partitions.append({
'name': name,
'start': start,
'end': end,
'sectors': sectors,
'size': size,
'type': type
})
partition_information = []
for part in partitions:
partition_information.append(f'<b>Partition {part["name"]}:</b><br>')
partition_information.append(f'<b>Start:</b> {part["start"]}<br>')
partition_information.append(f'<b>End:</b> {part["end"]}<br>')
partition_information.append(f'<b>Sectors:</b> {part["sectors"]}<br>')
partition_information.append(f'<b>Size:</b> {part["size"]}<br>')
partition_information.append(f'<b>Type:</b> {part["type"]}<br>')
partition_information.append('<br>')
nname = part["name"]
if not nname.startswith("/dev/"):
nname = "/dev/"+nname
print(nname)
df_output = subprocess.run(['df', '-B1', nname], capture_output=True, text=True).stdout.strip()
used_amount = df_output.splitlines()[1].split()[2]
partition_information.append(f'<b>Partition Size:</b><br>{used_amount}<br><br>')
total_used_bytes += int(used_amount)
partition_information = '\n'.join(partition_information)
cmd_vendor_name = ['cat', f'/sys/block/{disk.split("/")[-1]}/device/vendor']
vendor_name = subprocess.run(cmd_vendor_name, capture_output=True, text=True).stdout.strip()
cmd_model = ['cat', f'/sys/block/{disk.split("/")[-1]}/device/model']
model = subprocess.run(cmd_model, capture_output=True, text=True).stdout.strip()
cmd_serial = ['lsblk', '-no', 'SERIAL', disk]
serial_number = subprocess.run(cmd_serial, capture_output=True, text=True).stdout.strip()
cmd_total_sectors = ['cat', f'/sys/class/block/{disk.split("/")[-1]}/size']
total_sectors = subprocess.run(cmd_total_sectors, capture_output=True, text=True).stdout.strip()
cmd_physical_block_size = ['cat', f'/sys/block/{disk.split("/")[-1]}/queue/physical_block_size']
physical_block_size = subprocess.run(cmd_physical_block_size, capture_output=True, text=True).stdout.strip()
cmd_logical_block_size = ['cat', f'/sys/block/{disk.split("/")[-1]}/queue/logical_block_size']
logical_block_size = subprocess.run(cmd_logical_block_size, capture_output=True, text=True).stdout.strip()
return physical_block_size, logical_block_size, total_sectors, partition_information, model, serial_number, total_used_bytes, partition_table