Monday, June 8, 2015

Sharing NFS filesystem in linux



Sharing NFS filesystem in linux
-------------------------------

1. Source server - standalone2
2. Target  server- rhel11gr2rac1


Sharing NFS file system will be very helpful in real time environment, its enables a particular
file-system to be mounted across the servers for easy access.

Lets see an example on show to share the NFS.



On Source server.
-----------------

1. Set up the yum repository environment.

2. Install the NFS rpm's using yum


[root@standalone2 ~]# yum -y install nfs-utils nfs-utils-lib
Loaded plugins: security
Setting up Install Process
Package 1:nfs-utils-1.0.9-54.el5.x86_64 already installed and latest version
Package nfs-utils-lib-1.0.8-7.6.el5.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package nfs-utils-lib.i386 0:1.0.8-7.6.el5 set to be updated
--> Processing Dependency: libgssapi.so.2(libgssapi_CITI_2) for package: nfs-utils-lib
--> Processing Dependency: libgssapi.so.2 for package: nfs-utils-lib
--> Running transaction check
---> Package libgssapi.i386 0:0.10-2 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
 Package                                     Arch                               Version                                      Repository                            Size
========================================================================================================================================================================
Installing:
 nfs-utils-lib                               i386                               1.0.8-7.6.el5                                Server                                55 k
Installing for dependencies:
 libgssapi                                   i386                               0.10-2                                       Server                                22 k

Transaction Summary
========================================================================================================================================================================
Install       2 Package(s)
Upgrade       0 Package(s)

Total download size: 77 k
Downloading Packages:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                   652 kB/s |  77 kB     00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : libgssapi                                                                                                                                        1/2
  Installing     : nfs-utils-lib                                                                                                                                    2/2

Installed:
  nfs-utils-lib.i386 0:1.0.8-7.6.el5

Dependency Installed:
  libgssapi.i386 0:0.10-2

Complete!

3. Create an directory and create some sample files.

[root@standalone2 public]# mkdir /shared_nfs

[root@standalone2 public]# cd /shared_nfs/
[root@standalone2 shared_nfs]# echo "File1" > file1.txt
[root@standalone2 shared_nfs]# echo "File2" > file2.txt
[root@standalone2 shared_nfs]# echo "File3" > file3.txt

[root@standalone2 shared_nfs]# ls -lrt
total 12
-rw-r--r-- 1 root root 6 Jun  9 03:35 file1.txt
-rw-r--r-- 1 root root 6 Jun  9 03:35 file2.txt
-rw-r--r-- 1 root root 6 Jun  9 03:35 file3.txt


4. Now we need to make the /shared_nfs directory to be shared, we need to update that in export file.

[root@standalone2 shared_nfs]# vi /etc/exports

/shared_nfs 192.168.0.20(rw,sync)



There /shared_nfs is the directory which we are going to share, 192.68.0.20 is the ip address of the target server,
(rw is read / write , or you can update as ro for read only , sync is for sync).



5. Start or restart the NFS service on the server.

[root@standalone2 public]# service nfs restart
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]


if rhel6 if you get below error then start the rpcbind

[root@apache-datnd1 shared_nfs]# service nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno = Connection refused
rpc.rquotad: unable to register (RQUOTAPROG, RQUOTAVERS, udp).
                                                           [FAILED]
Starting NFS mountd:                                       [FAILED]
Starting NFS daemon: rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
rpc.nfsd: unable to set any sockets for nfsd
                                                           [FAILED]


[root@apache-datnd1 shared_nfs]# service rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@apache-datnd1 shared_nfs]#
[root@apache-datnd1 shared_nfs]#
[root@apache-datnd1 shared_nfs]#
[root@apache-datnd1 shared_nfs]# service nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]





On Target Server
----------------

1. Install the NFS related rpm on the target server.



[root@rhel11gr2rac1 pub]# yum -y install nfs-utils nfs-utils-lib
Loaded plugins: rhnplugin, security
This system is not registered with RHN.
RHN support will be disabled.
Setting up Install Process
Package nfs-utils-lib-1.0.8-7.6.el5.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package nfs-utils.x86_64 1:1.0.9-54.el5 set to be updated
---> Package nfs-utils-lib.i386 0:1.0.8-7.6.el5 set to be updated
--> Processing Dependency: libgssapi.so.2(libgssapi_CITI_2) for package: nfs-utils-lib
--> Processing Dependency: libgssapi.so.2 for package: nfs-utils-lib
--> Running transaction check
---> Package libgssapi.i386 0:0.10-2 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
 Package                                     Arch                                 Version                                       Repository                         Size
========================================================================================================================================================================
Installing:
 nfs-utils-lib                               i386                                 1.0.8-7.6.el5                                 ser                                55 k
Updating:
 nfs-utils                                   x86_64                               1:1.0.9-54.el5                                ser                               404 k
Installing for dependencies:
 libgssapi                                   i386                                 0.10-2                                        ser                                22 k

Transaction Summary
========================================================================================================================================================================
Install       2 Package(s)
Upgrade       1 Package(s)

