Automating External Drive Mounting via Terminal

Discussion in 'macOS' started by NATO, Jan 5, 2006.

  1. NATO macrumors 68000

    NATO

    Joined:
    Feb 14, 2005
    Location:
    Northern Ireland
    #1
    Hi,

    I've been trying to do this for some time, and I thought I had it sorted until I discovered a problem.

    Essentially, what I am currently doing is adding an entry to my crontab to spin-up and mount my 2 external Hard Drives at 3.55AM in preparation of an automatic backup which will start at 4.00AM. The commands I used were as follows:

    hdiutil mountvol disk2s3
    hdiutil mountvol disk4s3

    (my iPod mounted at disk3s3, hence the jump in disk numbers)

    However, upon rebooting my machine last night, I realised the disk2s3/disk3s3 no longer represented the drives they used to, so I was wondering if there was any way of mounting my drives based on their volume name 'eg. hdiutil mountvol 'Backup Drive 1'' etc to ensure only the correct drives mount, but it doesn't seem that this is possible.

    As I have a very limited knowledge of OS X Terminal commands and UNIX commands in general, I was wondering if anyone here could help solve my problem?
     
  2. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #2
    Paste this into a little shell script, I call my copy mountbyname.
    Code:
    #!/bin/sh 
    /usr/sbin/diskutil mount `/usr/sbin/disktool -l | grep "${@}" \
     | sed 's/.*\\(disk[0-9s]*\\).*/\\1/'`
    
    To mount a volume called Tuna casserole, you can then use the command

    /path/to/mountbyname "Tuna casserole"


    I'll note here that disktool is supposed to be a deprecated command. The same volume name information could be pulled out of diskutil, but unfortunately it's presented in a format that's a little hairier to parse (and the columnar format makes me leery of how the formatting may change in future OS releases.)
     
  3. NATO thread starter macrumors 68000

    NATO

    Joined:
    Feb 14, 2005
    Location:
    Northern Ireland
    #3
    Thanks iMeowbot, what a helpful reply! I'll give that a try as soon as I get home today. I tried everything last night with hdiutil and diskutil without success. I figured it might end up involving some sort of scripting, but I don't know enough about it yet to really do it myself.

    I'll post back this evening with an update.

    Thanks again :)
     
  4. NATO thread starter macrumors 68000

    NATO

    Joined:
    Feb 14, 2005
    Location:
    Northern Ireland
    #4
    Hi iMeowbot,

    I gave that script a try and unfortunately its not working quite right. Here's a listing of what appears when i do a disktool -l:

    Code:
    ***Disk Appeared ('disk2s3',Mountpoint = '/Volumes/NATO HD X2', fsType = 'hfs', volName = 'NATO HD X2')
    ***Disk Appeared ('disk2s1',Mountpoint = '', fsType = '', volName = '')
    ***Disk Appeared ('disk3',Mountpoint = '', fsType = '', volName = '')
    ***Disk Appeared ('disk3s1',Mountpoint = '', fsType = '', volName = '')
    ***Disk Appeared ('disk3s3',Mountpoint = '', fsType = 'hfs', volName = 'NATO HD X')
    ***Disk Appeared ('disk2',Mountpoint = '', fsType = '', volName = '')
    ***Disk Appeared ('disk1',Mountpoint = '', fsType = '', volName = '')
    ***Disk Appeared ('disk1s1',Mountpoint = '', fsType = '', volName = '')
    ***Disk Appeared ('disk1s3',Mountpoint = '/', fsType = 'hfs', volName = 'NATO HD')
    ***Disk Appeared ('disk0',Mountpoint = '', fsType = '', volName = '')
    ***Disk Appeared ('disk0s1',Mountpoint = '', fsType = '', volName = '')
    ***Disk Appeared ('disk0s3',Mountpoint = '/Volumes/NATO HD 2', fsType = 'hfs', volName = 'NATO HD 2')
    
    *Note : NATO HD and NATO HD 2 are the Internal Drives, NATO HD X and NATO HD X2 are the external drives.

    When I run the script, the following behaviour is observed:
    1. mountbyname "NATO HD" results in Volume disk2s3 mounted
    2. mountbyname "NATO HD 2" results in Volume disk0s3 mounted
    3. mountbyname "NATO HD X" results in Volume disk2s3 mounted
    4. mountbyname "NATO HD X2" results in Volume disk2s3 mounted

    Could you check this to see if I could fix this somehow? Sorry if I have given too much info, best to give you everything I have in order to help debug the script.

    Many Thanks :)
     
  5. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #5
    Yeah, I see the problem, it would never have occurred to me to use that many words in a volume name :D Back in a little while....

    Okay, this script tries a little harder to get the right names fished out. If works for me (famous last words) if I rename my volumes to match yours...

    Name whatever you like, same usage as the earlier script.

    Code:
    #!/usr/bin/python
    import sys, os
    
    if len(sys.argv) != 2:
        sys.stderr.write("usage: " + sys.argv[0] + " volume_name\n")
        sys.exit(1)
    
    P = os.popen("/usr/sbin/disktool -l")
    hdlist = P.readlines()
    P.close()
    
    mounted = found = False
    
    for l in hdlist:
        l =  l.split('(')[1].split(')')[0] # strip off between parens
        #["'disk1s3'", "Mountpoint = ''", " fsType = ''", " volName = ''"]
        litems = l.split(',')
        volname = litems[3].split("'")[1]
        if volname == sys.argv[1]:
            found = True
            devname = litems[0].split("'")[1]
            Pin, Pout, Perr = os.popen3("/usr/sbin/diskutil mount " + devname)
            mntoutput = Pout.readlines()
            Pin.close(); Pout.close(); Perr.close()
            if mntoutput[-1].split()[-1] == "mounted":
                mounted = True
            break
    
    if not found:
        sys.stderr.write(sys.argv[0] + ": '" + sys.argv[1] + "' not found\n")
        sys.exit(2)
    if not mounted:
        sys.stderr.write(sys.argv[0] + ": '" + sys.argv[1] + "' not mounted\n")
        sys.exit(3)
    
    edit: Ewww, diskutil doesn't believe in returning error codes. That's not good. Added a wee bit of checking.
    edit again: And diskutil writes errors to stdout?!! Barf!
     
  6. NATO thread starter macrumors 68000

    NATO

    Joined:
    Feb 14, 2005
    Location:
    Northern Ireland
    #6
    Wow... what can I say? It works a treat! (at least it has done in the testing I have done so far). I really can't thank you enough as I didn't think I'd be able to get that level of functionality without having to resort to painstakingly learn scripting etc! I've spotted a book on Amazon about learning UNIX on Mac OS X Tiger, then the more advanced one. Might give them a shot.

    Just goes to show the helpfulness of the Mac community as I thought I'd have to resort to manual backups!

    I'll be using your script from today onwards, I'll post back if there's any problems :)

    We really should make this a sticky, I'm sure I'm not the only one who's wanted to do this...
     
  7. NATO thread starter macrumors 68000

    NATO

    Joined:
    Feb 14, 2005
    Location:
    Northern Ireland
    #7
    Oh, I've changed one line to allow you to unmount and eject drives by name also (just for anyone else who's trying to do what I've been trying to do), just change the line

    Code:
    Pin, Pout, Perr = os.popen3("/usr/sbin/diskutil mount " + devname)
    
    to

    Code:
    Pin, Pout, Perr = os.popen3("hdiutil eject " + devname)
    
    (For some reason if I use diskutil instead of hdiutil, it shuts the correct drive but seems to spin up the other drive, but hdiutil works perfect)
     
  8. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #8
    Cool, glad it works. The hdiutil/diskutil spin-up difference makes sense, hdiutil works at a higher level because it thinks it's working with images. This thing probably isn't sticky-worthy, it's an ugly thrown-together kludge and it shows :D
     
  9. mrichmon macrumors 6502a

    Joined:
    Jun 17, 2003
    #9
    A cleaner way to identify physical disks in OS X is by the volume name or alternatively by the UUID. These mechanisms are used in the fstab file as mentioned in the thread http://forums.macrumors.com/showthread.php?t=170786

    There may be a way to use UUID or Volume name to control hdiutil. Alternatively you could set up an fstab for the drives and then just mount the drives using the plain old "mount" command.

    The question comes to mind as to why you would have these drives unmounted if they are always connected (and thus available to me mounted for the backup)? If you are leaving them unmounted so that they can spin down while leaving the rest of your drives spun-up, then you can control the spin-down times on a per drive basis.
     
  10. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #10
    Yeah, maintaining an fstab for known drives would be a possibility in some cases, but it's just a few steps beyond what I'd want to be explaining over the telephone at 2 in the morning. We're still dealing with the "yes, you really do need to leave everything plugged in and turned on" part :)

    diskutil sort of accepts volume names to mount, but it's buggy and doesn't always interpret them correctly. Hence, all the silly sniffing for device names. Mounts need to be forced because some users like to trash volumes that are "in the way". :rolleyes:
     
  11. mrichmon macrumors 6502a

    Joined:
    Jun 17, 2003
    #11
    Fair enough.


    Ahh. I'd assumed it was on your own machine rather than in a lab.

    In the lab situation you can hide the volumes so that they do not appear on the desktop and create symbolic links (or aliases) from the root partition to folders in the external volumes. This way you can effectively hide the existence of the external volumes from the user. Though obviously I have no way of knowing if this would work in your situation.
     
  12. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #12
    I wish it was all in one place, then we could just throw networking at the problem. There's lots of remote work involved. But chaos is kind of fun, so it all balances out :)
     
  13. 840quadra Moderator

    840quadra

    Staff Member

    Joined:
    Feb 1, 2005
    Location:
    Twin Cities Minnesota
    #13
    geez,

    Now I know what group to contact when I need to fix spotlight again. It took me forever to get it to index my home network drives, and it looks like this group would be up to the challenge of helping me with fixing it if it brakes later ;) .

    840
     
  14. superbovine macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #14
    *snicker* It always the little things....
     

Share This Page