我想将存在 ext4 文件系统结构的分区的所有位(超级块、元数据、日志等)清零,而不仅仅是wipefs
.
我可以使用实用程序快速将文件内容清零shred
,但不能将元数据清零。在支持 的块设备上TRIM/DISCARD
,这很容易,因为我可以blkdiscard
在整个分区上快速运行。
然而,在不可用的大型旋转 HDD 上TRIM/DISCARD
,将所有位设为 0 要么成为一个耗时的过程,要么意味着销毁/重新生成磁盘加密密钥(在自加密驱动器上),这意味着丢失整个驱动器,而不仅仅是 ext4 分区。
除了阅读mke2fs
代码并wipe2fs
基于它创建一个想象的工具之外,是否还有另一种方法可以快速擦除所有 ext4 超级块/元数据?
答案1
我几年前写了这篇文章,作为脚本化远程安装的一部分,以删除所有超级块,以便后续 mkfs 不会在无人值守安装过程中询问“您真的想这样做吗...”问题。测试仅限于具有 2 个分区的单个设备。
# 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
}
答案2
我们过去只是简单地在分区的第一个块上添加零
dd if=/dev/zero bs=512 count=512 of=/dev/your_partition