If ... then ... else and init.rc - Android Q&A, Help & Troubleshooting

Hi, I'd like to ask a general question about device boot. I'd like to mount /system, /data, and /cache partitions in selected locations based on detection of sdcard in the device.
How could I add an if ... then ... else statement to init.rc before that /system, /data, and /cache partitions are mounted? Any ideas?

Solved
I've found a way to perform this task. It's possible to place if ... then ... else statement into a shell script and put the shell script into ramdisk root directory of boot.img. Then, just run the script by busybox ash command from init.rc (with busybox located into ramdisk root directory).

cristian_c said:
I've found a way to perform this task. It's possible to place if ... then ... else statement into a shell script and put the shell script into ramdisk root directory of boot.img. Then, just run the script by busybox ash command from init.rc (with busybox located into ramdisk root directory).
Click to expand...
Click to collapse
Hey... I managed to mount /dev/block/mmcblk1p2 as my /data partition by editing my boot.img ramdisk(the 3. fstab files and the .rc files... i replaced [email protected] with dev/block/mmcblk1p2 wherever i saw them)
So i was trying to use an if statement in some of the .rc files to check if /dev/block/mmcblk1p2 was mounted on /data and if not then mount /dev/block/mmcblk0p9 (my usrdata partition) on /data... Soon i realised that this isn't exactly bash...
Please explain how you managed to get a .sh to run from your .rc files( or do you only need it in the init.rc, and remove all the /data mounting lines from them and do it all from the script???)
I don't always have access to a pc so if my sdcard somehow dies i want my phone to boot using the internal /data partition otherwise i will bootloop until i flash the original boot.img

nullbyte001 said:
Hey... I managed to mount /dev/block/mmcblk1p2 as my /data partition by editing my boot.img ramdisk(the 3. fstab files and the .rc files... i replaced [email protected] with dev/block/mmcblk1p2 wherever i saw them)
So i was trying to use an if statement in some of the .rc files to check if /dev/block/mmcblk1p2 was mounted on /data and if not then mount /dev/block/mmcblk0p9 (my usrdata partition) on /data... Soon i realised that this isn't exactly bash...
Please explain how you managed to get a .sh to run from your .rc files( or do you only need it in the init.rc, and remove all the /data mounting lines from them and do it all from the script???)
I don't always have access to a pc so if my sdcard somehow dies i want my phone to boot using the internal /data partition otherwise i will bootloop until i flash the original boot.img
Click to expand...
Click to collapse
I've found on fs_property:ro.mount.fs=EXT4 in init.rc. In that section, I've added the following iine:
Code:
exec /busybox ash /mount_partitions.sh
[of course, I've placed busybox arm compiled binary (the version provided by busybox android app should work) and a mount_partitions.sh script (created by myself) into boot image ramdisk (I mean / main directory, the same where init.rc is located) ]
You could also need to give permissions to busybox and to .sh script. You could also need to remount / in read-write mode, in case of issues with the above command.

Related

[Q] Can one mount an Android file system image?

So after a failed attempt to upgrade from CyanogenMod 10.1.3 to 10.2, I was unable to access /data or /sdcard because both systems were encrypted. I ended up having to factory reset my phone because it refused to co-operate or let me access my files. However, before I did that, I was able to run
Code:
adb shell "dd if=/dev/block/mmcblk0p2" > data.img
and
Code:
adb shell "dd if=/dev/block/mmcblk0p3" > sdcard.img
, which appears to have copied the raw partition images from the phone (at least, they're the right sizes).
According to my reading, Android (and, I'm inferring, CyanogenMod) encrypts filesystems using dm-crypt, with a AES-CBC ESSIV:SHA256 cipher, with the key being derived from the password using PBKDF2. Knowing the precious little I do about encrypted file systems, my guess is that if I configure the image in cryptsetup to create a drive mapping, I can mount the mapped drive and recover the data from the images.
According to /fstab.herring on my ahem, fresh, install of Android, the /data partition is in ext4 format whereas the /sdcard partition is vFAT. So, once I've gotten through the encryption on the partition images, they should mount normally, right?
I know that dm-crypt accepts plain, LUKS, LoopAES and TrueCrypt device formats. I'm inferring from the PBKDF2 extension that Android goes the LUKS route for encrypting. Is this conclusion correct?
Could someone explain whether it's possible to decrypt a dumped android image? I'm really hoping that the cypher information is stored on the file system and not on some key file that I nuked in the factory reset. If it can, in theory, be decrypted, am I using the right tools to approach the matter? If so, I'll continue fiddling with cryptsetup and mount, but no sense in wasting time if it's an impossible task.
Never did get a response to this question, so I'll try it again, but start with a simpler question:
If someone dds an Android (specifically Cyanogenmod 10.x) partition to an img file, is there any way to read that image from, say a Linux laptop? I dumped the contents of the /system partition using
Code:
adb shell "dd if=/dev/block/mmcblk0p1" > system.img
I expected system.img to be a normal ext4 partition. However, attempting to loopback mount it with
Code:
sudo mount -t ext4 -o loop,ro system.img ~/android/system
Gave me errors about corrupt group descriptors, bad magic numbers and other maladies indicative of a thoroughly corrupted file system. I'm assuming that:
/data has the same ext4 partition structure as /system; and
The process to mount /storage would be no different to mounting /system with the exception that the former uses vFAT as its file system
However, as my Android is currently working normally (well, as well as one can hope for Android to work), I know I don't have a corrupted file system.
So what's going on? Does Android use a special version of ext4 that other Linuxes don't recognise? Am I not dd-ing correctly? Is there a block-size issue I ignored to my peril?
Borden Rhodes said:
So after a failed attempt to upgrade from CyanogenMod 10.1.3 to 10.2, I was unable to access /data or /sdcard because both systems were encrypted. I ended up having to factory reset my phone because it refused to co-operate or let me access my files. However, before I did that, I was able to run
Code:
adb shell "dd if=/dev/block/mmcblk0p2" > data.img
and
Code:
adb shell "dd if=/dev/block/mmcblk0p3" > sdcard.img
, which appears to have copied the raw partition images from the phone (at least, they're the right sizes).
According to my reading, Android (and, I'm inferring, CyanogenMod) encrypts filesystems using dm-crypt, with a AES-CBC ESSIV:SHA256 cipher, with the key being derived from the password using PBKDF2. Knowing the precious little I do about encrypted file systems, my guess is that if I configure the image in cryptsetup to create a drive mapping, I can mount the mapped drive and recover the data from the images.
According to /fstab.herring on my ahem, fresh, install of Android, the /data partition is in ext4 format whereas the /sdcard partition is vFAT. So, once I've gotten through the encryption on the partition images, they should mount normally, right?
I know that dm-crypt accepts plain, LUKS, LoopAES and TrueCrypt device formats. I'm inferring from the PBKDF2 extension that Android goes the LUKS route for encrypting. Is this conclusion correct?
Could someone explain whether it's possible to decrypt a dumped android image? I'm really hoping that the cypher information is stored on the file system and not on some key file that I nuked in the factory reset. If it can, in theory, be decrypted, am I using the right tools to approach the matter? If so, I'll continue fiddling with cryptsetup and mount, but no sense in wasting time if it's an impossible task.
Click to expand...
Click to collapse
Can you give the result of the "file sdcard.img" and "file data.img" commands?
You are quite right. With regular LUKS container/partition, you would do (being root) the following. With the following commands, you can create a container named "safe", setup it, then format its content in ext3 and mount the partition:
Code:
dd if=/dev/zero bs=1M count=50 of=safe
losetup /dev/loop0 safe
cryptsetup luksFormat -c aes -h sha256 /dev/loop0
cryptsetup luksOpen /dev/loop0 safe
mkfs.ext3 /dev/mapper/safe
(losetup /dev/loop0 safe)
(cryptsetup luksOpen /dev/loop0 safe)
mkdir mnt
mount -t ext3 /dev/mapper/safe mnt
//HERE: do whatever you want in your mounted encrypted filesystem
umount mnt
cryptsetup luksClose safe
losetup -d /dev/loop0
For details, you can go there: http://blog.theglu.org/index.php/20...-couteau-suisse-du-chiffrement-de-partitions/
Sorry, the article is in French but you can translate it if you need to.
Here, using "hexdump", you can see the "safe" file has a LUKS magic at the beginning. And doing a "file safe" command, you can check it detects it as a "LUKS encrypted file".
If doing "file" on your .img files does not give you the same result, you may not be able to directly use the "cryptsetup" command and need to adapt it.
Finally: usually in Android the header containing the key is stored on another partition so you may have lost it when wiping your phone, sorry.
---------- Post added at 02:44 PM ---------- Previous post was at 02:41 PM ----------
Borden Rhodes said:
Never did get a response to this question, so I'll try it again, but start with a simpler question:
If someone dds an Android (specifically Cyanogenmod 10.x) partition to an img file, is there any way to read that image from, say a Linux laptop? I dumped the contents of the /system partition using
Code:
adb shell "dd if=/dev/block/mmcblk0p1" > system.img
I expected system.img to be a normal ext4 partition. However, attempting to loopback mount it with
Code:
sudo mount -t ext4 -o loop,ro system.img ~/android/system
Gave me errors about corrupt group descriptors, bad magic numbers and other maladies indicative of a thoroughly corrupted file system. I'm assuming that:
/data has the same ext4 partition structure as /system; and
The process to mount /storage would be no different to mounting /system with the exception that the former uses vFAT as its file system
However, as my Android is currently working normally (well, as well as one can hope for Android to work), I know I don't have a corrupted file system.
So what's going on? Does Android use a special version of ext4 that other Linuxes don't recognise? Am I not dd-ing correctly? Is there a block-size issue I ignored to my peril?
Click to expand...
Click to collapse
Can you give the result of the "file system.img" command?
Thanks, saidlike, for your reply:
saidelike said:
Can you give the result of the "file sdcard.img"...
Click to expand...
Click to collapse
sdcardPartitionDump.img: data
saidelike said:
... and "file data.img" commands?
Click to expand...
Click to collapse
data.img: data
saidelike said:
Can you give the result of the "file system.img" command?
Click to expand...
Click to collapse
system.img: Linux rev 1.0 ext4 filesystem data, UUID=57f8f4bc-abf4-655f-bf67-946fc0f9f25b (needs journal recovery) (extents) (large files)
Again, attempting to run
Code:
mount -t ext4 -o loop systemimg mountpoint/
yields
mount: wrong fs type, bad option, bad superblock on /dev/loop0,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so
Click to expand...
Click to collapse
Ignoring the results of data.img and sdcard.img for the time being, the fresh dump of the system partition shows that it's an EXT4 filesystem, but that it's heavily corrupted. fsck.ext4 on that partition basically asks me to fix every single inode, so it's not a simple unclean journal issue. Therefore, is it fair to conclude that CyanogenMod (and maybe AOSP too) have modified the ext4 partiiton type?
@Borden Rhodes
Maybe, my reply is too late, but you could try to make the same experiment with backup of your current data.
If you get the same results as with the old pre-wipe backup, then you still have a hope.

[Q] Hosts file ressetting on reboot

Does anybody know why my hosts file gets reset after a reboot no matter whether I change with adaway or adblock or if I manually replace the hosts file myself and set to read only. Have searched about and only solution I read about was get a custom kernel. Although I am already on a custom kitkat ROM and using dorimanx kernel for galaxy s2. Also never had problems with hosts resetting on jb or ICS. Even creating a symlink to data/data/hosts doesn't work. On reboot the symlink just gets replaced with the original useless hosts file.
Sorry to bump but it is irritating and can't find a solution.
just some hints,
hosts file is located on /system partition which should be read only mounted during runtime.
check:
Code:
# mount | grep /system
/dev/block/mmcblk0p9 /system ext4 ro,seclabel,relatime,user_xattr,barrier=1,data=ordered 0 0
ro is ok. If so, the file can be changed only during boot (some init script) or later by app with root permission.
check if some init script is not involved:
Code:
# grep hosts /init*
Code:
#grep hosts /system/etc/init.d/*
Is there any output of the commands? I'm on i9100/CM10.2 and there is no output.
Then would be good to revoke root permissions to all apps (not unroot) and see if the issue is still there.
and also check the time of last change of the file:
Code:
ls -l /system/etc/hosts
is it realy during boot?
After spending entirely too much time on this I finally have a working solution. Save the contents of this script to your internal SD card. Ensure the last line is a blank carriage return.
Code:
#!/system/bin/sh
sleep 10
mount -o rw,remount /system
cp /storage/emulated/0/Download/working/hosts /system/etc
Install Script Manager (SManager), and set this script as: on boot, executable, as root. This will then copy your custom "hosts" file under your SDcard to /system/etc after 10 seconds.
Using this script through init.d didn't work for me, which I guess means whatever was over-writing my "hosts" file was doing after the init.d process.
Definitely remount system partition to read only again. Append following line to your script:
Code:
mount -o ro,remount /system
d0gd3v said:
Definitely remount system partition to read only again. Append following line to your script:
Code:
mount -o ro,remount /system
Click to expand...
Click to collapse
good call, thanks

Move /data/apps to sdcard

What I want to do is partition my sdcard into 2 parts, 1 fat32 and the second as ext3. Mount both partitions at startup so i get /sdcard (fat) and /sdcardext3 (ext). Then create 2 folders /sdcardext3/apps and /sdcardext3/apps-private then delete the /data/apps and /data/apps-private folders and replace them with links to the folders on the sdcard 2nd partition I just created. Is this possible? I guess I'll be having to muss around with fstab but I'm not too well versed on that. Anyone got any tips on how I can achieve this. I know there are apps like foldermount and link2sd but they only work on a per app basis, i want a full "do it and forget it" solution that doesn't rely on an app.
Cheers
EDIT:
I'm seeing these 3 lines in /init.rc
Code:
mount rfs /dev/stl11 /data nosuid nodev usedm check=no
chown system system /data
chmod 0771 /data
Is it possible to create and format an rtf partition on the sdcard, then extract, change and repack the ramdisk image with the following changes?
Code:
mount rfs /dev/block/vold/179:2 /data nosuid nodev usedm check=no
chown system system /data
chmod 0771 /data
To move the data folder completely off the phone internal memory. My phone only has 512MB and my external SD is 64GB so I won't cry at the wasted half a gig.
So far I've managed to:
extract the boot.img from the stock firmware I'm using. Using a linux box with abootimg installed (apt-get abootimg)
I split it up into the kernel (zImage) and the ramdisk (initrd.img) .
Unpacked and cpio-ed initrd.img to files.
Edited init.rc and found two instances of
Code:
mount rfs /dev/stl11 /data nosuid nodev usedm check=no
in the file
Replaced /dev/stl11 with /dev/block/mmcblk0p2 and replaced rfs with ext3.
Combined the kernel and ramdisk back together with gzip, cpio and abootimg (newboot.img) and dumped it onto the phone using adb along with the flash_image command (which wasn't on the phone as standard).
Had to copy flash_image (in fact I had to cat flash_image >/data/flash_image because there's no cp command as standard either) to /data as it needed executable privs, chmod 777 /data/flash_image to allow it to execute,
Then execute /data/flash_image boot /sdcard/newboot.img
Reboot phone
Phone;s stuck on the Samsung splash screen, pah. Was worth a try anyway.

fstab and init.rc roles in boot process

Hi, I'd like to ask a general question about fstab and init.rc. I'd like to select an alternative fstab from the boot partition, between two different fstab files, one of these with emmc partitions and the other with partitions on external sdcard, in order to boot either emmc or sdcard, alternatively.
I've read the init documentation at: https://android.googlesource.com/platform/system/core/+/master/init/readme.txt about init.rc configuration file.
Reading my init.rc file a doubt has been emerging: if the init executable looks at fstab (via mount_all) by default, why does init.rc contain mount commands related mounting /system, /data, and /cache partitions, if they are already defined in fstab file? Not enough the fstab existence in order to mount partitions, or do I need always to operate mounts in init.rc?
Any ideas?
cristian_c said:
Hi, I'd like to ask a general question about fstab and init.rc. I'd like to select an alternative fstab from the boot partition, between two different fstab files, one of these with emmc partitions and the other with partitions on external sdcard, in order to boot either emmc or sdcard, alternatively.
I've read the init documentation at: https://android.googlesource.com/platform/system/core/+/master/init/readme.txt about init.rc configuration file.
Reading my init.rc file a doubt has been emerging: if the init executable looks at fstab (via mount_all) by default, why does init.rc contain mount commands related mounting /system, /data, and /cache partitions, if they are already defined in fstab file? Not enough the fstab existence in order to mount partitions, or do I need always to operate mounts in init.rc?
Any ideas?
Click to expand...
Click to collapse
The fstab file is just a reference..
The init.rc is actually mounting them
The fstab file is just a reference..
The init.rc is actually mounting them
Click to expand...
Click to collapse
Thanks for the help! I'll try to work with this stuff
Romeotamizh said:
The fstab file is just a reference..
The init.rc is actually mounting them
Click to expand...
Click to collapse
I have a problem related to this and i can't figure it out. Basically my rom is stuck in a plash screen and i have narrowed it down to this:
Code:
<11>[ 3.142415] fs_mgr: Cannot open file /fstab.qcom
<11>[ 3.142425] init: unable to read fstab /fstab.qcom: No such file or directory
so it looks like init.rc is sending fs_mgr to look for fstab in the root directory /,
but it is actually in /etc, i can't understand why is this happening, I think it may have to do with other previous errors that occur a few lines before
Code:
<11>[ 2.887007] init: /init.rc: 11: invalid command 'setcon'
<11>[ 2.887037] init: /init.rc: 57: invalid command 'load_all_props'
<11>[ 2.887088] init: could not import file '/init.recovery.logd.rc' from '/init.rc'
<13>[ 2.887160] init: (Parsing /init.recovery.usb.rc took 0.00s.)
<11>[ 2.887177] init: could not import file '/init.recovery.qcom.rc' from '/init.rc'
<13>[ 2.887186] init: (Parsing /init.rc took 0.00s.)
heres my code: https://github.com/edTheGuy00/android_device_zuk_z2pro
any help would be appreciated.
Maybe you should paste your init.rc here in order to spot the issue.
Solved
I've found a way to select alternative partitions during boot process. It's possible to use busybox ln commands inside a shell script (placing busybox binary and shell script into ramdisk root directory of boot.img) and run the script by busybox ash command from init.rc. The commands inside shell script should look like as the following:
Code:
/busybox ln -sf /dev/block/mmcblk1p2 /[email protected]
/busybox ln -sf /dev/block/mmcblk1p3 /[email protected]
/busybox ln -sf /dev/block/mmcblk1p4 /[email protected]
These commands should be put into an if...then block inside the shell script. The conditional statement should looks like this:
Code:
if [ -b /dev/block/mmcblk1 ]
That way, if external sdcard is detected, shell script will change targets of [email protected] symbolic links from internal emmc partitions to external sdcard ones. So, init will use /system, /data and /cache mountpoints with the latter in place of the default ones during mount.

Su/su.d/script.

Hi guys. Does any of you know how to push su.d directory into su folder? I mean I need radio.sh script in su/su.d to get root call sms manager working. Every time I try it disappears after reboot.
Rooted rom 34.3.A.0.217 with TWRP.
Anyone?
It's late though, but for the guidance of other noobs like me. /su/su.d/ can't be flashed in recovery because this isn't available. For system-less roots, binaries/libraries required to start supersu daemon and to manage root permissions, don't reside in /system/bin but in /data/su.img while (at least) two files reside in boot.img i.e. boot.img/ramdisk/init.supersu.rc and boot.img/ramdisk/sbin/.*daemonsu*.sh. When ROM boots, init.supersu.rc mounts /data/su.img on /su directory and calls /sbin/*daemonsu*.sh for initial setup which starts /su/bin/daemonsu and other se-linux related stuff and then executes any scripts found in /su/su.d/ and /system/su.d.
You can find this:
Code:
mido:/ # mount | grep loop
/dev/block/loop0 on /su type ext4 (rw,seclabel,noatime,data=ordered)
So, the init.d scripts placed in /su/su.d actually reside in /data/su.img which isn't mounted in recovery mode and anything flashed to /su/su.d goes to volatile rootfs which is gone on reboot. Either mount su.img in recovery, or place this script while ROM is running or instead flash to /system/su.d which is permanent filesystem unlike rootfs (which lives in RAM). And finally don't forget to set permissions to 0700 as advised here. Another thing, mostly init.d scripts don't work with .sh extension. Avoid this too.

Categories

Resources