LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Closed Thread
  Search this Thread
Old 05-26-2011, 11:41 AM   #1
dimm0k
Member
 
Registered: May 2008
Location: Brooklyn ZOO
Distribution: Slackware64 14.2
Posts: 496

Rep: Reputation: 49
Help creating a suspend/hibernate script to handle USB HDDs


Currently running Slackware 13.37 64-bit on a notebook and finally have suspend/hibernate after realizing that USB devices, especially USB HDDs, need to be disconnected before suspend/hibernate can work. Problem is I have 2 USB HDDs that are connected to my notebook whenever the notebook is stationary for the extra storage so I'd like to create a script that would get invoked that would stop the suspend/hibernate process if certain partitions are mounted. I know what I would like to accomplish, but I have basic scripting knowledge so I was hoping to get some assistance.

1. script would basically store a user specified string containing devices that are non-USB, ie: NONUSB="/dev/sda /dev/sdb"

2. possibly use /etc/mtab to get a list of what is currently mounted and then remove lines containing whatever is specified in $NONUSB and store those values in $USB

3. run a for loop that executes 'umount' on each token in $USB
3a. stop suspend/hibernate process if 'umount' fails at any point
3b. if 'umount' passes then suspend/hibernate

Is this possible?
 
Old 05-26-2011, 01:01 PM   #2
dimm0k
Member
 
Registered: May 2008
Location: Brooklyn ZOO
Distribution: Slackware64 14.2
Posts: 496

Original Poster
Rep: Reputation: 49
Ok, so so far I have the following which lets a user specify devices that are not USB and removes those specific entries from /etc/mtab, storing them in /tmp/mountedusb. From that file the script issues the umount command to unmount all the entries in /tmp/mountedusb. The script gives an "exit 1" to abort suspend/hibernation if umount fails along the way. Is it possible to avoid the use/creation of /tmp/mountedusb and have everything done by passing things in memory? Anything else I might be missing that I should include to make it a little more robust?

Code:
#!/bin/sh
# Original ehci_hcd removal script provided by http://math.umons.ac.be/an/W510/

# devices that are known to not be USB
NONUSB="/dev/sda /dev/sdc"
DEVICES="0000:00:1a.0 0000:00:1d.0"

case "${1}" in
  hibernate|suspend)
    # writes to disk
    /bin/sync

    # extract first column of /etc/mtab, remove everything that is not /dev/sd?
    cat /etc/mtab | cut -d" " -f1 | grep /dev/sd > /tmp/mountedusb

    # remove devices listed in NONUSB, essentially creating a file that contains only
    # USB/removable devices
    for nu in $NONUSB; do
      cat /tmp/mountedusb | grep -v $nu > /tmp/mountedusb.tmp
      mv -f /tmp/mountedusb.tmp /tmp/mountedusb
    done

    # unmount devices, abort suspend if umount fails
    for um in `cat /tmp/mountedusb`; do
      /bin/umount $um 2> /dev/null
      if [ $? -gt 0 ]; then
        exit 1
      fi
    done

    # Unbind ehci_hcd for all devices
    for d in $DEVICES; do
      echo -n "$d" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
    done
    ;;
  resume|thaw)
    # Bind ehci_hcd for all devices
    for d in $DEVICES; do
      echo -n "$d" | tee /sys/bus/pci/drivers/ehci_hcd/bind
    done
    ;;
esac

Last edited by dimm0k; 05-26-2011 at 02:22 PM.
 
Old 05-26-2011, 03:51 PM   #3
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 473Reputation: 473Reputation: 473Reputation: 473Reputation: 473
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help creating a suspend/hibernate script to handle USB HDDs dimm0k Slackware 1 05-26-2011 01:01 PM
[SOLVED] Can't suspend or hibernate 0zMe Slackware 5 06-27-2010 01:43 PM
Need to suspend or hibernate fletcher08 Linux - Newbie 1 04-18-2010 10:24 AM
Boot from USB key, does suspend or hibernate work? paulsiu Linux - Hardware 1 05-02-2008 02:34 PM
Possible Suspend/HIbernate fix angryfirelord Linux - Hardware 0 07-20-2007 06:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration