Bosse_B
Posts: 776
Joined: Thu Jan 30, 2014 9:53 am

Automount USB disk as same samba drive?

Wed Oct 29, 2014 1:38 pm

Is it possible and if so how do I set it up?
I need to make it possible for the pi to mount an external USB disk attached via a USB hub (because of power needs) automatically when it is connected.
Then I also want to be able to read/write files to it using a samba shared drive name via the network.

How can this be accomplished?
Bo Berglund
Sweden

Bosse_B
Posts: 776
Joined: Thu Jan 30, 2014 9:53 am

Re: Automount USB disk as same samba drive?

Fri Oct 31, 2014 12:28 pm

Seems like noone has a clue to this, or am I posting to the wrong sub-forum???
To simplify matters even further:
How can I get to a situation where a USB memory stick is automatically mounted when it is inserted?
And unmounted when the USB is removed?

I want to work with USB thumb drives like I do in Windows, i.e. I insert it into the USB connector and the drive is automatically mounted and usable.
Then later I just pull it out to move it to the Windows laptop or such, WITHOUT having to first log on via PuTTY and making the sudo umount command.

It seems such overkill to have to go to the command line whenever a USB is attached to make it usable and then again to start the command line when the USB needs to be removed.
There MUST be a better way.....
Bo Berglund
Sweden

User avatar
RaTTuS
Posts: 10406
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Automount USB disk as same samba drive?

Fri Oct 31, 2014 12:40 pm

use UUID and udev rules
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

forenbenutzer
Posts: 148
Joined: Thu Aug 02, 2012 7:08 pm

Re: Automount USB disk as same samba drive?

Fri Oct 31, 2014 1:15 pm

Pitendo - Case And Emulator Project - http://edv-huber.com/index.php/problemloesungen/12-pitendo

Bosse_B
Posts: 776
Joined: Thu Jan 30, 2014 9:53 am

Re: Automount USB disk as same samba drive?

Fri Oct 31, 2014 2:35 pm

Thanks,
but it seems like you have to somehow marry together the specific drive (usb thumb or a usb connected hard drive) to a mount inside the pi such that if you connect something other than this specific drive you are back at zero again...
I would like it to work like in Windows, i.e. when I plug in a usb thumb drive it gets the next free drive letter and is ready to use. The same happens for a usb disk drive.
So after this it is immediately ready to be used.

So if I plug in my Kingston USB thumb it should be mounted say as /media/EXTDATA.
If I then unplug it and plug in a SanDisk USB thumb drive it should also be mounted as /media/EXTDATA.
That is no matter which drive I plug in it should be mounted as EXTDATA. Provided I only plug in one at a time of course, but that is exactly what I intend doing.

If there is a problem with this due to the different drive types, then I could live with /media/EXTTHM and media/EXTHDD, so that any thumb drive mounts as EXTTHM and any hard drive mounts as EXTHDD.

Is this possible at all?

If so the next step up the ladder is to set up a samba share on these mount points so that the attached USB hard disk or thumb becomes immediately available on the network for file operations.

Unplugging and replugging the devices should immediately reflect the mount state.
Bo Berglund
Sweden

Bosse_B
Posts: 776
Joined: Thu Jan 30, 2014 9:53 am

Re: Automount USB disk as same samba drive?

Fri Oct 31, 2014 3:20 pm

UPDATE
There seems to be a difference between the way RASPBMC and RASPBIAN behave.
I used a KINGSTON Micro-USB thumb drive of 8 GB to test.
First I changed the name of the FAT32 volume from KINGSTON to USBTHUMB in Win7.

RASPBMC
Then I plugged it into the RASPBMC Pi and could observe that /media now contained a subdir named USBTHUMB in which there were actually also a few files. I did not have to use any mount commands.

RASPBIAN
Next I unplugged it from the Pi and plugged it into my Pi B+ running RASPBIAN.
But here there is no sign of anything at all inside the /media dir!
I tried to use:

Code: Select all

sudo mount -t vfat -o uid=pi,gid=pi /dev/sda1 /media/USBTHUMB
but got a message that the dir USBTHUMB did not exist. So I created it and repeated the mount command.
Now the mount succeeded and I could list the files.
After I unplugged the drive and then plugged it again the mount seems to have disappear, ls -l /media/USBTHUMB returns zero files. What causes the difference?

So it seems like my RASPBMC unit is further along the way I want the system to work than the RASPBIAN system.
Unfortunately it is on a RASPBIAN system I need to set this thing up such that I can exchange files with my daughter on the other side of the ocean. I have VPN connectivity to the Pi B+ on her end and I must transfer some files to her on a regular basis, but the SD-card is no use for this.
So she has to plug in a drive of some sort and then I have to be able to transfer the files...
So I need:
- Auto mounting of the USB drives (Notice: NOT only on Pi reboot!!!)
- Samba sharing of the auto mounted drive
- Auto umount when the drive is removed

Any ideas?
Last edited by Bosse_B on Fri Oct 31, 2014 3:22 pm, edited 1 time in total.
Bo Berglund
Sweden

User avatar
default_user8
Posts: 658
Joined: Mon Nov 18, 2013 3:11 am

Re: Automount USB disk as same samba drive?

Fri Oct 31, 2014 3:21 pm

Try using label in your fstab instead of uuid, both both devices will need to have identical names and formats for this to work.
Two heads are better than one, unless one's a goat head.

Bosse_B
Posts: 776
Joined: Thu Jan 30, 2014 9:53 am

Re: Automount USB disk as same samba drive?

Fri Oct 31, 2014 7:36 pm

I have gotten further now! USB drives mount automatically on insertion. :D

After extensive googling I found this informative page, which actually solved the first part of my problem.
What it suggested is that I install usbmount:

Code: Select all

sudo apt-get install usbmount
After I did so I can now attach up to 8 usb thumb drives to my pi and it looks like the first one attached will be mounted as /media/usb0.
I have tested with two different USB thumb drives as follows:
- Plug in device A and check content via ls -l /media/usb0. Files are listed.
- Unplug device A and plug in device B
- Check again ls -l /media/usb0. The files listed are those of device B.
This worked fine both for a FAT32 (device A) and ext4 (device B)

I tried plugging in both at the same time, but then the pi rebooted. Probably because of a power supply problem.

So now I will continue my quest of setting up SAMBA for this Pi.
I would point it towards /media/usb0
But it would be good to be able to define a different name like DATA instead....
Bo Berglund
Sweden

mikerr
Posts: 2774
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Automount USB disk as same samba drive?

Sat Nov 01, 2014 10:39 am

Bosse_B wrote: So now I will continue my quest of setting up SAMBA for this Pi.
I would point it towards /media/usb0
But it would be good to be able to define a different name like DATA instead....
You can set the samba share name to whatever you like,
the end users won't see "/media/usb0" only "\\pi\sharename"
Android app - Raspi Card Imager - download and image SD cards - No PC required !

Bosse_B
Posts: 776
Joined: Thu Jan 30, 2014 9:53 am

Re: Automount USB disk as same samba drive?

Sat Nov 01, 2014 12:36 pm

Maybe you can give me instructions on how I can do that?
The smb.conf file would be one place and mine looks like this:

Code: Select all

[data]
   comment = Data share
   path = /data
   browseable = yes
   read only = no
Is it just as simple as changing this to:

Code: Select all

[data]
   comment = Data share
   path = /media/usb
   browseable = yes
   read only = no
I.e. the section name in the file is actually the share name????
Bo Berglund
Sweden

Bosse_B
Posts: 776
Joined: Thu Jan 30, 2014 9:53 am

Re: Automount USB disk as same samba drive?

Sat Nov 01, 2014 5:37 pm

I edited the smb.conf file and changed the path to /media/usb
then restarted samba.
I get access now too so the symlink I did seems superfluous.
But I still have the same permission problems....
Bo Berglund
Sweden

NelsonOnPi
Posts: 2
Joined: Wed Jan 13, 2016 11:05 pm

Re: Automount USB disk as same samba drive?

Wed Jan 13, 2016 11:20 pm

