From 2c59e26751b333939a15aff354d5e6adf554373c Mon Sep 17 00:00:00 2001
From: Erik Alexandre Pucci <eap08@c3sl.ufpr.br>
Date: Mon, 21 Feb 2011 11:03:02 -0300
Subject: [PATCH] disk.tree: Major update

New features and improvements:

- Added many comments discribing the code;
- Updated df(1) check;
- Added USB identification for the new UCA Classmates*;
- Added USB identification for the ProInfo Projectors prototypes*;
- Considered one USB as a disk if the computer does not have any disks, but
only mounted USB's (mostly usefull for netbooks).

* For finding the USB used to mount the system.

Signed-off-by: Erik Alexandre Pucci <eap08@c3sl.ufpr.br>
Acked-by: Renan Franca de Miranda <rfm08@c3sl.ufpr.br>
Signed-off-by: Josiney de Souza <josineys@c3sl.ufpr.br>
---
 scripts/collect/inventory/disk.tree | 85 ++++++++++++++++++++++++++---
 1 file changed, 78 insertions(+), 7 deletions(-)

diff --git a/scripts/collect/inventory/disk.tree b/scripts/collect/inventory/disk.tree
index 2b20ccc..071ba29 100755
--- a/scripts/collect/inventory/disk.tree
+++ b/scripts/collect/inventory/disk.tree
@@ -19,7 +19,8 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 # USA.
 
-if ! test -x $(which df); then
+# Check if df(1) exists before verifying the disks
+if ! which df &> /dev/null; then
   exit 2
 fi
 
@@ -28,47 +29,97 @@ number=0
 disks=""
 raid=false
 distro=$(cut -sd ' ' -f3,4 /etc/issue)
+
+# If this is a Classmate notebook, find the USB used to mount the root and save
+# it to storage variable
 if test "${distro}" = "Metasys ClassmatePC"; then
     label=$(grep "\/ " /etc/fstab | cut -d"=" -f2 | cut -d" " -f1)
-    classmateStorage=$(ls -l --time-style="+%F %R" /dev/disk/by-label |
-                       grep "$label" | cut -d"/" -f3 | sed 's/[0-9]//g')
+    storage=$(ls -l --time-style="+%F %R" /dev/disk/by-label |
+              grep "$label" | cut -d"/" -f3 | sed 's/[0-9]//g')
+elif test -f "/etc/cmpc-release"; then
+    storage=$(ls -l --time-style="+%F %R" /dev/disk/by-label |
+              grep "Mdv_Root" | cut -d"/" -f3 | sed 's/[0-9]//g')
+fi
+
+# If this is a ProInfo Projector, find the USB used to mount the system and
+# save it to storage variable
+if test -f "/usr/sbin/projetor"; then
+    storage=$(ls -l --time-style="+%F %R" /dev/disk/by-label | grep "conf" |
+              cut -d"/" -f3 | sed 's/[0-9]//g')
 fi
 
+# If the directory "/dev/disk/by-id/" exists, obtain the disks information
 if test -d "/dev/disk/by-id/"; then
+
+    # Find every USB connected to the computer
     for line in $(ls -l --time-style="+%F %R" /dev/disk/by-id/ | grep "usb" |
             awk '{print $10}' | grep "[a-z]$"; ls -l --time-style="+%F %R"\
             /dev/cdrom* 2> /dev/null | awk '{print $10}' | grep "[a-z]$"); do
-        if test "${distro}" != "Metasys ClassmatePC" -o \
-                "$(basename $line)" != "${classmateStorage}"; then
+        # If it's not the USB used as root by Classmate notebook, nor the USB
+        # used by the Projector, then add it to the list to avoid counting it as
+        # a disk
+        if test "$(basename $line)" != "${storage}"; then
             test -n "${massStorage}" && massStorage="${massStorage}|"
             massStorage="${massStorage}$(basename $line)"
         fi
     done
+
+    # If massStorage list isn't empty, add "-v" to grep parameters, to exclude
+    # every USB listed here from verification
     test -n "${massStorage}" && ParamGrep="-v"
