2

I'm writing a program that will fill a USB drive with 0 bytes in order to turn it into a bootable disk (that part is coming later).

Right now what I have is this:

import io
import lib


class CancelBlockCipherException(Exception): pass


class Formatter(object):

    def __init__(self, usb):
        self.usb = usb

    def _erase_all(self, block_cipher):
        with io.FileIO(self.usb, "w") as _usb:
            while _usb.write(block_cipher): pass

    def format_usb(self, size, default="y"):
        block_cipher = b"\0" * size
        confirmation = lib.settings.prompt(
            "Your USB is about to be completely erased, everything on this "
            "drive will be gone, are you sure you want to continue", "y/N(default '{}')".format(default)
        )
        if confirmation.lower().startswith("y") or confirmation == "":
            lib.settings.LOGGER.warning("Erasing USB content..")
            self._erase_all(block_cipher)
        elif confirmation.lower() == "" or confirmation is None:
            lib.settings.LOGGER.warning("Erasing USB content..")
            self._erase_all(block_cipher)
        else:
            raise CancelBlockCipherException("User aborted block cipher.")

What this will do is take the size of the USB in bytes, and write \0 to /dev/sdb1 until it has completely filled the USB file with 0 bytes (at least that's what I think is happening, I've been wrong before.) What I would like to do is make this write to the file faster. Right now this can take anywhere from 10-15 minutes to complete, because writing \0 1,875,615,744(1.75GB) times will probably take up a lot of time. How can I successfully format this USB, more efficiently and quicker, using pure python?

mawi
  • 83
  • 4

1 Answers1

0

A really messy way would be to hard-code, for example, a million copies of \0 into the code, and just write that to the disk for as long as it takes? I'm not sure if that will drastically reduce the time.

Tim Leach
  • 45
  • 8