3

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?

1
  • note that (gzip) compression only works on /dev/block/dm-0 (but not actually on /dev/block/mmcblk0p30) because of encryption
    – alecxs
    Sep 28, 2021 at 15:13

1 Answer 1

2

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
4
  • thanx for answering i will try it and let you know i have windows so i cant try linx option but will try e2image or dd commands thanx a lot again Jan 3, 2020 at 12:37
  • Android tar binary doesn’t have selinux option. So is busybox. Aug 8 at 16:49
  • @KhurshidAlam forum.xda-developers.com/t/… Aug 8 at 21:10
  • @IrfanLatif Thanks. But how do you install those ? Adb push /system/bin ? Wouldn’t it overwrite existing ? Aug 9 at 17:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .