How to determine CRS_HOME

In the environment of the cluster owner or some scripts, you may want to set a variable for the cluster installation usually called CRS_HOME, GRID_HOME or simly ORACLE_HOME.
I like to do that in an automatic and
non-static way. But be careful, this means it could go wrong or is being set to an unexpected value.
The main part of this post is
how get the information and how to check for an acceptable value.

With 11g it was quite easy if you‘d use asm there has to be an entry in the oratab file. In the next statements I assume we are on Linux and using asm.
If you need a more generic solution, you can add some lines of code the check the OS and set the oratab file arcordingly.

CRS_HOME="$(awk -F : ' $1 ~ /^+ASM[0-9]$/ {print $2}' /etc/oratab)"

or if you don’t like awk

CRS_HOME="$(grep "^+ASM[0-9]:" /etc/oratab |cut -d : -f 2)"

awk was faster in my tests.
With 12c the asm instance is also always there until you use flex asm or 12.2 client cluster.
Here you have to rely on the asm proxy instance (apx) and for cluster without
flex asm you can check the asm entry.

export CRS_HOME=$(awk -F ':' ' $1 ~ /^+APX[0-9]$/ { print $2 }' /etc/oratab)
[ -z "$CRS_HOME" ] && export CRS_HOME=$(awk -F ':' ' $1 ~ /^+ASM[0-9]$/ { print $2 }' /etc/oratab)

This was quite a long time my favored solution until I faced an oratab with 2 asm entries leading to a wrong CRS_HOME which could cause a lot of bad problems.
It was set to “/u01/app/ /u01/app/”. This will be terrible for example
affecting find commands, i.e. find $CRS_HOME/rdbms/audit …
Esay to solve with a sort -u , but then I thought about the possibility to have asm1 with the old home and after the upgrade asm2 with the newer one. Is that possible? yes, it is.
The problem with the double entries is fixed, as long as the entries have the same value.
So we need a check for the different values:

[ -d "$CRS_HOME" ]
[ -e "$CRS_HOME/bin/crsctl" ]

One or maybe both checks should ensure the CRS_HOME is set to a proper value. The situation with the different entries is not fixed but can now be detected.
It’s hard to
automatically decide which is the correct one: the newest is not always the correct one.
I feel ok with the solution to exit the script directly. That‘s not possible for environment files, otherwise you can’t login anymore.
So I recommend to set the variable to an invalid value, with the same level of directories as the CRS_HOME , i.e. /u01/app/invalid/grid

export CRS_HOME=$(awk -F ':' ' $1 ~ /^+APX[0-9]$/ { print $2 }' /etc/oratab|sort -u)
[ -z "$CRS_HOME" ] && export CRS_HOME=$(awk -F ':' ' $1 ~ /^+ASM[0-9]$/ { print $2 }' /etc/oratab|sort -u)
if [ ! -d "$CRS_HOME" -o ! -e "$CRS_HOME/bin/crsctl" ]; then
export CRS_HOME="/u01/app/invalid/grid"
echo "CRS_HOME could not be determined and is set to $CRS_HOME" 1>&2

Another method I want to mention, because it’s the Oracle’s internal way..
has it hard coded in the startup script /etc/init.d/init.ohasd also

awk -F = ' $1 ~ /^ORA_CRS_HOME$/ {print $2} ' /etc/init.d/init.ohasd

work for me(tested on,,,, but it’s not described anywhere so it could be changed which each small patch.
If you want more you can implement this in the if part of the code, but I don’t trust this method.

the code is really small I uploaded it to gitlab to keep you aware of changes. DOWNLOAD
If you have seen some other entries or combination of entries which are not covered by my code slice it would be great, if you can send it to me 🙂

Founded Entries:
-only asm

+ASM1:/u01/app/ # line added by Agent

-asm and apx

+ASM1:/u01/app/ # line added by Agent
+APX1:/u01/app/ # line added by Agent
-only apx
+APX1:/u01/app/ # line added by Agent

– asm or apx twice

+ASM1:/u01/app/ # line added by Agent
+ASM2:/u01/app/ # line added by Agent

– 2 asm or 2 apx with diffrent versions

+ASM1:/u01/app/ # line added by Agent
+ASM2:/u01/app/ # line added by Agent

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s