mercredi 19 mars 2014

[script][any][Jubei's unified init.d script] topic




While doing stuff on Fenris ( http://forum.xda-developers.com/show....php?t=2675070 ) i discovered a LOT of init.d scripts out there so decided to unify all the useful and working tweaks into one single bash script


Code:


#!/system/xbin/bash


BUSYBOX="/system/xbin/busybox"
LC="log -p i -t cm"
DBUG=0


#sysrw() {
#        mount -o remount,rw /system
#}

#sysro() {
#        mount -o remount,ro /system
#}


: ' =========================================================
 function name: get_system_mount
 parameters: void
 returns:
        "rw": if system is mounted read/write
        "ro": if system is mounted read-only
 description:
    Returns the current system mount
============================================================= '
get_system_mount() {
        mount | grep /system | awk '{print }' | cut -d ',' -f1
}

: ' =========================================================
 function name: get_installed_packages
 parameters:
        : (--filter_system|--filter_data|--filter_sd)
                        Filter apps on system, data or sdcard
        : (--get_package_path)
                        Return the code path instead of the package names
 returns: void
 description:
    Gets a list of installed packages
============================================================= '
get_installed_packages() {
        get_package_path=false
        filter=""

        for i in $@; do
                case $i in
                        --filter_system)
                                filter="/system/"
                        ;;
                        --filter_data)
                                filter="/data/app"
                        ;;
                        --filter_sd)
                                filter="/mnt/"
                        ;;
                        --get_package_path)
                                get_package_path=true
                        ;;
                esac
        done

        if $get_package_path
        then
                pm list packages -f \
                        | grep "${filter}" \
                        | cut -d: -f2 \
                        | cut -d= -f1 \
                        | sort
        else
                pm list packages -f \
                        | grep "${filter}" \
                        | cut -d: -f2 \
                        | cut -d= -f2 \
                        | sort
        fi
}

: ' =========================================================
 function name: zipalign_apk
 parameters:
        : path to the apk file
 returns: void
 description:
    zipaligns an apk file if needed
============================================================= '
zipalign_apk() {
        local apk=
        if [ -e $apk ]; then
                zipalign -c 4 $apk
                exit_status=$?
                case $exit_status in
                        0)
                                echo "[!] ${apk} is already zipaligned"
                        ;;
                        *)
                                if zipalign -f 4 $apk /data/local/pkg.apk
                                then
                                        cp -f /data/local/pkg.apk $apk
                                        rm -f /data/local/pkg.apk
                                        echo "[X] Zipaligned ${apk}"
                                fi
                        ;;
                esac
        fi
}

: ' =========================================================
 function name: set_package_permission
 parameters:
        : package name
        : package code path
 returns: void
 description:
    Fixes permission on a package
============================================================= '
set_package_permission() {

        packagename=
        apk_path=
        packageuid=` grep $apk_path /data/system/packages.xml | sed 's%.*serId="\(.*\)".*%%' |  cut -d '"' -f1 `
        data_path=/data/data/$packagename
       
        if [ -e $apk_path ]; then

                echo "Setting permissions for ${packagename} ..."
                appdir=` dirname $apk_path `

                if [ $appdir == /system/app ]; then
                        chown 0 $apk_path
                        chown :0 $apk_path
                        chmod 644 $apk_path
                elif [ $appdir == /data/app ]; then
                        chown 1000 $apk_path
                        chown :1000 $apk_path
                        chmod 644 $apk_path
                elif [ $appdir == /data/app-private ]; then
                        chown 1000 $apk_path
                        chown :$packageuid $apk_path
                        chmod 640 $apk_path
                fi

                if [ -d $data_path ]; then

                        chmod 755 $data_path
                        chown $packageuid $data_path
                        chown :$packageuid $data_path

                        dirs=` find $data_path -mindepth 1 -type d `

                        for file in $dirs; do

                                perm=755
                                newuid=$packageuid
                                newgid=$packageuid
                                fname=` basename $file `

                                case $fname in
                                        lib)
                                                chmod 755 $file
                                                newuid=1000
                                                newgid=1000
                                                perm=755
                                        ;;
                                        shared_prefs)
                                                chmod 771 $file
                                                perm=660                                       
                                        ;;
                                        databases)
                                                chmod 771 $file
                                                perm=660
                                        ;;
                                        cache)
                                                chmod 771 $file
                                                perm=600
                                        ;;
                                        *)
                                                chmod 771 $file
                                                perm=771
                                        ;;
                                esac

                                chown $newuid $file
                                chown :$newgid $file

                                find $file -type f -maxdepth 1 ! -perm $perm -exec chmod $perm {} ';'
                                find $file -type f -maxdepth 1 ! -user $newuid -exec chown $newuid {} ';'
                                find $file -type f -maxdepth 1 ! -group $newgid -exec chown :$newgid {} ';'

                        done
                fi
        fi
}

