mkfs.ext4 -F - how to avoid data loss in the future?

8,131

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.

Share:
8,131

Related videos on Youtube

yael
Author by

yael

Updated on September 18, 2022

Comments

  • yael
    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
      Rolf about 6 years
      You can recover the data.
    • Vlastimil Burián
      Vlastimil Burián over 4 years
      @Rolf How? Why did you not post an answer? :)
    • Vlastimil Burián
      Vlastimil Burián over 4 years
      It remains a mystery why you have used the -F option at the end. Could you clarify it?
    • Rolf
      Rolf over 4 years
      @LinuxSecurityFreak How to recover data has been covered many times before.
  • yael
    yael almost 7 years
    yes I will thx for the good advice and have a nice day my friend
  • user2948306
    user2948306 almost 7 years
    I 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 use lsblk --fs /dev/sde. Because file -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 that file -s has an edge there, or not.