This is a data protection wrapper for mkfs.X
tools. Generally speaking, it can
be used with any tool of calling signature tool [options] target
, where the
target is either a block device or some raw image file (accepted filename
extensions: bin
, disk
, img
, part
, raw
).
Among mkfs.X
tools there are smart and dumb ones.
Smart tools check if the target contains something before doing anything. In
the case, some tools ask for confirmation (e.g. the mkfs.ext
family), while
others refuse to proceed (e.g. mkfs.xfs
).
Dumb tools just format the target without any checks (e.g. mkfs.fat
).
This script makes those dumb tools safer to use by emulating the smart
behavior. It relies on libblk
for the check, just like the mkfs.ext
family
does.
Notice: The smart behaviour is only emulated when you run the script on a
terminal. The mkfs.ext
family does the same to not break existing scripts
(quoted from mke2fs.c).
Notice: Some mkfs.X
tools accept an extra non-option argument: filesystem
size. This script does not support that, it assumes the last argument is the
target.
Notice: Some mkfs.X
tools can create the fs starting at some offset
(e.g. mkfs.fat
with its --offset
option). This script does not parse any
options, it just assumes the last argument is the target as-is.
There are two ways to use the script:
- standalone mode: when called as
ifempty tool ..
. It is easier to install but you have to manually prependifempty
each time - symlink mode: when you override your tools with symlinks to
ifempty
and call them like you always do with justtool ..
In any case:
- save the script as
/usr/local/bin/ifempty
and make it executable
Extra steps for the symlink mode:
- check if
/usr/local/sbin
is in yourPATH
and overrides/usr/sbin
- in
/usr/local/sbin
, create symlinks for the dumb tools, pointing to/usr/local/bin/ifempty
> ifempty mkfs.fat disk.img 'disk.img' contains some GPT partition table Proceed anyway? (y,N) n > ifempty mkfs.fat part1.img 'part1.img' contains some XFS superblock Proceed anyway? (y,N) n
> tree /usr/local/sbin /usr/local/sbin ├── mkfs.exfat -> /usr/local/bin/ifempty ├── mkfs.fat -> /usr/local/bin/ifempty ├── mkfs.msdos -> /usr/local/bin/ifempty └── mkfs.vfat -> /usr/local/bin/ifempty > command -v mkfs.fat /usr/local/sbin/mkfs.fat > mkfs.fat disk.img 'disk.img' contains some GPT partition table Proceed anyway? (y,N) n > mkfs.fat part1.img 'part1.img' contains some XFS superblock Proceed anyway? (y,N) n