##########################################################################################

vanir_first() {
# ICUP FUNI CULURSZ
        $LC "Init.d/20jubei ......vanir_first"
        [ -e /sys/class/misc/colorcontrol/v1_offset ] && chown system.system /sys/class/misc/colorcontrol/v1_offset && chmod 0666 /sys/class/misc/colorcontrol/v1_offset
        [ -e /sys/class/misc/colorcontrol/multiplier ] && chown system.system /sys/class/misc/colorcontrol/multiplier && chmod 0666 /sys/class/misc/colorcontrol/multiplier
        [ -e /sys/devices/platform/omapdss/manager0/gamma ] && chown system.system /sys/devices/platform/omapdss/manager0/gamma && chmod 0666 /sys/devices/platform/omapdss/manager0/gamma
       
        # Expand kernel perms
        [ -e /dev/cpuctl/apps/cpu.notify_on_migrate ] && chown system.system /dev/cpuctl/apps/cpu.notify_on_migrate && chmod 0666 /dev/cpuctl/apps/cpu.notify_on_migrate
       
        #use ksm
        [ -e /sys/kernel/mm/uksm ] && echo 1 > /sys/kernel/mm/uksm/run && echo 0 > /sys/kernel/mm/ksm/run  || echo 1 > /sys/kernel/mm/ksm/run
}


vanir_entropy() {
        $LC "Init.d/20jubei ......vanir_entropy"
        if [ -f /system/lib/modules/frandom.ko ]; then
        insmod /system/lib/modules/frandom.ko 2>/dev/null
        $BUSYBOX insmod /system/lib/modules/frandom.ko 2>/dev/null
        insmod -f /system/lib/modules/frandom.ko 2>/dev/null
        $BUSYBOX insmod -f /system/lib/modules/frandom.ko 2>/dev/null
        fi
       
        if [ -f /system/lib/modules/frandom.ko ]; then sleep 5; fi
       
        RANDOMDEVICE=urandom
        if [ -c /dev/erandom ]; then
        chmod 444 /dev/frandom
        chmod 444 /dev/erandom
        if [ ! -f /dev/urandom.MOD ]; then
                touch /dev/urandom.MOD
                mv /dev/urandom /dev/urandom.ORIG && ln /dev/erandom /dev/urandom
                sleep 2
        fi
        if [ ! -c /dev/urandom.ORIG ]; then
                $BUSYBOX mknod -m 666 /dev/urandom.ORIG c 1 9
                sleep 2
        fi
        ( CB_RunHaveged /dev/urandom.ORIG 0<&- &>/dev/null 2>&1 ) &
        RANDOMDEVICE=frandom
        else
        if [ ! -c /dev/urandom ]; then
                $BUSYBOX mknod -m 666 /dev/urandom c 1 9
                sleep 2
        fi
        ( CB_RunHaveged /dev/urandom 0<&- &>/dev/null 2>&1 ) &
        fi
       
        if [ ! -f /dev/random.MOD ]; then
        touch /dev/random.MOD
        rm /dev/random && ln /dev/$RANDOMDEVICE /dev/random
        fi
       
        sys_pid=`$BUSYBOX pgrep system_server 2>/dev/null`
       
        $BUSYBOX renice -10 $sys_pid 2>/dev/null
       
        for i in $($BUSYBOX pgrep aveged 2>/dev/null); do
        $BUSYBOX renice +20 $i 2>/dev/null
        done

}



##########################################################################################
align_apks() {
        $LC "Init.d/20jubei ......align_apks"
        local apks=` get_installed_packages --get_package_path `
        for apk in $apks; do
                zipalign_apk $apk
        done
        sync
}

##########################################################################################
merge_sysctl() {
        $LC "Init.d/20jubei ......merge_sysctl"
        sysctl -p /system/etc/sysctl.conf
}

##########################################################################################
wipe_data_cache() {
        $LC "Init.d/20jubei ......wipe_data_cache"
        find /data/data -type d -iname "*cache*" -maxdepth 2 -mindepth 2 -exec rm -rf {} ';'
}

##########################################################################################fix_permissions
perms() {
        $LC "Init.d/20jubei ......perms"
        local packages=` pm list packages -f | cut -d: -f2 `
        for package in $packages; do
                local packagename=` echo $package | cut -d '=' -f2 `
                local apk_path=` echo $package | cut -d '=' -f1 `
                set_package_permission $packagename $apk_path       
        done
        sync
}

