Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

List removable media?

  • 13-07-2012 5:22am
    #1
    Moderators, Arts Moderators Posts: 35,738 Mod ✭✭✭✭


    I'm trying to update a script I've written (to copy and rename images from a camera) to allow for several connected devices (SD card, camera via USB, USB storage device...) and I want to offer the user a choice of which removable media to copy from.

    Is there any way of listing just those devices which were hot-plugged?


Comments

  • Registered Users, Registered Users 2 Posts: 14,049 ✭✭✭✭Johnboy1951


    I'm trying to update a script I've written (to copy and rename images from a camera) to allow for several connected devices (SD card, camera via USB, USB storage device...) and I want to offer the user a choice of which removable media to copy from.

    Is there any way of listing just those devices which were hot-plugged?

    How you do it I guess depends a lot on how the system is set up.
    If the devices are mounted then their mount points will be under /media/ most likely, and this may suffice, as they must be mounted to be accessible.
    If they have labels then by default the labels are likely used for the mount points.

    You might also have a look at

    ls /dev/disk/by-id/usb*

    to see if it might be useful to you.
    Unfortunately, from my point of view, it will also list USB hubs etc, so is not specific to storage volumes.

    There *should* be a way to get storage volumes only ..... cannot recall at the mo .... :(

    EDIT
    To get a list of partitions this might do

    ls /dev/disk/by-id/usb* | grep -i part


  • Moderators, Arts Moderators Posts: 35,738 Mod ✭✭✭✭pickarooney


    I'll have a look at that. My /media folder has a couple of internal drives mounted inside it but maybe one way would be to make a blacklist of permanent drives and then list anything else.

    Up until recently, Ubuntu mounted every first plugged device at /media/disk by default and then /media/disk1 for the next one and so on but now it mounts them with an alphanumeric folder name and I don't know where it reads that from, if it's something I can interpret myself or if it's just a randomly generated string that persists on subsequent mounts.


  • Registered Users, Registered Users 2 Posts: 14,049 ✭✭✭✭Johnboy1951


    I'll have a look at that. My /media folder has a couple of internal drives mounted inside it but maybe one way would be to make a blacklist of permanent drives and then list anything else.

    Up until recently, Ubuntu mounted every first plugged device at /media/disk by default and then /media/disk1 for the next one and so on but now it mounts them with an alphanumeric folder name and I don't know where it reads that from, if it's something I can interpret myself or if it's just a randomly generated string that persists on subsequent mounts.

    I also mount internal partitions in the /media directory as those are temporary mounts for backups etc.
    They were not enumerated in the list generated from the command above.

    I don't use Ubuntu so have no idea what way they do things.


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 93,596 Mod ✭✭✭✭Capt'n Midnight


    could you parse the output from df ?


  • Registered Users, Registered Users 2 Posts: 13,077 ✭✭✭✭bnt


    This page has a Python script that lists USB devices: it uses HAL, which may not be present on your system by default. I just tried a slightly modified version on my Ubuntu (64-bit) machine, and it works, only detailing the removable devices I have plugged in:
    import dbus  
    bus = dbus.SystemBus()
    
    proxy = bus.get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager")
    iface = dbus.Interface(proxy, "org.freedesktop.Hal.Manager")
    
    devices = iface.GetAllDevices ()
    
    for device in devices:
      try:
          proxy1 = bus.get_object("org.freedesktop.Hal", device)
          iface1 = dbus.Interface(proxy1, "org.freedesktop.Hal.Device")
          props = iface1.GetAllProperties()
    
          removable = iface1.GetProperty("storage.removable")
          if removable == 1:
            print (iface1.GetProperty("block.device"), iface1.GetProperty("storage.vendor"), iface1.GetProperty("info.product"))
      except:
        pass
    
    The original has a more complex print statement (with joins) which is needed for Python 2 to give plain text, but Python 3 doesn't need it.

    You are the type of what the age is searching for, and what it is afraid it has found. I am so glad that you have never done anything, never carved a statue, or painted a picture, or produced anything outside of yourself! Life has been your art. You have set yourself to music. Your days are your sonnets.

    ―Oscar Wilde predicting Social Media, in The Picture of Dorian Gray



  • Advertisement
  • Moderators, Arts Moderators Posts: 35,738 Mod ✭✭✭✭pickarooney


    ls /dev/disk/by-id/usb*

    To get a list of partitions this might do

    ls /dev/disk/by-id/usb* | grep -i part

    I can get these to work but they return symlinks to the partitions and use yet another alphanumeric string which is neither the disk's UUID nor the short alphanumeric ID used for the mount folder.

    I'll try the python script, see if I can wrap it in my own script.


  • Registered Users, Registered Users 2 Posts: 14,049 ✭✭✭✭Johnboy1951


    It depends on the status of the storage volume .... if it is not yet mounted then it can be difficult to separate and the command above does separate the actual storage volumes from all other devices.

    Of course if on the other hand the volumes are automatically (or previously) mounted then it becomes much easier.
    Even easier again if such partitions are mounted under /media every time.

    In that case maybe something like this would do - if it is the mount points you want ......

    mount | grep -i /media | cut -d " " -f3

    I had understood initially you wanted to get the device name only ..... but it seems you want to use the result for more than display purposes ......


  • Registered Users, Registered Users 2 Posts: 218 ✭✭Tillotson


    Is this what you're looking for?
    #!/bin/sh
    
    for disk in /sys/block/*; do 
        [ $(cat $disk/removable) = 1 ] && basename $disk
    done
    


  • Registered Users, Registered Users 2 Posts: 14,049 ✭✭✭✭Johnboy1951


    Tillotson wrote: »
    Is this what you're looking for?
    #!/bin/sh
    
    for disk in /sys/block/*; do 
        [ $(cat $disk/removable) = 1 ] && basename $disk
    done
    

    That I believe would list empty hub connections as devices, and if a device is inserted into one, would add it to the list ..... but I may be mistaken .... so not a true list of connected physical devices such as cameras, USB sticks etc


  • Moderators, Arts Moderators Posts: 35,738 Mod ✭✭✭✭pickarooney


    Sorry I haven't got back to this. I'm expecting a replacement hard drive in a couple of days and will give these tips a run out then.


  • Advertisement
Advertisement