dotfiles/.local/bin/img2ext4

37 lines
1.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python2
import sys
import mmap
ext4Magic = '\x53\xEF'
ext4MagicOffset = 1080
if len(sys.argv) < 2:
print("No I/O supplied")
exit(1)
def copyExt4Image(origin, offset, size, dest):
with open(origin, 'r') as input:
with open(dest, 'w') as output:
input.seek(offset - ext4MagicOffset)
output.write(input.read(size + ext4MagicOffset))
def findMagics(input):
magics = [0]
with open(input, 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
while magics[-1] != -1:
magics.append(mm.find(ext4Magic, magics[-1] + ext4MagicOffset))
return magics
systemimg = sys.argv[1]
offsets = findMagics(systemimg)
if len(offsets[1:]) != 0:
for num, offset in enumerate(offsets[1:], start=1):
print('ext4 magic number found at byte %d' % offset)
name = "{}_part{}.img".format(systemimg, num)
print('Creating {}...'.format(name))
copyExt4Image(systemimg, offset, offsets[num + 1] - offset, name)
else:
print("Not ext4 images found")