3

I'm trying to create a bootable USB drive and need to format the USB into FAT32 so that I can extract all the files into it.

Is it possible to format a USB drive using pure python? Can I format it into FAT32 without the use of external commands?

I know that in bash I can do this: sudo dd if=/dev/zero of=/dev/sdb bs=4k && sync to format the USB drive, how can I do similar using just python?

owasp
  • 45
  • 1
  • 5
  • If you're using windows: https://bytes.com/topic/python/answers/537724-formatting-device-script-windows – inspectorG4dget Jul 19 '17 at 18:27
  • @inspectorG4dget What about Linux? – owasp Jul 19 '17 at 18:34
  • If you need `sudo` for `dd` to work, you'll need `sudo` on your Python program as well. Is that OK? – Mark Ransom Jul 19 '17 at 21:58
  • @MarkRansom Yessir, I figured I'd need sudo for this anyways – owasp Jul 19 '17 at 22:09
  • 4
    Am I the only one to note the inconsistency in the question? You mention that you need to **FORMAT** a USB drive, but then talk about **filling the device with 0's**!!! That is NOT formatting a drive!? Formatting a drive means running something like 'mkfs.ext4' under Linux, writing several filesystem structures to the block device to allow later storing folders and files... – Ariel Sep 10 '18 at 15:57

2 Answers2

1

The following is a close approximation to the dd command you gave in the question. I'm not sure if there's a way to force a sync after completion.

import io
block = b'\0' * 4096
with io.FileIO('/dev/sdb', 'w') as f:
    while f.write(block):
        pass
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Can you explain this a little bit? I really appreciate this also, but where did you get the numbers and what does `io.File` do? – owasp Jul 20 '17 at 01:33
  • Also wouldn't doing `b"{}".format(block)` to turn each item into `\x00` work better..? – owasp Jul 20 '17 at 01:38
  • @owasp if you look at the [documentation for `io.FileIO`](https://docs.python.org/3/library/io.html#raw-file-i-o) you can see that it's a raw unbuffered file interface, which is what you need. `4096` is the equivalent of `4k`, it's `4*1024` where `1024` is the closest binary number to `1000`. There's no need to use `format`, `block` is already a byte string which is exactly what `write` needs. – Mark Ransom Jul 20 '17 at 02:34
  • I have a question for you pertaining to this, lets assume I have a drive that is 1.75GB, would 4096 still suffice in the size of the block? – owasp Jul 20 '17 at 15:11
  • @owasp you could use something larger if you want, but it should probably be a *multiple* of 4096. The only thing I claim with this answer is that it's equivalent to your `dd`, whether that's sufficient or not is up to you. – Mark Ransom Jul 20 '17 at 15:34
  • This answer shows how to fully erase a block device using pure Python, NOT how to format a drive as FAT32... – Ariel Sep 10 '18 at 16:07
  • @Ariel the title of the question says one thing and the body says another. They claim that the `dd` command does exactly what they need. You are correct that it is *not* formatting as FAT32. – Mark Ransom Sep 10 '18 at 20:14
  • @MarkRansom yes sorry i didn't repeat it here, but i did add a comment on the original posting. It was not criticism to your answer, i just find somewhat strange that nobody ever pointed out this fact. – Ariel Sep 11 '18 at 21:13
  • @MarkRansom You can force a sync after completion with os.sync() or better on a per file/fd basis with: os.fsync() (https://docs.python.org/3/library/os.html#os.fsync). Both available in Python 3 at least, did not check 2.7. – Ariel Sep 11 '18 at 21:16
0

You can use subprocess to inline bash code

  • 1
    I don't want to subprocess, I want it in pure python – owasp Jul 19 '17 at 21:37
  • @owasp, then you need to write an implementation of `mkfs.vfat` (or the equivalent for whichever filesystem you want to format the drive with) in Python. Just _zeroing_ a drive is trivial, and you accepted an answer that shows how to do that; but _formatting_ a drive is much harder, and implementing filesystem creation in native Python (when the OS-provided tools are written in C) is a substantial amount of work. – Charles Duffy Sep 11 '22 at 00:35