Total download size: 480 k
Downloading Packages:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                    23 MB/s | 480 kB     00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : libgssapi                                                                                                                                        1/4
  Installing     : nfs-utils-lib                                                                                                                                    2/4
  Updating       : nfs-utils                                                                                                                                        3/4
  Cleanup        : nfs-utils                                                                                                                                        4/4

Installed:
  nfs-utils-lib.i386 0:1.0.8-7.6.el5

Dependency Installed:
  libgssapi.i386 0:0.10-2

Updated:
  nfs-utils.x86_64 1:1.0.9-54.el5

Complete!


2. You can display the nfs which are shared from the source server using below.

[root@rhel11gr2rac1 shared_from_192.168.0.30]# showmount -e 192.168.0.30
Export list for 192.168.0.30:
/public     192.168.0.20
/shared_nfs 192.168.0.20


-- We could see that 2 file systems are shared from the source server.


3. Create a new directory on the taret server and you can use that to mount the filesystem.

[root@rhel11gr2rac1 shared_from_192.168.0.30]# mkdir /shared_file_nfs


[root@rhel11gr2rac1 shared_from_192.168.0.30]# mount 192.168.0.30:/shared_nfs /shared_file_nfs
[root@rhel11gr2rac1 shared_from_192.168.0.30]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              21G   18G  2.7G  87% /
/dev/sda1             289M   17M  258M   6% /boot
tmpfs                 816M     0  816M   0% /dev/shm
/dev/sdb1              25G   14G  9.8G  59% /oracle
192.168.0.30:/shared_nfs
                       21G   14G  6.4G  68% /shared_file_nfs



-- Now we could see that the NFS is mounted on the target server.












Thursday, June 4, 2015

Linux Runlevels and using it for Automated Startup or Shutdown



This example is based on the version Linux 5.

A runlevel defines the state of the machine after boot. In Simple we can say as the mode in which you want the system to be restated, it is just like in windows while the system is booting up we will have option like start with safe mode, Networking and so on.

Different runlevels are typically assigned to the single-user mode,
multiuser mode without network services started, multiuser mode with network services started, system shutdown, and system reboot system states.

The different run level action can be viewed from as below.

# cat /etc/inittab


#####################################################################
# Default runlevel. The runlevels used by RHS are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
#####################################################################


As you can see for 0 and 6 it is mentioned as do not set, this is because we dont want our system to shutdown (runlevel 0) or
reboot (runlevel 6) immediately once our system boots.

To know the default run level in which your system is configured type as below.

[root@standalone2 rc0.d]# runlevel
N 5

We get the output as 5 which means by default the system is configured to start at run level 5.

you can also see that there are directories like rc0.d, rc1.d, rc2.d, rc3.d, rc4.d, rc5.d, rc6.d under the /etc folders, these directories contains the files/scripts which to be called during the runlevel. Normally the files under this will start with S which is for start and K which is for to kill the process/service.

Using these run levels you can configure your script to start/stop services during shutdown/start/reboot of the system.


Now here my system is running in runlevel 5, now i need to schedule a script to do some action based on runlevels.

Parameter will be passed as start with S and parameter stop will be passed with K automatically by the system.


Now we need to configure the script like as below.

Runlevel 0  - stop
Runlevel 1  - stop
Runlevel 2  - stop
Runlevel 3  - stop
Runlevel 4  - stop
Runlevel 5  - start
Runlevel 6  - stop


Below is the sample script. Named as testme

#################################################################
#!/bin/bash
#
#
case $1 in
"start")
        echo `date` >> /home/oracle/startup.log
        echo 'startup' >> /home/oracle/startup.log
        ;;
"stop")
        echo `date` >> /home/oracle/shutdown.log
        echo 'shutdown' >> /home/oracle/shutdown.log
        ;;
esac
#################################################################

Copy the sciprt inside the /etc/init.d directory.

We will give a sequence number as 40 for this script.

Now we need to create a symbolic link file under the appropirate run level directories.


[root@standalone2 init.d]# cd /etc/rc0.d/
[root@standalone2 rc0.d]# ln -s /etc/init.d/testme K40testme
[root@standalone2 rc0.d]# ls -lrt K40testme
lrwxrwxrwx 1 root root 18 Jun  4 04:10 K40testme -> /etc/init.d/testme

Create the same in all the directories except rc5.d.

K40testme ( Refers K for kill which will pass the parameter as stop , 40 is the sequence or order in which the script
has to be executed, testme is the mainfile)

[root@standalone2 rc1.d]# cd ../rc2.d/
[root@standalone2 rc2.d]# ln -s /etc/init.d/testme K40testme
[root@standalone2 rc2.d]# cd ../rc3.d/
[root@standalone2 rc3.d]# ln -s /etc/init.d/testme K40testme
[root@standalone2 rc3.d]# cd ../rc4.d/
[root@standalone2 rc4.d]# ln -s /etc/init.d/testme K40testme
[root@standalone2 rc4.d]# cd ../rc6.d/
[root@standalone2 rc6.d]# ln -s /etc/init.d/testme K40testme

and in rc5.d create as below.

[root@standalone2 rc5.d]# ln -s /etc/init.d/testme S40testme
[root@standalone2 rc5.d]# ls -lrt S40*
lrwxrwxrwx 1 root root 18 Jun  4 04:13 S40testme -> /etc/init.d/testme



As you can see the testme script is showing as off for all the run levels except 5.

Lets test the script now.

Now am running runing runlevel 6 (init 6) which will reboot the system, also as per
our script it should first call the stop and while booting it will call the runlevel 5 so
our script should start.

[root@standalone2 rc5.d]# init 6

== Once the server is up we could see that the script has been executed successfully.

[oracle@standalone2 ~]$ cd /home/oracle
[oracle@standalone2 ~]$ ls -lrt startup.log
-rw-r--r-- 1 root root 144 Jun  4 05:23 startup.log

---------------------------------------------------------------------

Instead of manually creating links we can use chkconfig to automate the process.



Here we will use script to bring up an oracle database online when system starts.

Oracle database comes with default script dbstart and dbshut will can be used for this purpose.

to use this, create a script as below.


#!/bin/bash
#
# chkconfig: 35 99 10
# description: Auto start/stop of database
#
# Set ORA_HOME to the Oracle Home where the lsnrctl and dbstart
# commands can be found
ORA_HOME=/u01/app/oracle/product/11.2.0.3/dbhome_1
# Set ORA_OWNER to the owner of the Oracle software
ORA_OWNER=oracle
LCKFILE=/var/lock/subsys/ora_db_start
case "$1" in
    'start')
           # Start the listener:
  touch $LCKFILE
           su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start"
           # Start the databases:
           su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
    ;;
    'stop')
           # Stop the listener:
  rm -f $LCKFILE
           su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl stop"
           # Stop the databases:
           su - $ORA_OWNER -c $ORA_HOME/bin/dbshut
    ;;
esac



Save the filename as ora_db_start in /etc/init.d


Now we need to set the run levels of this.

You can either manually create links under the rc folders or can use chkconfig which will automatically creates
the links files.

# chkconfig --add ora_db_start


-- you can see that the script ora_db_start it contains chkconfig: 35 99 10 where 35 is the 3 and 5 runlevels which
should be on and other run levels should be off, 99 is the Start sequence and 10 is the kill sequence.


===============

You can also create a user defined script if required, as per below.


Create a script as oracle_db under /etc/init.d/ with below contents.

#!/bin/bash
#
# chkconfig: 35 99 10
# description: Starts and stops the Oracle listener and database
#
# Define variables for use in this script
export ORACLE_HOME=/u01/app/oracle/product/11.2.0.3/dbhome_1
ORA_OWNER=oracle
case "$1" in
    'start')
           touch /var/lock/subsys/oracle_db
           # Start the databases:
           su - oracle -c $ORACLE_HOME/bin/db_startup.sh
    ;;
    'stop')
           rm -f /var/lock/subsys/oracle_db
           # Stop the databases:
           su - oracle -c $ORACLE_HOME/bin/db_shutdown.sh
        ;;
esac



Create the below scripts as oracle user under the $ORACLE_HOME/bin directory.


db_startup.sh

#!/bin/bash
export ORACLE_HOME=/u01/app/oracle/product/11.2.0.3/dbhome_1
export ORACLE_SID=testdb
export PATH=$ORACLE_HOME/bin:$PATH
sqlplus -s "/ as sysdba" <> /u01/app/oracle/product/11.2.0.3/dbhome_1/db_startup.log
startup;
exit;
EOF


db_shutdown.sh

#!/bin/bash
export ORACLE_HOME=/u01/app/oracle/product/11.2.0.3/dbhome_1
export ORACLE_SID=testdb
export PATH=$ORACLE_HOME/bin:$PATH
sqlplus -s "/ as sysdba" <> /u01/app/oracle/product/11.2.0.3/dbhome_1/db_shutdown.log
shutdown immediate;
exit;
EOF


Now set the run levels for the script.


[root@standalone2 ~]# chkconfig --add oracle_db

[root@standalone2 ~]# chkconfig --list oracle_db
oracle_db       0:off   1:off   2:off   3:on    4:off   5:on    6:off

As you can see the run level are set on for 3 and 5 and for the remaining it is off, you can also
verify the symbolic link files.


Now Lets check whether it is working as expected, currently the db is up and running.


[root@standalone2 ~]# init 6


--Once the server is up we could see that the db is up and running.

verifying the logs.

[oracle@standalone2 dbhome_1]$ ls -lrt *.log

-rw-r--r-- 1 oracle oinstall   65 Jun  4 08:07 db_shutdown.log
-rw-r--r-- 1 oracle oinstall  466 Jun  4 08:10 db_startup.log

[oracle@standalone2 dbhome_1]$ cat db_shutdown.log
Database closed.
Database dismounted.
ORACLE instance shut down.


[oracle@standalone2 dbhome_1]$ cat db_startup.log
ORACLE instance started.

Total System Global Area  680607744 bytes
Fixed Size                  2231472 bytes
Variable Size             406848336 bytes
Database Buffers          268435456 bytes
Redo Buffers                3092480 bytes
Database mounted.
Database opened.
ORACLE instance started.


-- Run levels are working fine as expected.