fixup /dev/block/ramzswap0 creation
[xdandroid:rootfs.git] / bin / userinit.sh
1 #!/bin/sh
2 #
3 CONF_FILE="/sdcard/hero.user.conf";
4
5 renice=0
6
7 compcache_en=0
8 cc_disksize=24
9 cc_memlimit=24
10 cc_backingswap_en=0
11 cc_backingswap=/dev/block/mmcblk0p3
12 CC_DEVICE=/dev/block/ramzswap0
13
14 linux_swap_en=0
15 linux_swap_partition=/dev/block/mmcblk0p3
16
17 sys_vm_en=0
18 page_cluster=3
19 laptop_mode=0
20 dirty_expire_centisecs=3000 
21 dirty_writeback_centisecs=500
22 dirty_background_ratio=5
23 dirty_ratio=10
24 vfs_cache_pressure=10
25 overcommit_memory=0
26 overcommit_ratio=80
27 swappiness=75
28
29
30 CC_NEW_DECT=0;
31
32 swap_file_en=0;
33 linux_swap_file_size=32;
34 linux_swap_file=/sdcard/swap.file;
35
36 usage() {
37   echo
38   echo "Usage:"
39   echo "/system/sd/userinit.sh [-s|-c config.file|-i]"
40   echo 
41   echo "Default:"
42   echo "Sets system configuration based on the paratermers"
43   echo "that listed in /system/sd/user.conf"
44   echo 
45   echo "  -s: check configuration status and cross" 
46   echo "      verifiction with user configurations "
47   echo "  -c config.file: use user parameters that defined"
48   echo "                  in config.file "
49   echo "  -i: Current system settings and info "
50   echo
51   echo
52   exit 0
53 }
54
55
56 parse_args() 
57 {
58         STATUS_CHK=0;
59         DISP_SYS_INFO=0;
60         while [ $# != 0 ]; do
61         case $1 in
62                 -s) STATUS_CHK=1;shift;
63                         ;;
64                 -c) CONF_FILE=$2; shift;shift;
65                         ;;
66                 -i) DISP_SYS_INFO=1; shift;
67                         ;;
68                 -*)     usage;
69                         ;;
70                 *)      usage;
71                         ;;
72         esac;
73
74         done;
75         
76         if [ ! -e ${CONF_FILE} ]; then
77                 usage;
78         fi
79
80 }
81
82
83 # reads user specfic parameters
84 GET_USER_CONF() {
85         MATCH=0;
86         if [ -e ${CONF_FILE} ]; then
87
88                 ### clean windows format if needed
89                 if [ "`grep -c \r $CONF_FILE`" != "0" ]; then
90                         dos2unix $CONF_FILE
91                 fi
92
93                 while read inputline;
94                 do
95                         # skip comments
96                         COMMENTS=$(echo $inputline | grep "#" | wc -l);
97                         COMMENT2=$(echo $inputline | grep "#" | awk -F"#" '{print $1}');
98                         [[ $COMMENTS -eq 1 ]] && [[ -z "$COMMENT2" ]] && continue
99                         
100                         # skip empty lines
101                         [[ -z "$inputline" ]] && continue
102                         
103                         inputline=$(echo $inputline | awk -F"#" '{print $1}');
104
105                         # check begin of configurations
106                         BEGIN_CHK=$(echo $inputline | grep "{" | wc -l);
107                         END_CHK=$(echo $inputline | grep "}" | wc -l);
108
109                         if [ $MATCH -eq 0 ]; then
110                                 if [ ${BEGIN_CHK} -eq 1 ] ; then
111                                         MATCH=1;
112                                         GROUP=$(echo $inputline | grep "{" | awk -F"{" '{print $1}');
113                                         GROUP_PARS="${GROUP}_pars=\""
114                                         eval "$GROUP=1"
115                                 fi;
116                         else
117                                 if [ ${END_CHK} -eq 1 ]; then
118                                         MATCH=0;
119                                         if [ $GROUP != "custom_shells" ]; then
120                                                 GROUP_PARS="$GROUP_PARS\""
121                                                 eval $GROUP_PARS
122                                         fi
123                                 else
124                                         if [ $GROUP == "custom_shells" ]; then
125                                                 #inputline=$(echo $inputline | sed -e 's/;//g');
126                                                 #inputline=$(echo $inputline | sed -e 's/"/\"/g');
127                                                 custom_shells_pars="$custom_shells_pars #"$inputline" "
128                                         else
129                                                 eval $inputline
130                                                 PAR=$(echo $inputline | awk -F"=" '{print $1}');
131                                                 if [ $(echo $inputline | grep _en | wc -l) -eq 0 ]; then
132                                                         GROUP_PARS="$GROUP_PARS $inputline "
133                                                 fi;
134                                         fi;
135                                 fi;
136                         fi;
137                 done < $CONF_FILE ;
138         fi;
139         
140         LAST_LINE_CHK=`tail -1 $CONF_FILE | grep "}" | wc -l`
141         if [ ${LAST_LINE_CHK} -eq 1 ] && [ $MATCH -eq 1 ]; then
142                 MATCH=0;
143                 GROUP_PARS="$GROUP_PARS\""
144                 eval $GROUP_PARS
145         fi
146         
147         # checking compcache version
148 #       if [ -e /system/bin/rzscontrol ]; then 
149 #               CC_NEW_DECT=1; 
150 #               CC_STATUS_FLAG="rzscontrol ${CC_DEVICE} -s";
151 #               CC_VERSION="0.6+"
152 #       else
153                 CC_NEW_DECT=0; 
154                 CC_STATUS_FLAG="cat /proc/ramzswap";
155                 CC_VERSION="0.5"
156 #       fi;
157         
158         # overwrite swapiness 
159         if [ ${compcache_en} -eq 1 ] ; then 
160                 swappiness=${swappiness}; 
161         fi;
162 }
163
164 USER_CONF_CHECK()
165 {
166 if [ ${STATUS_CHK} -eq 1 ] || [ ${DISP_SYS_INFO} -eq 1 ]; then
167         echo === user.conf ===
168         echo "*** general ***"
169         #echo general=$general
170         echo renice=$renice
171         echo 
172         #echo compcache_pars=$compcache_pars
173         echo "*** CompCache ***"
174         #echo compcache=$compcache
175         echo compcache_en=$compcache_en
176         echo cc_memlimit=$cc_memlimit
177         echo cc_disksize=$cc_disksize
178         echo cc_backingswap_en=$cc_backingswap_en
179         echo cc_backingswap=$cc_backingswap
180         echo 
181         echo "*** Swap File ***"
182         echo swap_file_en=$swap_file_en
183         echo linux_swap_file_size=$linux_swap_file_size
184         echo linux_swap_file=$linux_swap_file
185         echo 
186         echo "*** Linux Swap ***"
187         #echo linux_swap_pars=$linux_swap_pars
188         #echo linux_swap=$linux_swap
189         echo linux_swap_en=$linux_swap_en
190         echo linux_swap_partition=$linux_swap_partition
191         echo 
192         echo "*** VM ***"
193         #echo sys_vm_pars=$sys_vm_pars
194         #echo sys_vm=$sys_vm
195         echo sys_vm_en=$sys_vm_en
196         echo swappiness=$swappiness
197         echo page_cluster=$page_cluster
198         echo laptop_mode=$laptop_mode
199         echo dirty_expire_centisecs=$dirty_expire_centisecs
200         echo dirty_writeback_centisecs=$dirty_writeback_centisecs
201         echo dirty_background_ratio=$dirty_background_ratio
202         echo dirty_ratio=$dirty_ratio
203         echo vfs_cache_pressure=$vfs_cache_pressure
204         echo overcommit_memory=$overcommit_memory
205         echo overcommit_ratio=$overcommit_ratio
206 fi;
207
208 }
209
210
211 # try to optimize call aswering
212 renicerloop()
213 {
214 while [ 1 ]
215 do
216         renice -18 `pidof com.android.mms`
217         renice  5  `pidof com.google.process.gapps`
218         renice -18 `pidof com.android.phone`
219         renice -18 `pidof android.process.media`
220         renice -18 `pidof mediaserver`
221         renice -16 `pidof com.htc.launcher`
222         renice -15 `pidof com.htc.music`
223         sleep 500
224 done
225 }
226
227 CC_SWAPOFF()
228 {
229         CC_ON=$(lsmod | grep ramzswap | wc -l);
230         if [ $STATUS_CHK -eq 0 ] && [ ${CC_ON} -ge 1 ]; 
231         then
232                 swapoff ${CC_DEVICE};
233                 #rm -fr ${CC_DEVICE};
234                 #rmmod ramzswap;
235                 #CC_ON=$(lsmod | grep ramzswap | wc -l);
236                 #if [ ${CC_ON} -ge 1 ]; then
237                 #       echo unable to remove compcache modules
238                 #fi;
239         fi;
240
241 }
242
243 # compcache setup
244 COMPCACHE()
245 {
246         if [ $STATUS_CHK -eq 1 ] && [ ${compcache_en} -eq 1 ]; then
247                 echo 
248                 echo === CompCache status ===
249                 echo CompCache version ${CC_VERSION}
250                 SYS_VAL=`$CC_STATUS_FLAG | grep DiskSize | awk -F" " '{print $2}'`
251                 CC_ON=$(lsmod | grep ramzswap | wc -l);
252                 
253                 if [ ! -z $SYS_VAL ] && [ -e ${CC_DEVICE} ] && [ ${CC_ON} -ge 1 ]; then
254                         echo Compcache enabled
255                 else
256                         echo !!!ERROR Compcache disabled
257                         dmesg | grep ramzswap | tail -10
258                 fi;
259                 
260                 for i in $compcache_pars;
261                 do
262                         USER_VAL=$(echo $i | awk -F"=" '{print $2}');
263                         USER_PAR=$(echo $i | awk -F"=" '{print $1}');
264                         if [ $USER_PAR == "cc_disksize" ] && [ ${cc_backingswap_en} -eq 0 ] ; then
265                                 USER_PAR="DiskSize"
266                                 USER_VAL=$((${USER_VAL}*1024));
267                                 SYS_VAL=`$CC_STATUS_FLAG | grep ${USER_PAR} | awk -F" " '{print $2}'`
268                                 echo "CompCache: $USER_PAR "$SYS_VAL"(system) "$USER_VAL"(user)"
269
270                         elif [ $USER_PAR != "cc_disksize" ] && [ ${cc_backingswap_en} -eq 1 ] ; then
271                                 if [ $USER_PAR == "cc_memlimit" ]; then
272                                         USER_PAR="MemLimit"
273                                         USER_VAL=$((${USER_VAL}*1024));
274                                         SYS_VAL=`$CC_STATUS_FLAG | grep ${USER_PAR} | awk -F" " '{print $2}'`
275                                         echo "CompCache: $USER_PAR "$SYS_VAL"(system) "$USER_VAL"(user)"
276                                 elif [ $USER_PAR == "cc_backingswap" ] && [ ${CC_NEW_DECT} -eq 1 ]; then
277                                         USER_PAR="BackingSwap"
278                                         SYS_VAL=`$CC_STATUS_FLAG | grep ${USER_PAR} | awk -F" " '{print $2}'`
279                                         echo "CompCache: $USER_PAR "$SYS_VAL"(system) "$USER_VAL"(user)"
280                                 else 
281                                         USER_PAR="DiskSize"
282                                         SYS_VAL=`$CC_STATUS_FLAG | grep ${USER_PAR} | awk -F" " '{print $2}'`
283                                         echo "CompCache: Backing_swap "$USER_VAL" , size $SYS_VAL"
284                                 fi
285                                 
286                                 
287                         fi;
288                         
289                         # backing swap detection is still missing
290                 done
291                 
292                 echo 
293                 echo === CompCache status output ===
294                 $CC_STATUS_FLAG
295                 
296                 
297         elif [ $compcache_en -eq 1 ]; then
298                 CC_SWAPOFF;
299
300                 cc_disksize=$((${cc_disksize}*1024));
301                 CC_DISKSIZE_FLAG="disksize_kb=${cc_disksize}";
302                 cc_memlimit=$((${cc_memlimit}*1024));
303                 CC_MEMSIZE_FLAG="memlimit_kb=${cc_memlimit}";
304                 CC_BACKINGSWAP_FLAG="backing_swap=${cc_backingswap}";           
305                 
306                 
307                 if [ ${cc_backingswap_en} -eq 1 ] && [ -e ${cc_backingswap} ]; then
308                         CC_DISKSIZE_FLAG="";
309                 else
310                         CC_MEMSIZE_FLAG="";
311                         CC_BACKINGSWAP_FLAG="";
312                 fi;
313
314 #               if [ ${CC_NEW_DECT} -eq 0 ]; then
315
316                         mknod /dev/block/ramzswap0 b 253 0
317                         [ $? -eq 0 ] || fail "Failed to create the block device"
318
319                         insmod /lib/modules/lzo_compress.ko
320                         insmod /lib/modules/lzo_decompress.ko
321                         insmod /lib/modules/xvmalloc.ko
322                         insmod /lib/modules/ramzswap.ko ${CC_DISKSIZE_FLAG} ${CC_MEMSIZE_FLAG} ${CC_BACKINGSWAP_FLAG};
323                         echo insmod /lib/modules/ramzswap.ko ${CC_DISKSIZE_FLAG} ${CC_MEMSIZE_FLAG} ${CC_BACKINGSWAP_FLAG};
324
325 #               else
326 #                       if [ ! -z ${CC_DISKSIZE_FLAG} ]; then CC_DISKSIZE_FLAG="--${CC_DISKSIZE_FLAG}"; fi;
327 #                       if [ ! -z ${CC_MEMSIZE_FLAG} ]; then CC_MEMSIZE_FLAG="--${CC_MEMSIZE_FLAG}"; fi;
328 #                       if [ ! -z ${CC_BACKINGSWAP_FLAG} ]; then CC_BACKINGSWAP_FLAG="--${CC_BACKINGSWAP_FLAG}"; fi;
329 #                       modprobe ramzswap  
330 #                       rzscontrol ${CC_DEVICE} ${CC_DISKSIZE_FLAG} ${CC_MEMSIZE_FLAG} ${CC_BACKINGSWAP_FLAG} --init;
331 #                       
332 #                       echo "rzscontrol ${CC_DEVICE} ${CC_DISKSIZE_FLAG} ${CC_MEMSIZE_FLAG} ${CC_BACKINGSWAP_FLAG} --init";
333 #               fi;
334                 
335                 echo "${swappiness}" > /proc/sys/vm/swappiness
336                 swapon ${CC_DEVICE};
337                 [ $? -eq 0 ] || fail "Failed to turn on the swap"
338
339         elif [ $compcache_en -eq 0 ]; then
340                 CC_SWAPOFF;
341         fi;
342 }
343
344 SWAPFILE_CHK()
345 {
346         if [ ! -e  ${linux_swap_file} ] && [ ${swap_file_en} -eq 1 ]; then
347                 linux_swap_file_size=$((${linux_swap_file_size}*1024));
348                 echo "Creating swap file: ${linux_swap_file}"
349                 dd if=/dev/zero of=${linux_swap_file} bs=1024 count=${linux_swap_file_size}
350                 mkswap ${linux_swap_file}
351                 if [ -e  ${linux_swap_file} ]; then
352                         echo "SWAP file "${linux_swap_file}" created "
353                 else
354                         echo "ERROR!!! Unable to create swapfile "${linux_swap_file}""
355                 fi;
356         elif  [ -e  ${linux_swap_file} ] && [ ${swap_file_en} -eq 1 ]; then
357                 LINUXSWAP_OFF;
358         elif  [ -e  ${linux_swap_file} ] && [ ${swap_file_en} -eq 0 ]; then
359                 LINUXSWAP_OFF;
360                 echo Remove "${linux_swap_file}"
361                 rm ${linux_swap_file}
362         fi;
363
364 }
365
366
367 LINUXSWAP_OFF()
368 {
369         LINUX_SWAP_ON=$(cat /proc/swaps | grep ${linux_swap_partition} | wc -l);
370         if [ $STATUS_CHK -eq 0 ] && [ ${LINUX_SWAP_ON} -eq 1 ]; then 
371                 swapoff ${linux_swap_partition};
372                 echo swapoff ${linux_swap_partition};
373         fi;
374 }
375
376 # set linux swap
377 LINUXSWAP()
378 {
379         if [ $STATUS_CHK -eq 1 ] && [ ${linux_swap_en} -eq 1 ]; then
380                 echo 
381                 echo === Linux Swap status ===
382                 for i in $linux_swap_pars;
383                 do
384                         USER_VAL=$(echo $i | awk -F"=" '{print $2}');
385                         USER_PAR=$(echo $i | awk -F"=" '{print $1}');
386                         if [ $USER_PAR == "linux_swap_partition" ]; then
387                                 if [ ! -n ${USER_VAL} ]; then
388                                         echo !!! Linux SWAP error :${USER_VAL} is not a device node;
389                                 fi;
390                                 LINUX_SWAP_ON=$(cat /proc/swaps | grep ${USER_VAL} | wc -l);
391                                 if [ ${LINUX_SWAP_ON} -ne 1 ]; then 
392                                         echo !!! Linux SWAP error: Linux swap is not enabled;
393                                 else
394                                         echo Linux SWAP enabled on ${USER_VAL}
395                                 fi;
396                         fi;
397                 done
398                 
399         elif [ ${linux_swap_en} -eq 1 ] && [ ${cc_backingswap_en} -eq 0 ];  then
400                 LINUXSWAP_OFF;
401                 
402                 if [ -n ${linux_swap_partition} ] && [ ${swap_file_en} -eq 0 ]; then
403                         echo mkswap ${linux_swap_partition};
404                         mkswap ${linux_swap_partition};
405                 fi;
406                 echo ${swappiness} > /proc/sys/vm/swappiness;
407                 echo swapon ${linux_swap_partition};
408                 swapon ${linux_swap_partition};
409         elif [ ${linux_swap_en} -eq 0 ] ; then
410                 LINUXSWAP_OFF;
411         fi;
412 }
413
414 # set virtual memory
415 SET_VM()
416 {
417         if [ $STATUS_CHK -eq 1 ] && [ ${sys_vm_en} -eq 1 ]; then
418                 echo 
419                 echo === VM status ===
420                 for i in $sys_vm_pars;
421                 do
422                         USER_VAL=$(echo $i | awk -F"=" '{print $2}');
423                         USER_PAR=$(echo $i | awk -F"=" '{print $1}');
424                         if [ $USER_PAR == "page_cluster" ]; then USER_PAR=page-cluster; fi;
425                         SYS_VAL=`cat /proc/sys/vm/"$USER_PAR"`
426                         if [ $USER_VAL -ne "$SYS_VAL" ] || [ -z $SYS_VAL ]; then
427                                 echo "ERROR!!! Set VM: $USER_PAR - "$SYS_VAL"(system) "$USER_VAL"(user)"
428                         else
429                                 echo "Set VM: $USER_PAR - "$SYS_VAL"(system) "$USER_VAL"(user)"
430                         fi;
431                 done
432                 
433         elif [ ${sys_vm_en} -eq 1 ] && [ ${sys_vm} -eq 1 ];
434         then
435                 echo ${page_cluster} > /proc/sys/vm/page-cluster; 
436                 echo ${laptop_mode} > /proc/sys/vm/laptop_mode;
437                 echo ${dirty_expire_centisecs} > /proc/sys/vm/dirty_expire_centisecs;
438                 echo ${dirty_writeback_centisecs} > /proc/sys/vm/dirty_writeback_centisecs;
439                 echo ${dirty_background_ratio} > /proc/sys/vm/dirty_background_ratio; 
440                 echo ${dirty_ratio} > /proc/sys/vm/dirty_ratio; 
441                 echo ${vfs_cache_pressure} > /proc/sys/vm/vfs_cache_pressure;
442                 echo ${overcommit_ratio} > /proc/sys/vm/overcommit_ratio;
443                 echo ${overcommit_memory} > /proc/sys/vm/overcommit_memory;
444                 echo ${swappiness} > /proc/sys/vm/swappiness;
445         fi;
446 }
447
448
449 SYS_INFO ()
450 {
451         if [ ${DISP_SYS_INFO} -eq 1 ]; then
452                 echo
453                 echo === free ===
454                 free
455                 echo
456                 echo === swaps  ===
457                 cat /proc/swaps 
458                 echo
459                 echo === ${CC_STATUS_FLAG} ===
460                 ${CC_STATUS_FLAG}
461                 echo
462                 echo === meminfo ===
463                 cat /proc/meminfo
464                 echo
465                 echo === VM configurations ===
466                 sysctl -a | grep vm
467                 exit 0;
468         fi
469         
470
471 }
472
473
474 USER_CMD()
475 {
476         i=1;
477         while [ $i -ne 0 ];
478         do
479                 i=$((${i}+1));
480                 cmd=`echo $custom_shells_pars | cut -d "#" -f $i`
481                 if [ -z "$cmd" ]; then
482                         i=0;
483                 else 
484                         /bin/sh -c "$cmd"                       
485                 fi;
486         done
487 }
488
489 #main program
490 parse_args $*
491 GET_USER_CONF;
492 USER_CONF_CHECK;
493 SYS_INFO;
494
495 SWAPFILE_CHK;
496 COMPCACHE;
497 LINUXSWAP;
498 SET_VM;
499 USER_CMD;
500 if [ ${STATUS_CHK} -eq 0 ] && [ $renice -eq 1 ]; then
501         renicerloop > /dev/null 2>&1 &
502 fi
503
504 exit 0;