##########################################################################################crond

cron_deamon() {
        $LC "Init.d/20jubei ......cron_deamon"

        # crond has "/bin/sh" hardcoded
        if [ ! -h /bin ]; then
                ln -s /system/bin /bin
        fi
       
        # set timezone (if you're not between -0500 and -0800 you get PST)
        # todo - support other timezones
        local timezone=`date +%z`
        if [ $timezone = "-0800" ]; then
        TZ=PST8PDT
        elif [ $timezone = "-0700" ]; then
        TZ=MST7MDT
        elif [ $timezone = "-0600" ]; then
        TZ=CST6CDT
        elif [ $timezone = "-0500" ]; then
        TZ=EST5EDT
        else
                TZ=PST8PDT
        fi
        export TZ
       
        # use /data/cron, call the crontab file "root"
        if [ -e /data/cron/root ]; then
        mkdir -p /data/cron
        cat > /data/cron/root << EOF
0 20 * * * sync; echo 3 > /proc/sys/vm/drop_caches
0 20 * * * sync; fstrim -v /system
0 20 * * * sync; fstrim -v /data
0 20 * * * sync; fstrim -v /cache

01 * * * * run-parts /system/etc/cron/cron.hourly
02 4 * * * run-parts /system/etc/cron/cron.daily
22 4 * * 0 run-parts /system/etc/cron/cron.weekly
EOF
        fi
        crond -c /data/cron

}


##########################################################################################
boost_sd() {
        $LC "Init.d/20jubei ......boost_sd"
        if [ -e /sys/devices/virtual/bdi/179:0/read_ahead_kb ]; then
                echo 2048 > /sys/devices/virtual/bdi/179:0/read_ahead_kb
        fi
}

##########################################################################################
bat_life() {
        $LC "Init.d/20jubei ......bat_life"
        echo "1000" > /proc/sys/vm/dirty_expire_centisecs
        echo "2000" > /proc/sys/vm/dirty_writeback_centisecs
}

##########################################################################################
touch_reponse() {
        $LC "Init.d/20jubei ......touch_reponse"
        echo "7025" > /sys/class/touch/switch/set_touchscreen
        echo "8002" > /sys/class/touch/switch/set_touchscreen
        echo "11001" > /sys/class/touch/switch/set_touchscreen
        echo "13030" > /sys/class/touch/switch/set_touchscreen
        echo "14005" > /sys/class/touch/switch/set_touchscreen
}
##########################################################################################
mem_minfree() {
        $LC "Init.d/20jubei ......mem_minfree"
        if [ -e /sys/module/lowmemorykiller/parameters/adj ]; then
                echo "0,1,2,4,6,15" > /sys/module/lowmemorykiller/parameters/adj
        fi
       
        if [ -e /sys/module/lowmemorykiller/parameters/minfree ]; then
                echo "2560,4096,6144,12288,14336,18432" > /sys/module/lowmemorykiller/parameters/minfree
        fi
       
        if [ -e /proc/sys/vm/swappiness ]; then
                echo "10" > /proc/sys/vm/swappiness
        fi
       
        if [ -e /proc/sys/vm/vfs_cache_pressure ]; then
                echo "50" > /proc/sys/vm/vfs_cache_pressure
        fi
       
        if [ -e /proc/sys/vm/dirty_ratio ]; then
                echo "22" > /proc/sys/vm/dirty_ratio
        fi
       
        if [ -e /proc/sys/vm/dirty_background_ratio ]; then
                echo "4" > /proc/sys/vm/dirty_background_ratio
        fi
}

##########################################################################################
kern_sleepers() {
        $LC "Init.d/20jubei ......kern_sleepers"
        mount -t debugfs none /sys/kernel/debug
        echo "NO_ARCH_POWER" > /sys/kernel/debug/sched_features;
        echo "NO_GENTLE_FAIR_SLEEPERS" > /sys/kernel/debug/sched_features;
        echo "NO_NORMALIZED_SLEEPERS" > /sys/kernel/debug/sched_features
        echo "NO_NEW_FAIR_SLEEPERS" > /sys/kernel/debug/sched_features
        umount /sys/kernel/debug
}


##########################################################################################sqlite3
vacume_sqlite() {
        $LC "Init.d/20jubei ......vacume_sqlite"
        local i
        for i in `find /data -iname "*.db"`; do
                /system/xbin/sqlite3 $i 'VACUUM;'
                /system/xbin/sqlite3 $i 'REINDEX;'
        done
       
        if [ -d "/dbdata" ]; then
                for i in `find /dbdata -iname "*.db"`; do
                        /system/xbin/sqlite3 $i 'VACUUM;'
                        /system/xbin/sqlite3 $i 'REINDEX;'
                done
        fi
       
        if [ -d "/datadata" ]; then
                for i in `find /datadata -iname "*.db"`; do
                        /system/xbin/sqlite3 $i 'VACUUM;'
                        /system/xbin/sqlite3 $i 'REINDEX;'
                done
        fi
       
        for i in `find /sdcard -iname "*.db"`; do
                /system/xbin/sqlite3 $i 'VACUUM;'
                /system/xbin/sqlite3 $i 'REINDEX;'
        done
}

##########################################################################################
sleep_wifi() {
        $LC "Init.d/20jubei ......sleep_wifi"
        local sqlite=/system/xbin/sqlite3
        local RETURN_VALUE
        wifi_idle_wait=10000
        RETURN_VALUE=$($sqlite /data/data/com.android.providers.settings/databases/settings.db "select value from secure where name='wifi_idle_ms'")
       
        if [ $RETURN_VALUE="" ]; then
                $sqlite /data/data/com.android.providers.settings/databases/settings.db "insert into secure (name, value) values ('wifi_idle_ms', $wifi_idle_wait )"
        fi
        $sqlite /data/data/com.android.providers.settings/databases/settings.db "update secure set value=$wifi_idle_wait where name='wifi_idle_ms'"
        RETURN_VALUE=$($sqlite /data/data/com.android.providers.settings/databases/settings.db "select value from secure where name='wifi_idle_ms'")
}
##########################################################################################
dis_iostats() {
        $LC "Init.d/20jubei ......dis_iostats"
        if [ -e $i/queue/iostats ]; then
                echo "0" > $i/queue/iostats
        fi
}
       
##########################################################################################setrenice
set_renice() {
        $LC "Init.d/20jubei ......set_renice"
        renice -20 `pidof com.android.phone`
        renice -19 `pidof com.android.inputmethod.latin`
        renice -19 `pidof com.swype.android.inputmethod`
        renice -17 `pidof com.android.systemui`
        renice -15 `pidof com.android.launcher`
        renice -9 `pidof com.android.settings`
        renice -9 `pidof com.android.vending`
        renice -6 `pidof com.sec.android.app.camera`
        renice -6 `pidof com.sec.android.app.fm`
        renice -6 `pidof com.google.android.apps.maps`
        renice -4 `pidof com.google.android.apps.googlevoice`
        renice -3 `pidof android.process.media`
}

##########################################################################################
proc_tweaks() {
        $LC "Init.d/20jubei ......proc_tweaks"
        echo "0" > /proc/sys/vm/swappiness
       
        echo "3" > /proc/sys/vm/page-cluster
       
        echo "10" > /proc/sys/vm/vfs_cache_pressure
       
        echo "2000" > /proc/sys/vm/dirty_writeback_centisecs
       
        echo "1000" > /proc/sys/vm/dirty_expire_centisecs
       
        echo "0" > /proc/sys/vm/laptop_mode
        echo "90" > /proc/sys/vm/dirty_ratio
        echo "85" > /proc/sys/vm/dirty_background_ratio
       
        echo "0" > /proc/sys/vm/oom_kill_allocating_task
        echo "8" > /proc/sys/vm/page-cluster
        echo "4096" > /proc/sys/vm/vm.min_free_kbytes
        echo "10" > /proc/sys/fs/lease-break-time
        echo "0" > /proc/sys/vm/panic_on_oom
        echo "64000" > /proc/sys/kernel/msgmni
        echo "64000" > /proc/sys/kernel/msgmax
        echo "10" > /proc/sys/fs/lease-break-time
}

