2

Should I clean my USB device right before using dd to it, in case I'm copying a file with a size lower than the USB capacity?

For example:
my USB stick is 32 GB full of data,
my .iso file is 4 GB.

After dding to the drive, will all the data be erased and overwritten? Or will only 4 GB of data be overwritten with the .iso file?

1
  • 1
    If you dd an iso image to the device (which is correct, rather than a partition), then the old partition table and any filesystem metadata will be lost. Whatever was previously on that flash drive may not be overwritten, but it will no longer be accessible (by any conventional method). There's no need to clean or prepare the flash drive. BTW that iso image had better be a hybrid iso image to be useful.
    – sawdust
    Feb 21, 2017 at 22:57

1 Answer 1

1

In your example dd will write 4 GB, the rest of your USB stick will not change. It doesn't mean the data there will be easily accessible though.

You write about .iso file but I will use the general term "disk image".

The old partition table will be overwritten with something new. If the image is sane, it doesn't contain structures (like a partition table or filesystem header) that point beyond the image itself. After you place it on your USB stick your OS has no reason nor clue to snoop around in what now appears as the unused part of the drive.

If, on the other hand, the image contains a partition table that says there is a partition far far away beyond the image (e.g. the image used to be larger but it was cropped), and by accident there was a partition at the same place, this old partition will be accessible after dding.

There are programs made to snoop around: forensics and recovery tools. When properly used, they can find old files or even entire filesystems in this leftover space after your dding.

Should I clean my USB device?

It depends. If you're afraid the non-overwritten data will interfere with the newly written content, then the answer is: no, because it won't interfere, unless the image is somewhat invalid.

But if you're afraid somebody will get to (some of) your old files, then yes, you should explicitly overwrite the entire capacity of your USB stick; dding a smaller image is not enough.

The easy way to do this is to write zeros. Overwriting the entire device before dding the image is a waste of time and write cycles. I would do:

cat my_image /dev/zero | sudo dd of=/dev/my_usb

This way you will write the image followed by zeros. You will eventually get something like "no space left on device", it's OK.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .