python3 -c "
CHUNK = 128*512
LV_START = 5120000*512
VIRT_SIZE = 9372172288*512

def read_virt(offset, length):
    result = bytearray(length)
    pos = offset
    remaining = length
    with open('/dev/md0','rb') as f:
        while remaining > 0:
            group     = pos // (5*CHUNK)
            in_group  = pos % (5*CHUNK)
            chunk_idx = in_group // CHUNK
            intra     = in_group % CHUNK
            seg_len   = min(CHUNK-intra, remaining)
            dst_off   = pos - offset
            if chunk_idx != 4:
                phys = LV_START + group*4*CHUNK + chunk_idx*CHUNK + intra
                f.seek(phys)
                data = f.read(seg_len)
                result[dst_off:dst_off+len(data)] = data
            pos += seg_len
            remaining -= seg_len
    return bytes(result)

# Check last 8MB of virtual disk
last_8mb_start = VIRT_SIZE - 8*1024*1024
print(f'Checking last 8MB: virtual bytes {last_8mb_start} to {VIRT_SIZE}')
print(f'= virtual sectors {last_8mb_start//512} to {VIRT_SIZE//512}')

data = read_virt(last_8mb_start, 8*1024*1024)
nonzero = sum(1 for b in data if b != 0)
zero_runs = 0
in_zero = False
for b in data:
    if b == 0 and not in_zero:
        in_zero = True
        zero_runs += 1
    elif b != 0:
        in_zero = False

print(f'Non-zero bytes: {nonzero} / {8*1024*1024}')
print(f'Zero runs: {zero_runs}')
print(f'First 32 bytes: {data[:32].hex()}')
print(f'Last 32 bytes: {data[-32:].hex()}')

# Find first non-zero from the end
for i in range(len(data)-1, -1, -1):
    if data[i] != 0:
        print(f'Last non-zero byte at offset {i} from last_8mb_start')
        print(f'= virtual byte {last_8mb_start+i}')
        break
"
