文件

151 行
7.1 KiB
Python

#!/usr/bin/env python3
"""Read-only compatibility probe. Does not write or repair the source device."""
import argparse
import hashlib
import json
import os
from pathlib import Path
import shutil
import stat
import subprocess
import time
def run(*args, timeout=30):
return subprocess.check_output(args, text=True, timeout=timeout).strip()
def partition_writes(device):
fields = (Path('/sys/class/block') / device.name / 'stat').read_text().split()
return {'writes_completed': int(fields[4]), 'sectors_written': int(fields[6])}
def sample_hash(device):
with device.open('rb', buffering=0) as stream:
return hashlib.sha256(stream.read(1024 * 1024)).hexdigest()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--partuuid', required=True)
parser.add_argument('--serial', required=True)
parser.add_argument('--workdir', required=True, type=Path)
args = parser.parse_args()
if os.geteuid() != 0:
raise SystemExit('Run this hardware probe as root.')
work = args.workdir.resolve(strict=True)
device = (Path('/dev/disk/by-partuuid') / args.partuuid).resolve(strict=True)
if not stat.S_ISBLK(device.stat().st_mode):
raise SystemExit('Expected a block device.')
props = dict(line.split('=', 1) for line in run(
'udevadm', 'info', '--query=property', '--name=' + str(device)
).splitlines() if '=' in line)
if props.get('ID_BUS') != 'usb' or props.get('ID_SERIAL_SHORT') != args.serial:
raise SystemExit('USB identity mismatch; refusing to continue.')
if props.get('ID_PART_ENTRY_TYPE', '').lower() != '7c3457ef-0000-11aa-aa11-00306543ecac':
raise SystemExit('Partition is not declared APFS.')
with device.open('rb', buffering=0) as stream:
stream.seek(32)
if stream.read(4) != b'NXSB':
raise SystemExit('Missing APFS container signature.')
if subprocess.run(['findmnt', '-rn', '-S', str(device)], stdout=subprocess.DEVNULL).returncode == 0:
raise SystemExit('Source partition is already mounted.')
mount = work / 'mnt'
copies = work / 'read-check'
mount.mkdir(exist_ok=True)
if os.path.ismount(mount) or any(mount.iterdir()) or copies.exists():
raise SystemExit('Probe paths are occupied; refusing to reuse them.')
copies.mkdir()
report = {
'device': str(device), 'partuuid': args.partuuid,
'source_write_stats_before': partition_writes(device),
'source_header_sha256_before': sample_hash(device),
'files': [],
}
original_ro = run('blockdev', '--getro', str(device))
mounted = False
try:
subprocess.run(['blockdev', '--setro', str(device)], check=True)
report['block_read_only_during_test'] = run('blockdev', '--getro', str(device))
# The upstream implementation hard-codes read-only device opens and mounts.
subprocess.run([str(work / 'build/bin/apfs-fuse'), '-o', 'ro,nodev,nosuid,noexec',
str(device), str(mount)], check=True, timeout=30)
mounted = os.path.ismount(mount)
if not mounted:
raise RuntimeError('Driver exited without mounting.')
report['mount'] = json.loads(run('findmnt', '-J', '-M', str(mount),
'-o', 'SOURCE,FSTYPE,OPTIONS'))['filesystems'][0]
if 'ro' not in report['mount']['options'].split(','):
raise RuntimeError('Mount is not read-only.')
user_files = []
hidden_files = []
scanned = 0
for base, dirs, files in os.walk(mount, followlinks=False):
dirs.sort()
for name in sorted(files):
path = Path(base) / name
info = path.lstat()
scanned += 1
if stat.S_ISREG(info.st_mode) and 0 < info.st_size <= 512 * 1024**2:
relative = path.relative_to(mount)
hidden = any(part.startswith('.') for part in relative.parts)
(hidden_files if hidden else user_files).append((path, info.st_size))
if scanned >= 10000:
break
if scanned >= 10000:
break
report['scanned_files'] = scanned
report['eligible_user_files'] = len(user_files)
report['eligible_hidden_files'] = len(hidden_files)
selected = sorted(user_files or hidden_files, key=lambda item: item[1], reverse=True)[:3]
report['test_file_kind'] = 'user_files' if user_files else 'hidden_metadata_only'
for index, (source, expected_size) in enumerate(selected, 1):
target = copies / ('sample-' + str(index))
source_hash = hashlib.sha256()
started = time.monotonic()
read_bytes = 0
with source.open('rb') as inp, target.open('xb') as out:
while chunk := inp.read(1024 * 1024):
out.write(chunk)
source_hash.update(chunk)
read_bytes += len(chunk)
out.flush()
os.fsync(out.fileno())
elapsed = time.monotonic() - started
destination_hash = hashlib.sha256()
with target.open('rb') as stream:
while chunk := stream.read(1024 * 1024):
destination_hash.update(chunk)
if read_bytes != expected_size or source_hash.digest() != destination_hash.digest():
raise RuntimeError('Copied content did not match.')
report['files'].append({'index': index, 'bytes': read_bytes,
'sha256': source_hash.hexdigest(), 'copy_matches': True,
'elapsed_seconds': round(elapsed, 6),
'copy_MB_per_second': round(read_bytes / elapsed / 1e6, 3)})
report['status'] = 'passed' if selected else 'mounted_but_no_sample_files'
except Exception as error:
report['status'] = 'failed'
report['error'] = str(error)
raise
finally:
if mounted or os.path.ismount(mount):
subprocess.run(['umount', str(mount)], check=True, timeout=30)
if original_ro == '0':
subprocess.run(['blockdev', '--setrw', str(device)], check=True)
report['source_write_stats_after'] = partition_writes(device)
report['source_header_sha256_after'] = sample_hash(device)
report['source_writes_unchanged'] = report['source_write_stats_before'] == report['source_write_stats_after']
report['source_header_unchanged'] = report['source_header_sha256_before'] == report['source_header_sha256_after']
report['unmounted_after_test'] = not os.path.ismount(mount)
report['block_read_only_restored'] = run('blockdev', '--getro', str(device)) == original_ro
(work / 'result.json').write_text(json.dumps(report, indent=2) + '\n')
# Only this probe's destination copies are removed; source files remain untouched.
shutil.rmtree(copies)
print(json.dumps(report, indent=2))
if not report['source_writes_unchanged'] or not report['source_header_unchanged']:
raise SystemExit('Unexpected source changes detected.')
if __name__ == '__main__':
main()