Bad flash - need to recover /data

Search This thread

JavaScout

Senior Member
Apr 3, 2012
679
275
Hi, friends.
I'd really appreciate your help.
My phone is suffering from consequences of kernel flash, which was obviously not a very successful :silly:
Short <intro>, if I may:
I have been using Cyanogenmod 12.1 nightlies (lollipop 5.1.1) for a long time. Everything was great.
One day I decided to try out Dorimanx kernel for lollipop, compatible with CM, of course. Big mistake.
After the flash (wiped Cache, Dalvik, System before the flash) I lost /eMMC, since Dori is lacking ext4 support for /eMMC, which I wasn't aware of.
Tried to flash my previous rom back (wiped Cache, Dalvik, System before the flash), but I got even more processes failing, probably due to different SELinux policies (Dori - permissive, stock CM - enforcing).
While I had Dori on board, I managed to backup /eMMC(copy-paste over MTP bridge to Windows PC) and /data(as binary image and TWRP-backup).
Now, even when I restore those two (eMMC or /data) under new clean ROM installation - I keep getting errors.

</intro>
/emmc issue example:
Code:
[ 12-20 22:20:42.885 8856: 9388 E/CacheManager ]
Cannot instantiate cache!
java.io.FileNotFoundException: /storage/sdcard0/Android/data/com.android.gallery3d/cache/imgcache.idx: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:456)
...
[ 12-20 22:20:42.923 8856: 9388 W/Worker ]
Exception in running a job
java.lang.NullPointerException: Null pointer exception during instruction 'monitor-enter v6'
at com.android.gallery3d.data.ImageCacheService.getIm ageData(ImageCacheService.java:65)
/data issue example:
Code:
[ 12-14 17:03:07.745  6350: 6350 W/com.google.android.gms ]
type=1400 audit(0.0:2308): avc: denied { lock } for comm=4173796E635461736B202331 path="/data/data/com.google.android.gms/databases/reminders.db" dev=mmcblk0p10 ino=123233 scontext=u:r:untrusted_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=file
What I've tried so far:
  1. e2fsck (/emmc)
  2. twrp - fix permissions (/data)
  3. twrp format partition + copy paste from PC (/emmc)
  4. find /emmc/Android/data/ -type d -exec chmod 775 {} ';' (/emmc)
  5. find /emmc/Android/data/ -type f -exec chmod 664 {} ';' (/emmc)
  6. restorecon -FR (/emmc)
  7. install Kitkat-permissive rom on top of broken /data
Unfortunately, nothing helps.
I really want to keep my old /data partition, what's why I spent on this around 2 weeks, and I'll keep trying.
Can you please guide me from the technical perspective?