+
+    # If there is only USB and no hard disk, remove the first USB found from the
+    # exclusion list and consider it a valid disk (for computers which use USB
+    # as root disk)
+    if test -z "$(ls -l --time-style="+%F %R" /dev/disk/by-id/ |
+            egrep ${ParamGrep} "$massStorage" | cut -d"/" -f3 |
+            grep "[a-z]$" | sort -u)" -a -z "${storage}"; then
+        if test -n "$(echo ${massStorage} | grep '|')"; then
+            massStorage="$(echo ${massStorage} | cut -d'|' -f2-)"
+        else
+            massStorage=""
+            ParamGrep=""
+        fi
+    fi
+
+    # If "/proc/mdstat" exists, than check for RAID disks
     test -f "/proc/mdstat" && raid=true
 
+    # Verify every disk in "/dev/disk/by-id/", excluding all USB's, unless it's
+    # the USB used as root by Classmate notebook
     for line in $(ls -l --time-style="+%F %R" /dev/disk/by-id/ |
             egrep ${ParamGrep} "$massStorage" | cut -d"/" -f3 |
             grep "[a-z]$" | sort -u); do
 
+        # Enumerate the disks and save the model of each one
         disks="${disks}<hdd${number}>"
         disks="${disks}<model value=\"$(ls -l --time-style="+%F %R"\
         /dev/disk/by-id/ | grep "$line$" | awk '{print $8}' | grep "ata" |
             cut -d"-" -f2)\" type=\"string\"/>"
 
+        # Multiply the number of blocks by their standard size
         diskSize=$(($(cat /sys/block/$line/size)*512))
 
-        # Size of disk in giga bytes.
+        # Size of disk in giga bytes
         disks="${disks}<size value=\"$((${diskSize}/1073741824
             ))\" type=\"int\"/>"
         diskUsed=0
 
-        # Check to RAID.
+        # RAID check
         if ${raid} && test -n "$(grep "raid" /proc/mdstat)"; then
+
+            # Check every disk in the RAID
             for dev in $(grep "${line}" /proc/mdstat | cut -d" " -f1); do
+
+                # Obtain the number of disks in the RAID array
                 numDisksArray=$(grep -T1 "${dev}" /proc/mdstat | grep "blocks" |
                     cut -d"[" -f2 | cut -d"/" -f1)
+
+                # Obtain the RAID type
                 type=$(grep  "${dev}" /proc/mdstat | cut -d" " -f4)
+
+                # If the disk is being used, obtain the amount used according to
+                # the RAID type
                 used=$(df -kPx swap | grep "$dev" | awk '{print $3}')
                 if test -n "${used}"; then
                     case ${type} in
@@ -87,12 +138,26 @@ if test -d "/dev/disk/by-id/"; then
                 fi
 
             done
+
+        # There is no RAID
         else
+            # If it's a Metasys Classmate notebook, $label will have the USB
+            # used as the system root
             if test "${distro}" = "Metasys ClassmatePC"; then
                 for used in $(df -kPx swap | grep "${label}" |
                               awk '{print $3}'); do
                     (( diskUsed=${diskUsed}+${used} ))
                 done
+
+            # If it's a Mandriva Classmate notebook, there is no label, so it
+            # makes a grep for the root mount point
+            elif test -f "/etc/cmpc-release"; then
+                for used in $(df -kPx swap | grep "/" | head -n 1 |
+                              awk '{print $3}'); do
+                    (( diskUsed=${diskUsed}+${used} ))
+                done
+
+            # Else, $line will have the disk name
             else
                 for used in $(df -kPx swap | grep "${line}" |
                               awk '{print $3}'); do
@@ -106,7 +171,13 @@ if test -d "/dev/disk/by-id/"; then
         disks="${disks}</hdd${number}>"
         (( number=${number}+1 ))
     done
+
+    # Print the disk quantity and the details of every disk found (except USB's,
+    # unless it's a Classmate notebook or there is no disk - i.e. the system is
+    # mounted upon an USB)
     printf "<hdd quantity=\"$number\">${disks}</hdd>\n"
+
+# There is no disk in the local system
 else
     printf "<hdd quantity=\"0\"/>\n"
 fi
-- 
GitLab