Advertisement
Guest User

findCopyInterruptLocation.py

a guest
Aug 3rd, 2013
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This script finds the last sector copied by GParted given an initial sector
  2. # and an offset to the location of its copy.
  3. #
  4. # This is done simply by comparing sector number n to sector number
  5. # n+<offset_of_copy>. If these match it increments n and continues.
  6. #
  7. #        *** WARNING: In its current form, this script is very slow! ***
  8. #
  9. # You may prefer to increment n by a much larger number to save time (simply
  10. # modify numSectorsToIncrement), but this won't find the exact last copied
  11. # sector, just one within your increment size.
  12.  
  13. import sys
  14.  
  15. n                   = 123456789   # <- EDIT THIS!
  16. offset              = 314159      # <- EDIT THIS!
  17. partitionName       = "/dev/sda3" # <- EDIT THIS!
  18. numIncrementSectors = 1           # <- Possibly edit this...
  19. bytesPerSector      = 512         # <- Only edit this if you know what you're doing
  20.  
  21. m=n+offset
  22.  
  23. partition=open(partitionName,'rb') # Open partition
  24.  
  25. #Read data at sector n
  26. partition.seek(n*bytesPerSector,0)
  27. nData=partition.read(1*bytesPerSector)
  28. print("Initial sector data:\n\n" + nData + "\n")
  29.  
  30. #Read data at sector m
  31. partition.seek(m*bytesPerSector,0)
  32. mData=partition.read(1*bytesPerSector)
  33.  
  34. if (nData != mData):
  35.     partition.close()
  36.     sys.exit("Initial sectors don't match, are you sure your numbers are correct?")
  37.  
  38. print("Starting at sector offset " + str(m-numIncrementSectors))
  39.  
  40. while (nData == mData):
  41.     n += numIncrementSectors             # Increment sector pointer
  42.     partition.seek(n*bytesPerSector)     # Seek to pointer position
  43.     nData=partition.read(bytesPerSector) # Read data at that position
  44.    
  45.     m += numIncrementSectors             # Same as above
  46.     partition.seek(m*bytesPerSector)
  47.     mData=partition.read(bytesPerSector)
  48.    
  49.     if (( m % (1024*1024*1024/512)) == 0): # Check how far we've got
  50.         print("Offset: " + str(m/(1024*1024*1024/512)) + " GiB, current sector offset: " + str(m-numIncrementSectors))
  51.        
  52. print("Last matching original/copied sector has absolute offset: " + str(m-numIncrementSectors))
  53. print("Last matching copied/original sector has absolute offset: " + str(n-numIncrementSectors))
  54.  
  55. partition.close() # Close partition
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement