Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: Bash  |  size: 7.36 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2.  
  3. . /usr/pluto/bin/Config_Ops.sh
  4. . /usr/pluto/bin/SQL_Ops.sh
  5.  
  6. export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin
  7.  
  8. TPL_GENERIC_INTERNAL_DRIVE=1790
  9. DD_UUID=267
  10.  
  11. ## Function to remove undesired paths
  12. function substractPaths {
  13.         local availPath=$1
  14.         local unavailPath=$2
  15.         local retPath=""
  16.  
  17.         for aPath in $availPath;do
  18.                 available=1
  19.  
  20.                 for uPath in $unavailPath; do
  21.                         if [[ "$aPath" == "$uPath" ]]; then
  22.                                 available=0
  23.                         fi
  24.                 done
  25.  
  26.                 if [[ $available == 1 ]]; then
  27.                         retPath="$retPath $aPath"
  28.                 fi
  29.         done
  30.         echo $retPath
  31. }
  32.  
  33. function Detect {
  34.         ## Available Paths
  35.         availPath=$(ls /dev/disk/by-path)
  36.  
  37.         ## Removes drive paths that aren't storage drives
  38.         Hdds=""
  39.         for Drive in $availPath; do
  40.                 drivePathDevName=$(udevadm info --query=all --name="/dev/disk/by-path/$Drive" | grep 'N:' | awk '{ print $2 }')
  41.                 confirmDrive=$(echo $drivePathDevName | grep -E 'sd|hd|xd|md' | awk '{ print $1 }')
  42.                 if [[ "$confirmDrive" == "" ]]; then continue; fi
  43.  
  44.                 Hdds="$Hdds $Drive"
  45.         done
  46.         availPath=$Hdds
  47.  
  48.         ## Looks for standard device names mounted in mtab and learns path. Note some mounted drives show only UUID
  49.         mountedDevName=$(cat /etc/mtab | awk '/dev\/(sd|hd|md|xd)./ {print $1}')
  50.         if [ "$mountedDevName" != "" ]; then mountedDevPath=$(udevadm info --query=all --name="$mountedDevName" | grep 'S: disk/by-path/' | sed 's,disk/by-path/,,g' | awk '{ print $2 }'); fi
  51.  
  52.         ## Grabs UUID of mounted devices in mtab and learns path. Note some mounted drives only show device names.
  53.         mountedUuid=$(cat /etc/mtab | awk '/uuid/ { print $1 }' | sed 's,/dev/disk/by-uuid/,,g' | sort -u)
  54.         mountedPath=$(udevadm info --query=all --name="/dev/disk/by-uuid/$mountedUuid" | grep 'S: disk/by-path/' | sed 's,disk/by-path/,,g' | awk '{ print $2 }')
  55.  
  56.         ## Remove the mounted paths
  57.         availPath=$(substractPaths "$availPath" "$mountedPath")
  58.         availPath=$(substractPaths "$availPath" "$mountedDevPath")
  59.  
  60.         ## Remove partitions if they're alias is mounted
  61.         for Path in $availPath; do
  62.                 for path_alias in $(udevadm info --query=symlink --name="/dev/disk/by-path/$Path" | awk '{ print $4 }' | sed 's,disk/by-uuid/,,g'); do
  63.                         for alias_mounted in $(mount | awk '/dev\/disk\// {print $1}' | sed 's/\/dev\///g'); do
  64.                                 if [[ "$path_alias" == "$alias_mounted" ]]; then
  65.                                         availPath=$(substractPaths "$availPath" "$Path")
  66.                                 fi
  67.                         done
  68.                 done
  69.         done
  70.  
  71.         ## Remove unmountable path additions (swap/extended/boot sector)
  72.         auxPath=""
  73.         for Path in $availPath; do
  74.  
  75.                 ## If is extended partition
  76.                 if file -sL /dev/disk/by-path/$Path | grep -q "extended partition table" ; then
  77.                         continue
  78.                 fi
  79.  
  80.                 ## If is swap partition
  81.                 if file -sL /dev/disk/by-path/$Path | grep -q "swap file" ; then
  82.                         continue
  83.                 fi
  84.                 ## If is boot sector
  85.                 if file -sL /dev/disk/by-path/$Path | grep -q "boot sector;" ; then
  86.                         continue
  87.                 fi
  88.  
  89.                 auxPath="$auxPath $Path"
  90.         done
  91.         availPath=$auxPath
  92.  
  93.         ## Remove paths that belong to a mounted RAID
  94.         if [[ -x /sbin/mdadm ]]; then
  95.                 auxPath=""
  96.                 for Path in $availPath; do
  97.                         if [[ "$(mdadm --examine /dev/disk/by-path/$Path 2>&1)" == *"No md superblock"* ]]; then
  98.                                 auxPath="$auxPath $Path"
  99.                         fi
  100.                 done
  101.                 availPath="$auxPath"
  102.         fi
  103.  
  104.         ## Test to see if we found any available paths
  105.         if [[ "$availPath" != "" ]]; then
  106.  
  107.                 for pathPosition in $availPath; do
  108.                         ## I assume someone left this non working MessageSend line in for reference, and have done the same.
  109.                         #/usr/pluto/bin/MessageSend $DCERouter 0 $OrbiterIDList 1 741 159 228 109 "[$pathPosition]" 156 $PK_Device 163 "$InfoMessage"
  110.  
  111.                         ## Get info about this volume
  112.                         partition_diskname=$(udevadm info --query=all --name="/dev/disk/by-path/$pathPosition" | grep 'ID_MODEL=' | cut -d'=' -f2)
  113.                         partition_serial=$(udevadm info --query=all --name="/dev/disk/by-path/$pathPosition" | grep 'ID_SERIAL_SHORT=' | cut -d'=' -f2)
  114.                         partition_label=$(udevadm info --query=all --name="/dev/disk/by-path/$pathPosition" | grep 'ID_FS_LABEL=' | cut -d'=' -f2)
  115.                         partition_uuid=$(udevadm info --query=all --name="/dev/disk/by-path/$pathPosition" | grep 'ID_FS_UUID=' | cut -d'=' -f2)
  116.                         partition=$(udevadm info --query=all --name="/dev/disk/by-path/$pathPosition" | grep 'N:' | awk '{ print $2 }')
  117.                        
  118.                         ## fallback to serial number if no UUID available (for instance FAT devices)
  119.                         if [[ "$partition_uuid" == "" && "$partition_serial" != "" ]]; then
  120.                                 partition_uuid="$partition_serial"
  121.                         fi
  122.  
  123.                         ## fallback to volume label if no UUID or serial number available
  124.                         if [[ "$partition_uuid" == "" && "$partition_label" != "" ]]; then
  125.                                 partition_uuid="$partition_label"
  126.                         fi
  127.  
  128.                         if [[ "$partition_uuid" == "" ]]; then
  129.                                 continue
  130.                         fi
  131.  
  132.                         ## Mount the partition to get it's size
  133.                         mountpoint_temp=$(mktemp -d)
  134.  
  135.                         mount -o ro "/dev/$partition" "$mountpoint_temp"
  136.  
  137.                         partition_size=$(df -h "/dev/$partition" | tail -n +2 | awk '{ print $2 }')
  138.                         umount -lf "$mountpoint_temp";  rmdir "$mountpoint_temp"
  139.  
  140.                         ## Sends data to MessageSend
  141.                         thisHost=$(hostname)
  142.                         Sent="false"
  143.                         Count=0
  144.                         while [[ "$Sent" == "false" ]]; do
  145.                                 /usr/pluto/bin/MessageSend $DCERouter $PK_Device -1001 2 65 55 "$DD_UUID|$partition_uuid|277|$partition_size" 54 "$partition_uuid" 52 8 49 1790 13 "$partition_size $partition_diskname [$partition] on $thisHost"
  146.                                 err=$?
  147.  
  148.                                 if [[ "$err" == "0" ]]; then
  149.                                         Sent="true"
  150.                                 else
  151.                                         Count=$(( Count + 1 ))
  152.                                         sleep 2
  153.                                 fi
  154.  
  155.                                 if [[ "$Count" == "10" ]]; then
  156.                                         Sent="true"
  157.                                 fi
  158.                         done
  159.                 done
  160.         fi
  161. }
  162.  
  163. Detect