##########################################################################################
speedy() {
        $LC "Init.d/20jubei ......speedy"
        rm -r /data/local/tmp/*
        rm -r /data/tmp/*
        rm -r /data/system/usagestats/*
        rm -r /data/system/appusagestats/*
        rm -r /data/system/dropbox/*
        rm -r /data/tombstones/*
        rm -r /data/anr/*
        chmod 000 /data/system/userbehavior.db
        chmod 000 /data/system/usagestats/
        chmod 000 /data/system/dropbox/
        chmod 000 /data/anr/
        chmod 000 /data/tombstones/
        chmod 000 /data/system/appusagestats/
        chmod 000 /data/data/com.google.android.location/files/cache.cell
        chmod 000 /data/data/com.google.android.location/files/cache.wifi
       
       
        local MMC=`ls -d /sys/block/mmc*`
        for q in $MMC; do
                echo "0" > $q/queue/rotational
                echo "1" > $q/queue/iosched/fifo_batch
                echo "1" > $q/queue/iosched/low_latency
                echo "1" > $q/queue/iosched/back_seek_penalty
                echo "1000000000" > $q/queue/iosched/back_seek_max
                echo "3" > $q/queue/iosched/slice_idle
                echo "16" > $q/queue/iosched/quantum
                echo "512" > $q/queue/nr_requests
        done
       
       
        sysctl -w net.ipv4.tcp_timestamps=0
        sysctl -w net.ipv4.tcp_tw_reuse=1
        sysctl -w net.ipv4.tcp_tw_recycle=1
        sysctl -w net.ipv4.tcp_sack=1
        sysctl -w net.ipv4.tcp_window_scaling=1
        sysctl -w net.ipv4.tcp_keepalive_probes=5 # Retries before connection is considered dead
        sysctl -w net.ipv4.tcp_keepalive_intvl=156 #
        sysctl -w net.ipv4.tcp_fin_timeout=30
        sysctl -w net.ipv4.tcp_rmem='6144        87380 524288' # 65536        131072 524288
        sysctl -w net.ipv4.tcp_wmem='6144        131072 524288' # 65536        131072 524288
        sysctl -w net.ipv4.tcp_ecn=0 # Explict Congestion Notification
        sysctl -w net.ipv4.tcp_max_tw_buckets=360000 #
        sysctl -w net.ipv4.tcp_synack_retries=2 #Default: 5
       
        # Ignore all icmp packets or pings
        sysctl -w net.ipv4.icmp_echo_ignore_all=1
        sysctl -w net.ipv6.icmp_echo_ignore_all=1
       
        # Net Core Settings
        # Location: /proc/sys/net/core
        sysctl -w net.core.wmem_max=524288 #404480
        sysctl -w net.core.rmem_max=524288 #404480
        sysctl -w net.core.rmem_default=110592
        sysctl -w net.core.wmem_default=110592
       
        #
        sysctl -w net.ipv4.route.flush=1

}


##########################################################################################
#
# General Queue Tweaks
#
queue_tweaks() {
        $LC "Init.d/20jubei ......queue_tweaks"
        local i
        for i in /sys/block/*/queue; do
        echo 512 > $i/nr_requests
        echo 2 > $i/rq_affinity
        echo 0 > $i/nomerges
        echo 0 > $i/add_random
        echo 0 > $i/rotational
        done
       
        # kill iostats hardbad
        for i in `find /sys/devices/platform -name iostats`; do echo "0" > $i; done
}

##########################################################################################
notime() {
        $LC "Init.d/20jubei ......notime"
        mount -o remount,noatime,nobh /system
        mount -o remount,rw,noatime /data
        mount -o remount,rw,noatime,nobh /cache
        mount -o remount,noatime /dev
        mount -o remount,noatime /proc
        mount -o remount,noatime /sys
}


##########################################################################################
trim_drop() {
        $LC "Init.d/20jubei ......trim_drop"
        $BUSYBOX sync
        fstrim -v /system
        fstrim -v /data
        fstrim -v /cache
        sysctl -w vm.drop_caches=3
}

#####################################################################################################################################
#######
sysrw
#######
vanir_first
vanir_entropy
align_apks
merge_sysctl
#wipe_data_cache
perms
cron_deamon
boost_sd
bat_life
touch_reponse
mem_minfree
kern_sleepers
vacume_sqlite
sleep_wifi
dis_iostats
set_renice
proc_tweaks
speedy
queue_tweaks
#########
sysro
#########
notime
trim_drop

##########################################################################################sound mod
#Full Access To ALSA
chmod 777 /dev/snd/*;
chown 0.0 /dev/snd/*;

#Using ALSA Drivers
export AUDIODRIVER=alsa;
#integrated into build prop
        setprop AUDIODRIVER alsa;
        #setprop ro.sound.driver alsa;

#integrated into build prop
        #setprop persist.audio.fluence.mode broadside;
        #setprop audio.converter.samplerate.preferred libsamplerate;
        #setprop audio.converter.samplerate.libsamplerate.quality zero_order;
        #setprop default.pcm.rate_converter samplerate_zero_order;
        #setprop ro.qc.sdk.audio.ssr true;
        #setprop ro.qc.sdk.audio.fluencetype fluencepro;
        #setprop af.resampler.quality 4;


A few notes, this is a bash script so if your custom rom is stuck with sh, dont bother
I am using vanir entropy binary so get it off a vanir nightly if you are also using it
All the steps are functions so just comment off a function to mask its opperation
Loopy smoothness may yet make a comeback
Debug logcat to come





Aucun commentaire:

Enregistrer un commentaire