miércoles, 13 de febrero de 2013

Network Manager and NFS problem on Shutdown

I power up my personal computer with debian system installed. I login and automatically my network manager connect to predefined wireless network because the application nm-applet (network manager applet) or similar has send the order to network manager daemon using dbus communication. I mount one folder using NFS o CIFS.
When i shutdown my computer the error message () appear at the end of shutdown process.
This situation is because the network manager daemon is release before unmount network file systems. The idea is correct because the network manager daemon establish a connection when the nm-applet send the order and the nm-applet only run when user as logging.  But the nfs is a basic part of system like networking. The problem is that networkmanager is used to respond user request (at user level) and change basic system configuration like network parameters. The nfs depend of network interfaces and is release before the network is down but it refer to basic networking daemon, not to networkmanager.
The network manager daemon can modify network connection but must take in consideration that it can release the current connection on shutdown because is the networking daemon who must to do it.
networking - work at system level
network file system - work at system an user level.
networkmanager - work at user level but modifying networking ( problem because start late in time and change system configuration that must be defined at system boot)

The solution is change the release order of daemon on system level 6 and 0. 

ls /etc/rc6.d/
...
K02network-manager -> ../init.d/network-manager
K03sendsigs -> ../init.d/sendsigs
K04rsyslog -> ../init.d/rsyslog
K05umountnfs.sh -> ../init.d/umountnfs.sh
K06nfs-common -> ../init.d/nfs-common
K06rpcbind -> ../init.d/rpcbind
K07hwclock.sh -> ../init.d/hwclock.sh
K07networking -> ../init.d/networking
K08umountfs -> ../init.d/umountfs
K09umountroot -> ../init.d/umountroot
K10reboot -> ../init.d/reboot

ls /etc/rc0.d/

K02network-manager -> ../init.d/network-manager
K03sendsigs -> ../init.d/sendsigs
K04rsyslog -> ../init.d/rsyslog
K05umountnfs.sh -> ../init.d/umountnfs.sh
K06nfs-common -> ../init.d/nfs-common
K06rpcbind -> ../init.d/rpcbind
K07hwclock.sh -> ../init.d/hwclock.sh
K07networking -> ../init.d/networking
K08umountfs -> ../init.d/umountfs
K09umountroot -> ../init.d/umountroot
K10halt -> ../init.d/halt

Move network-manager from position 2 to position 7 at the same level that networking because it do the same that networking at release (deconfigure network interfaces).

martes, 15 de enero de 2013

Moving out Firefox's profile to ram filesystem

Firefox's profile dir

Hi, I need to optimize firefox execution getting somes files out of disk to ram and come back to disk to retaing data. In linux firefox use the folder $HOME/.mozilla/firefox to store all available profiles. I use this script to make it possible.

FF_BIN=/opt/32/firefox/firefox
PROFILE_NAME=$USER

# Launch firefox with profile in ramfs, wait and update modified files to backup

RAMFS=/ramfs
PROFILE_ROOT=$HOME/.mozilla/firefox   #firefox root directory
PROFILE_FILE=$PROFILE_ROOT/profiles.ini

#check preconditions
[ ! -e $PROFILE_FILE ] && echo "error: not file $PROFILE_FILE " >> /opt/bin.log && exit


#read all section when name is present hold. for next keys use name.key = value

while IFS="\=" read -r key val ; do
    [ -z $val ] && continue             # eval -zero string if true eval continue, the result is not use

    if [ $key == "Name" ] ; then
        PREFIX=$val
        continue
    fi

    [ -z $PREFIX ] && continue

    export "${PREFIX}_$key=$val"        #make visible out of while do not use dot because it is not valid for variable name 

done < $PROFILE_FILE 

# copy to ramfs only one time, remain until next reboot, get backup on every exit
eval PROFILE_DIR=\$\{${PROFILE_NAME}_Path\}
eval PROFILE_PATH=$PROFILE_ROOT/\$\{${PROFILE_NAME}_Path\}

[ -z $PROFILE_DIR ] && echo "error: profile $PROFILE_NAME not found in $PROFILE_FILE " >> /opt/bin.log && exit
[ ! -d ${PROFILE_PATH}.backup ] && echo "error: directory ${PROFILE_PATH}.backup not found " >> /opt/bin.log && exit

