zeroing out part of an SDcard

Chris Murphy lists at colorremedies.com
Thu Aug 21 16:02:12 UTC 2014


On Aug 21, 2014, at 6:51 AM, Robert Moskowitz <rgm at htt-consult.com> wrote:

> More on my compressing images from SDcards.
> 
> I only need the boot info and 1st partition, so I remove the other partitions, but of course there is still 1s out there so it will not compress efficiently. I want to zero out the end of the card so first I use fdisk:
> 
> # fdisk -l /dev/sdb
> 
> Disk /dev/sdb: 7.4 GiB, 7969177600 bytes, 15564800 sectors
> Units: sectors of 1 * 512 = 512 bytes
> Sector size (logical/physical): 512 bytes / 512 bytes
> I/O size (minimum/optimal): 512 bytes / 512 bytes
> Disklabel type: dos
> Disk identifier: 0x0009e2ad
> 
> Device Boot Start End Blocks Id System
> /dev/sdb1 8192 1007615 499712 83 Linux

These 487MB aren't zero'd, so whatever is reported as free space by the file system will have stale data on its sectors. Mount sdb1 and use fstrim -v <mountpoint> and see if the command is accepted.

> 
> Then dd:
> 
> # dd if=/dev/zero of=/dev/sdb bs=1M seek=1007620

It's kinder to your sdcard to create a 2nd partition for the remaining space and trim it. This can be done with either mkfs.btrfs or mkfs.ext4, if the kernel detects it's solid state then both commands issue trim for all of those blocks, and then creates a file system. I'd use btrfs only because it writes out far less metadata.

However, quite a few sdcards don't respond to trim. And if they do, some have non-deterministic read of 'erased/trimmed' pages, in which case even though they're marked as erased, they can still return old data or garbage. And in that case unfortunately zeroing is the only way out.


Chris Murphy


More information about the users mailing list