How to create filesystem in TWRP? Getting error: "Unable to format to remove encryption" The screen appears after booting into TWRP and trying to format memory:



I was trying to flash a custom ROM using TWRP recovery but it's showing Unable to mount storage &amp; Failed to mount '/data' (Invalid argument).

I tried several methods like formatting memory but it's also not happening. It's showing Unable to format to remove encryption. Also tried to change the file format but nothing helped me. Now whenever I switch my phone on it automatically boot up into TWRP recovery mode. I tried this method and almost every method possible but nothing helped.

I'm using Moto E4 Plus (nicklaus).
You can always format from command-line to be sure where the problem is (if any). This will erase your whole /data partition which contains personal data, apps etc.


Boot to TWRP recovery.
Find block device for userdata partition. Usually it's symlinked at /dev/block/by-name/userdata or /dev/block/bootdevice/by-name/userdata. See FINDING BLOCK DEVICE section in this answer.
Make sure userdata partition is not mounted (which already seems the case here). Execute mount to see all mounted filesystems. To un-mount open terminal or adb shell and execute:

~# umount /data
~# umount /sdcard

Find partition size:

~# SIZE=$(blockdev --getsz /dev/block/by-name/userdata)


You get size in 512-byte sectors.
Leave (at least) 16 KB (32 sectors) at partition end for crypto footer - it's required on devices using Full Disk Encryption (FDE):

~# SIZE=$((SIZE - 32))

Create filesystem on the rest of space. To create f2fs filesystem:

~# mkfs.f2fs -l data /dev/block/by-name/userdata $SIZE


To create ext4 filesystem, size is to be provided in block count. If using 4KB block size:

~# mkfs.ext4 -L data -b 4096 /dev/block/by-name/userdata $((SIZE * 512 / 4096))


Or if older Android-specific make_ext4fs is available:

~# make_ext4fs -L data -l $((SIZE * 512)) /dev/block/by-name/userdata


data is filesystem/volume label.




NOTES:


blockdev and mkfs.*/make_ext4fs are usually bundled with TWRP or you need to get static binaries.
Why we need to leave space for crypto-footer? See quote from Partition map:


  Starting in Android 3.0, the recovery.fstab file gains an additional optional field, options. Currently the only defined option is length , which lets you explicitly specify the length of the partition. This length is used when reformatting the partition (e.g., for the userdata partition during a data wipe/factory reset operation, or for the system partition during installation of a full OTA package). If the length value is negative, then the size to format is taken by adding the length value to the true partition size. For instance, setting "length=-16384" means the last 16k of that partition will not be overwritten when that partition is reformatted. This supports features such as encryption of the userdata partition (where encryption metadata is stored at the end of the partition that should not be overwritten).

You don't need to provide filesystem size if you don't intend to encrypt your device or if device uses File Based Encryption (FBE). To create filesystem on whole partition:

~# mkfs.f2fs -l data /dev/block/by-name/userdata