mkfs.ext4 -F - how to avoid data loss in the future?
Solution 1
how to know if some data already exist on /dev/sde
You can try to mount it. You can try examining the disk partition table. But if you don't use the proper tool that can understand what's actually on the disk, whatever tool you use will likely report that there's no data on the disk. So in the final analysis, you need to know what's on the disk before you do something that will destroy the data on it.
As a system administrator, you have the power to destroy data. You need to be careful.
therefore all data get lost ... how to avoid this?
Don't run
mkfs.ext4 -j -m 0 /dev/sde -F
on a disk that has data you don't want to lose.
Seriously - that's the "fix" - don't do it. You ran a command to make a new filesystem on /dev/sde
and even used the -F
"force" option to ensure the command run no matter what it might do. Per the mkfs.ext4
man page:
-F Force mke2fs to create a filesystem, even if the specified device is not a partition on a block special device, or if other parameters do not make sense. ...
The data is gone. Learn from this and be more careful in the future.
Solution 2
On my computer (Fedora 25), mkfs.ext4 will warn if a filesystem on the device is already mounted. It will also warn if the device e.g. sde
actually contains a partition table, suggesting you meant to format a partition of the device e.g. sde1
.
This works nicely in combination with popular GUIs such as GNOME, because they tend to auto-mount filesystems. With some exceptions.
So you can't rely on this in every possible situation. However it shows there are some pleasing protections against this data loss.
EDIT: as Andrew says, if you use the -F
option then you're asking to remove this protection. Since you don't say why you added the -F option
it's a bit hard to help with this.
Related videos on Youtube
yael
Updated on September 18, 2022Comments
-
yael almost 2 years
Before we run this
mkfs
command to create a filesystem on a device, for example/dev/sde
:how to know if some data already exist on this device?:
mkfs.ext4 -j -m 0 /dev/sde -F
I performed the
mkfs.ext4
on/dev/sde
, but did not get any warning about data that exist(-ed) on this drive, therefore all data got lost.How to avoid this in the future?
# mkfs.ext4 -j -m 0 /dev/sde -F mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1310720 inodes, 5242880 blocks 0 blocks (0.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2153775104 160 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
-
Rolf about 6 yearsYou can recover the data.
-
Vlastimil Burián over 4 years@Rolf How? Why did you not post an answer? :)
-
Vlastimil Burián over 4 yearsIt remains a mystery why you have used the
-F
option at the end. Could you clarify it? -
Rolf over 4 years@LinuxSecurityFreak How to recover data has been covered many times before.
-
-
yael almost 7 yearsyes I will thx for the good advice and have a nice day my friend
-
user2948306 almost 7 yearsI tend to use
file -s /dev/sde
instead of mounting - e.g. it could contain an LVM PV, which is not a mountable filesystem itself but can contain filesystems. In principle I guess you're supposed to uselsblk --fs /dev/sde
. Becausefile -s
won't show RAID devices accurately, at least when the RAID metadata is stored at the end of the device.lsblk --fs
should be more accurate/reliable for the most common block device contents. Dunno about databases directly on block devices, it might be thatfile -s
has an edge there, or not.