How to create and use multiple partitions on external SD card with different filesystems (Ext4, exFAT, NTFS)?


Question

I want to create 4 partitions on external MicroSDcard through ADB shell commands, not on Windows or Linux. Also want to show all partitions in Android GUI (SETTINGS > STORAGE > SDCARD) and make them usable for all apps and file explorers.




  1. Dedicated first partition exFAT or NTFS or FAT32 (whichever Android permits and gives optimum performance) to store apps, photos and media.


  2. Second partition ext4, dedicated to applications linked through Link2SD. Or I want some applications or auto scripts or tools or commands to set the apps mainfest to always install on SD card.


  3. Third partition ext4 or NTFS or exFAT, dedicated for auto recording random backups or shadow images of partitions for different time, if possible through commands, scripts or apps like Titanium backup or others.


  4. Fourth partition ext4 or NTFS or exFAT, dedicating for Virtual memory
    (as we do for pagefile.sys in Windows or Linux swap partition).




If I format 1st partition with ext4, exFAT or NTFS, Android OS doesn't show it in SETTINGS > STORAGE> SDCARD > PARTITIONS and asks to mount or erase the partitions.



Now to limit and specify the questions:




  1. Can SD card have 4 partitions and will Android and SD card support it?


  2. Examples of commands to partition in Android; fdisk or parted or the best one.


  3. Is it necessary in Android that 1st partition should be FAT32? Or can we make it exFAT or NTFS to which Android Apps2SD and others apps can download?


  4. Can we force mount command in some startup boot init file to mount all four partitions?


  5. Will this mounting show the partitions in Settings and device file manager?


  6. Or can we view these partitions through ES File Manager or Solid Explorer or Root Manager and take their backups too?




Please guide me through the safe, secure, best and feasible method and procedure which does not damage the device hardware or SD card.


Answer

A few points on four partitions:





  1. Dedicated first partition exFAT or NTFS or FAT32 (whichever Android permits and gives optimum performance) to store apps, photos and media.




    For apps consider Application Class but that's expensive. Others will exert performance penalty more or less. On filesystem selection, the biggest downside with FAT32 is its 4GB maximum file size limit. Also it doesn't have some built-in phenomenon to deal with growing fragmentation which may reduce performance over time. You can find many online resources on pros and cons of FAT32 and exFAT. Also see the MS's official Filesystem Functionality Comparison.




    Is it necessary in Android that 1st partition should be FAT32?




    Not necessary if your device supports other filesystems. FAT32 is Android's default for secondary external storage (SD cards and USB drives) since the start days. Since SDXC cards (32GB+) come pre-formatted with exFAT (though not a technical limit), many OEMs add exFAT support to their devices. However exFAT was closed-source until recently, so it's not part of stock Android (except some limited addition in Android 9). But now it's very likely to be added to Android's kernel source. See some relevant details in this answer.



    So check if your kernel supports exFAT: grep exfat /proc/filesystems. NTFS is still closed-source and doesn't have a stable in-kernel open-source driver. However it's possible to mount exFAT and NTFS using FUSE drivers. In each case vold must support the filesystem.




    Or can we make it exFAT or NTFS to which Android Apps2SD and others apps can download?




    Irrespective of filesystem, all apps don't have write access to external SD card unless you make some changes to ROM. See details in How to save files to external SD card? and Android's Storage Journey.


  2. There are apps available to move unsupported apps to SD card. Or you can manually (bind) mount a SD card partition (or directory) inside /data. Surely you need ext4 (or f2fs on new devices) for this purpose (to enforce UNIX permissions). But it doesn't necessarily needs to be a partition, you can place a loop file on first partition.



    Trying to modify app's manifest (or asking how to do so) can be considered unethical or illegal.


  3. You don't need a dedicated partition to store backups. Instead first partition can be used.


  4. If you want to use whole partition for swap, that doesn't need a filesystem (but mkswap). If you want to create swap file instead (which isn't a bad idea), again you can use first partition for that. However using external SD card for swap is not always a good idea. From zram commit to mainstream kernel:




    embedded systems normally are reluctant to use eMMC or SDCard as swap because there is wear-leveling.

    ...

    Although we have real storage as swap, it was a problem, too. Because it sometime ends up making system very unresponsive caused by slow swap storage performance.




    Android supports swap with zram back-end (which creates a compressed block device within RAM) since Android 4.4. Android's OOM killers (old in-kernel lowmemorykiller and new userspace lmkd) do take swap into account. If your kernel supports ZRAM, using external SD card for SWAP is not recommended. It can worsen the performance and will kill the SD card very soon due to increased I/O operations.





If I format 1st partition with ext4, exFAT or NTFS, Android OS doesn't show it in SETTINGS > STORAGE> SDCARD > PARTITIONS and asks to mount or erase the partitions.




It can be due to unsupported filesystem (see explanation above) or multiple partitions (see explanation below). However if a partition is manually mounted with proper permissions, apps are able to read/write files there. You may use sdcardfs or FUSE to emulate the filesystem for fixed permissions. But this way Android framework is unaware of mounted filesystem and Android apps won't be notified of new storage. Neither you can manage the mounted storage from device Settings.




Can SD card have 4 partitions and will Android and SD card support it?




Android kernel by-default supports GPT which can have up to 128 partitions. MBR also supports 4 primary (or more logical) partitions (inside extended partition). Generally there is no technical limit but Android's userspace may mount only first partition, or may not recognize multi-partitioned SD card at all e.g. due to unknown partition type including Linux (0x83). Though vold supports multiple partitions before Android 6 too, you need to define FUSE (sdcard) init services, add entries to fstab, configure storage in framework-res.apk etc. If mounting multiple partitions doesn't work without these changes, you need to mount (additional or all) partitions manually (or rebuild your ROM with modified code).




Will this mounting show the partitions in Settings and device file manager?




No. See explanation above. Not sure what you mean by "device file manager".




Or can we view these partitions through ES File Manager or Solid Explorer or Root Manager and take their backups too?




Yes and no; depends on how, where and with what filesystem permissions you mounted the filesystem. See explanation above. Not sure what you mean by "take their backups too".




Examples of commands to partition in Android; fdisk or parted or the best one.




Partitioning works alike on all OSes. There's a long list of CLI and GUI tools and you'll find hundreds of tutorials online. Assuming that your SD card is enumerated as mmcblk1 and you want to create first partition of 32GB while leaving the rest for second, a one liner on Android can be:



~# sgdisk -Z -n 0:0:+32G -n 0:0:0 -p /dev/block/mmcblk1


See sgdisk manpage for explanation and more options. If needed use partx -uv /dev/block/mmcblk1 to update kernel of partition table changes, or simply reinsert SD card.



After partitioning use mkfs.fat/mkfs.exfat/mkfs.ntfs/mkfs.ext4 to create filesystem. All tools are available for Linux and work on Android as well.




Can we force mount command in some startup boot init file to mount all four partitions?




Yes. Create an init service or init.d script. See details in How to run an executable on boot?


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