Skip to content

Instantly share code, notes, and snippets.

@invisiblek
Forked from luca020400/img2ext.py
Created November 12, 2015 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save invisiblek/d101e986c9c168610e57 to your computer and use it in GitHub Desktop.
Save invisiblek/d101e986c9c168610e57 to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
import sys
import mmap
ext4Magic = '\x53\xEF'
ext4MagicOffset = 1080
if len(sys.argv) < 3:
print("No I/O supplied")
exit(1)
def copyExt4Image(origin, dest):
with open(origin, 'rb') as input:
with open(dest, 'wb') as output:
input.seek(offset - ext4MagicOffset)
output.write(input.read())
def findMagic(input):
with open(input, 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
return mm.find(ext4Magic, ext4MagicOffset)
systemimg = sys.argv[1]
systemimgout = sys.argv[2]
offset = findMagic(systemimg)
if offset == ext4MagicOffset:
print('The given file is a proper ext4 image')
elif offset != -1:
print('ext4 magic number found at byte %d' % offset)
print('Creating %s...' % systemimgout)
copyExt4Image(systemimg, systemimgout)
else:
print("ext4 magic number not found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment