How to move 7GB ext4 partition on external SD card to 30GB partition on another SD card?


Question

I had rooted my Android Lollipop 5.1.1 phone and installed Link2SD and linked installed apps on ext4 7GB 2nd partition of external SD card of 16GB size . The apps installed occupied 6.5GB space and I am getting low space now. I got 64GB external SD card and want to dedicate 30GB.



I created image of 7GB 2nd partition of external SD card through following command:



dd bs=4k count= if=/dev/block/mmcblk1p2 | gzip -5 -c > /storage/sdcard1/sdcard2root.img.gz


Now whats the way to clone this image to a 30GB partition?


Answer

It's easy to achieve on a Linux PC:




  • Attach both SD cards to PC.

  • Dump 2nd partition from old SD card to new one. It's also possible directly from Android device. See Creating Partition Dump.

  • Resize filesystem size to 30GB (the new partition size) using resize2fs.



COPY WHOLE FILESYSTEM:



The above approach works on Android too if you are able to attach new SD card through USB OTG SD card reader, while the old one is inside phone.



Or you can do it in 2 steps, as you are already trying:




  • Create partition dump:



    ~# gzip -9ck /dev/block/mmcblk1p2 >/storage/sdcard1/sdcard2root.img.gz


    You can use dd or cat or even cp. Make sure you have enough space on primary external (physically internal) storage to save dumped (and optionally compressed) image of SD card's 2nd partition.



    Compression is useful only with sparseness. To avoid copying freed filesystem blocks (of deleted files) use e2image -rap. Or discard logical to physical block mapping using fstrim (on mounted filesystem) or e2fsck -E discard, and drop page cache using sync; echo -n 1 >/proc/sys/vm/drop_caches.


  • Insert new SD card and do partitioning using parted or gdisk or fdisk.


  • Write partition image to new SD card:



    ~# gzip -dck /storage/sdcard1/sdcard2root.img.gz >/dev/block/mmcblk1p2

  • Maximize filesystem size to partition size:



    ~# resize2fs /dev/block/mmcblk1p2






COPY FILESYSTEM CONTENTS:



Another method is to only copy/move filesystem contents (files, directories etc.) and not the whole filesystem structure. But files' metadata needs to be preserved, or things may break (since the partition is to be mounted inside /data). It includes timestamps, permission mode, owner, group and extended attributes, particularly SELinux contexts. Some executable binaries (in /system) might also have file capabilities set (saved as extended attributes) but that's usually not required for user data. Other metadata like ACLs (also saved as extended attributes) and file attributes (inode flags) are not generally used on Android devices.



On Linux tools like rsync (or even cp) can preserve the required metadata. For reference see this and this answer. On Android (to move the contents in 2 steps) tar is a good choice:



~$ cd /path/to/old_sdcard/mountpoint/
~$ tar --create --preserve-permissions --selinux --verbose --gzip --file=/storage/sdcard1/sdcard2root.tar.gz .


Insert new SD card, do partitioning (if required), and create filesystem using mkfs.ext4 (by default it takes whole partition). To restore backup:



~$ cd /path/to/new_sdcard/mountpoint/
~$ tar --extract --preserve-permissions --selinux --verbose --gunzip --file=/storage/sdcard1/sdcard2root.tar.gz

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