Init Script and File Copy - Xperia Z5 Q&A, Help & Troubleshooting

I've added a simple shell script to my Z5 (Stock 5.1.1) as an init service. The scipt is intended to simply copy a file to the sdcard. I can see that my script is run on init, that it verifies the source file exists, but the cp command returns 1. dmesg output shows selinux write denials, but it selinux is in permissive mode.
Manually executing the script works fine, and I know the service is executing. I'm sure I'm missing something obvious, but so far this has eluded me. Any help would be appreciated.
script
Code:
#!/system/bin/sh
if [ -e /system/path/myfile ]
then
log -p i -t MYSTUFF "copy file"
cp /system/path/myfile /sdcard/myfile
log -p i -t MYSTUFF "Returned: ${?}"
fi
service setup
Code:
service my-service /system/path/myscript.sh
class myclass
user root
group root
oneshot
on property:init.svc.usf-post-boot=stopped
class_start myclass

Related

HOW TO: Better AD Block

I've seen other post, so I thought I should share this with anyone who may be interested.
This script is based on "Adblock script" by Adrian Jon Kriel, http://lifehacker.com/5060053/set-up-universal-ad-blocking-through-your-router
This will only work on rooted phone.
Download: adblock.sh
or copy-n-paste source code below
Code:
#!/bin/sh
## Adblock script for G1
## By Jason Driver
## firestrife23(at)gmail(dot)com
## Version 1.1
## May 19 2009
##
## This script is based on "Adblock script" by Adrian Jon Kriel,
## http://lifehacker.com/5060053/set-up-universal-ad-blocking-through-your-routerr
##
## This script is free: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
## Required AD Blocklist
eval MVPSSOURCE="1" ## default 1
eval PGLSOURCE="1" ## default 1
## Extra AD Blocklist, Extremist Only
## HUGE FILE!! your G1 may experience performance hit!!
eval HSFSOURCE="0" ## default 0
eval HFPSOURCE="0" ## default 0
## DO NOT EDIT BELOW, UNLESS YOU KNOW WHAT YOU'RE DOING!!!!
eval GENFILE="hosts"
eval MVPSOURCEFILE="http://www.mvps.org/winhelp2002/hosts.txt"
eval PGLSOURCEFILE="http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts"
eval HSFSOURCEFILE="http://www.it-mate.co.uk/downloads/hosts.txt"
eval HFPSOURCEFILE="http://hostsfile.mine.nu/Hosts"
echo "ADBLOCK: Testing for Internet Connection..."
if ping -c 1 yahoo.com > /dev/null; then
echo "ADBLOCK: Success"
eval GOTSOURCE="0"
echo "" > $GENFILE
## download
if [ "$MVPSSOURCE" = "1" ] ; then
if wget -q $MVPSOURCEFILE -O - >> $GENFILE ; then
echo "ADBLOCK: Downloaded MVPS Source"
eval GOTSOURCE="1"
else
echo "ADBLOCK: Failed to download MVPS Source"
fi
fi
if [ "$PGLSOURCE" = "1" ] ; then
if wget -q $PGLSOURCEFILE -O - >> $GENFILE ; then
echo "ADBLOCK: Downloaded [email protected] Source"
eval GOTSOURCE="1"
else
echo "ADBLOCK: Failed to download [email protected] Source"
fi
fi
if [ "$HSFSOURCE" = "1" ] ; then
if wget -q $HSFSOURCEFILE -O - >> $GENFILE ; then
echo "ADBLOCK: Downloaded I.T. Mate Group Source"
eval GOTSOURCE="1"
else
echo "ADBLOCK: Failed to download I.T. Mate Group Source"
fi
fi
if [ "$HFPSOURCE" = "1" ] ; then
if wget -q $HFPSOURCEFILE -O - >> $GENFILE ; then
echo "ADBLOCK: Downloaded Hosts File Project Source"
eval GOTSOURCE="1"
else
echo "ADBLOCK: Failed to download Host File Project Source"
fi
fi
echo "ADBLOCK: Adding Mobile Ads to blocklist"
echo '127.0.0.1 p.admob.com' >> $GENFILE
echo '127.0.0.1 analytics.admob.com' >> $GENFILE
echo '127.0.0.1 r.admob.com' >> $GENFILE
if [ "$GOTSOURCE" = "1" ]; then
echo "ADBLOCK: Got Source Files"
echo "ADBLOCK: Stripping Source Files"
sed -i -e '/^[0-9A-Za-z]/!d' $GENFILE
sed -i -e '/%/d' $GENFILE
sed -i -e 's/[[:cntrl:][:blank:]]//g' $GENFILE
sed -i -e 's/^[ \t]*//;s/[ \t]*$//' $GENFILE
echo "ADBLOCK: Sanitize, and Optimising"
sed -i -e 's/[[:space:]]*\[.*$//' $GENFILE
sed -i -e 's/[[:space:]]*\].*$//' $GENFILE
sed -i -e '/[[:space:]]*#.*$/ s/[[:space:]]*#.*$//' $GENFILE
sed -i -e '/^$/d' $GENFILE
sed -i -e 's/127.0.0.1/127.0.0.1 /g' $GENFILE
sed -i -e 's/www.//g' $GENFILE
echo "ADBLOCK: Removing Duplicates (Resource Friendly)"
cat $GENFILE | sort -u > $GENFILE.new
mv $GENFILE.new $GENFILE
## including important informations
sed -i -e '1i127.0.0.1 localhost' $GENFILE
sed -i -e '2i##' $GENFILE
sed -i -e '3i##-----------------------------------------' $GENFILE
sed -i -e '4i## Generated by Adblock script v1.1 ' $GENFILE
sed -i -e '5i##-----------------------------------------' $GENFILE
sed -i -e '6i##' $GENFILE
## removing last line (garbage)
sed -i -e '$d' $GENFILE
## info section
echo "ADBLOCK: Unique Hosts Blocked" $(awk 'END { print NR-6 }' $GENFILE)
else
echo "ADBLOCK: Error Files Not Downloaded"
fi
else
echo "ADBLOCK: Error No Internet"
fi
## replacing old hosts with new hosts file
echo "ADBLOCK: Remounting /system to read-write"
mount -o remount,rw /dev/block/mtdblock3 /system
rm /system/etc/hosts
cp /sdcard/hosts /system/etc/hosts
echo "ADBLOCK: Updated Hosts"
echo "ADBLOCK: Remounting /system to read-only"
mount -o remount,ro /dev/block/mtdblock3 /system
rm $GENFILE
be sure to place this script at root level of sdcard
Example:
Code:
/sdcard/adblock.sh
Open terminal emulator and excute as following.
Code:
$ su
# cd /sdcard
# sh adblock.sh
I Hope you find this useful. Enjoy!
Just wondering, is this all your own shell code? Looks awfully similar to one I used on my router -- variable names and all.
Either way -- good post. Just wondering since you posted it with GPL and all
Edit: Ah, here is the script I previously used and thought it reminded me of; "Adblock script" by Adrian Jon Kriel, http://lifehacker.com/5060053/set-up-universal-ad-blocking-through-your-router
yes you're right. Mostly is borrowed from tomato firmware forums (can't remember where), however their script is meant for dnsmasq, and I revised this to work with hosts file.
EDIT: Saw your source link, the reason I GPL'ed this script to prevent anyone else taking credit for Adrain Jon Kriel's work, I'll revision first post to give him proper credit thanks for link.
firestrife23 said:
yes you're right. Mostly is borrowed from tomato firmware forums, however their script is meant for dnsmasq, and I revised this to work with hosts file.
Click to expand...
Click to collapse
Ahh, just wondering -- good work with it. Though I'd assume the original other (no matter how much you edited it) would like a little credit on it too
how/why is this better than the app? doesn't it do the same thing?
Lieu10ant said:
how/why is this better than the app? doesn't it do the same thing?
Click to expand...
Click to collapse
It does the same as app, however my script is only 4.3kb.
Basically it combine multiple hosts source into single hosts file by clean up garbage such as ^M (MS-DOS end of line) which can be quite problematic for linux/unix in general (yes Android is Linux), and trimmed down to much smaller file size by removing comments, duplications, and finally sort everything by 0-9 and A-Z order to speed up hosts look up.
Does this need to be rerun on any sort of schedule? Or will it take care of looking for new address to add to the hosts file?
Love this script, thanks for sharing!
nice work!
way to condense this down to nearly nothing. Quick question, does this autoupdate? Do I have to run this from time to time to update the hosts its blocking?
Thanks!
No it does not auto update, you can just rerun the script yourself.
autoupdate
so then, how could we force this to run at boot so Android will always be adfree? Could we add a line to userinit.sh?
innerspace said:
so then, how could we force this to run at boot so Android will always be adfree? Could we add a line to userinit.sh?
Click to expand...
Click to collapse
I have my doubts that the internet connection is live until phone is fully booted. And it certainly is not if you have SIM card pin enabled.
We have 3 options from where I sit.
We can add it to the userinit.sh (or user.conf) file(s) and perhaps set it up with a delay (since most of our internet won't be on right when the phone is booting) ie sleep 120 like from this post.
We can use Autostart from the Market.
We can just run the script manually.
Thanks, works nice.
(nice name above me )
I keep getting an error, it can't connect to any of the sites to download the files... like it says 'bad address' and then 'error files not downloaded'
Can someone please help me?
I am running JACHero v1.4
I'm pretty sure I'm trying this wrong.
I used the autostart app and created a .sh file (I'll post the content at the end of this) and the corresponding directory in data.
I started the phone and got a SU permission request (which I expected). But I don't know if the script ran right.
BTW I created this script using notepad in XP. Probably not the smartest choice, but I really don't know what I'm doing and figured I'd give it a shot.
Here is the content of the .sh file I created:
--------------------------------------
setPriorityLater()
{
# sleep for 2 minutes
sleep 120
# phone and all processes should surely by up by now
su
cd /sdcard
sh adblock.sh
}
setPriorityLater > /dev/null 2>&1 &
-----------------------------------------
I copied the text of the script from http://forum.xda-developers.com/showpost.php?p=4355721&postcount=78 and inserted the commands to run this script.
Be honest, I screwed it up didn't I?
Couldn't you just use Gscript and run it every so often?
Gilliland12 said:
I'm pretty sure I'm trying this wrong.
I used the autostart app and created a .sh file (I'll post the content at the end of this) and the corresponding directory in data.
I started the phone and got a SU permission request (which I expected). But I don't know if the script ran right.
BTW I created this script using notepad in XP. Probably not the smartest choice, but I really don't know what I'm doing and figured I'd give it a shot.
Here is the content of the .sh file I created:
--------------------------------------
setPriorityLater()
{
# sleep for 2 minutes
sleep 120
# phone and all processes should surely by up by now
su
cd /sdcard
sh adblock.sh
}
setPriorityLater > /dev/null 2>&1 &
-----------------------------------------
I copied the text of the script from http://forum.xda-developers.com/showpost.php?p=4355721&postcount=78 and inserted the commands to run this script.
Be honest, I screwed it up didn't I?
Click to expand...
Click to collapse
It's recommended to use notepad++. Trust me. It's much better than notepad.
cannot create hosts
C:\Documents and Settings\Administrator\Desktop>adb shell
# sh /sdcard/adblock.sh
sh /sdcard/adblock.sh
ADBLOCK: Testing for Internet Connection...
ADBLOCK: Success
/sdcard/adblock.sh: cannot create hosts: read-only file system
/sdcard/adblock.sh: cannot create hosts: read-only file system
ADBLOCK: Failed to download MVPS Source
/sdcard/adblock.sh: cannot create hosts: read-only file system
ADBLOCK: Failed to download [email protected] Source
ADBLOCK: Adding Mobile Ads to blocklist
/sdcard/adblock.sh: cannot create hosts: read-only file system
/sdcard/adblock.sh: cannot create hosts: read-only file system
/sdcard/adblock.sh: cannot create hosts: read-only file system
ADBLOCK: Error Files Not Downloaded
ADBLOCK: Remounting /system to read-write
rm: cannot remove '/system/etc/hosts': No such file or directory
cp: cannot stat '/sdcard/hosts': No such file or directory
ADBLOCK: Updated Hosts
ADBLOCK: Remounting /system to read-only
rm: cannot remove 'hosts': No such file or directory
#
Binary100100 said:
C:\Documents and Settings\Administrator\Desktop>adb shell
# sh /sdcard/adblock.sh
sh /sdcard/adblock.sh
ADBLOCK: Testing for Internet Connection...
ADBLOCK: Success
/sdcard/adblock.sh: cannot create hosts: read-only file system
/sdcard/adblock.sh: cannot create hosts: read-only file system
ADBLOCK: Failed to download MVPS Source
/sdcard/adblock.sh: cannot create hosts: read-only file system
ADBLOCK: Failed to download [email protected] Source
ADBLOCK: Adding Mobile Ads to blocklist
/sdcard/adblock.sh: cannot create hosts: read-only file system
/sdcard/adblock.sh: cannot create hosts: read-only file system
/sdcard/adblock.sh: cannot create hosts: read-only file system
ADBLOCK: Error Files Not Downloaded
ADBLOCK: Remounting /system to read-write
rm: cannot remove '/system/etc/hosts': No such file or directory
cp: cannot stat '/sdcard/hosts': No such file or directory
ADBLOCK: Updated Hosts
ADBLOCK: Remounting /system to read-only
rm: cannot remove 'hosts': No such file or directory
#
Click to expand...
Click to collapse
I had the same problem, thn i moved:
Code:
echo "ADBLOCK: Remounting /system to read-write"
mount -o remount,rw /dev/block/mtdblock3 /system
at the beginning of the script, and now it works fine

No CP command in Dev Magic

I've been messing around with my Dev Magic and found a site for the G1 with some stuff to try:
http://gettinthru.blogspot.com/2009/04/mods-for-tmobile-g1-at-your-own-risk.html
In there, some of the things suggested use the linux command "cp" to copy files from/to the SD card. Problem is, I can't see it listed. Here is the listing of commands in /system/bin:
system_server
mediaserver
app_process
surfaceflinger
dalvikvm
dexopt
rild
wlan_loader
sdutil
service
netcfg
dumpsys
hcid
dd
cmp
df
date
cat
bugreport
chmod
chown
wpa_supplicant
wpa_cli
wipe
watchprops
vmstat
umount
top
vold
sync
stop
start
smd
sleep
setprop
setconsole
sendevent
schedtop
set_grp_id
route
rmmod
rmdir
renice
rm
reboot
radiooptions
ps
qemud
printenv
notify
netstat
mv
mount
mkdosfs
lsmod
mkdir
ls
log
ln
ioctl
kill
installd
iftop
insmod
ifconfig
id
hd
htclogkernel
getevent
getprop
flash_image
dvz
dmesg
dumpcrash
dhcpcd
debug_tool
toolbox
dumpstate
servicemanager
hciattach
logcat
sdptool
dbus-daemon
gzip
showlease
sh
schedtest
ping
logwrapper
iptables
linker
debuggerd
dosfsck
gdbserver
pm
svc
input
am
ime
monkey
akmd
As you can see, no cp. Any suggestions like a .apk of the command to push over to it?
Cheers
you can use dd to copy files aslong as you dont have busybox ( on the dream "all" the rooted images come with busybox )...
However you can use busybox from a non-rooted device by copying it to the folder "sqlite_stmt_journals" as it has both exec/write rights...
Seem to be sorted now...
I found that I can install Busybox like the G1 owners have as standard:
http://www.androidfanatic.com/cms/community-forums.html?func=view&id=228&catid=9
Folowed the guide and have cp and loads of stuff to play with now.
Cheers for your responses.
No problem... Wont take long before custom images are put together that include busybox as the magic now can also be rooted like the dream.
the "rootme" rom i provide in my rooting procedure has busybox installed, as well as apps2sd
the easiest way to replace the "cp" command is using "cat"
cat "sourcefile" > "targetfile"
if you need to copy more files, create a small script with a "for" loop.
Tom

A Real Linux Android System

Before reading.
This article requires that your phone is Rooted and has Busybox installed.
HTC owners, S-off your phone as well.
Android is a great operating system for SmartPhones. Only problem with it (From a more technical users point of view) is the small useless bad excuse of a linux system running behind the UI. Some of the smaller issues can be fixed by installing Busybox that provides a better basic set of tools to work with, but it's not enough. What I would like is to be able to use almost any real linux cli application, daemon, library etc. Also some compilers would be great. I'm using an HTC desire which has more than enough power to do things like compiling (I know, I have done it.), and so has a lot of other SmartPhones. Why would one need this when there is cross compilers you can use on regular and faster machines? Well, because It's fun, why else. I did not spend all that money on a SmartPhones just to make a few phone calls and write a few text messages. My old Sony Ericsson did that quite well. I spend that money to get a phone with many possibilities that I could play with.
I tried to search the web for some helping instructions on how to port a real linux system to this phone. What I mostly got, was people who ported linux systems that would run as an extra OS on the phone, some even with an Xserver accessible via VNC. Sounds fun, but not what I was after. I did not want an extra OS using CPU and unnecessary RAM. I just wanted some extra tools, libs etc to work with and the regular linux user management. I managed to find an old article from someone using Debians armel version on an old G1 phone. I thought that if it worked on that phone, it would be possible to do so on mine. So I started playing around with that.
The way this works is by installing a bare minimum debian on an second partition. This will not boot anything up since it will just work as extra tools and lib using the already booted android linux system.. You will still only have one OS but more tools (Including the nice APT package system) to work with.
Now there is two ways of using debian on android.
The first way is to setup the Debian in an chroot environment. This has some flaws like I have not yet been able to make the chroot start second jobs, which means that you will be able to start applications and daemons running in the current shell session. But you will not be able to make them run in the background. But as an development environment it will still work great, as you will be able to use the Debian libs and compilers.
The second way (The way I was after) is to setup Debian using the UAFS kernel module. This is used to merge directories into one with different rules for each. For an example you would be able to merge /debian_root/etc and /system/etc into a shared /etc with RW on Debians etc and RO on Androids forcing any changes to be made in /debian_root/etc. Using this option you will use Debian to extend the Android Linux part. Android UI with Debian Shell.
This article is split into 3 parts. The first part is the setup of Debian. Must be done whether you want chroot or a merged system. In the second part we will cover the setup of the chroot environment.
The third part is the setup of the merged system. This will require one thing in order to work.
You will need a kernel with the uafs module which is not default in most android kernels. You will need to compile your own kernel. People using the Thalamus kernel, can download a kernel with this module kernel-2.6.37.2_AUFS.zip. A big thanks to Thalamus for providing this. Note that this kernel was a special request from me that he was kind enough to provide. It comes with absolutely no guaranties. Do not email spam him about errors you might encounter using it as this is not an official release.
Note that once you mount the Debian system, you will not be able to mount the SD Card when connecting it to your PC. You can download the App “Multi Mount SD-Card” from Market which will be able to mount your SD Card on your PC while mounted on your phone. This app should also speed up transfers on 2.6.37 kernels, or so I have heard.
Part 1: Building Debian
We are going to run debian from a file containing a ext2 filesystem. If your SD Card is big enough you might be able to create an extra partition and force it to mount as something else other than ext-sd, but in this article we are going to use a partitioned file.
Also, this will require that you have the tool “debootstrap” on your PC. This is found in Ubuntu and Debian, dont know about other Linux distro's or Operating systems. If you don't use Linux or an distro with debootstrap you can download a Ubuntu Live CD from the Ubuntu website. This can be run on CD, DVD and USB disks without installing it. For this article I have used Ubuntu 10.10.
Open a terminal on your Ubuntu/Debian/LiveCD PC and cd to the folder where you want to temp. store your debian. I will call this path “debian_dir”
The first thing we will do is to create the partitioned file.
I'm creating a file with 1.5GB of space. You can create the size you want.
Don't use 0 in the size, dd don't like that.
Code:
cd debian_dir
dd if=/dev/zero of=debian.img seek=1499999999 bs=1 count=1
mke2fs -F debian.img
mkdir debian
mount -o loop debian.img debian/
Now that we have created the file and mounted it, we should download and place debian inside of it. First we will install debootstrap.
Code:
apt-get install debootstrap
debootstrap --arch armel --foreign squeeze debian http://ftp.us.debian.org/debian
umount debian/
Debian Squeeze is Debian 6.0 which replaces the old Lenny.
Copy the debian.img file to your SD Card on your phone. I placed mine in the root of the SD Card.
Now open a terminal on your phone. You can download one from market. There are some free once, but the paid “Better Terminal” is the best to work with in my opinion.
Note: All shell work is done on the phone from now on.
First we need some temp. exports.
Type the fallowing:
Code:
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
export HOME=/root
export TERM=linux
export DM=/data/local/debian
export DI=/sdcard/debian.img
Now we will create the Debian dir and then create a loop device which we will use to mount the Debian image to the Debian dir.
Code:
mkdir -d $DM
mknod /dev/loop0 b 7 0
mount -o loop,noatime $DI $DM
Now that we have debian mounted in our debian folder, we need to finalize the installation. Not all files as been installed yet. Don't forget to activate Mobile Network or Wifi.
chroot $DM /debootstrap/debootstrap –second-stage
We also need to insert the debian repository and a DNS (I use OpenDNS).
You can use whatever you want for the DNS, you routers IP for an example. It should have some default Ips configured.
Code:
echo “deb http://ftp.us.debian.org/debian squeeze main contrib non-free” > $DM/etc/apt/sources.list
echo “208.67.222.222” > $DM/etc/resolv.conf
Now change the root to the debian dir and set a root password and make sure that certain folders already exists.
Code:
chroot $DM /bin/bash
passwd root
mkdir root
exit
Now we need to make sure that certain variables exists and that the values is as they should be. So we will alter the /system/bin/sh that android uses as login shell.
Code:
cd /system/bin
mv sh sh0
Create a new /system/bin/sh file with the fallowing content.
This will ensure that these variables is created and/or changed every time you enter a new shell session. You can call another shell like bash or ash instead /system/xbin/sh, just make sure to use ash or sh at the top, since bash does not seam to understand [email protected]
Warning: It is very important that the /system/bin/sh file is made correct. Android will not boot if anything is wrong with this file. It should be exactly like the one I posted, and you cannot make it on your PC and push it to the phone. Don't know why, it works fine with other shell scripts, but not that one.
Enter an adb shell from your PC and use vi or nano (You can download nano here here) to copy and paste the content directly into the ADB Shell.
Note: ADB from the recovery will always work. It does not use /system/bin/sh
Code:
#!/system/xbin/sh
/system/xbin/sh --login [email protected]
Make sure that it has execute rights. Then type sh in the terminal to make sure that it works before you close your ADB Shell session.
Code:
chmod a+x /system/bin/sh
sh
sh0 will not be used, it's up to you wether you want to save it or not. Just make sure that /system/xbin/sh exists, otherwise
copy /system/bin/sh0 to /system/xbin/sh.
Now we need the profile file that is needed by this method. This file will make sure that we have all our variables and the data that they should contain.
Android does not keep very good track of these things. Place this data in the file /system/etc/profile
Code:
export TERM=linux
export TERMINFO=/etc/terminfo
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/vendor/bin:/system/bin:/system/xbin
if [ "$EUID" == "0" ]; then
if [ -z "$HOME" ] || [ -z "$USER" ] || [ -z "$GROUPS" ]; then
export HOME=/root
export USER=root
export GROUPS=0
fi
else
# Make sure that this user id variable exists.
if [ -z $EUID ]; then
export EUID=$(echo $(id | cut -d "(" -f 1) | cut -d "=" -f 2)
fi
# If we have an /etc/passwd file available,
# we should also make sure to get name, group and home info from that file and
# store in the default linux variables.
if [ -f /etc/passwd ]; then
# Create the user info variables based on user id and /etc/passwd
export USER=$(grep "x:${EUID}:" /etc/passwd | cut -d : -f 1)
export GROUPS=$(grep "x:${EUID}:" /etc/passwd | cut -d : -f 4)
export HOME=$(grep "x:${EUID}:" /etc/passwd | cut -d : -f 6)
fi
fi
export ADID=$EUID
if [ "$PS1" ]; then
if [ "$BASH" ]; then
# The file bash.bashrc already sets the default PS1.
if [ ! -z $HOME ] && [ -f $HOME/.bashrc ]; then
. $HOME/.bashrc
elif [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
else
export PS1='\h:\W\$ '
fi
else
if [ "$EUID" == "0" ]; then
PS1='\w # '
else
PS1='\w $ '
fi
fi
else
PS1='\h:\W\$ '
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
We will also need an /etc/bash.bashrc which is needed because /etc/profile not always gets executed.
Code:
if [ "$ADID" != "$EUID" ]; then
# Some times /etc/profile is not executed (For an example when using Androids su command).
# /etc/bash.bashrc however it used instead. So we just execute it manually in these cases.
# If you create a local bashrc ($HOME/.bashrc), remember to include this check.
# /etc/bash.bachrc is not used when a local file exists.
if [ -f /etc/profile ]; then
. /etc/profile
# Since /etc/profile links back to this file,
# we will make sure not to start an unending loop.
# the profile file should update this, but it's better to be on the safe side.
if [ "$ADID" != "$EUID" ]; then
export ADID=$EUID
fi
fi
fi
PS1='\h:\W\$ '
And last we have some user and group configurations in order to make Debian better work with Android. Open your Android terminal emulator and make sure it opens as user and not root. Now type "/system/xbin/id".
What we need here is the uid, gid and the ids of each accosiated groups along with the names of it all. In my terminal I have "uid=10048(app_48) gid=10048(app_48) groups=1015(sdcard_rw),3003(inet)".
What we will do it create this user along with the groups in our Debian inveroment. However, I use the uid name and gid name "bterm" instead of app_48, but still with the original IDs. The inet and sdcard_rw groups will be created as system groups. Do the same for your app user and group if you don't want any login possibilities with this user. I however have assigned home and password to this user.
This needs to be doen in chroot
Code:
chroot $DM /bin/bash
groupadd --system --gid 1015 sdcard_rw
groupadd --system --gid 3003 inet
groupadd --gid 10048 bterm
adduser --home /home/bterm --gid 10048 --uid 10048 bterm
gpasswd -a bterm sdcard_rw
gpasswd -a bterm inet
exit
Now your ready for the next step. Remember that the user you have created, is the user used by your terminal emulator when you are not logged on as root. This ID is only assigned to that specific app, if you install another terminal, it will get another ID.
Part 2 – Setting up a chroot environment
The best way of doing this is to make two small scripts and place them in /system/bin or /system/xbin. One script for doing the mount after phone boot. And one script for entering the chroot when open a new shell session.
Here is the mounting script that I call mkdebian.
This needs to be executed once every time the phone has rebooted. If your ROM supports init scripts, you can add it there for auto-mount on boot.
Code:
#!/system/bin/sh
export DM=/data/local/debian
export DI=/sdcard/debian.img
# Create the loop device and mount the debian image
mknod /dev/loop0 b 7 0
mount -o loop,noatime $DI $DM
# Let's make sure that we can access our sd card from within the chroot
if [ ! -d "$DM/mnt/sdcard" ]; then
mkdir $DM/mnt/sdcard
fi
mount --bind /mnt/sdcard $DM/mnt/sdcard
# Now we make sure that the chroot has access to all the device maps
if [ ! -d "$DM/dev" ]; then
mkdir $DM/dev
fi
mount --bind /dev $DM/dev
# We also need to make proc, sys etc available from within the chroot
chroot $DM mount -t proc proc /proc
chroot $DM mount -t sysfs sysfs /sys
# And last fix the mtab
chroot $DM rm -f /etc/mtab
chroot $DM ln -s /proc/mounts /etc/mtab
echo “Debian was mounted in $DM/”
Now for the script that I called “debian”.
This script will make you enter the chroot environment using one single command. Every time you exist the debian chroot, you can enter back using the command “debian”.
Code:
#!/system/bin/sh
chroot /data/local/debian/ /bin/bash
And that is it. Open a terminal, type debian and start using your debian environment. It might be a good idea to run aptitude update or apt-get update before you start using the package manager.
Part 3 – Settings up a merged Android/Debian environment
This is the fun part to play with. A merged Android/Debian environment without the usage of chroot. This only requires a script to be executed at boot, or after boot.
Remember that this will not work unless your kernel has the aufs module.
Code:
#!/system/xbin/sh
export DM=/data/local/debian
export DI=/mnt/sdcard/debian.img
# Now we mount the debian image to our debian folder.
mknod /dev/loop0 b 7 0
mount -o loop,noatime $DI $DM
# These two directories exists in both android and debian,
# so these are going to be merged together.
mount -t aufs -o dirs=$DM/etc=rw:/system/etc=ro aufs /system/etc
# For some reason, mounting /sbin with aufs does not work.
# So we make a little cheat with symbolic links instead.
mount -o remount,rw /
mv /sbin /sbin0
ln -s $DM/sbin /
rm -rf /sbin/adbd
rm -rf /sbin/ueventd
ln -s /sbin0/adbd /sbin/
ln -s /sbin0/ueventd /sbin/
# Let's get all the debian root directories out to the real root dir.
rm -rf /root
for x in tmp root lib bin boot home media opt selinux srv usr var
do
ln -s $DM/$x /
done
if [ -f /system/xbin/bash ]; then
mount --bind /bin/bash /system/xbin/bash
mount --bin /system/xbin/bash /system/xbin/sh
else
mount --bind /bin/bash /system/xbin/sh
fi
echo "Debian has been merged with Android..."
Run this at startup and start using all of debians possibilities (Don't run it yet).
The / dir is left with RW permissions. If this is set to RO then apt-get will not work. It writes tmp log files to this dir when running. It does not matter since all changes to this dir is restored to default on every boot.
Also dont forget to run apt-get update or aptitude update to update the package list.
You are now able to install and run almost anything (CLI Only) that runs on debian systems and other linux dists. Personally I'm currently running OpenSSH Server and Lighttpd with PHP. A perfect small transportable web server.
You can use either /etc/init.d/[your service] start
or
service [your service] start
to start daemons you have installed.
Now you can use the "mkdebian" command to merge your new Debian with Android.
Note: The command “su” from debian works differently than the android “su” command.
The debian command uses the regular linux management to provide access which means that it will ask for your root password that you created in the first part. The android command will just provide you with su rights without a password. Both commands provides su rights to both android and debian, so it is possible to remove the android command and create a new Android/Java based SU app for the Android UI that will require root password.
Note: There is a small problem with the Android "su" command. It does not recheck the /etc/profile.
That means that if you log on using the regular user, and then uses the "su" command, user variables does not get updated with su paths.
Using the Debian "su" command will work fine, just note that this command will promp for password. Also logging in using the root account
directly works fine.
Good luck
[Edit: 2011-03-09 11:06]
- Changed the /etc/profile script in Part 1
- Added /etc/bash.bashrc script in Part 1
- Changed User and Group setup and moved it from Part 3 to Part 1
- Fixed error in /system/bin/sh
All of this will fix some permission problems with Debian
[Edit: 2011-03-08 11:21]
- Fixed problems with user management by adding an /etc/profile and changing /system/bin/sh and the debian merge script. Using the "login" command is no longer needed.
[Edit: 2011-03-07 08:49]
- Fixed bug in Part 1 sh script.
[Edit: 2011-03-07 00:20]
- Added a new /system/bin/sh script to Part 1
- Added the way we create missing users and groups in Part 3
[Edit: 2011-03-06 19:36]
- Inserted a warning text in part 1 about avoiding problems when creating a new /system/bin/sh.
Applying Host name
If you want to set a custom hostname for your phone, you can create an init script (If your ROM supports it).
Code:
#!/system/bin/sh
# Get the hostname if it exists, or set it to localhost
if [ -f /system/etc/hostname ]; then
_HOSTNAME=$(cat /system/etc/hostname)
if [ -z $_HOSTNAME ]; then
_HOSTNAME=localhost
fi
else
_HOSTNAME=localhost
fi
# Update the Hostname
echo "$_HOSTNAME" > /proc/sys/kernel/hostname
# Update the hosts file
echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts
# If the hostname is not localhost, we add one more line to the hosts file
if [ "$_HOSTNAME" != "localhost" ]; then
echo "127.0.0.1 $_HOSTNAME" >> /etc/hosts
fi
This will look in /system/etc/ for the regular linux hostname file. If it exists, it will take the hostname from that file and set as the global hostname.
If it does not exist, "localhost" is used instead. Also it creates the /system/etc/hosts file.
Add or Edit your hostname like this.
Code:
echo "myhostname" > /system/etc/hostname
Note: Remember to delete the hosts and hostname files in your debian /etc/ dir when using merged environment. Otherwise they will overwrite the once in /system/etc/ as debian has first priority.
Manipulating the Android Permissions
Androids permissions works by providing a User ID for each application. This user (Application) can then get a range of different permissions by being added to groups with the permissions required by the app. For an example an app that needs to write to the SD Card, should be a member of Androids sdcard_rw group which translates into 1015 in a Linux group id. Using the numeric values of the Android groups, we can assign these permissions to shell users, daemons etc.
An example:
I had problems with my Http Server because I wanted my www folder on my sdcard. But, the www-data user and group could not be allowed to write to the SD Card, which I needed it to do, because Android controls what permissions the SD Card should have, not even root can change that.
What I did was creating a system group that I named sdcard_rw (Same name as the android, just to better keep track of the groups) with the same Id as the Android sdcard_rw (1015). I then assigned that group to my www-data user (gpasswd -a www-data sdcard_rw) and now my lighttpd had Read/Write access to my sdcard.
You can use the same example for creating a regular SSH User by using the Android shell group or for any other type of user that needs some Android permissions to work properly.
[Edit: 2011-03-08 21:13]
- Added an permission example
Great...
I was just looking into this myself for the last few days.
strange how there isn't more of this being talked about.
at least I have been unable to find anything.
Omg this sounds awesome,
Will try this when u get home and will report back
Sent from my Nexus One using XDA App
I'm still trying to work out some problems with User Rights on SD Card. Can't get lighttpd to be allowed to create files in my SD Card www dir (Does not matter what user and group I set it to use). I can't even get the root account to be allowed to change permissions on SD Card files and folders. It's not the biggest problem, but will report back when I figure it out.
Nice! I've been constantly disappointed for the past year at how un-Linuxy Android is under the hood and unimpressed with the old chroot tricks. This looks much better and I'll try it soon.
I have added a new /system/bin/sh script that can be found in Part 1. This script will automatically look for bash and, if that exists, try to find bash.bashrc, .profile or .bashrc in /etc or the users home dir. If bash does not exist, it looks for sh instead. If you have both one of the Android specific compiled bash, then Debians bash is used when available as this is made to work in a real linux environment. Android bash does not seam to work that way. Also debians packages gets updates via APT.
Also I changed the way groups and users is created in Part 3. Manually inserting them to /etc/passwd, /etc/shadow and /etc/group did not seam to be enough. The users and groups where only half registered. For an example the command "login" gave me a lot of errors, and i had problems with user rights. By using adduser and addgroup in a chroot before the first merge, the user management seams to work as it should now.
I was playing with Debian on my phone just a few days ago too. I've found that same G1 article you mentioned, and installed Lenny in a chroot environment. Background daemons work fine for me, I've installed OpenSSH and I'm using ConnectBot connecting to 127.0.0.1 to get a terminal, so I don't have to chroot every time. I even have X with tightvnc server + android-vnc-viewer. Running the desktop Firefox on my phone is fun Although not very useful. I'm still thinking about what awesome things we could do with this that we couldn't otherwise.
Anyway, great howto.
Do you have any details on the Thalamus kernel? What phone is it for? Overclocked, audio hack, etc? Or is it just a vanilla kernel with the extra module?
What applications can we dream up?
The first thing that springs to mind is could this be used to create a low power server running bittorrent and the like?
Would a cheap device like the Vodafone 845 be under-powered for this?
Can you connect to a usb hub to allow multiple devices to be connected to it? I'm thinking usb network adapters, external storage, etc.
How much additional effort on top of this guide would be needed?
Just curious...
will not... - -!
Well I learned one thing. Don't make Shell scripts when you are tired. I started a shell loop on phone bootup with my /system/bin/sh fix (Which had been changed). When using the merged environment, you need (for now) to enter /bin/login when you open a new shell and login as user with the password you created in Part 3. This will make Debian take over the user management which among other things will make sure that the right variables etc. exists. I have tried to locate where android controls new shell sessions, but I cant seam to find it.
When you make Debian take over, you can use your Linux shell as any other. .bashrc, .profile, /etc/bash.bashrc, /etc/profile files, change default shell (bash, sh, ash) in /etc/passwd and so on.
#9 Well the kernel is the same that is found in the Oxygen 2.0.1, so it's an gingerbread rom (2.3.3). If it works only in Oxygen, I don't know but I don't think so.
#10 He he, Could not say. For now I'm just trying to make it work as I want it to work. After that I might start playing around with different things.
The "login" command is no longer needed. I finally found a way to successfully control user variables and management, making Android and Debian work together as one when merging the two.
This is amazing. Totally going to do this later tonight when I get home.
If you merge using AUFS instead of making a chroot, you should perhaps use the system user (Id: 1000, Group: 1000) as the regular user.
There are only two static users in Android, 0 and 1000.
Android just isn't made to work as an multi-user system. This is how the Java UI is made, and we really need to Linux part to be compatible with that part of the OS. Instead of multi-users, android uses it's apps as users. Each app works as one user with different id. So the 10048 I used as regular user is just the ID of mine "Better Terminal". Switching to another terminal app changed the EUID.
Use the root and only root to work with in the shell. And then delete Debians "su" command and stick with the one from android. Debians need all users that execute "su" to be written to the /etc/passwd.
All GNU tool and library, that's Great. thanks a lot.
I have added an example of how to apply Android rules to shell users, daemons etc. Some daemons will need this in order to function.
I have added this in Thread 2 below the hostname example.
I also changed the main Thread. Found a fix for the rest of the permission problems, including the Android "su" command issue.
I have tested this Android/Debian merge using several methods (ADB, SSH, different local terminals) and it worked on most. I had one problem with one local terminal app that did not execute /etc/profile and some Debian users in /etc/passwd that didn't either.
To fix this I changed /system/bin/sh from using "/system/xbin/sh --login [email protected]" to instead use "/system/xbin/sh --rcfile /system/etc/profile [email protected]". I then moved the Debians /bin/bash to /usr/bin/bash and made a similar script file in /bin/bash > "/usr/bin/bash --rcfile /etc/profile [email protected]". You can make the same for /bin/sh if you don't like using bash for your /etc/passwd users.
Remember to change the bash bind mount in the mkdebian script from /bin/bash to /usr/bin/bash
Use whatever method works on your phone.
This looks like a ton of fun, and could be very useful. I'd like to try this on my NookColor (N2Acard, CM7.1), probably using The Guardian Project's lilDebi image as a base. They go the chroot route with their installer, and I'm going to have to do some hacking on their scripts to make them work with the NookColor. I figure if I'm going to have to hack on it, I might as well put my efforts toward a more ideal solution.
Question: It doesn't look like the CM7.1 kernel supports aufs. That is,
Code:
cat /proc/filesystems
doesn't return aufs as one of the supported filesystems.
Aside from a lack of elegance, is there any reason we can't apply your /sbin workaround to "merging" the whole filesystem? [I recognize there's a certain amount of ignorance behind the question; I figure there's only one way to cure that: Ask.]
OR, how difficult might it be to take a complete CM7.1 kernel and add aufs support to it?
'preciate any help I can get!
mailman1175 said:
This looks like a ton of fun, and could be very useful. I'd like to try this on my NookColor (N2Acard, CM7.1), probably using The Guardian Project's lilDebi image as a base. They go the chroot route with their installer, and I'm going to have to do some hacking on their scripts to make them work with the NookColor. I figure if I'm going to have to hack on it, I might as well put my efforts toward a more ideal solution.
Question: It doesn't look like the CM7.1 kernel supports aufs. That is,
Code:
cat /proc/filesystems
doesn't return aufs as one of the supported filesystems.
Aside from a lack of elegance, is there any reason we can't apply your /sbin workaround to "merging" the whole filesystem? [I recognize there's a certain amount of ignorance behind the question; I figure there's only one way to cure that: Ask.]
OR, how difficult might it be to take a complete CM7.1 kernel and add aufs support to it?
'preciate any help I can get!
Click to expand...
Click to collapse
here's where you get aufs patches.
you'll have to build the kernel yourself to make it work.
to the OP:
using the aufs mount method instead of a chroot...
I have a small problem, I don't know if it shows up with your .img filesystem method, but with an sd-ext partition used intead of the .img file, apt-get fouls massively when trying to update/upgrade libc6.
it complains about the libc6*.so existing in two places at once, and requests the existing one be removed. If it's removed, then nothing in the Linux install works, because just about everything is compiled against libc. I can use busybox to manipulate files with the libc6 removed or hidden away, but can't seem to get past the error since dpkg and apt don't work without some version of libc installed.
Any hints/suggestions? I like your scripts a lot better than the chroot ones I cobbled together from everyone else's work last year for similar purposes before I got aufs working in a kernel.

[HOW-TO] Fix when the phone stuck in boot splash after apply V6 Supercharger

You may have applied V6 Supercharger to your phone. And after reboot, you stuck at the boot splash. Reboot once and V6 Supercharger install script tell you that you aren't supercharged. Here is how to fix it. (If you get looped in boot animation, this is NOT your fix. Sorry!)
Depend on your rom, you may have to do one of following:
Open the (rooted) File Manager, open the directory /etc/init.d/ and open file 99SuperCharger. OR
Turn on ADB on your phone, use your computer to pull the file /etc/init.d/99Supercharger by running
Code:
adb pull /etc/init.d/99SuperCharger
Edit the file at the almost bottom of the file, change the following code:
Code:
if [ "`ps | grep -m 1 [a]ndroid`" ]; then HellzYeah;
else HellzYeah &; fi;
To look like this:
Code:
if [ "`ps | grep -m 1 [a]ndroid`" ] || [ $1 = "bypass" ]; then HellzYeah;
else
rm $bootloopcookie
$0 bypass &
fi;
Depend on what you have done on step 1, do the following:
If you use (root) file manager, save the file. Grant the superuser permission if needed. Then browse to /data and delete file !!SuperChargerBootLoopMessage.log. Don't worry if it's not exist.
If you use your computer to edit your file, run the following command:
Code:
adb root
adb remount
abd push /path/of/99SuperCharger /etc/init.d/99SuperCharger
adb shell
And inside the adb shell, run:
Code:
chmod 755 /etc/init.d/99SuperCharger
rm /data/!!SuperChargerBootLoopMessage.log
exit
Don't worry if file /data/!!SuperChargerBootLoopMessage.log doesn't exist.
And that's it! The V6 Supercharger bootscript is worked as it's designed to. Thanks to zeppelinrox to make this great script.
For one who want to know some technical info (and my assumption of the cause), wait me until tomorow and i'll explain how i found it. read on:
In order to run script in /etc/init.d at boot, the line like this is inserted in init.rc:
Code:
on boot
...
# Start the init.d service
exec /system/bin/sysinit
(In case of cm - for example)
But, what does the /system/bin/sysinit do? Just this:
Code:
#!/system/bin/sh
export PATH=/sbin:/system/sbin:/system/bin:/system/xbin
/system/bin/logwrapper /system/xbin/run-parts /system/etc/init.d
And this is the real source of problem. For some reason run-parts with logwrapper doesn't move on when you run a shell script function in background. If you scroll up in file 99SuperCharger, you'll found that "HellzYeah" is actually a function. And when it's execute in a boot process, "HellzYeah" will wait for boot process to continue. But run-parts (and the whole boot process) is waiting for the script to finish. You see? A deadlock!
The workaround is to execute function in the other way. What my modification do is re-executing the script with the parameter "bypass" in the background. This way, run-parts will not wait for it anymore. Then, when the script is re-executed, it will detect that it's called with the parameter "bypass". It'll run the function "HellzYeah", wait for boot process to finish, and, when the boot process is finished, supercharge your phone!
(if anyone is reading this, please inform zeppelinrox about this problem. I'm not able to post on the development forum yet.)
(This is my first post in xda. Hopefully it's helpful)

[Scripts support init.d] Developer something that should take into account

Introduction:
Developers and users in general hope that this information will be of help and understand how important this issue is
to develop the script according assume smartphone
Warning:
In most of the roms we can find a folder called init.d where you will find a variety of script that are optimizations for
the system itself there is only a detail as you may have noticed the scripts that are used are the same as those used in xperia x8,
type, among other variety as well as file modifications Encourage build.prop only device in this topic we will learn to notice how
the scripts are built and running as smoothly as possible.
Because I say that the scripts are the same? Simple is good since I have seen your code and have compared with the terminals I have
the code and some things should not be there just do not serve the terminal to Encourage and lagee much scripts are not universal
trust me
Some things like values?? optimzacion kernel (sysctl) such xperia L example of code are these:
-kernel= kernel.auto_msgmni = 0
-vm= vm.block_dump = 0
-fs= fs.aio-max-nr = 65536
-net= net.core.dev_weight = 64
note: and so on the more codes each terminal should be just the scripts for each terminal
some files should not come optimizations init.d support
host file this file is normally used to block advertising terminal does support that file in init.d wonder?
comes as other files named sqlite3 that is used to access the root does support init.d ???? now the other file called zipalign that is
normally used to sign apk applications to sign in support init.d ???
the init.d support should be different for each terminal is different because each firmware should not be general
important: it is my view and my knower that is also something constructive
1.-Basic commands and file handling
command: #!/bin/bash
information: +-/home: User home directories
command cd
command to change directory.
command ls
list contents of directories
command cp
copy files/directories
command rm
delete files/directories
command mkdir
create directories
command sysctl
Configure kernel parameters at runtime
2.-All configurations such scripts
/system/etc
3.- startup file
path of the file to be started: /system/etc/init.d/init
example code:
Seed file support init.d: /system/etc/udev/rules.d/init.rules
KERNEL=="mount*", ACTION=="change", SUBSYSTEM=="platform", RUN+="/system/etc/init.d/init"
5.- permissions script
chmod 755
6.- Example of script
#!/bin/bash
# This is a comment
echo "Hello world"
7.- Conditional if o if-else
example
code if:
if value
then
values
fi
code if-else:
if value
then
values
else
8.- Code if o if-else
example:
-a file: True if the file exists
-e file: The same
-b file: True if file exists and has a special block
-c file: True if file exists and is of type character
-d file: True if file exists and is a directory
-f file: True if file exists and is a common file
-g file: True if file exists and its set bit in September GroupID
-h file: True if file exists and is a symbolic link
-k file: True if file exists and its sticky bit set
-p file: True if file exists and is a named pipe
-r file: True if file exists and is readable
-s file: True if file exists and is greater than 0
-u file: True if file exists and has the setuid bit set
-w file: True if file exists and is writable
-x file: True if file exists and has execute permissions
-O file: True if file exists and is nuetro EUID user
-G file: True if file exists and is in our group EGID
-L file: True if file exists and is a symbolic link
-S file: True if file exists and is a socket
-N file: True if file exists and has changed since the last reading
9.- Checks
example:
if [ ! -e $1 ]
then
echo “No”
fi
10.- the main thing you have to develop the script support init.d
Note: In the coming days I will do a better, more detailed documentation :fingers-crossed:
reserved

Categories

Resources