Automount stops working after resume

Asked by Collin Stocks

Sometimes when I suspend my computer and then resume, the automount program seems to stop working. I put a usb drive into the port, and it does not mount automatically. However, I can see that the device was added to /dev/disk/by-id, by-path, by-uuid, etc.

I came up with a little python hack to get it to work, but it sometimes does not get the permissions right, and the disk may be unwritable (from my user account). I will post the hack in the following post, in case anyone else might find it useful.

I am running Ubuntu Feisty 7.04 on a Dell Inspiron 6000 with a pentium m processor.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Collin Stocks
Solved:
Last query:
Last reply:
Revision history for this message
Collin Stocks (collinstocks) said :
#1

#!/usr/bin/python

import os
from time import sleep

# before we do any work, check if we are root
if not os.getuid()==0: # root user id
 os.system("sudo %s"%__file__)
 raise SystemExit

devs=os.listdir("/dev/disk/by-id")

while True:
 sleep(1)
 new_devs=os.listdir("/dev/disk/by-id")
 if not devs==new_devs:
  if len(devs)<len(new_devs):
   print "device added!"
   for dev in new_devs:
    if dev in devs:
     continue
    if dev.lower().startswith("usb"):
     if dev.lower()[:-1].endswith("part"):
      mountpath="/media/%s"%dev
      print "mount \"/dev/disk/by-id/%s\" \"%s\""%(dev,mountpath)
      os.mkdir(mountpath)
      os.chmod(mountpath,600644)
      os.system("mount \"/dev/disk/by-id/%s\" \"%s\""%(dev,mountpath))
   devs=new_devs
  elif len(new_devs)<len(devs):
   print "device removed!"
   for dev in devs:
    if dev in new_devs:
     continue
    if dev.lower().startswith("usb"):
     if dev.lower()[:-1].endswith("part"):
      mountpath="/media/%s"%dev
      try:
       os.rmdir(mountpath)
      except OSError:
       print "could not remove directory!"
   devs=new_devs

Revision history for this message
Collin Stocks (collinstocks) said :
#2

This hack only works for usb drives, but about that is really all I care. And sorry for the poor indents: launchpad changed tabs to spaces. At least there are indents, though.

Revision history for this message
Collin Stocks (collinstocks) said :
#3

Just as an edit to the hack, replace os.chmod(mountpath,600644) with os.chmod(mountpath,int("600644",8)), otherwise it will get the permissions wrong. And if you replace os.system("sudo %s"%__file__) with os.system("gksudo %s"%__file__), you can run the program without a terminal.

Revision history for this message
Collin Stocks (collinstocks) said :
#4

Major change in the hack following this message. It now names the mount points the same way the automounter is supposed to, and deletes them when they are unmounted.

Revision history for this message
Collin Stocks (collinstocks) said :
#5

#!/usr/bin/python

import os
from time import sleep

# before we do any work, check if we are root
if not os.getuid()==0: # root user id
    os.system("gksudo %s"%__file__)
    raise SystemExit

def getDevs():
    devs={}
    for directory in "/dev/disk/by-id","/dev/disk/by-label":
        try:
            paths=os.listdir(directory)
        except OSError:
            continue
        for path in paths:
            path=os.path.abspath(os.path.join(directory,path))
            devpath=os.path.abspath(os.path.join(directory,os.readlink(path)))
            if directory.endswith("by-label"):
                devs[devpath]=os.path.basename(path)
            else:
                devs[devpath]="disk"
    return devs

devs=getDevs()
mountpoints=os.listdir("/media")

while True:
    sleep(1)
    new_devs=getDevs()
    if not devs==new_devs:
        for dev in new_devs:
            if dev in devs:
                continue
            if dev[-1] in ORDS:
                print "device added"
                print dev
                print new_devs[dev]
                if new_devs[dev]=="disk":
                    i=0
                    name="disk%i"%i
                    while name in mountpoints:
                        i+=1
                        name="disk%i"%i
                    new_devs[dev]=name
                mountpath="/media/%s"%new_devs[dev]
                print "mount \"%s\" \"%s\""%(dev,mountpath)
                try:
                    os.mkdir(mountpath)
                except OSError:
                    pass
                os.chmod(mountpath,int("600644",8))
                os.system("mount \"%s\" \"%s\""%(dev,mountpath))
        devs=new_devs
    mountpoints=os.listdir("/media")
    for mountpoint in mountpoints:
        mountpoint=os.path.join("/media",mountpoint)
        if mountpoint in open("/etc/fstab").read():
            continue
        if os.path.ismount(mountpoint):
            continue
        if os.path.islink(mountpoint):
            continue
        if os.path.isfile(mountpoint):
            continue
        print "device removed"
        print mountpoint
        try:
            os.rmdir(mountpoint)
        except OSError:
            print "could not remove directory!"

Revision history for this message
Launchpad Janitor (janitor) said :
#6

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
Collin Stocks (collinstocks) said :
#7

This problem no longer exists, for some reason. I hope that my little program can be of some help to people who also have it.

Revision history for this message
Collin Stocks (collinstocks) said :
#8

The problem is solved. No idea how.