forked from openebs/rawfile-localpv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs_util.py
47 lines (39 loc) · 1.22 KB
/
fs_util.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
import json
import os
import subprocess
def path_stats(path):
fs_stat = os.statvfs(path)
return {
"fs_size": fs_stat.f_frsize * fs_stat.f_blocks,
"fs_avail": fs_stat.f_frsize * fs_stat.f_bavail,
"fs_files": fs_stat.f_files,
"fs_files_avail": fs_stat.f_favail,
}
def device_stats(dev):
output = subprocess.run(
f"blockdev --getsize64 {dev}", shell=True, check=True, capture_output=True
).stdout.decode()
dev_size = int(output)
return {"dev_size": dev_size}
def dev_to_mountpoint(dev_name):
try:
output = subprocess.run(
f"findmnt --json --first-only {dev_name}",
shell=True,
check=True,
capture_output=True,
).stdout.decode()
data = json.loads(output)
return data["filesystems"][0]["target"]
except subprocess.CalledProcessError:
return None
def mountpoint_to_dev(mountpoint):
res = subprocess.run(
f"findmnt --json --first-only --nofsroot --mountpoint {mountpoint}",
shell=True,
capture_output=True,
)
if res.returncode != 0:
return None
data = json.loads(res.stdout.decode().strip())
return data["filesystems"][0]["source"]