TWRP

I have been using LineageOS on my Redmi Note 4 phone from the day I bought it. Till yesterday, I was running LineageOS 15.1 on it with encryption enabled. Since I wanted to try out the new Pie release, I tried installing the Resurrection Remix 7.0 ROM based on the Android Pie release. After setting everything up and restoring all the apps and data using Titanium Backup, I tried encrypting the device. However the encryption process failed and caused the phone to do a quick restart.

Since this was totally unexpected, I checked the error messages using the adb logcat command. The error message was

E Cryptfs : Bad magic for real block device /dev/block/bootdevice/by-name/userdata
E Cryptfs : Not a valid ext4 superblock
Orig filesystem overlaps crypto footer region.  Cannot encrypt in place.

This was strange because the userdata partition that is mounted at /data was formatted normally using the ext4 filesystem. Searching the internet lead me to this Reddit post which gave some useful pointers about the issue.

I tried to resize the userdata partition by formatting it from the bootloader and voilĂ  the encryption succeeded on the next boot after a clean install. So I started restoring the apps and data from the backup and when it was about to complete, I got an error notification which said internal storage running out, some system functions may not work.

I was surprised by this since my phone had 64 GB of internal storage and there is no way for all of it to get used up. I also wondered if it was due to the encryption of the new apps and data (like what Bitlocker does on Windows by showing the disk to be 100% full while it is doing the initial encryption). (After resolving this issue later, I found this to be this reported issue). I started to suspect that something is broken with the installed ROM. I wanted to flash the stock MIUI ROM to check the partition size but it was difficult to find a working download link for the official fastboot ROM files because of issues (intentional?) with the Xiaomi site as usual.

After flashing the stock ROM via the fastboot interface and then flashing TWRP and then the ROM, GApps and Magisk, I booted into the OS to find the internal storage size detected correctly. However, when I tried to encrypt the phone it failed again with the initial error.

Suspecting some issue with the ROM, I installed LineageOS 15.1 because the encryption was working fine on it. But unfortunately, even with LineageOS 15.1, the encryption was failing with the same error. Since I had already spent too much time trying to fix this, I gave up for the time being and went to sleep.

This morning, I researched this issue further and found this post, which revealed this to be an issue with TWRP 3.2+ not formatting the /data partition properly and the solution was to use a 3.1 version to format the userdata partition and then use the latest TWRP to flash Oreo ROMs or newer because of the compatibility requirements. Luckily for me, it worked perfectly and now my phone can run the Pie-based ROM with data encrypted.

One important lesson I learned from this experience is to be conservative when flashing/switching ROMs because I often end up spending a lot of time to get everything working as expected and also have to suffer phone downtime. Another thing I reminded myself (again!) to do is to periodically download and keep backups of the stock fastboot ROMs because of how unreliable the official site can be.