P.S. The /emmc issue was a dumb thing that I always forget:
Code:
[email protected]:/mnt/media_rw # find sdcard0 -type d -exec chmod 775 {} ';'
[email protected]:/mnt/media_rw # find sdcard0 -type f -exec chmod 664 {} ';'
[email protected]:/mnt/media_rw # chown -R media_rw:media_rw sdcard0
But the 2nd issue with /data is still unsolved.
Please assist! :(
 
Last edited:

Dragoon Aethis

Senior Member
Jun 9, 2012
90
69
www.dragonic.eu
If you need to restore your data only, you could manually mount the /data image on your PC (if you're not using Linux as your desktop OS, you can install Ubuntu or Mint under a virtual machine like VMware Player, which is free - if you need help with that, or mounting the image, PM me), wipe your device and copy over data for any apps you need to the new file system. Then, reapply proper permissions for copied data. Titanium Backup Pro can do that for you with a GUI, but requires a CWM backup if I remember correctly and won't talk with TWRP/raw images (I might be wrong).

Now, this all looks nice and all, but that "reapply proper permissions" part is a bit messy. It's because Android creates a new Linux user and group for every individual Android app you install. There are some special users/apps sharing certain details, but regular apps will have a new account like "u0_a47" or something like that created for them. If you just copy and paste any data to your new filesystem, you'll get hit with access errors (pretty much exactly what you've described). Reinstall all apps from .apks (if you have bare .apks backed up somewhere, or have extracted them from your /data, you can reinstall them all en masse with adb), then in terminal (as root, otherwise you won't be able to peek inside /data/data): ls -la /data/data > /storage/sdcard0/applist.txt, pull /storage/sdcard0/applist.txt to your computer (with adb), and then the messy part: Copy over any data you want, and reapply proper permissions for any app you've restored with chown -R uX_aY /data/data/org.your.app, where uX_aY is your app's user/group (listed in applist.txt), and org.your.app is your app identifier (also listed in applist.txt).

This is messy as hell, but it works.
 
  • Like
Reactions: JavaScout

JavaScout

Senior Member
Apr 3, 2012
679
275
Thank you for the answer! Can you confirm, please, if I'm getting this right?

1. Install from google play all the needed apps under clean rom (to get list of proper permissions)
2. Save output of ls -al from clean rom in a text file (proper permissions)
3. Extract apps from backed up /data image
4. Push them into /data of the clean rom
5. Run chown on each of the restored folders, using ouput of ls -al
6. Reboot

Now, the point where I extract apps from image is also tricky... afaik, single app is scattered between multiple directories under /data. Do you know any explanatory article on this subject? What's app structure under /data etc?
 

Dragoon Aethis

Senior Member
Jun 9, 2012
90
69
www.dragonic.eu
Extract apps and data from /data image, but yeah, everything else looks good. Under Android 6.0 (older versions aren't that different if I remember correctly), with a single user, you have two folders: /data/app which contains installed .apks, and /data/data which contains your app's data - everything else is system-specific and will get generated automatically if needed, regular apps can't touch these at all. Simply extract these two folders and you're good to go.
 

JavaScout

Senior Member
Apr 3, 2012
679
275
Are those changes persistent? I did chown for some /emmc files under TWRP recovery, reinstalled rom, and i can tell that root explorer& terminal do not show my changes at all... :-/
 

JavaScout

Senior Member
Apr 3, 2012
679
275
Unfortunately, your solution is missing something... still getting errors... [ 12-26 19:53:38.802 4124: 4124 W/com.socialnmobile.dictapps.notepad.color.note ]

type=1400 audit(0.0:197): avc: denied { lock } for comm=4173796E635461736B202331 path="/data/data/com.socialnmobile.dictapps.notepad.color.note/databases/colornote.db" dev=mmcblk0p10 ino=123103 scontext=u:r:untrusted_app:s0 tcontext=u:eek:bject_r:system_data_file:s0 tclass=file



[ 12-26 19:53:38.802 4124: 4124 W/com.socialnmobile.dictapps.notepad.color.note ]

type=1400 audit(0.0:198): avc: denied { lock } for comm=4173796E635461736B202331 path="/data/data/com.socialnmobile.dictapps.notepad.color.note/databases/colornote.db" dev=mmcblk0p10 ino=123103 scontext=u:r:untrusted_app:s0 tcontext=u:eek:bject_r:system_data_file:s0 tclass=file



[ 12-26 19:53:38.802 3821: 4125 E/FlurryAgent ]



java.io.FileNotFoundException: /data/data/com.socialnmobile.dictapps.notepad.color.note/files/.flurrydatasenderblock.ba285603-75ea-4129-84cd-8ac36726fb02: open failed: EACCES (Permission denied)

at libcore.io.IoBridge.open(IoBridge.java:456)
 

Dragoon Aethis

Senior Member
Jun 9, 2012
90
69
www.dragonic.eu
I don't know how to copy it over and set correct permissions to make it work with restrictive SELinux policies, but you can use a database viewer/editor or sqlite3 binary on the device to export/import the data inside.
1. Copy your colornote.db with data somewhere accessible (not /data/....). Wipe the app's data completely, then start it once. It should regenerate all required files. Connect your phone to PC, and fire up adb shell. (You can do this with the terminal on the device directly too.) Get root access with "su".
2. "cd" to the folder with your colornote.db (which you want to use). Then (as root) do this on your device: https://stackoverflow.com/questions/4389930/exporting-data-from-sqlite-3#4390636 - sqlite3 and any text editor (vi, most likely) should be available in your ROM (if not, install Busybox). You should now end up with 2 .sql files in the same folder.
3. Still as root: "sqlite3 /data/data/com.socialnmobile.dictapps.notepad.color.note/databases/colornote.db", then ".schema" (there's a dot before "schema"). It'll write out a few statements like "CREATE TABLE foo". There shouldn't be too many, you'll have to "DROP TABLE foo;" for every single one of them, then ".read schema.sql", followed by ".read inserts.sql" - both files created in step 2, you'll have to supply full paths to be read. Then ".exit", and you're done.

(And someone will probably come and tell me all of this can be done in a few seconds with two-three commands I didn't know about before. :p)
 

JavaScout

Senior Member
Apr 3, 2012
679
275
Thanks for your time, mate. I will probably try to dig into CM sources of SELinux implementation, or to read a really detailed book on the subject. I guess it's the only way to fix my backed up /data.
 

JavaScout

Senior Member
Apr 3, 2012
679
275
Dragoon Aethis, tell me one more thing, please: if I grab/push single application, as you suggested, getting bad selinux feedback, then MOVING app to sdcard (built-in function of CM rom), and moving back? Should it restore proper permissions, what do you say? Of course, I'll try that on the weekend, since my i9100 is a daily driver, I cannot experiment during the week :) I thought of this, because most of the time sdcard has different filesystem than /data, which makes impossible to keep permissions there.
 

Dragoon Aethis

Senior Member
Jun 9, 2012
90
69
www.dragonic.eu
Weeeell, you can format /sdcard as ext4 and your problems are gone, but so are proper Android permissions for that it thinks should be on /sdcard :D Moving it might work, but moving apps to SD is hit and miss most of the time anyways, so you'd have to try.
 

JavaScout

Senior Member
Apr 3, 2012
679
275
Ok, I'll try. My /emmc is ext4 anyway. Not sure if this is a good thing, since I'm expecting the transferred application to lose permissions and selinux properties during move & restore. It is the only way to trigger creation of the proper properties. Otherwise it's only copy-paste of the same broken properties, as well as the files themselves. Of course I haven't seen the implementation on the github, but I will try that on weekend, and reply back.
 

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    If you need to restore your data only, you could manually mount the /data image on your PC (if you're not using Linux as your desktop OS, you can install Ubuntu or Mint under a virtual machine like VMware Player, which is free - if you need help with that, or mounting the image, PM me), wipe your device and copy over data for any apps you need to the new file system. Then, reapply proper permissions for copied data. Titanium Backup Pro can do that for you with a GUI, but requires a CWM backup if I remember correctly and won't talk with TWRP/raw images (I might be wrong).

    Now, this all looks nice and all, but that "reapply proper permissions" part is a bit messy. It's because Android creates a new Linux user and group for every individual Android app you install. There are some special users/apps sharing certain details, but regular apps will have a new account like "u0_a47" or something like that created for them. If you just copy and paste any data to your new filesystem, you'll get hit with access errors (pretty much exactly what you've described). Reinstall all apps from .apks (if you have bare .apks backed up somewhere, or have extracted them from your /data, you can reinstall them all en masse with adb), then in terminal (as root, otherwise you won't be able to peek inside /data/data): ls -la /data/data > /storage/sdcard0/applist.txt, pull /storage/sdcard0/applist.txt to your computer (with adb), and then the messy part: Copy over any data you want, and reapply proper permissions for any app you've restored with chown -R uX_aY /data/data/org.your.app, where uX_aY is your app's user/group (listed in applist.txt), and org.your.app is your app identifier (also listed in applist.txt).

    This is messy as hell, but it works.
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone