Steve_BZ has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys,

I'm trying to open a USB drive on Kubuntu to write to, but clearly I need to find out which drives are available first. The following code works fine after the drives have been examined using dolphin or whatever, but otherwise not. So I can plug in a USB drive and run the code and I get negative results. If I then inspect a drive with dolphin and run the code, the drive is detected. I guess I need to mount the drive somehow, but how?

Advice welcome.

Code follows.

Thanks

Steve

use strict; use warnings; package dir_test; for (my $i=0;$i<=5;$i++){ if ($i==0 and opendir (MYDIR, "/media/disk/.")){ print "/media/disk/","\n"; closedir (MYDIR); } else { if ($i>0 and opendir (MYDIR, "/media/disk-$i/.")){ print "/media/disk-$i/","\n"; closedir (MYDIR); } else { print "not /media/disk-$i/","\n"; } } }

Replies are listed 'Best First'.
Re: Opening a USB drive for storing info.
by davido (Cardinal) on May 23, 2011 at 02:52 UTC

    It's entirely possible that I'm way off on this, and it's sort of a Ubuntu/Linux topic as opposed to Perl but here goes my stab at helping you along...

    It seems to me that USB drives are demand-mounted by Ubuntu's desktop environment (the Gnome / Unity environment), and not fstab settings. My experience has been that the drives don't get mounted until I literally click on them from the Unity desktop. They do exist, however, in /dev/*, and as such, you can mount them manually, or even create an fstab entry.

    It's possible your script needs to mount the drive for you rather than waiting for Gnome/Unity to get around to it.

    If the USB drive uses a FAT based filesystem, you may need to mount with cifs as well.

    Again, this is based on limited experience; I haven't fiddled more than a few minutes with plugging USB drives into my Ubuntu box, and that was not recently either. But to the best of my recollection, those were obstacles I discovered, and either using the GUI to demand-mount the drive, or manually mounting it were the solutions I used.

    I don't really like having things mounted in /media anyway. It just doesn't feel linux-ish. But that's personal preference, and the good thing about mounting it yourself manually is you get to follow your own whim. I put them in /mnt/*. My NAS is set up to mount in /mnt/lsxhl (Linkstation XHL), for example.

    Update: The following link discusses setting up auto-mount, as well as manually mounting USB drives. It does look like automounting of USB drives is controlled by nautilus. https://help.ubuntu.com/community/Mount/USB


    Dave

      If the USB drive uses a FAT based filesystem, you may need to mount with cifs as well.

      Nope, CIFS is the modern replacement for SMB; both are used only to connect to Windows and Samba shares. It is a network file system like NFS and others, and it has no relation with FAT file systems except that a Windows or Samba server can share drives or folders on a drive formatted with a FAT filesystem.

      Most USB sticks come preformatted with VFAT-32. Using a mount command without an explicit filesystem option (-t) auto-detects that on any recent Linux distribution. Note that you may need root privileges to mount filesystems not listed in /etc/fstab.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Ah, you're right... I was thinking of my Buffalo Linkstation Live NAS LS-XHL, with respect to CIFS. The other advice seems to be relatively on-track. ;)


        Dave

      Hi Davido,

      You are absolutely right. This is the problem. It seems there is some general confusion out there regarding automount. I can pre-set automount for devices I know I am going to use, but setting it generally 'on' for unknown devices is eluding me. I'm on Kubuntu 10.4, and the various forums have plenty of advice on the subject which I'm working through.

      Thanks for pointing me in the right direction.

      Regards

      Steve

Re: Opening a USB drive for storing info.
by wind (Priest) on May 22, 2011 at 22:56 UTC

    Don't forget to examine $! for the actual cause for a failure with opendir.

    The following is your code cleaned up a little bit:

    for my $i (0..5) { my $dir = $i ? "/media/disk-$i/" : "/media/disk/"; opendir my $dh, $dir or die "Can't opendir $dir: $!"; print $dir,"\n"; closedir $dh; }

      Hi Wind,

      Nice coding. Thanks for the tidy up.

      I changed it a little. I changed 'die' to 'warn', because I wanted all the possibilities tested, and I only 'closed' where the open had been successful. I didn't know you could write "for .." like that and I've never got around to using the "? .. : .." operator. So thanks for all that.

      So:

      #!/usr/bin/perl -w use strict; for my $i (0..5) { my $dir = $i ? "/media/disk-$i/" : "/media/disk/"; if (opendir my $dh, $dir){ print $dir," opened successfully.\n"; closedir $dh; } else { warn "Can't opendir $dir: $!"; } }

      So I inserted a USB-drive, ran the code and I got (Sorry, my machine is set to Portuguese. "Arquivo ou diretório não encontrado" means "file or directory not found ... ". ):

      Can't opendir /media/disk/: Arquivo ou diretório não encontrado at /ho +me/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-1/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-2/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-3/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-4/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-5/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10.

      before I used the dolphin file-manager and afterwards I got:

      /media/disk/ opened successfully. Can't opendir /media/disk-1/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-2/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-3/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-4/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-5/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10.

      So It looks as though I need to do something else. In fact if I do $dir I get the same result:

      $ dir /media/disk/ dir: impossível acessar /media/disk/: Arquivo ou diretório não encontr +ado

      Thanks again for a great post.

      Regards

      Steve

        maybe you need to use something called dmesg to see diagnostic info like [ 131.981346] hub 1-0:1.0: unable to enumerate USB device on port 4