FORUMS

i9100 emulated sdcard, partition resizing and full disk encryption

5 posts
Thanks Meter: 4
 
By PelzigesOhr, Junior Member on 30th November 2015, 10:14 PM
Post Reply Email Thread
I recently upgraded a Galaxy S2 to Cyanogenmod 12.1 / android 5.1. I also wanted to use full disk encryption with the device and being able to access the internal memory via MTP instead of having a mass storage device. The removable sd card may be accessed via MTP, too, but will not be encrypted. Updating the recovery images to reflect the changes in the storage configuration (in case this is necessary) is not in scope, either.

Cyanogenmod by default only encrypts the /data partition. Newer models emulate the sdcard storage and save the data to /data/media, so everything gets encrypted. However, being an older phone there is still a separate sdcard-partition on the phone which is physically and directly mounted and which will not be touched by encryption.

In order to achieve the goal of full disk encryption two steps are necessary:
1. Change storage configuration to emulated media
2. Shrink old /sdcard partition and grow /data partition

= Change storage configuration to emulated media =
Emulating the sdcard in /data/media instead of physically mounting it directly requires changes to fstab.hardware (fstab.smdk4210), storage_list.xml and init.hardware.rc (init.smdk4210.rc). These files have to be changed in the Cyanogenmod source code and compiled to a new image. The configuration is based on the "Emulated primary, physical secondary" example given in h t t p s : / / source . android . com / devices / storage / config-example.html (cannot properly post URL due to new user restriction).

Here are the relevant changes in init.hardware.rc (init.smdk4210.rc):
Code:
--- init.smdk4210.rc.bak	2015-11-22 23:01:49.259579157 +0000
+++ init.smdk4210.rc.final	2015-11-30 20:21:37.977943177 +0000
@@ -2,35 +2,47 @@
 import init.gps.rc
 
 on init
-    export EXTERNAL_STORAGE /storage/sdcard0
+    export EXTERNAL_STORAGE /storage/emulated/legacy
+    export EMULATED_STORAGE_SOURCE /mnt/shell/emulated
+    export EMULATED_STORAGE_TARGET /storage/emulated
     export SECONDARY_STORAGE /storage/sdcard1
 
-    mkdir /mnt/media_rw/sdcard0 0700 media_rw media_rw
-    mkdir /mnt/media_rw/sdcard1 0700 media_rw media_rw
     mkdir /mnt/media_rw/usbdisk0 0700 media_rw media_rw
 
-    mkdir /storage/sdcard0 0770 root root
-    mkdir /storage/sdcard1 0770 root root
+    mkdir /mnt/shell/emulated 0700 shell shell
+    mkdir /storage/emulated 0555 root root
+    mkdir /mnt/media_rw/sdcard1 0700 media_rw media_rw
+    mkdir /storage/sdcard1 0700 root root
+
     mkdir /storage/usbdisk0 0770 root root
 
+    mkdir /storage/sdcard1 0775 system system
+
     mkdir /efs 0771 radio system
     mkdir /preload 0771 system system
     mkdir /mnt 0775 root system
     mkdir /mnt/.lfs 0755 root root
 
     # for backwards compatibility
-    symlink /storage/sdcard0 /sdcard
-    symlink /storage/sdcard0 /mnt/sdcard
-    symlink /storage/sdcard1 /extSdCard
-    symlink /storage/sdcard1 /mnt/extSdCard
     symlink /storage/usbdisk0 /usbdisk0
     symlink /storage/usbdisk0 /mnt/usbdisk0
 
+    symlink /storage/emulated/legacy /sdcard
+    symlink /storage/emulated/legacy /mnt/sdcard
+    symlink /storage/emulated/legacy /storage/sdcard0
+    symlink /mnt/shell/emulated/0 /storage/emulated/legacy
+    symlink /storage/sdcard1 /ext_card
+    symlink /storage/sdcard1 /mnt/ext_card
+  
+
+
 # Disable CFQ slice idle delay
     write /sys/block/mmcblk0/queue/iosched/slice_idle 0
 
 on fs
     mount_all /fstab.smdk4210
