Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dbbb45d2fe | |||
| 8b51af821f | |||
| 1b7e347de0 | |||
| 304f499fbf | |||
| 8caebbb36d | |||
| 884686a1cb | |||
| a3f475224b | |||
| 6952b160f1 | |||
| 52fcbfc205 | |||
| 27e3f92186 | |||
| cd3051a1b1 | |||
| c6bb2b5207 | |||
| 179f9ac5a6 | |||
| a732f31ae7 | |||
| b926d6253c | |||
| b9ed726caa |
@@ -1,11 +1,21 @@
|
||||
# HyPyUtils
|
||||
HyDEV Utils for Python
|
||||
|
||||
`pip install hypy_utils`
|
||||
|
||||
## Modules
|
||||
|
||||
Some modules have extra requirements that are not installed along with hypy_utils. These are listed below:
|
||||
|
||||
| Module | Requirements |
|
||||
|--------------------|--------------------------|
|
||||
| `tqdm_utils` | tqdm |
|
||||
| `downloader` | tqdm, requests |
|
||||
| `scientific_utils` | numpy, numba, matplotlib |
|
||||
| `git_utils` | dateutil |
|
||||
|
||||
## BadBlocks - HDD sector scanning for Linux
|
||||
|
||||
Usage: `python3 -m hypy_utils.badblocks -d /dev/sda`
|
||||
|
||||

|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 832 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 539 KiB |
+35
-1
@@ -1,14 +1,18 @@
|
||||
from __future__ import annotations
|
||||
|
||||
__version__ = "1.0.22"
|
||||
__version__ = "1.0.29"
|
||||
|
||||
import time
|
||||
import logging
|
||||
from typing import Callable
|
||||
|
||||
from .color_utils import *
|
||||
from .serializer import *
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Timer:
|
||||
start: int
|
||||
|
||||
@@ -39,3 +43,33 @@ def run_time(func: Callable, *args, **kwargs):
|
||||
_ = [func(*args, **kwargs) for _ in range(iter)]
|
||||
ms = (time.time_ns() - start) / 1e6
|
||||
print(f'RT {name:30} {ms:6.1f} ms')
|
||||
|
||||
|
||||
def safe(func: Callable, on_error: Callable[[Exception], Any] = None) -> Callable:
|
||||
"""
|
||||
Wrapper for safely executing a function and returning the result of on_error if an exception occurs
|
||||
|
||||
If on_error is None, it will return None on error
|
||||
|
||||
Example Usage:
|
||||
>>> safe(lambda x: 1 / x)(0)
|
||||
None
|
||||
>>> safe(lambda x: 1 / x)(2)
|
||||
0.5
|
||||
|
||||
:param func: Function that needs safe execution
|
||||
:param on_error: Function to execute when an error occurs
|
||||
:return: Wrapped function
|
||||
"""
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except Exception as e:
|
||||
if on_error:
|
||||
return on_error(e)
|
||||
else:
|
||||
log.exception(e)
|
||||
return None
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def main():
|
||||
print('🐱')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
print('🐱')
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="html5">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>BadBlocks Scan Result for /dev/sda</title>
|
||||
<script src="https://unpkg.com/petite-vue"></script>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
</head>
|
||||
|
||||
<body v-scope @vue:mounted="mounted" class="p-4 relative flex flex-col gap-3">
|
||||
<div>
|
||||
<h1 class="text-2xl font-bold mb-4">BadBlocks Scan Result for /dev/sda</h1>
|
||||
<p>Scan started on {{ first.timestamp }} and ended on {{ last?.timestamp }}.</p>
|
||||
<p>
|
||||
Total blocks: {{ last.end_block }} blocks |
|
||||
Block size: {{ d.block_size }} |
|
||||
Total size: <span class="text-red-500">{{ (last.end_block * d.block_size / 1_000_000_000_000).toFixed(2) }} TB</span>
|
||||
<span class="text-gray-400">= {{ (last.end_block * d.block_size / 1024 / 1024 / 1024 / 1024).toFixed(2) }} TiB</span></p>
|
||||
<p><span class="text-red-500">Red</span> blocks indicate bad blocks or blocks that take too long (8x normal time) to scan. Hover over a block to see more information.</p>
|
||||
<p>Made with ♥ by <a href="https://github.com/hykilpikonna" class="text-red-500 underline">Azalea</a> | GitHub @ <a href="https://github.com/hykilpikonna/HyPyUtils" class="text-red-500 underline">hykilpikonna/HyPyUtils</a></p>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-wrap gap-0.5">
|
||||
<div v-for="(log, index) in d.logs" :key="index"
|
||||
class="inline-block w-2 h-2"
|
||||
:style="{backgroundColor: getBlockColor(log)}"
|
||||
@mouseenter="showHoverInfo($event, log, index)" @mouseleave="hideHoverInfo"></div>
|
||||
</div>
|
||||
|
||||
<!-- Tooltip for showing hover information -->
|
||||
<div v-if="hover"
|
||||
:style="{top: hover?.y + 'px', left: hover?.x + 'px'}"
|
||||
class="absolute bg-gray-800 text-white text-sm rounded px-2 py-1 shadow-md pointer-events-none transition-opacity duration-150">
|
||||
<p>Start: {{ hover?.l?.start_block?.toString(16) }}</p>
|
||||
<p>End: {{ hover?.l?.end_block?.toString(16) }}</p>
|
||||
<p>Duration: {{ hover?.l?.duration?.toFixed(2) }}</p>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script>
|
||||
PetiteVue.createApp({
|
||||
d: { logs: [] }, // timestamp, duration, start_block, end_block, bad_blocks
|
||||
max_dur: 0, min_dur: 0, hover: null, firs: null, last: null,
|
||||
onInit() {
|
||||
// Extract all durations and sort them
|
||||
const durations = this.d.logs.map(l => l.duration).sort((a, b) => a - b);
|
||||
|
||||
// Compute Q1 index; you can choose to do an interpolation if you need higher accuracy
|
||||
const q1Index = Math.floor(durations.length * 0.25);
|
||||
|
||||
// Use Q1 as our new "min"
|
||||
this.min_dur = durations[q1Index];
|
||||
this.max_dur = this.min_dur * 8;
|
||||
|
||||
console.log(`Q1 duration: ${this.min_dur}, Max duration: ${this.max_dur}`);
|
||||
|
||||
this.first = this.d.logs[0];
|
||||
this.last = this.d.logs[this.d.logs.length - 1];
|
||||
},
|
||||
mounted() {
|
||||
if (this.d.logs.length) return this.onInit() // For injecting data from server-side
|
||||
fetch('http://localhost:8080/badblocks_log__dev_sda.json').then(resp => resp.json())
|
||||
.then(data => { this.d = data; this.onInit() })
|
||||
},
|
||||
getBlockColor(log) {
|
||||
if (log.bad_blocks.length) return 'red'
|
||||
const ratio = 1 - ((log.duration - this.min_dur) / (this.max_dur - this.min_dur))
|
||||
return `rgb(${Math.round(255 * (1 - ratio))}, ${Math.round(255 * ratio)}, 0)`
|
||||
},
|
||||
showHoverInfo(event, log, index) {
|
||||
const rect = event.target.getBoundingClientRect();
|
||||
this.hover = { l: log,
|
||||
x: rect.left + window.scrollX + 10,
|
||||
y: rect.top + window.scrollY - 30
|
||||
}
|
||||
},
|
||||
hideHoverInfo() { this.hover = null }
|
||||
}).mount()
|
||||
</script>
|
||||
</html>
|
||||
@@ -0,0 +1,162 @@
|
||||
import argparse
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import platform
|
||||
from shutil import which
|
||||
import signal
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from hypy_utils import color
|
||||
from hypy_utils.logging_utils import setup_logger
|
||||
|
||||
log = setup_logger()
|
||||
speeds = []
|
||||
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
global pending_stop
|
||||
pending_stop = True
|
||||
log.error("^C received, signaling for the main process to stop...")
|
||||
log.warning("Please wait for the current block to finish scanning, then the program will exit.")
|
||||
log.warning("If you want to stop immediately, press ^\\ (NOT RECOMMENDED)")
|
||||
|
||||
|
||||
pending_stop = False
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
|
||||
def to_gb(block: int):
|
||||
return block * BLOCK_SIZE / (1024 * 1024 * 1024)
|
||||
|
||||
|
||||
def disk_info() -> tuple[int, int]:
|
||||
# Get the disk size in blocks
|
||||
disk_size = int(subprocess.run(f"blockdev --getsize64 {DISK}", capture_output=True, text=True, shell=True).stdout) // BLOCK_SIZE
|
||||
log.info(f"Disk size: {to_gb(disk_size):,.0f} GB, {disk_size:#x} blocks")
|
||||
|
||||
# Get the size of a logical sector (LDA)
|
||||
lss = int(subprocess.run(f"blockdev --getss {DISK}", capture_output=True, text=True, shell=True).stdout)
|
||||
pss = int(subprocess.run(f"blockdev --getpbsz {DISK}", capture_output=True, text=True, shell=True).stdout)
|
||||
log.info(f"Logical sector size: {lss} bytes, physical sector size: {pss} bytes")
|
||||
|
||||
return disk_size, lss
|
||||
|
||||
|
||||
def run_badblocks(start_block: int, end_block: int):
|
||||
# Print block address in hex
|
||||
log.debug(f"Scanning from {start_block:#x} ({to_gb(start_block):,.0f} GB) to {end_block:#x} ({to_gb(end_block):,.0f} GB)")
|
||||
|
||||
command = f"badblocks -b 4096 -v {DISK} {end_block} {start_block}"
|
||||
duration = time.time()
|
||||
result = subprocess.run(command, capture_output=True, text=True, shell=True, start_new_session=True)
|
||||
duration = time.time() - duration
|
||||
|
||||
# stdout should be a list of bad blocks, parse it
|
||||
bad_blocks = [int(r) for r in result.stdout.strip().split("\n") if r]
|
||||
|
||||
# Write the log as json
|
||||
logf = json.loads(LOG_FILE.read_text())
|
||||
logf["logs"].append({
|
||||
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"duration": duration,
|
||||
"start_block": start_block,
|
||||
"end_block": end_block,
|
||||
"bad_blocks": bad_blocks,
|
||||
"stderr": result.stderr,
|
||||
})
|
||||
LOG_FILE.write_text(json.dumps(logf, indent=2))
|
||||
|
||||
# Print logs
|
||||
if bad_blocks:
|
||||
log.error(f"> Bad blocks found: ")
|
||||
for block in bad_blocks:
|
||||
# Pint in hex
|
||||
log.error(f"> {block:#x} = LDA {block * BLOCK_SIZE // lss:#x} = {block * BLOCK_SIZE / (1024 * 1024 * 1024):,.0f} GB")
|
||||
else:
|
||||
log.debug(color(f"> Clean!"))
|
||||
|
||||
# Print summary (speed, progress, eta, etc.)
|
||||
# The stored speed is in blocks per second
|
||||
speed = (end_block - start_block) / duration
|
||||
speeds.append(speed)
|
||||
avg_spd = sum(speeds) / len(speeds)
|
||||
progress = end_block / disk_size
|
||||
|
||||
# Calculate ETA
|
||||
eta = (disk_size - end_block) / avg_spd
|
||||
eta = str(datetime.timedelta(seconds=eta))[:-7]
|
||||
|
||||
# Convert speed to MB/s
|
||||
speed *= BLOCK_SIZE / (1024 * 1024)
|
||||
avg_spd *= BLOCK_SIZE / (1024 * 1024)
|
||||
|
||||
log.info(f"> {progress * 100:.2f}% | Cur {speed:.1f} MB/s | Remain {eta} | "
|
||||
f"Avg {avg_spd:.1f} MB/s")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Take in disk and block size as optional arguments
|
||||
parser = argparse.ArgumentParser("Bad block detection utility")
|
||||
parser.add_argument("command", type=str, help="Command to run", choices=["scan", "plot"])
|
||||
parser.add_argument("--disk", "-d", type=str, help="Disk to scan")
|
||||
parser.add_argument("--block-size", "-b", type=int, default=4096, help="Block size in bytes")
|
||||
parser.add_argument("--start", "-s", type=int, help="Start block")
|
||||
parser.add_argument("--end", "-e", type=int, help="End block")
|
||||
parser.add_argument("--rescan", action="store_true", help="Rescan the whole disk")
|
||||
args = parser.parse_args()
|
||||
|
||||
DISK = args.disk
|
||||
BLOCK_SIZE = args.block_size
|
||||
START = args.start
|
||||
END = args.end
|
||||
|
||||
try:
|
||||
assert platform.system() != "Windows", "Windows is not supported, go use DiskGenius or something"
|
||||
assert which("badblocks"), "badblocks command not found, please install e2fsprogs"
|
||||
assert which("blockdev"), "blockdev command not found, please install util-linux"
|
||||
assert DISK and Path(DISK).exists(), f"Disk {DISK} does not exist"
|
||||
assert BLOCK_SIZE % 512 == 0, "Block size must be a multiple of 512"
|
||||
assert os.geteuid() == 0, "You need to run as root to access the disk"
|
||||
except AssertionError as e:
|
||||
log.error(e.args[0])
|
||||
exit(1)
|
||||
|
||||
LOG_FILE = Path(__file__).parent / f"badblocks_log_{DISK.replace('/', '_')}.json"
|
||||
|
||||
if not LOG_FILE.exists():
|
||||
LOG_FILE.write_text(json.dumps({"logs": [], "block_size": BLOCK_SIZE}, indent=2))
|
||||
elif not args.rescan:
|
||||
# Check if the block size matches
|
||||
block_size = json.loads(LOG_FILE.read_text())["block_size"]
|
||||
if block_size != BLOCK_SIZE:
|
||||
raise ValueError(f"Block size mismatch: {block_size} != {BLOCK_SIZE}")
|
||||
|
||||
# Resume from the last run
|
||||
logs = json.loads(LOG_FILE.read_text())["logs"]
|
||||
if logs:
|
||||
last_log = logs[-1]
|
||||
START = last_log["end_block"]
|
||||
log.info(f"Resuming from {START:#x}")
|
||||
|
||||
gb_approx = 1024 * 1024 * 1024 // BLOCK_SIZE
|
||||
disk_size, lss = disk_info()
|
||||
|
||||
if args.command == "scan":
|
||||
for start in range(START or 0, END or disk_size, gb_approx):
|
||||
end = min(start + gb_approx, disk_size)
|
||||
run_badblocks(start, end)
|
||||
if pending_stop:
|
||||
break
|
||||
|
||||
# Plot
|
||||
ouf = Path(f"badblocks{DISK.replace('/', '_')}.html")
|
||||
html = ((Path(__file__).parent / 'badblocks.html').read_text()
|
||||
.replace("d: { logs: [] }", f"d: {LOG_FILE.read_text()}")
|
||||
.replace("/dev/sda", DISK)
|
||||
)
|
||||
ouf.write_text(html)
|
||||
log.info(f"Results saved to {ouf}.")
|
||||
log.warning(f"You can open the html {ouf.absolute().as_uri()} in your browser. I can't open it for you because this script is running in sudo.")
|
||||
@@ -84,3 +84,19 @@ def deep_dict(o: object, exclude: set | None):
|
||||
if isinstance(o, list):
|
||||
return [deep_dict(v, exclude) for v in o]
|
||||
return o
|
||||
|
||||
|
||||
def get_rec(cd: dict, key: str):
|
||||
"""
|
||||
:param cd: Dictionary
|
||||
:param key: Recursive key in the format of keya.keyb.keyc...
|
||||
"""
|
||||
if '.' not in key:
|
||||
return cd.get(key)
|
||||
|
||||
ks = key.split('.')
|
||||
while len(ks) > 0:
|
||||
cd = cd.get(ks.pop(0))
|
||||
if cd is None:
|
||||
break
|
||||
return cd
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import pickle
|
||||
from pathlib import Path
|
||||
|
||||
import zstandard as zstd
|
||||
import orjson
|
||||
|
||||
from . import write
|
||||
|
||||
zstd_d = zstd.ZstdDecompressor()
|
||||
zstd_c = zstd.ZstdCompressor(level=5, write_checksum=True, threads=-1)
|
||||
|
||||
|
||||
def load_json_zst(file_path: str | Path) -> dict | list:
|
||||
"""
|
||||
Load a .json.zst file and return its parsed content.
|
||||
|
||||
Parameters:
|
||||
file_path (str): The path to the .json.zst file.
|
||||
|
||||
Returns:
|
||||
dict or list: The parsed JSON content.
|
||||
"""
|
||||
with Path(file_path).open('rb') as f:
|
||||
return orjson.loads(zstd_d.stream_reader(f).read())
|
||||
|
||||
|
||||
def write_json_zst(file_path: str | Path, data: dict | list, **kwargs):
|
||||
"""
|
||||
Dump data to a .json.zst file.
|
||||
|
||||
Parameters:
|
||||
file_path (str): The path to the .json.zst file.
|
||||
data (dict or list): The data to dump.
|
||||
"""
|
||||
write(file_path, zstd_c.compress(orjson.dumps(data, **kwargs)))
|
||||
|
||||
|
||||
def load_pickle_zst(file_path: str | Path):
|
||||
with Path(file_path).open('rb') as f:
|
||||
return pickle.loads(zstd_d.stream_reader(f).read())
|
||||
|
||||
|
||||
def write_pickle_zst(file_path: str | Path, data):
|
||||
write(file_path, zstd_c.compress(pickle.dumps(data)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
write_pickle_zst('test.pickle.zst', {'a': 1, 'b': 2})
|
||||
assert load_pickle_zst('test.pickle.zst') == {'a': 1, 'b': 2}
|
||||
write_json_zst('test.json.zst', {'a': 1, 'b': 2})
|
||||
assert load_json_zst('test.json.zst') == {'a': 1, 'b': 2}
|
||||
@@ -22,18 +22,12 @@ setup(
|
||||
classifiers=[
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3.7",
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Programming Language :: Python :: 3.9",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Programming Language :: Python :: 3.12",
|
||||
"Programming Language :: Python :: 3.13",
|
||||
],
|
||||
packages=find_packages(exclude=("tests",)),
|
||||
include_package_data=True,
|
||||
install_requires=[],
|
||||
entry_points={
|
||||
"console_scripts": [
|
||||
"hypy_utils=hypy_utils.__main__:main",
|
||||
]
|
||||
},
|
||||
install_requires=[]
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user