RAMFS_PROFILE_PATH=${RAMFS}$PROFILE_PATH

if [ ! -d $RAMFS_PROFILE_PATH ] ; then
    mkdir -p $RAMFS_PROFILE_PATH
    cp -axr ${PROFILE_PATH}.backup/. $RAMFS_PROFILE_PATH
    ln -sf $RAMFS_PROFILE_PATH  $PROFILE_ROOT
fi

$FF_BIN -p $PROFILE_NAME -no-remote  $@

#update modified files
if [ ! -z $RAMFS_PROFILE_PATH ] ; then
# -E to preserve exection flag.
rsync -avqE --delete-after $RAMFS_PROFILE_PATH/. ${PROFILE_PATH}.backup
fi

exit

   

Understanding Bash quoting

We use this simple bash file to validate all examples

#!/bin/bash
echo "Arguments count : $#"
echo "All : $@"

for (( i=0; i<=$#; i++ )); do
    eval arg=\${$i}
    echo $i : $arg
done

exit

Especial character # use to comment a line. See next.

./print-arg.sh arg1 arg2 arg3  #arg4 ignore 
Arguments count = 3
All : arg1 arg2 arg3
0 : arg1
1 : arg2
2 : arg3

To avoid bash interprets the character # we use quotes, simple or double do not matter

./print-arg.sh arg1 arg2 arg3  '#arg4' ignore 
Arguments count = 5
All : arg1 arg2 arg3 #arg4 ignore
0 : arg1
1 : arg2
2 : arg3
3 : #arg4
4 : ignore
./print-arg.sh arg1 arg2 arg3   "#arg4" ignore 
Arguments count = 5
All : arg1 arg2 arg3 #arg4 ignore
0 : arg1
1 : arg2
2 : arg3
3 : #arg4
4 : ignore

Bash interprets the blank space has argument separation, to avoid this we use simple or double quoted

./print-arg.sh "arg1 arg2" arg3    "#arg4 ignore" 
Arguments count : 3
All : arg1 arg2 arg3 #arg4 ignore
0 : ./print-arg.sh
1 : arg1 arg2
2 : arg3
3 : #arg4 ignore

To pass espaces use simple or double quoting, to pass quote as another character, use simple to double and double to simple. Use double if there is some spaces and use escape char \ to pass double quote bettewn them.
Some special chars in bash

  • * : use for expand files in current path
  • space : Use for arguments and elements separation
  • # : from this character to the end of the line text is a comment
  • $ : for variables or parameters sustitution
  • ; : end of command, another can start from here, new line is not necessary
  • ! : show execute command from history number
  • \ : wrap line at the end, the command continues in the next line, like a continuos line,
  • \ : cancel or scape the meaning of next character, ($,\,',",!,#,space,*) making this part of string ("\" - need another " to enclose)
  • ' : enclose text to avoid interprets of next characters ($,",\,#,space,;,",!,*)
  • " : enclose text to avoid interprets of characters (',;,#,\,space,) - (\") can not avoided
  • ",' : another character than space near of close quote is part of string ( " "abcd - is one string with spaces a begining)

Tips:
You can get enclose twice at the same string if close and open quote are near one to another. ('\') is \ enclosed, (' 'abcd) is an enclose espace plus letters, (' ''abcd') is the same, two enclosed together. ('\'') another ' needed to finish the second enclosure.
(\") enclosed betwen double quote is like an " Ej ("\"") ("\"""abcd" - two enclosed string near appear as one string.).

./print-arg.sh "ab'cd" 'ab"cd' 'ab''cd' "ab""cd" !645 "$HOME \$HOME \" !645 \!" '\" \$ $HOME !645'  
Arguments count : 7
All : ab'cd ab"cd abcd abcd ll /home/main $HOME " ll \! \" \$ $HOME !645 \# \# ab\ cd
0 -./print-arg.sh-
1 -ab'cd-
2 -ab"cd-
3 -abcd-
4 -abcd-
5 -ll-
6 -/home/main $HOME " ll \!-
7 -\" \$ $HOME !645-
8 -#-
9 -ab cd-