-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_qgroup.py
executable file
·221 lines (186 loc) · 7.53 KB
/
parse_qgroup.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
"""
Report on the QGroups.
Script to make a table of the subvolumes on the system and link them to
their sizes
"""
# import os
import sys
# import re
# import tkinter as tk
import subprocess
# TODO
# Implement a GUI for this using:
# https://wiki.wxpython.org/Getting%20Started#Building_a_simple_text_editor
# Constants
VERBOSE = True
class Filesystem:
def __init__(self, label, uuid, mountpoint):
self.label = label
self.uuid = uuid
self.mountpoint = mountpoint
def __str__(self):
return f'{self.label}, {self.uuid}, {self.mountpoint}'
class Snapshot:
def __init__(self,
filesystem: str,
snap_id: int,
gen: int,
parent: int,
groupid: int,
path: str,
excl_size: int,
refr_size: int):
self.filesystem = filesystem
self.snap_id = snap_id
self.gen = gen
self.parent = parent
self.groupid = groupid
self.path = path
self.excl_size = excl_size
self.refr_size = refr_size
def __str__(self):
return (f'ID {self.parent}/{self.snap_id} GroupID {self.groupid}'
f' {self.filesystem}/{self.path},'
f' EX: {pretty_bytes(self.excl_size)},'
f' RF: {pretty_bytes(self.refr_size)}')
def pretty_bytes(size: int, scale: int = 0, long: bool = False) -> str:
"""
Return a human readable size given an input in KBytes.
given a number of b, return a nice looking number like 1.21 GB
optional:
use powers of ten instead? This is controlled using the scale=1 arg
:param size: size in KBytes
:param scale: 0 for powers of 2, 1 for powers of 10
:param long: long form or sohort form of name
:return: pretty-description
"""
base = (1024, 1000)[scale]
powers = {
0: {'S0': 'B', 'L0': 'Bytes', 'S1': 'B', 'L1': 'Bytes'},
1: {'S0': 'KiB', 'L0': 'Kibibytes', 'S1': 'KB', 'L1': 'Kilobytes'},
2: {'S0': 'MiB', 'L0': 'Mebibytes', 'S1': 'MB', 'L1': 'Megabytes'},
3: {'S0': 'GiB', 'L0': 'Gibibytes', 'S1': 'GB', 'L1': 'Gigabytes'},
4: {'S0': 'TiB', 'L0': 'Tebibytes', 'S1': 'TB', 'L1': 'Terabytes'},
5: {'S0': 'PiB', 'L0': 'Pebibytes', 'S1': 'PB', 'L1': 'Petabytes'},
6: {'S0': 'EiB', 'L0': 'Exbibytes', 'S1': 'EB', 'L1': 'Exabytes'},
7: {'S0': 'ZiB', 'L0': 'Zebibytes', 'S1': 'ZB', 'L1': 'Zettabytes'},
8: {'S0': 'YiB', 'L0': 'Yobibytes', 'S1': 'YB', 'L1': 'Yottabytes'},
9: {'S0': 'RiB', 'L0': 'Ronibytes', 'S1': 'RB', 'L1': 'Ronnabytes'},
10: {'S0': 'QiB', 'L0': 'Quebibytes', 'S1': 'QB', 'L1': 'Quettabytes'}
}
size = int(size) * base # Usage reported in KB as default
pretty_size = ''
size = size / base # divide to get KiB/KB
for power, suffix in powers.items():
suffix_key = (f'L{scale}', f'S{scale}')[long]
divisor = base ** power
# print(f'1024 ^ {power} = {divisor}')
if size > divisor:
pretty_size = f'{(size/divisor):0.2f} {suffix[suffix_key]}'
return pretty_size
def run_external_cmd(cmd: list) -> subprocess.CompletedProcess:
"""
Run and external command and return the results.
:param cmd: command in the form of a list
:return: Completed process instance
"""
result = subprocess.run(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True)
# print('OUT:', result.stdout)
# print('ERR:', result.stderr)
return result
def btrfs_mounted_fs() -> list:
# Look for all btrfs mounted_fs
mounted_fs = []
mounts = run_external_cmd('mount')
for mount in mounts.stdout.split('\n'):
# print(mount)
components = mount.rstrip('\n').split(' ')
# print(components)
if len(components) > 3:
if components[4] == 'btrfs':
mounted_fs.append(components[2])
return mounted_fs
def dict_of_snapshots(filesystem: list) -> dict:
"""Return a dict of the snapshots for a given filesystem."""
# Returns a dict of {'id': 'snapshot_object'...}
snapshots = {}
snapshots['5'] = Snapshot(filesystem, 5, 0, 0, 0, '/', 0, 0)
parents = {}
parents[5] = 1
cmd = ['sudo', 'btrfs', 'subvolume', 'list', filesystem]
subvols = run_external_cmd(cmd)
for subvol in subvols.stdout.split('\n'):
if len(subvol) > 0:
(_, snap_id, _, gen, _, _, parent_id, _, path) = subvol.split()
snapshot = Snapshot(filesystem, snap_id, gen,
parent_id, 0, path, 0, 0)
snapshots[snap_id] = snapshot
if VERBOSE:
print(f'snapshot: {snapshot}')
print(f' There are: {len(snapshots)} snapshots.')
return snapshots
def get_size_of_snapshots(filesystem: str, snapshots: dict) -> dict:
"""Get the sizes of the filesystem snapshots."""
# get the qgroups to determine the size of each snapshop
cmd = ['sudo', 'btrfs', 'qgroup', 'show', '--raw', filesystem]
qgroup_show = run_external_cmd(cmd)
err = 'ERROR: can\'t list qgroups: quotas not enabled'
if err in qgroup_show.stderr.split('\n'):
print('Quotas not enabled:', qgroup_show.stderr.split('\n')[0])
cmd = ['sudo', 'btrfs', 'quota', 'enable', filesystem]
print('Starting the quota enabling', cmd)
result = run_external_cmd(cmd)
print('OUT:', result.stdout)
print('ERR:', result.stderr)
else:
for snapshot in qgroup_show.stdout.split('\n'):
# print('line:', snapshot)
if len(snapshot) > 0:
(qgroupid, refer, excl, path) = snapshot.split()
if ('Qgroupid' not in qgroupid and
r'--------' not in qgroupid):
if VERBOSE:
print(qgroupid, refer, excl, path)
qgroupid, snap_id = qgroupid.split('/')
snapshot = snapshots[snap_id]
# check the path
if path != snapshots[snap_id].path:
print(('Paths don\'t match!'
f' {path} - {snapshots[snap_id].path}'))
else:
# print(qgroupid, snap_id, components, snapshot)
snapshots[snap_id].qgroupid = qgroupid
snapshots[snap_id].refr_size = refer
snapshots[snap_id].excl_size = excl
if VERBOSE:
print(f'snapshot: {snapshots[snap_id]}')
return snapshots
def main(filesystems: list) -> None:
# get the list of snapshots for all of the filesystems
print(filesystems)
snapshots_by_filesystem = {}
for filesystem in filesystems:
print(f'Retrieving snapshots for {filesystem}')
snapshots = dict_of_snapshots(filesystem)
snapshots = get_size_of_snapshots(filesystem, snapshots)
for snap_id, snapshot in snapshots.items():
print(f'{snap_id}: {snapshot}')
snapshots_by_filesystem[filesystem] = snapshots
# win = tk.Tk()
# win.title('Parsing QGroups')
# win.resizable(0,0)
# win.mainloop()
if __name__ == '__main__':
# pass whatever command line args are there
print(sys.argv, len(sys.argv))
if len(sys.argv) == 1:
print('looking')
filesystems = btrfs_mounted_fs()
else:
filesystems = sys.argv[1:]
main(filesystems)