Using udev rules to mount and umount USB drives, how do I get the ownership away from root and to pi?

In /etc/udev/rules.d I added this file as 20-myusb.rules

ACTION=="add", KERNEL=="sd[a-d][0-9]", SYMLINK+="usbdisk%n"
ACTION=="add", KERNEL=="sd[a-d][0-9]", RUN+="/bin/mkdir -p /media/usb"
ACTION=="add", KERNEL=="sd[a-d][0-9]", RUN+="/bin/mount /dev/%k /media/usb"
ACTION=="remove", KERNEL=="sd[a-d][0-9]", RUN+="/bin/umount /media/usb"
ACTION=="remove", KERNEL=="sd[a-d][0-9]", RUN+="/bin/rmdir /media/usb"

The intention is to let any of our technicians remove a USB drive after data collection and replace it with a new one.
However, this technique leaves the USB drive owned by root and I'd like to write to it from /home/pi without hassle,
otherwise it works great and gives me a consistent name to write to with any USB drive.
How do I transfer ownership to pi?

DirkS
Posts: 9898
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Automount USB disk as same samba drive?

Thu Jan 14, 2016 8:46 am

NelsonOnPi wrote:How do I transfer ownership to pi?
Does http://www.reactivated.net/writing_udev ... #ownership help?

Bosse_B
Posts: 776
Joined: Thu Jan 30, 2014 9:53 am

Re: Automount USB disk as same samba drive?

Thu Jan 14, 2016 9:50 am

Of course you need to have samba installed first but this I think you already have...

I solved the auto-mount problem like this (partly described in this thread already).
Resources:
Setting up a SAMBA server on Raspberry Pi
Automatically mount USB drive on Raspberry
Here are my own notes:

Code: Select all

sudo apt-get install usbmount
Now change the permissions on all /media/usbx directories to 777

Code: Select all

sudo chmod 777 /media/usb0
...
sudo chmod 777 /media/usb7
Then any USB thumb drive attached will get mounted to /media/usb0 (or usb1 if there is already a usb thumb drive attached to the pi)

To make the mount fully r/w edit the conf file:

Code: Select all

sudo nano /etc/usbmount/usbmount.conf
Of course you will need to have samba installed already, but this I think you have...

Change to the following setting:

Code: Select all

MOUNTOPTIONS="noexec,nodev,noatime,nodiratime"
FS_MOUNTOPTIONS="-fstype=vfat,uid=pi,gid=pi,dmask=000,fmask=111"
In order to share the USB via SAMBA:
Edit the /etc/samba/smb.conf file and create a section:

Code: Select all

[data]
   comment = Data share
   path = /media/usb
   browseable = yes
   read only = no
Bo Berglund
Sweden

NelsonOnPi
Posts: 2
Joined: Wed Jan 13, 2016 11:05 pm

Re: Automount USB disk as same samba drive?

Thu Jan 14, 2016 11:58 pm

DirkS wrote:
NelsonOnPi wrote:How do I transfer ownership to pi?
Does http://www.reactivated.net/writing_udev ... #ownership help?
No luck so far. I changed my rule file to:

ACTION=="add", KERNEL=="sd[a-d][0-9]", SYMLINK+="usbdisk%n"
# ACTION=="add", KERNEL=="sd[a-d][0-9]", RUN+="/bin/mkdir -p /media/aruusb", OWNER="pi"
ACTION=="add", KERNEL=="sd[a-d][0-9]", RUN+="/bin/mount /dev/%k /media/aruusb", OWNER="pi"
KERNEL=="sd[a-d][0-9]", OWNER="pi"
ACTION=="remove", KERNEL=="sd[a-d][0-9]", RUN+="/bin/umount /media/aruusb"
# ACTION=="remove", KERNEL=="sd[a-d][0-9]", RUN+="/bin/rmdir /media/aruusb"

I don't think I need to mkdir and rmdir upon USB drive unplug and new drive plug in
Line 4 is in case the USB is plugged in when the Pi starts up, don't know if I need it.
Pi still gives USB ownership and group to root.
This is my first udev rule experience, so I could use some more advice. Thanks!

Return to “General discussion”