+    setprop ro.crypto.fuse_sdcard true
+
     swapon_all /fstab.smdk4210
 
     mkdir /efs/bluetooth
@@ -428,11 +440,10 @@
     oneshot
     keycodes 114 115 116
 
-service fuse_sdcard0 /system/bin/sdcard -u 1023 -g 1023 -d /mnt/media_rw/sdcard0 /storage/sdcard0
+service sdcard /system/bin/sdcard -u 1023 -g 1023 -l /data/media /mnt/shell/emulated
     class late_start
-    disabled
 
-service fuse_sdcard1 /system/bin/sdcard -u 1023 -g 1023 -d /mnt/media_rw/sdcard1 /storage/sdcard1
+service fuse_sdcard1 /system/bin/sdcard -u 1023 -g 1023 -w 1023 -d /mnt/media_rw/sdcard1 /storage/sdcard1
     class late_start
     disabled
Here are the relevant changes in fstab.hardware (fstab.smdk4210):
Code:
--- fstab.smdk4210.bak	2015-11-29 23:52:30.652913883 +0000
+++ fstab.smdk4210.final	2015-11-30 20:20:23.513945994 +0000
@@ -11,8 +11,8 @@
 /dev/block/mmcblk0p12                       /preload                ext4      noatime,nosuid,nodev,journal_async_commit                 wait
 
 # vold-managed volumes ("block device" is actually a sysfs devpath)
-/devices/platform/dw_mmc/mmc_host/mmc0/mmc0*     auto    auto    defaults        wait,voldmanaged=sdcard0:11,nonremovable,noemulatedsd
-/devices/platform/s3c-sdhci.2/mmc_host/mmc1*     auto    auto    defaults        wait,voldmanaged=sdcard1:auto,noemulatedsd
+/devices/platform/dw_mmc/mmc_host/mmc0/mmc0*     auto    auto    defaults        wait,voldmanaged=sdcard0:11,nonremovable
+/devices/platform/s3c-sdhci.2/mmc_host/mmc1*     auto    auto    defaults        wait,voldmanaged=sdcard1:auto
 /devices/platform/s3c_otghcd/usb*                auto    auto    defaults        voldmanaged=usbdisk0:auto
 
 # recovery
Here are the relevant changes in storage_list.xml:
Code:
--- storage_list.xml.bak2	2015-11-30 21:38:14.565769302 +0000
+++ storage_list.xml.final	2015-11-30 21:43:21.697757684 +0000
@@ -1,16 +1,13 @@
 <?xml version="1.0" encoding="utf-8"?>
 <StorageList xmlns:android="h t t p : / / schemas . android . com / apk / res / android "(cannot properly post URL due to new user restriction)>
-    <storage android:mountPoint="/storage/sdcard0"
-             android:storageDescription="@string/storage_internal"
-             android:primary="true"
-             android:removable="false"
-             android:allowMassStorage="true" />
+    <storage android:storageDescription="@string/storage_internal"
+             android:emulated="true"
+             android:mtpReserve="100" />
 
     <storage android:mountPoint="/storage/sdcard1"
              android:storageDescription="@string/storage_sd_card"
-             android:primary="false"
              android:removable="true"
-             android:allowMassStorage="true" />
+             android:maxFileSize="4096" />
 
     <storage android:mountPoint="/storage/usbdisk0"
              android:storageDescription="@string/storage_usb"
@@ -18,3 +15,4 @@
              android:removable="true" />
 
 </StorageList>
Compile Cyanogenmod and flash your phone. It should boot, but the amount of available storage should be significantly lower as the sdcard is emulated on the /data partition which has not been grown, yet.

= Shrink old /sdcard partition and grow /data partition =
Use PIT Magic to resize the partitions to an appropriate size. For a stock i9100 having 16GB of internal memory my configuration was as follows (according to the backup .pit-file downloaded via heimdall):
Code:
DATAFS:
start:		1,392,640
block count:	4,194,304
end:		5,586,943

UMS:
start:		5,586,944
block count:	24,133,632
end:		29,720,575

Hidden:
start:		29,720,576
block count:	1,048,576
end:		30,769,151
I changed the partition layout to the following sizes:
Code:
DATAFS:
start:		 1,392,640
block count:	28,295,167
end:		29,687,807

UMS:
start:		29,687,808
block count:	    32,768
end:		29,720,575

Hidden:
start:		29,720,576
block count:	1,048,576
end:		30,769,151
Deleting the UMS or hidden partitions or shrinking the hidden led to Cyanogenmod not booting. Upload the new .pit-file using heimdall and boot the modified Cyanogenmod. Reboot to Cyanogenmod recovery and wipe data. Formatting again using the Cyanogenmod recovery was required as apparently only this recovery honours the "encryptable=footer,length=-16384" option for the /data partition in fstab.hardware which directs the formatting tools to leave 16 kiB of space at the end of the partition for in-place encryption.
The Following User Says Thank You to PelzigesOhr For This Useful Post: [ View ] Gift PelzigesOhr Ad-Free
6th April 2016, 11:34 PM |#2  
Senior Member
Flag the wild south
Thanks Meter: 239
 
More
Is there a way to achieve emulated SD, without changing source code and recompiling?
Like editing fstab and other config files?
I run CM13 at the moment and would like to achieve full encryption through emulated SD.
13th April 2016, 06:31 PM |#3  
Junior Member
Thanks Meter: 9
 
More
I was playing around with the idea that i would resize the sdcard partition to 8mb and resize the /data partition (~14gb), so that applications have a hefty space and i would use the external sdcard (sdcard1) for storing data and media files. Would that be possible to with an emulated sdcard ? I was hoping that with the introduction of Android 6.0 it would allow me to merge (LVM?) the /sdcard0 and /sdcard1 but instead it merges /data and /sdcard1.
14th April 2016, 07:31 PM |#4  
OP Junior Member
Thanks Meter: 4
 
More
Quote:
Originally Posted by fireburner-de

Is there a way to achieve emulated SD, without changing source code and recompiling?
Like editing fstab and other config files?

I cannot prove that there is no way without having to recompile. However, I couldn't find storage_list.xml in the filesystem on my phone, so I assume that this file is only used during image compilation and therefore it's probably hard/impossible to achieve emulated SD without recompiling.

Maybe this configuration is easier to achieve using Cyanogenmod 13 / Android 6 as the storage_list.xml resource overlay has been removed.
The Following User Says Thank You to PelzigesOhr For This Useful Post: [ View ] Gift PelzigesOhr Ad-Free
16th September 2016, 11:47 PM |#5  
Junior Member
Thanks Meter: 0
 
More
PelzigesOhr, thank you sharing and documenting your experience so well, it has inspired me to try the same on Replicant 4.2 (distro based on CyanogenMod 10).

https:// github . com / GrimKriegor / replicant42-device_samsung_galaxys2-common / commit / 84c5a91a45b059a147921d0ea32367534904b314

However it seems the best way to create a partition table is using PitMagic, which seems to be proprietary software. Would you consider sharing your PIT file please?

Thank you for your time, all of this is greatly appreciated!

EDIT:
If you still have memory of the heimdall parameters used to flash this partition table, please do share as well.

EDIT2:
Managed to create a PIT file similar to yours, thanks for documenting it.
The following thread also includes a patch compatible with Replicant/Android 4.2.
Best of luck!
redmine . replicant . us / boards / 39 / topics / 13707
22nd September 2016, 05:02 AM |#6  
OP Junior Member
Thanks Meter: 4
 
More
Quote:
Originally Posted by GrimKriegor

Would you consider sharing your PIT file please?

Thank you for your time, all of this is greatly appreciated!

EDIT:
If you still have memory of the heimdall parameters used to flash this partition table, please do share as well.

EDIT2:
Managed to create a PIT file similar to yours, thanks for documenting it.
The following thread also includes a patch compatible with Replicant/Android 4.2.

Thanks for the praise, I'm happy that the instructions are of use to someone else. Good to know that you managed everything yourself, I'll answer anyway: This forum doesn't seem to support attachments, so I haven't attached the .pit-file. Feeding the parameters I provided to PIT Magic should yield a good result, though. PIT Magic is indeed a proprietary Windows binary, but it runs fine using wine (at least that's the way I use it).

All the flashing has been done by loading clockworkmodrecovery (
Code:
heimdall flash --KERNEL clockworkmodrecovery.6050.i9100.touch.img
) and afterwards sideloading the compiled ROM.
The Following User Says Thank You to PelzigesOhr For This Useful Post: [ View ] Gift PelzigesOhr Ad-Free
22nd September 2016, 08:28 PM |#7  
Junior Member
Thanks Meter: 0
 
More
Quote:
Originally Posted by PelzigesOhr

Thanks for the praise, I'm happy that the instructions are of use to someone else. Good to know that you managed everything yourself, I'll answer anyway: This forum doesn't seem to support attachments, so I haven't attached the .pit-file. Feeding the parameters I provided to PIT Magic should yield a good result, though. PIT Magic is indeed a proprietary Windows binary, but it runs fine using wine (at least that's the way I use it).

All the flashing has been done by loading clockworkmodrecovery (

Code:
heimdall flash --KERNEL clockworkmodrecovery.6050.i9100.touch.img
) and afterwards sideloading the compiled ROM.

Thanks for the reply sir.
Which partitions did you upload via Heimdall when you flashed the PIT file? I believe repartitioning could delete the contents of important partitions such as BOOT and MODEM, but I am unsure. Do you think maintaining their block boundaries would preserve the data in the respective partitions?
23rd September 2016, 07:25 AM |#8  
Junior Member
Thanks Meter: 0
 
More
guy i can't mount /storage/sdcard0
i use Cyanogenmod 12.1 android 5.1.1
i need help guy
23rd September 2016, 08:36 PM |#9  
OP Junior Member
Thanks Meter: 4
 
More
Quote:
Originally Posted by GrimKriegor

Which partitions did you upload via Heimdall when you flashed the PIT file? I believe repartitioning could delete the contents of important partitions such as BOOT and MODEM, but I am unsure. Do you think maintaining their block boundaries would preserve the data in the respective partitions?

The exact command I used for repartitioning with Heimdall-1.3.1 was as follows:
Code:
heimdall flash --repartition --pit <filename>
Data should be preserved and if you don't mess with the boundaries of partitions other than DATAFS, UMS and HIDDEN you should be fine. Before repartitioning I backed up all partitions as a precautionary measure, but I didn't have to restore any of them. See also http://forum.xda-developers.com/gala...n-rid-t2353551
23rd September 2016, 09:09 PM |#10  
Junior Member
Thanks Meter: 0
 
More
Quote:
Originally Posted by PelzigesOhr

The exact command I used for repartitioning with Heimdall-1.3.1 was as follows:

Code:
heimdall flash --repartition --pit <filename>
Data should be preserved and if you don't mess with the boundaries of partitions other than DATAFS, UMS and HIDDEN you should be fine. Before repartitioning I backed up all partitions as a precautionary measure, but I didn't have to restore any of them. See also http://forum.xda-developers.com/gala...n-rid-t2353551

Ah! This is excellent, thanks for clarifying this! Just one last question if I may, how did you backup the partitions? Did you use Heimdall to download their contents as image files, did you use DD or maybe even ADB?

Thank you for your time!
23rd September 2016, 09:32 PM |#11  
OP Junior Member
Thanks Meter: 4
 
More
Quote:
Originally Posted by GrimKriegor

Just one last question if I may, how did you backup the partitions? Did you use Heimdall to download their contents as image files, did you use DD or maybe even ADB?

Good guess, I used dd and adb. Get a shell on the phone with adb and dump the partitions to files using dd:
Code:
dd if=/dev/block/mmcblk0pX of=/sdcard/mmcblk0pX.bin bs=512
And then copy the files to your computer using adb pull. You may want to get the mountpoints of the partitions (e.g. using mount) so that you can associate them easily.
The Following User Says Thank You to PelzigesOhr For This Useful Post: [ View ] Gift PelzigesOhr Ad-Free
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes