0

I want to make zero all bits of a partition where an ext4 filesystem structure existed (superblocks, metadata, journal, etc), not just the filesystem signature deleted by wipefs.

I can quickly zero out files content using shred utility, but not the metadata. On block devices supporting TRIM/DISCARD, this is easy as I can quickly run blkdiscard on the entire partition.

However on large rotational HDDs where TRIM/DISCARD is not available, making all bits zero either becomes a large time consuming process or implies destroying/regenerating the disk encryption key (on self encrypting drives) which implies losing entire drive, not just the ext4 partition.

Other than reading mke2fs code and creating an imaginary wipe2fs tool based on it, is there already another way to quickly wipe all ext4 superblocks/metadata?

3
  • I'm not aware of any such utilities but given a programming background you could hack e2image to do exactly what you're looking for as this utility by default saves only metadata. Feb 24, 2021 at 19:48
  • What about using dd you can “dd if=/dev/urandom of=/dev/path bs=4096” does this meet your needs Feb 24, 2021 at 19:54
  • 1
    @JasonCroyle he said he doesn't want to zero the whole drive because that takes too long. Though I do wonder if you actually care about not leaving behind anything someone might find later, why are you trying to take a shortcut? Zero the whole thing or there is probably something somewhere that you don't want left behind.
    – psusi
    Feb 24, 2021 at 20:38

1 Answer 1

2

I wrote this several years ago as part of a scripted remote install to remove all the superblocks so that a subsequent mkfs wouldn't ask "do you really want to do this..." question in the middle of an unattended install. Testing was limited to a single device with 2 partitions.

# Overwrite any existing superblocks for the filesystem on each partition
#
# Zero out the superblock where the filesystems will be. If we don't do this,
# when we reimage a disk mkfs will see the superblocks of the filesystem which
# may have previously been here and and ask for a y/n confirmation before
# proceeding which requires more manual intervention (see disk geometry below)
# This gets a little complicated since the mkfs scatters superblocks across the
# partition to protect against failure so you just can't just zero out the
# beginning of the partition.
# Note that fdisk reports in units of 512 byte blocks (-u) ($FD_BS) but
# the file system may have different blocksize ($FS_BS). When we dd seek to zero
# out the superblock, we need to seek X file system blocks (in FS_BS) from
# the beginning of the partition (in FD_BS) so we use the FS_MULT to help make
# the math more obvious.

overwriteAnySuperblocks() {

fdisk -u -l ${TGTDEV} | grep -v EFI |
        sed -n '/^\//{ s/\*/ /; p}' | # only line that start with /, delete '*'
    while read PART FIRST_BLOCK IGNORE_REST_OF_LINE
    do
        FD_BS=512   # fdisk reports in 512 byte blocks
        echo "Partition $PART starts at $FIRST_BLOCK fdisk blocks" >&2
        # get the file system block size - may be a 4k file system
        FS_BS=$(dumpe2fs "${PART}" 2>/dev/null |
                sed -n '/^Block size/s/^Block size: *\([0-9]*$\)/\1/p')
        echo "File system block size from dumpe2fs - FS_BS=$FS_BS" >&2
        [[ -z "${FS_BS}" ]] && continue # no filesystem on this partition
        # file system block as a multiple of 512 byte blocks
        FS_MULT=$(( FS_BS / FD_BS ))
        echo "File system block size $FS_BS is $FS_MULT x $FD_BS fdisk blocks" >&2
        # zero out the beginning of each partition
        # zero out backup superblocks
        dumpe2fs "${PART}" 2>/dev/null |
            sed -n '/superblock/s/^.*block at \([0-9]*\).*/\1/p' |
            while read SUPERBLOCK
            do
                echo "Zeroing superblock at FS $SUPERBLOCK" >&2
                #echo dd if=/dev/zero of=${TGTDEV} bs=${FD_BS} \
                #    seek=$((${FIRST_BLOCK}+(${SUPERBLOCK}*${FS_MULT}))) \
                #    count=2048
                dd if=/dev/zero of=${TGTDEV} bs=${FD_BS} \
                    seek=$(( FIRST_BLOCK + (SUPERBLOCK*FS_MULT) )) \
                    count=2048
            done
    done

}

1
  • I've been running into problems with ghosts of departed filesystems; after re-partitioning an emmc, e2fsck sees a valid filesystem when it is actually subtly corrupt. Thanks for showing how hard this is to do right; I'm just going to blast the whole partition with dd. Jun 16, 2022 at 18:16

You must log in to answer this question.

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