pythondiskimage

Python Disk Imaging


Trying to make a script for disk imaging (such as .dd format) in python. Originally started as a project to get another hex debugger and kinda got more interested in trying to get raw data from the drive. which turned into wanting to be able to image the drive first. Anyways, I've been looking around for about a week or so and found the best way get get information from the drive on smaller drives appears to be something like:

with file("/dev/sda") as f:
 i=file("~/imagingtest.dd", "wb")
 i.write(f.read(SIZE))

with size being the disk size. Problem is, which seems to be a well known issue, trying to use large disks shows up as (even in my case total size of 250059350016 bytes):

"OverflowError: Python int too large to convert to C long"

Is there a more appropriate way to get around this issue? As it works fine for a small flash drive, but trying to image a drive, fails.

I've seen mention of possibly just iterating by sector size (512) per the number of sectors (in my case 488397168) however would like to verify exactly how to do this in a way that would be functional.

Thanks in advance for any assistance, sorry for any ignorance you easily notice.


Solution

  • Yes, that's how you should do it. Though you could go higher than the sector size if you wanted.

    with open("/dev/sda",'rb') as f:
        with open("~/imagingtest.dd", "wb") as i:
            while True:
                if i.write(f.read(512)) == 0:
                    break