From fd00440460d6d5300d23fc5fafb0fccd18791f30 Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Thu, 12 Feb 2015 21:55:59 +0000
Subject: LinHES-system: fstrim.cron: trim SSDs weekly

checkXFSfrag.sh: skip defrag on SSDs
---
 abs/core/LinHES-system/PKGBUILD        | 10 ++++++----
 abs/core/LinHES-system/checkXFSfrag.sh | 22 +++++++++++++++-------
 abs/core/LinHES-system/fstrim.cron     |  4 ++++
 abs/core/LinHES-system/xfs_defrag.cron |  2 +-
 4 files changed, 26 insertions(+), 12 deletions(-)
 create mode 100755 abs/core/LinHES-system/fstrim.cron

diff --git a/abs/core/LinHES-system/PKGBUILD b/abs/core/LinHES-system/PKGBUILD
index c7e3f5d..914ce85 100755
--- a/abs/core/LinHES-system/PKGBUILD
+++ b/abs/core/LinHES-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-system
 pkgver=8.3
-pkgrel=7
+pkgrel=8
 arch=('i686' 'x86_64')
 install=system.install
 pkgdesc="Everything that makes LinHES an automated system"
@@ -28,7 +28,7 @@ binfiles="LinHES-start optimize_mythdb.py myth_mtc.py
  mythwelcome-config.py mythwelcome-set-alarm.sh mythwelcome-test-wakeup.sh"
 
 source=(LinHES-session LinHES-profile.sh $binfiles
-    alsa-base cacheclean.cron myth_mtc.cron xfs_defrag.cron etc_care.cron
+    alsa-base cacheclean.cron fstrim.cron myth_mtc.cron xfs_defrag.cron etc_care.cron
     readme_is_xml readme_light add_storage.readme
     system-sudo.rules msg.cfg)
 
@@ -62,6 +62,7 @@ package() {
     #install -m755 -D $srcdir/smolt.cron $pkgdir/etc/cron.weekly/smolt.cron
     install -m755 -D $srcdir/cacheclean.cron $pkgdir/etc/cron.weekly/cacheclean
     install -m755 -D $srcdir/xfs_defrag.cron $pkgdir/etc/cron.weekly/xfs_defrag
+    install -m755 -D $srcdir/fstrim.cron $pkgdir/etc/cron.weekly/fstrim
     install -m755 -D $srcdir/etc_care.cron $pkgdir/etc/cron.weekly/etc_care
     install -m755 -D $srcdir/myth_mtc.cron $pkgdir/etc/cron.hourly/myth_mtc
 
@@ -118,7 +119,7 @@ md5sums=('8415c4b2587a8a1e6a312484c1edafcb'
          'bacc813b48bafcc6fe906e5969930501'
          '8e02efe1ad0df9a179075147eebb05b9'
          '8b0298f70f97cc1dc2a58b9a73c64bd3'
-         '911b0fbc8d9178dac1a193346c9decaf'
+         '716710e0db42147b079c9ec1c7080efa'
          '34fc1f58ad1eabf4eff4979d420760c0'
          '69ecfe682f1b61c2c4c95d890b9f523c'
          'a94fe6d980f4b810f2e2ae5352084b39'
@@ -132,8 +133,9 @@ md5sums=('8415c4b2587a8a1e6a312484c1edafcb'
          '410795ef9039e4c6c0484e706ecfd567'
          'eb879fee9603a05d5420d4ce8ed9e450'
          '02c810c2f47b7c4495fdacaf54189473'
+         '1aac1872bf9569ea58ceb2f6f766a654'
          'dc48a53d4e5fe27b218657ccc318d07f'
-         '474d5c6883fcfffae4f199aceb3b1356'
+         '4d7164800122243be6513b29c8fc498e'
          'd14fdc38878c70011754317b0f2ea59e'
          '36bdfa8d877cea20ca3870faf08e08d5'
          '59d0602bac7e06f11abba1894acc8bb0'
diff --git a/abs/core/LinHES-system/checkXFSfrag.sh b/abs/core/LinHES-system/checkXFSfrag.sh
index 99b833c..74b73c4 100755
--- a/abs/core/LinHES-system/checkXFSfrag.sh
+++ b/abs/core/LinHES-system/checkXFSfrag.sh
@@ -28,24 +28,32 @@
 # DO NOT ALTER HEADER FROM THIS LINE UP.
 #
 e='/bin/echo -e'                                # Use the echo command, not built-in.
-xfsfsr=/usr/sbin/xfs_fsr                            # Set variable with the path to xfs_fsr.
-xfsdb=/usr/sbin/xfs_db                              # Set variable with the path to xfs_db.
+xfsfsr=/usr/bin/xfs_fsr                            # Set variable with the path to xfs_fsr.
+xfsdb=/usr/bin/xfs_db                              # Set variable with the path to xfs_db.
 pctmax=15                                        # Set maxiumum frag percent needed for defrag.
                                                 # This is zero here for testing purposes only
                                                 # a higher number should be used in production.
 array=`df -T|grep xfs|cut -f 1 --delim=" "`     # Array of all XFS file systems.
 for i in ${array[@]};
 do
-        percentage=`$xfsdb -c frag -r ${i}|cut -f 7 --delim=" "`
-        percent2=`$e $percentage|cut -f 1 --delim=.`
-        if [ "$percent2" -gt "$pctmax" ]
+        #check that the device is SATA and skip defrag on SSDs
+        device=`echo ${i} | cut -f 3 --delim="/" | sed 's/[0-9]//g'`
+        isSATA=`cat /sys/block/${device}/queue/rotational`
+        if [[ $isSATA -eq 1 ]]
         then
-                $e "${i} is $percentage fragmented. Running defragment on ${i}."
+            percentage=`$xfsdb -c frag -r ${i}|cut -f 7 --delim=" "`
+            percent2=`$e $percentage|cut -f 1 --delim=.`
+            if [ "$percent2" -gt "$pctmax" ]
+            then
+                $e "${i} is $percentage% fragmented. Running defragment on ${i}."
                 # Only uncomment one of the following two lines.
                 #$xfsfsr -v ${i}        # Uncomment for verbose defrag.
                 $xfsfsr ${i}           # Uncomment for quiet defrag.
+            else
+                $e "${i} is $percent2% fragmented and is below the fragmentation threshold of $pctmax%. Skipping."
+            fi
         else
-                $e "${i}\t$percent2 is not above fragmentation threshold of $pctmax."
+            echo "${i} is an SSD. Skipping."
         fi
 done
 exit 0
diff --git a/abs/core/LinHES-system/fstrim.cron b/abs/core/LinHES-system/fstrim.cron
new file mode 100755
index 0000000..ad0b4aa
--- /dev/null
+++ b/abs/core/LinHES-system/fstrim.cron
@@ -0,0 +1,4 @@
+#!/bin/bash
+#. /etc/profile
+/usr/bin/fstrim -av | /usr/bin/logger -t fstrim
+
diff --git a/abs/core/LinHES-system/xfs_defrag.cron b/abs/core/LinHES-system/xfs_defrag.cron
index 4d35507..e47176b 100755
--- a/abs/core/LinHES-system/xfs_defrag.cron
+++ b/abs/core/LinHES-system/xfs_defrag.cron
@@ -1,3 +1,3 @@
 #!/bin/bash
 #. /etc/profile
-/usr/LH/bin/checkXFSfrag.sh > /var/log/xfs_defrag.log
+/usr/LH/bin/checkXFSfrag.sh | /usr/bin/logger -t xfs_defrag
-- 
cgit v0.12

