## Automount USB disk as same samba drive?

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

### Automount USB disk as same samba drive?

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: 836
Joined: Thu Jan 30, 2014 9:53 am

### Re: Automount USB disk as same samba drive?

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

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

### Re: Automount USB disk as same samba drive?

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?

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

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

### Re: Automount USB disk as same samba drive?

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: 836
Joined: Thu Jan 30, 2014 9:53 am

### Re: Automount USB disk as same samba drive?

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

default_user8
Posts: 678
Joined: Mon Nov 18, 2013 3:11 am

### Re: Automount USB disk as same samba drive?

Try using label in your fstab instead of uuid, both both devices will need to have identical names and formats for this to work.

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

### Re: Automount USB disk as same samba drive?

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

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: 2783
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

### Re: Automount USB disk as same samba drive?

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: 836
Joined: Thu Jan 30, 2014 9:53 am

### Re: Automount USB disk as same samba drive?

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

Is it just as simple as changing this to:

Code: Select all

[data]
comment = Data share
path = /media/usb
browseable = yes

I.e. the section name in the file is actually the share name????
Bo Berglund
Sweden

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

### Re: Automount USB disk as same samba drive?

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?

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=="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: 9979
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

### Re: Automount USB disk as same samba drive?

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

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

### Re: Automount USB disk as same samba drive?

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

Bo Berglund
Sweden

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

### Re: Automount USB disk as same samba drive?

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: