Make persistent changes to init.rc
Asked Answered
A

8

42

I want to change the init.rc file of an android pad. But after I change it and reboot the system, the original init.rc comes back.

How can I make the change to the init.rc persistently without rebuild the system (since I don't have the source code of the system)? Or is there any way to work around?

Antiphrasis answered 19/3, 2012 at 9:55 Comment(5)
how you did changes to init.rc ?? by pulling file with ADB ?? and then pushing to device ??Deerstalker
is the device had root permission ??Deerstalker
Yes, busybox is installed, and whoami=root. I can change and replace the init.rc in / without problem. But the old init.rc is recovered after reboot. And it seems all added new files in / directory are gone after reboot. This happend not only in two different pads and also in Emulator. As I know, the Emulator should be modified with the ramdisk.img, but I did not try. I want to find the way to change init.rc persistently in the real pad.Antiphrasis
Faced same issue, you can find working soln - gist.github.com/ashutosh-mishra/…Concernment
I am still having issues trying to modify init.rc (even with magisk installed and dm-verity disabled) - is there a recommended way to update init.rc if you don't have access to the source for the ROM?Horace
V
14

Unpack the uramdisk using following command in host PC(Linux)

mkdir /tmp/initrc cd /tmp/initrd
sudo mount /dev/sdb1 /mnt          

sdb1 is partion where uramdisk/uInitrd resides.

dd bs=1 skip=64 if=/mnt/uInitrd of=initrd.gz
gunzip initrd.gz

At this point running the command file initrd should show:

mkdir fs
cd fs
cpio -id < ../initrd

Make changes to init.rc

Pack uramdisk using following commands:

find ./ | cpio -H newc -o > ../newinitrd
cd ..
gzip newinitrd
mkimage -A arm -O linux -C gzip -T ramdisk -n "My Android Ramdisk Image" -d newinitrd.gz uInitrd-new
Vie answered 4/8, 2012 at 8:47 Comment(3)
How do you know that /dev/sdb1 is where uramdisk/uInitrd? When I attach the phone I only can see the sdcard contet under /dev/sdb. How can I search for uramdisk?Factitive
/dev/sdb1 doesn't exist on my device. You may need to discuss how to determine correct block device where boot image resides.Nobleminded
It will vary depending on Android device. If your device is rooted, try executing "adb shell mount" for a list of mounted file systems. This will give you some good hints as to where to look.Thebault
S
12

A number of Android devices include code to prevent root modifications to the system files. The way this is done is by using the recovery partition. On reboot, they basically restore the system partition using the recovery image. If your system is doing that then you cannot make persistent changes - the best you could do would be to hook up something to run after reboot to re-apply your change. In CyanogenMod they had hooks in the init.rc to run sdcard scripts if found. Perhaps you can create an app or widget to then launch a script to make the mods required using a setuid root script from the data partition. Without building your own ROM you are quite restricted in this area.

Possibly you could fetch the recovery image and try unpacking that, making your changes and repacking and flashing it. But make sure you can recover with fastboot before you try this.

Southey answered 19/3, 2012 at 11:31 Comment(3)
Thank you for your info. It seems that the recover partition method is used by these two pads. The hook method is what I want to do with the init.rc, with which I can do some control during system boot. But the recover prevents it from happening. If there exists the recovery image, where is it? How can I found it?Antiphrasis
If you can backup using nandroid that is one way to get the recovery image. Otherwise you can use dd and copy the recovery partition to a file. Once you have an image of the recovery partition you should be able to use unyaffs to unpack it. You should look into how the Android sources create these to find out how to remake one from a tree of files.Southey
tried with the hint from:android-dls.com/wiki/…. But the recovery img through "cat /dev/mtd/mtd1 > mtd1.img" failed in being opened by unyaffs with error "broken image file". Any suggestion? Thanks!Antiphrasis
C
11

When an android system boots, uboot unpacks a special compressed ball of files in your boot partition called 'uRamdisk' to RAM, and defines those files to comprise the root directory of the system. uRamdisk normally contains a bunch of directories (system, data, media, etc.) that serve as mountpoints for partitions that contain the files that go in them, but also has some very basic files vital to your system, including the init binary and startup scripts like init.rc.

when you edit the init.rc, you've actually just edited the unpackaged copy of init.rc that resides in your RAM. To really change it then, you have to copy your uRamdisk, extract it, edit the init.rc from there, repackage uRamdisk and then replace the new one with the old one in /boot.

Try looking up the 'xuramdisk' and 'mkuramdisk' scripts, these make the process very simple.

Crepuscule answered 21/5, 2012 at 2:1 Comment(0)
H
10

Try this site: http://bootloader.wikidot.com/linux:boot:android Read the section at the bottom: •The Android boot image: boot.img ◦Unpack, re-pack boot image: http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack%2C_Edit%2C_and_Re-Pack_Boot_Images#Background

Hunsinger answered 4/5, 2012 at 13:36 Comment(0)
T
4

Your root partition (where /init.rc lives) is a ramdisk which is unpacked from an initrd file and mounted every time your device boots. Any changes you make are to the ramdisk only, and will be lost on the next reboot.

If you can get the initrd file, you can mount it on your Linux host system, modify the files there, unmount it, and write it back to your Android.

The initrd file exists in its own partition on the device. If you can figure out which partition it is, you can grab it from the device onto your host, mount it, modify it, and write it back to the device. This is what tripler was talking about above.

In general, modifying boot.img is something that only system developers do. If you're building the entire Android system, you'll have access to the necessary source code. My workflow for this looks like this:

# Modify init.rc
m -j8 bootimage_signed
adb reboot bootloader
fastboot flash boot $OUT/boot.img
fastboot reboot
Thebault answered 30/1, 2015 at 3:12 Comment(2)
I tried to make static link for init.rc in sdcard(ln-s /sdcard/init.rc init.rc), so that change to init.rc will be easier , but it didn't work. any idea ?Perreault
By "static link" did you mean hard link? You can't make that across filesystems, so you must've linked to another file in your ramdisk. Again, this would be lost on the next reboot. If you made a symbolic link, I presume it was to another file system, which might not have been mounted yet when it was needed. Either way, I don't think this will work. One way or another, you need to modify the init.rd image.Thebault
S
3

I don't know if you are still trying to do this but without knowing your exact device nobody can give you an exact answer.

Try taking a dd image of all your internal partitions and use some scripts like those included with android kitchen on xda forums. Your recovery and boot partitions will both have a ram disk but odds are you want to modify the init.rc in the boot.img not recovery, unless you only want the changes present in recovery mode.

The unyaffs thing doesn't apply to all devices and most devices have different partition layouts so you have to figure out which is boot and what type of fs it is. Maybe if you give your device specs you can get a better answer.

Sumption answered 9/9, 2012 at 22:35 Comment(0)
I
2

Please note that it may be easier for you to use an app like Scripter to run a script at boot time than modify this file.

Before following @tripler's instructions above you need a file called boot.img which can be extracted by (run on rooted Android device, untested without root):

dd if=/dev/block/platform/<someplatform>/by-name/boot of=/sdcard/boot.img

Then connect your Android to your computer and copy the boot.img file from there.

Script:

http://linuxclues.blogspot.ca/2012/11/split-bootimg-python-android.html

Here is a modified, easier to see version of tripler's instructions (assuming boot.img is in tmp):

cd /tmp
mkdir fs
# Now use the linked script above to split the boot.img file into ramdisk.gz and kernel
python split_boot_img.py -i boot.img -o parts
cd fs
gunzip -c ../parts/ramdisk.gz | cpio -id
# make changes to init.rc

At that point you will have to rebuild the boot.img back together before reflashing, which will be device-specific. Can't help you with that, sorry!

Inconsistent answered 29/5, 2017 at 13:21 Comment(0)
E
-2

You have to edit/change the init.rc before building your Android pad file system. This is the preferred way, and always works.

Encephalon answered 19/3, 2012 at 10:33 Comment(1)
I don't have the source code of the pad's system, how can i build that? Is there a work around way to solve this problem without rebuilding the system?Antiphrasis

© 2022 - 2024 — McMap. All rights reserved.