Loading ...
Sorry, an error occurred while loading the content.

scsi-stop and cron-job?

Expand Messages
  • Mai Kee Reis
    Can anybody please give me a hint how to spin down my HDD if not used a while? My system is a NSLU2 with SlugOS 5.3 LE. The following does work fine, if
    Message 1 of 4 , Sep 22 10:02 AM
    • 0 Attachment
      Can anybody please give me a hint how to spin down my HDD if not used a while?
      My system is a NSLU2 with SlugOS 5.3 LE.

      The following does work fine, if invoked manually:
      /opt/sbin/scsi-stop /dev/sdb

      So I think about using cron - good idea?

      Or will this cause trouble to attached processes? (Samba,...)
      I'm not shure, but I think I saw a solution with crontab + some
      kind of disk statistics, but can't google or remember anymore

      (invoking '/opt/sbin/scsi-idle /dev/sdb 180' does not do the job
      as it shall by documentation)

      Greetings,
      Mai Kee
    • Thomas Reitmayr
      Hi Mai Kee, you can read /sys/block/ /stat (with corresponding to your HDD, eg. sdb) and look for changes. After a certain timeout you could issue
      Message 2 of 4 , Sep 22 10:58 AM
      • 0 Attachment
        Hi Mai Kee,
        you can read /sys/block/<dev>/stat (with <dev> corresponding to your
        HDD, eg. sdb) and look for changes. After a certain timeout you could
        issue your scsi-stop command.
        Altough my disk on a TS-109 could spin down automatically, I use such a
        script to be able to take other aspects into account - the disk will
        never spin down as long as I have one of its NFS shares mounted from my
        PC.

        That's my script:

        =========================================
        #!/bin/sh

        #DEV=sdb
        DEV=`blkid -t LABEL=MEDIA | sed 's/[0-9].*//'`
        DEV=${DEV#/dev/}
        STATFILE=/sys/block/$DEV/stat
        RMTAB="/var/lib/nfs/rmtab"
        let 'CYCLES=10*60/5'
        LOGFILE=/var/log/spindown.log
        state=active

        rw=0
        count=0
        echo "" >> $LOGFILE
        date "+%D %T - start" >> $LOGFILE
        while true ; do
        rw_old=$rw
        read reads a2 a3 a4 writes rest < $STATFILE
        let "rw=$reads+$writes"
        if [ "$rw" != "$rw_old" -o -s "$RMTAB" ] ; then
        count=0
        state=active
        lastacc="`date '+%D %T - idle'`"
        else
        if [ "$state" = "active" ] ; then
        let "count=$count+1"
        fi
        fi
        if [ "$state" = "active" -a $count -gt $CYCLES ] ; then
        echo "$lastacc" >> $LOGFILE
        date "+%D %T - spindown" >> $LOGFILE
        hdparm -Y /dev/$DEV
        state=standby
        fi
        sleep 5
        done
        =========================================

        You will probably need to tweak it a bit, eg. set DEV=sdb or change the
        disk LABEL, remove the code watching $RMTAB, or maybe do not write a log
        file (especially if your /var/log directory is not located on a ram
        disk). And of course "hdparm -Y" should be your scsi-stop command.
        The script will wait for 10 minutes of inactivity, set via CYCLES.

        Hope that helps.
        -Thomas


        Am Dienstag, den 22.09.2009, 19:02 +0200 schrieb Mai Kee Reis:
        > Can anybody please give me a hint how to spin down my HDD if not used a while?
        > My system is a NSLU2 with SlugOS 5.3 LE.
        >
        > The following does work fine, if invoked manually:
        > /opt/sbin/scsi-stop /dev/sdb
        >
        > So I think about using cron - good idea?
        >
        > Or will this cause trouble to attached processes? (Samba,...)
        > I'm not shure, but I think I saw a solution with crontab + some
        > kind of disk statistics, but can't google or remember anymore
        >
        > (invoking '/opt/sbin/scsi-idle /dev/sdb 180' does not do the job
        > as it shall by documentation)
        >
        > Greetings,
        > Mai Kee
        >
        >
        >
        >
        >
        > ------------------------------------
        >
        > Yahoo! Groups Links
        >
        >
        >
        >
      • Mai Kee Reis
        Hi Thomas, your script helps a lot, I think! Need to learn sh syntax again ;-) but i like it. Some small questions: Do you know what rmtab counterpart for
        Message 3 of 4 , Sep 23 2:17 PM
        • 0 Attachment
          Hi Thomas,

          your script helps a lot, I think!
          Need to learn sh syntax again ;-) but i like it.
          Some small questions:
          Do you know what rmtab counterpart for SAMBA is?
          Did I understand right, you don't evaluate the in-que-read/write operations?
          why can you use hdparm? AFAIK, the slug has no IDE port, and hdparm does
          not work via USB, as I read here:
          <http://www.nslu2-linux.org/wiki/FAQ/SpinDownUSBHarddisks#method1>

          with regards & respect,
          Mai Kee

          Thomas Reitmayr wrote:
          > Hi Mai Kee,
          > you can read /sys/block/<dev>/stat (with <dev> corresponding to your
          > HDD, eg. sdb) and look for changes. After a certain timeout you could
          > issue your scsi-stop command.
          > Altough my disk on a TS-109 could spin down automatically, I use such a
          > script to be able to take other aspects into account - the disk will
          > never spin down as long as I have one of its NFS shares mounted from my
          > PC.
          >
          > That's my script:
          >
          > =========================================
          > #!/bin/sh
          >
          > #DEV=sdb
          > DEV=`blkid -t LABEL=MEDIA | sed 's/[0-9].*//'`
          > DEV=${DEV#/dev/}
          > STATFILE=/sys/block/$DEV/stat
          > RMTAB="/var/lib/nfs/rmtab"
          > let 'CYCLES=10*60/5'
          > LOGFILE=/var/log/spindown.log
          > state=active
          >
          > rw=0
          > count=0
          > echo "" >> $LOGFILE
          > date "+%D %T - start" >> $LOGFILE
          > while true ; do
          > rw_old=$rw
          > read reads a2 a3 a4 writes rest < $STATFILE
          > let "rw=$reads+$writes"
          > if [ "$rw" != "$rw_old" -o -s "$RMTAB" ] ; then
          > count=0
          > state=active
          > lastacc="`date '+%D %T - idle'`"
          > else
          > if [ "$state" = "active" ] ; then
          > let "count=$count+1"
          > fi
          > fi
          > if [ "$state" = "active" -a $count -gt $CYCLES ] ; then
          > echo "$lastacc" >> $LOGFILE
          > date "+%D %T - spindown" >> $LOGFILE
          > hdparm -Y /dev/$DEV
          > state=standby
          > fi
          > sleep 5
          > done
          > =========================================
          >
          > You will probably need to tweak it a bit, eg. set DEV=sdb or change the
          > disk LABEL, remove the code watching $RMTAB, or maybe do not write a log
          > file (especially if your /var/log directory is not located on a ram
          > disk). And of course "hdparm -Y" should be your scsi-stop command.
          > The script will wait for 10 minutes of inactivity, set via CYCLES.
          >
          > Hope that helps.
          > -Thomas
          >
          >
          > Am Dienstag, den 22.09.2009, 19:02 +0200 schrieb Mai Kee Reis:
          >
          >> Can anybody please give me a hint how to spin down my HDD if not used a while?
          >> My system is a NSLU2 with SlugOS 5.3 LE.
          >>
          >> The following does work fine, if invoked manually:
          >> /opt/sbin/scsi-stop /dev/sdb
          >>
          >> So I think about using cron - good idea?
          >>
          >> Or will this cause trouble to attached processes? (Samba,...)
          >> I'm not shure, but I think I saw a solution with crontab + some
          >> kind of disk statistics, but can't google or remember anymore
          >>
          >> (invoking '/opt/sbin/scsi-idle /dev/sdb 180' does not do the job
          >> as it shall by documentation)
          >>
          >> Greetings,
          >> Mai Kee
          >>
          >>
          >>
          >>
          >>
          >> ------------------------------------
          >>
          >> Yahoo! Groups Links
          >>
          >>
          >>
          >>
          >>
          >
          >
          >
        • Thomas Reitmayr
          Hi Mai Kee, it is the script I use on my QNAP TS-109, which has its disk(s) attached via SATA, so for the NSLU2 you would just replace hdparm with scsi-stop. I
          Message 4 of 4 , Sep 23 4:10 PM
          • 0 Attachment
            Hi Mai Kee,
            it is the script I use on my QNAP TS-109, which has its disk(s) attached
            via SATA, so for the NSLU2 you would just replace hdparm with scsi-stop.

            I am not 100% sure what you mean with "in-que-read/write operations".
            The parameters from /sys/block/$DEV/stat used in the script are the
            number of completed reads and writes
            (see /usr/src/linux/Documentation/iostats.txt). However you could
            actually simplify this by just watching for _any_ change
            in /sys/block/$DEV/stat, as shown in the following stripped down version
            of the script (untested!):

            #!/bin/sh

            DEV=sdb
            TIMEOUTMIN=10
            TICKSEC=10
            let "CYCLES=$TIMEOUTMIN*60/$TICKSEC"

            state=active
            stat=""
            count=0
            while true ; do
            stat_old=$stat
            stat=`cat /sys/block/$DEV/stat`
            if [ "$stat" != "$stat_old" ] ; then
            # disk activity detected
            count=0
            state=active
            else
            [ "$state" = "active" ] && let count++
            fi
            if [ "$state" = "active" -a $count -gt $CYCLES ] ; then
            # spin down
            /opt/sbin/scsi-stop /dev/$DEV
            state=standby
            fi
            sleep $TICKSEC
            done

            Regarding samba, you could watch the client and/or "Locked files"
            entries returned by the tool 'smbstatus' and only spin down the disk if
            there are no such entries (if that's the behavior you desire). However I
            am not that familiar with samba, so this might not cover all your use
            cases...

            Best regards,
            -Thomas


            Am Mittwoch, den 23.09.2009, 23:17 +0200 schrieb Mai Kee Reis:
            > Hi Thomas,
            >
            > your script helps a lot, I think!
            > Need to learn sh syntax again ;-) but i like it.
            > Some small questions:
            > Do you know what rmtab counterpart for SAMBA is?
            > Did I understand right, you don't evaluate the in-que-read/write operations?
            > why can you use hdparm? AFAIK, the slug has no IDE port, and hdparm does
            > not work via USB, as I read here:
            > <http://www.nslu2-linux.org/wiki/FAQ/SpinDownUSBHarddisks#method1>
            >
            > with regards & respect,
            > Mai Kee
          Your message has been successfully submitted and would be delivered to recipients shortly.