Files
ext4recovery/misc_tools/check_translation.py
2026-04-30 11:04:05 +00:00

81 lines
2.5 KiB
Python

CHUNK = 128*512
LV_START = 5120000*512
BSIZE = 4096
# What if block 5251104 is an ABSOLUTE block number on md0
# (not relative to LV start)?
# Then: md0_byte = 5251104 * 4096 = 21,508,521,984
# And we DON'T add LV_START
abs_byte = 5251104 * BSIZE
print(f'Block 5251104 as absolute md0 byte: {abs_byte}')
print(f'md0 sector: {abs_byte//512}')
# Read it directly from md0 (no translation)
with open('/dev/md0','rb') as f:
f.seek(abs_byte)
data = f.read(BSIZE)
import struct
nonzero = sum(1 for b in data if b != 0)
print(f'Nonzero bytes: {nonzero}/4096')
print(f'First 32: {data[:32].hex()}')
# Try as directory
off = 0
entries = []
while off < BSIZE-8:
ino = struct.unpack_from('<I', data, off)[0]
rec_len = struct.unpack_from('<H', data, off+4)[0]
name_len= data[off+6]
ftype = data[off+7]
if rec_len < 8: break
if ino > 0 and name_len > 0:
name = data[off+8:off+8+name_len].decode('utf-8',errors='replace')
tname = {1:'file',2:'dir',7:'link'}.get(ftype,'?')
entries.append((ino,name,ftype))
off += rec_len
if entries:
print('Directory entries:')
for ino,name,ftype in entries:
tname = {1:'file',2:'dir',7:'link'}.get(ftype,'?')
print(f' {tname} inode={ino} {name!r}')
else:
print('No valid directory entries')
# Also try: what if the block number is relative to LV start
# but WITHOUT the 4/5 chunk translation?
# i.e. just: md0_byte = LV_START + block * BSIZE
direct_byte = LV_START + 5251104 * BSIZE
print()
print(f'Block 5251104 relative to LV, no translation: {direct_byte}')
with open('/dev/md0','rb') as f:
f.seek(direct_byte)
data2 = f.read(BSIZE)
nonzero2 = sum(1 for b in data2 if b != 0)
print(f'Nonzero bytes: {nonzero2}/4096')
print(f'First 32: {data2[:32].hex()}')
off = 0
entries2 = []
while off < BSIZE-8:
ino = struct.unpack_from('<I', data2, off)[0]
rec_len = struct.unpack_from('<H', data2, off+4)[0]
name_len= data2[off+6]
ftype = data2[off+7]
if rec_len < 8: break
if ino > 0 and name_len > 0:
name = data2[off+8:off+8+name_len].decode('utf-8',errors='replace')
tname = {1:'file',2:'dir',7:'link'}.get(ftype,'?')
entries2.append((ino,name,ftype))
off += rec_len
if entries2:
print('Directory entries (no translation):')
for ino,name,ftype in entries2:
tname = {1:'file',2:'dir',7:'link'}.get(ftype,'?')
print(f' {tname} inode={ino} {name!r}')
else:
print('No valid directory entries')