I encountered a problem that is related to this thread:
I created a LUKS crypto block device with integrity. I'd want to wipe
each sector (i.e. logical block) so the sector's integrity checks,
before I could read from the sector. Upon "luksFormat", cryptsetup(8)
offers to wipe all sectors by default. If I'd overwrite a sector, the
sector's integrity bits'd be overwritten, too, so I'd no need to care
whether its previous integrity checks. However, if a partial write'd
cause the kernel to read this sector, and possibly along with neighbor
sectors within page size, and the related integrity'd fail to check,
the write'd fail. Unfortunately, mke2fs writes partial sectors. So I
could not mke2fs on a not wiped such device. So I wrote this "sparseblk" utility.
I could create a fs image on a plain file, then write the plain file
to the device. This "sparseblk" utility skips holes in its input by lseek(fd,
offset, SEEK_DATA), and pads data to the sector boundary when writing.
The kernel writes whole sectors after the fs is mounted.
hypothetical scenario: https://gitlab.com/cryptsetup/cryptsetup/issues/335#note_270050959
truncate -s 1G /tmp/1.img /tmp/2.img
truncate -s 980M /tmp/1.img /tmp/2.img
mke2fs /tmp/2.img
cryptsetup luksFormat \
--key-file /etc/motd \
--sector-size $(getconf PAGESIZE) \
--cipher chacha20-plain \
--integrity poly1305 \
--integrity-no-wipe \
/tmp/1.img
cryptsetup open --key-file /etc/motd /tmp/1.img dm-0
argv[0] < /tmp/2.img > /dev/mapper/dm-0
mount /dev/mapper/dm-0 /mnt/1
I've not tested it with e2fsck and resize2fs, etc., yet.
Anyway, if I'd be serious about security, I'd wipe the whole device.
You may use this "sparseblk" utility for other purposes. You may ignore the "logical
block size not a multiple of page size" warning if you use this "sparseblk" utility
for other purposes.
posix is adopting SEEK_DATA SEEK_HOLE: http://austingroupbugs.net/view.php?id=415
"sparseblk": https://gist.github.com/insulsa/18b7d31bd82ddade14db07f413c0b2d2