Wednesday, February 6, 2019

Oracle ASM : Shell script to map physical disk devices to ASMLIB disks

Oracle ASM : Shell script to map physical disk devices to ASMLIB disks

ASMLIB is a support library for the Automatic Storage Management (ASM) feature of the Oracle Database and is available for the Linux operating system. When ASMLIB is used to manage ASM disks, the device path information is not presented in GV$ASM_DISK.PATH. This post shows how to get the physical disk device name associated with the ASMLIB disk name.

Finding asmlib disk name and asm disk name

By default when you scan disks with oracleasm scandisks, the corresponding disk device files (ASMLIB disk names) are stored into the path /dev/oracleasm/disks. For example :
# ls -lrt /dev/oracleasm/disks
total 0
brw-rw---- 1 oracle oinstall 201, 40112 Oct  3 11:08 REDO_1
brw-rw---- 1 oracle oinstall 201, 31232 Oct  3 11:08 REDO_2
brw-rw---- 1 oracle oinstall 201, 15408 Oct  3 11:09 DATA_1
brw-rw---- 1 oracle oinstall 201,  6336 Oct  3 11:09 DATA_2
brw-rw---- 1 oracle oinstall 201, 62432 Oct  3 11:09 DATA_3
...
You can use the the ASM utility kfed, to read the disk headers and get the disk name that is being used in the ASM disk group. for example :
export ORACLE_HOME=[grid home]
# cd $ORACLE_HOME/bin
# ./kfed read /dev/dm-17
....
kfdhdb.dsknum:                        0 ; 0x024: 0x0000
kfdhdb.grptyp:                        1 ; 0x026: KFDGTP_EXTERNAL
kfdhdb.hdrsts:                        3 ; 0x027: KFDHDR_MEMBER
kfdhdb.dskname:           DATADG_DISK01 ; 0x028: length=19
kfdhdb.grpname:                  DATADG ; 0x048: length=14
kfdhdb.fgname:            DATADG_DISK01 ; 0x068: length=19
....
As shown above, we can grep for dskname, to get the ASM disk name.

Finding physical disk device

From the output of command “ls -lrt /dev/oracleasm/disks” we get the major and minor number which can be used to find the physical device corresponding to the asm disk. for example :
# ls -lrt /dev/oracleasm/disks/DATA_1
brw-rw---- 1 oracle oinstall 201, 15408 Oct  3 11:09 DATA_1
From above output grep the major,minor numbers – 201, 15408 in the directory /dev to get the physical devices.
# ls -lrt /dev/ | grep "201,15408"
brw------- 1 root root 201,  15408 Oct  3 10:48 dm-17

script to map ASM disk to physical disk

Here is a small script from oracle (I modified it a little to include path of kfed command). The script can be run as root or any other user (oracle or grid).
#!/bin/bash
## ASMLIB_DISK  -- disk name in ASMLIB
## ASM_DISK -- disk name in ASM
## DEVICE -- physical disk name
GRID_HOME=`cat /etc/oratab  | grep ^+ASM | awk -F":" '{print $2}'`
for ASMLIB_DISK in `ls /dev/oracleasm/disks/*`
  do
    ASM_DISK=`$GRID_HOME/bin/kfed read $ASMLIB_DISK | grep dskname | tr -s ' '| cut -f2 -d' '`
    majorminor=`ls -l $ASMLIB_DISK | tr -s ' ' | cut -f5,6 -d' '`
    device=`ls -l /dev/ | tr -s ' ' | grep -w "$majorminor" | cut -f10 -d' '`
    echo "ASMLIB disk name : $ASMLIB_DISK"
    echo "ASM_DISK name : $ASM_DISK"
    echo "Physical disk device : /dev/$device"
done
Sample output from above script :
# ./asm_device_mapping.sh
ASMLIB disk name : DATA_01
ASM_DISK name : DATA_01
Physical disk device : /dev/dm-6476
ASMLIB disk name : DATA_02
ASM_DISK name : DATA_02
Physical disk device : /dev/dm-6473
...

script to find deleted ASMLIB disk

If an ASMLIB disk was already deleted, it will not show in /dev/oracleasm/disks. We can check for devices that are (or were) associated with ASM(LIB) with the following shell script:
#!/bin/bash
GRID_HOME=`cat /etc/oratab  | grep ^+ASM | awk -F":" '{print $2}'`
for device in `ls /dev/sd*`
  do
    asmdisk=`$GRID_HOME/bin/kfed read $device | grep ORCL | tr -s ' ' | cut -f2 -d' ' | cut -c1-4`
    if [ "$asmdisk" = "ORCL" ]
      then
      echo "Disk device $device may be an ASM disk"
    fi
done
The scripts takes a peek at sd devices in /dev. The script needs to be run as privileged user. Of course we can look at /dev/dm*, /dev/mapper, etc or all devices in /dev, although that may not be a good idea. Sample kfed read on a ASM disk :
./kfed read /dev/dm-7 | grep ORCL
kfdhdb.driver.provstr:ORCLDISKTESTDB_ARCH_08 ; 0x000: length=23
Note : The kfed binary should be available in RDBMS home (prior to version 11.2) and in the Grid Infrastructure home (in version 11.2 and later). For the purpose of this post I have assumed Grid Infrastructure version to be 11.2 or higher.
The same can be achieved without kfed with a script like this:
#!/bin/bash
for device in `ls /dev/sd*`
do
  asmdisk=`od -c $device | head | grep 0000040 | tr -d ' ' | cut -c8-11`
  if [ "$asmdisk" = "ORCL" ]
    then
    echo "Disk device $device may be an ASM disk"
  fi
done
the od command dumps the files in octal and other formats. -c option is to select ASCII characters or backslash escapes.

No comments:

Post a Comment

  How to Change Instance Type & Security Group of EC2 in AWS By David Taylor Updated April 29, 2023 EC2 stands for Elastic Compute Cloud...