How to resize the /data F2FS partition


Question

Context:



I wiped all the partitions from my Moto G device with the TWRP recovery and installed Cyanogen 13 on it. Then I had issues when enabling Encryption: the phone just restarts but does not encrypt. This is a known issue and is described here.



I wanted to follow a procedure described in this answer (based on this), which reportedly works. The explanation is:




When encrypting the phone, partition /dev/block/mmcblk0pXX -
originally containing an ext4 filesystem which is mounted as /data and
/sdcard - now contains an encryption container. This will be decrypted
early in the boot process and returns a logical partition
/dev/block/dm-0 which then holds the ext4 filesystem for /data and
/sdcard. (...)



To fix that, the filesystem in /dev/block/mmcblk0pXX needs to be at
least 16KiB smaller than the partition itself, which can easily be
done by resize2fs.






In short, the proposed solution is to resize the /data partition, removing some sectors from it...




Important is the number of blocks (...) From this number, we subtract 8, which leaves not 16KiB but a safe 32 KiB of space for the encryption header (you don't mind 16KiB on a 12GB volume, seriously)




The actual number of blocks to be removed from the /data partition depends on the sector size for the partition. In the example above it's 4KB, so 8x4 =32.






The problem



These instructions only apply to the ext2/3/4 file systems; the necessary commands (e2fsck, tune2fs, resize2fs) won't work with the F2FS partition used by Cyanogen v13.



I found fsck.f2fs in the /sbin directory, which I used instead of e2fsck and tune2fs. Below are the steps from the root ADB Shell, booted to TWRP:





~ # mount | grep data

/dev/block/platform/msm_sdcc.1/by-name/modem on /firmware type ext4 (rw,seclabel,relatime,data=ordered)
/dev/block/mmcblk0p36 on /data type f2fs (rw,seclabel,relatime,background_gc=on,user_xattr,inline_xattr,acl,inline_data,active_logs=6)
/dev/block/mmcblk0p36 on /sdcard type f2fs (rw,seclabel,relatime,background_gc=on,user_xattr,inline_xattr,acl,inline_data,active_logs=6)
/dev/block/mmcblk0p33 on /cache type ext4 (rw,seclabel,relatime,data=ordered)

~ # umount /dev/block/mmcblk0p36

~ # fsck.f2fs -f /dev/block/mmcblk0p36

Info: Force to fix corruption
Info: sector size = 512
Info: total sectors = 11583232 (in 512 bytes)

(...)




So, my /data partition is mmcblk0p36, and is 11 583 232 sectors in size. Each sector is 512 bytes, thus I must shrink the partition to a final size of 11 583 168 sectors in order to leave 32KB unallocated.



But how can it be done? Everything seem to indicate that shrinking an F2FS partition is not possible, but I'm not sure. If it is not, and assuming I did a full backup of the /data partition, what are the steps to delete and re-create it with a smaller size?


Answer

As of today (May 30, 2016), the F2FS file system doesn't support shrinking. If it does by the time someone reads this, just let me know in the comments.



The solution is then to delete and re-create the entire filesystem for /data. From this related answer on Unix & Linux Stack Exchange:



mkfs.f2fs /dev/block/mmcblk0p36 11583168


This will do the trick. After rebooting and restoring the backup, and finally, rebooting to Android, the encryption process worked.



Important: Be sure to check Use rm -rf instead of formatting from the TWRP settings prior to restoring the backup.


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes