Default [GUIDE] Make Link2SD use a disk image instead of a second ext2 partition

Was using Link2SD to move apps to the second partition of a micro SD card for the usual reasons... Then did -something- that broke the micro SD card slot. Now what to do?

Possible solutions:
  • Is it possible to repartition the internal NAND? -- Wouldn't that involve unpacking and repacking the kernel image? Better not... Although it was an inexpensive tablet, it would be an expensive brick.
  • Is it possible to bind-mount a directory on the "internal SD" for use with Link2SD? -- Would that even work with vfat? (Permissions and stuff...)
  • What about replacing the ext2 SD-card partition with a disk image

Materials:
  • Android tablet. (Broken SD card slot optional.)
  • Root with busybox installed.
  • Link2SD installed and configured -- This has to be done prior to breaking the SD card slot. Link2SD's startup scripts are needed, which it may not create without a "properly" formatted and installed SD card.
  • Computer with Linux installed OR a Linux Live DVD -- To create the disk image and transfer the contents from the physical SD card to the image file. I used Ubuntu.
  • USB transfer cable (optional) -- To transfer the disk image from the computer to the tablet. Also to execute terminal commands on the tablet. Possible to do over a network, but the cable can be faster and more convenient.
Do not try this unless you are fully prepared to brick your device.
Method:
  • Create the disk image on the computer:

    Code:
    qemu-img create -f raw link2sd-disk.img 3G
  • Format the disk image to ext2:

    Code:
    mkfs.ext2 link2sd-disk.img
  • Mount the disk image to a newly created directory:

    Code:
    mkdir /tmp/link2sd-disk
    sudo mount -o loop link2sd-disk.img /tmp/link2sd-disk
  • Mount the original SD card with the second ext2 partition.
  • Copy the contents of the sd card to the disk image. Use rsync to keep file permissions and other metadata. The trailing "/" on the directory names is important.

    Code:
    sudo -s
    rsync --archive --hard-links /media/path-to-sd-card/ /tmp/link2sd-disk/
  • Unmount the sd card and disk image.

    Code:
    sudo umount /tmp/link2sd-disk
  • Transfer the disk image to your tablet.

    Code:
    adb push link2sd-disk.img /mnt/sdcard/link2sd-disk.img
  • Edit /system/bin/debuggerd on the tablet:

    Code:
    adb pull /system/bin/debuggerd
    Edit debuggerd with your favorite text editor. Pay careful attention to the actual identity of the internal sd card as used on your tablet (/dev/block/mtdblock9). Notice that "busybox mount" is used. The normal mount command does not support the necessary options.

    Code:
    #!/system/bin/sh
    #added by link2sd
    LOG=/data/link2sd-debuggerd.log
    
    ls -la /dev/block/ 1>>$LOG 2>>$LOG
    
    echo  1>>$LOG 2>>$LOG
    echo "$(date) mounting..." > $LOG
    
    busybox mount -t vfat -o rw,relatime,uid=1000,gid=1015,fmask=0002,dmask=0002,shortname=mixed,errors=remount-ro /dev/block/mtdblock9 /mnt/sdcard 1>>$LOG 2>>$LOG
    busybox mount -t ext2 -o loop,rw /mnt/sdcard/link2sd.img /data/sdext2 1>>$LOG 2>>$LOG
    
    echo  1>>$LOG 2>>$LOG
    
    mount >> $LOG
    
    echo  1>>$LOG 2>>$LOG
    echo debuggerd.bin launched >> $LOG
    exec /system/bin/debuggerd.bin
  • Backup the original file and transfer the new file and set permissions.

    Code:
    adb shell mv /system/bin/debuggerd /system/bin/debuggerd.001
    adb push debuggerd /system/bin/debuggerd
    adb shell chmod 755 /system/bin/debuggerd
  • All done. Reboot.

Hope the above is useful or enlightening. Let me know if I need to explain anything in greater detail.