Gert
Posts: 181
Joined: Wed Oct 24, 2012 10:19 pm

How to mount usb stick with ext4 for all users

Mon Oct 07, 2019 1:45 am

Hi All,

Is it a good idea to use ext4 on a USB stick for a RPI4 as occasional storage?

Using gparted I went ahead and partitioned and formatted the USB stick accordingly.

Mounting via fstab I found 'somewhere' a line that was supposed to be 'good' for USB stick. (maybe as a side question someone can explain the intricacies of this line to me). Turns out this line mounts the USB stick nicely. But access is limited to root, only.

Code: Select all

# For HP 128G ext4 formatted USB Flash stick with options to minimize flash wear & use as samba share.
UUID=ad80c51e-a88c-417d-9333-bb08fef294b4 /media/usbstick ext4 noatime,nodiratime,errors=remount-ro 0 0
...

gottsch@rpi4b:/media/usbstick $ touch me_user
touch: cannot touch 'me_user': Permission denied
gottsch@rpi4b:/media/usbstick $ sudo touch root
gottsch@rpi4b:/media/usbstick $ ls -lrt
total 16
drwx------ 2 root root 16384 Oct  6 18:05 lost+found
-rw-r--r-- 1 root root     0 Oct  6 18:34 root
From another fstab 'recommendation' I have a line that mounts a 2.5inch disk drive. Non-root user access for this one is fine.

Code: Select all

# Ntfs 5T external USB disk (Seagate) with options to become samba share
UUID=F02E77D02E778E7A    /media/usbdisk5t ntfs-3g auto,user,exec,rw,nofail,uid=1001,gid=1001	0 0
So I decided to do some transplant surgery and mix what I thought were nice entries from one line into the other. And promptly failed.

Code: Select all

# For HP 128G ext4 formatted USB Flash stick with options to minimize flash wear & use as samba share.
UUID=ad80c51e-a88c-417d-9333-bb08fef294b4 /media/usbstick ext4 noatime,nodiratime,uid=1001,gid=1001,errors=remount-ro 0 0
...
mount /dev/sdb1
mount: /media/usbstick: wrong fs type, bad option, bad superblock on /dev/sdb1, missing codepage or helper program, or other error.
So, what would be the correct and USB stick optimized fstab entry here? And maybe an explanation of why it is so hard to get this right.

Cheers,
Gert

oksage
Posts: 19
Joined: Fri Dec 01, 2017 6:44 am

Re: How to mount usb stick with ext4 for all users

Mon Oct 07, 2019 2:39 am

I used to mount a storage partition with this:
UUID=0618B4FC18B4CAA9 /home/paul/Mnt/backups ntfs-3g auto,uid=1000,gid=1000,dmask=022,fmask=133 0 0

UUID is the specific id for the drive (or partition?)
then the mount point
then the filesystem (yours is correct for ext4)
uid (user) and gid (group) were for the first user I believe.
dmask and fmask set permissions for directory and files
0 0 are the mount priority, I believe.

You could give the users a common group name, then use that groups gid, probably. You can look up uid with

Code: Select all

id -u
and gid with

Code: Select all

id -g
The answer at this page might help:
https://superuser.com/questions/430707/ ... users-simu
where it says
So as an example, to assign everything on the drive the root uid and gid use something like uid=0,gid=0,umask=000 this should make everything on the drive be owned by root, but still be usable through the open public umask.

If you want more control, you should add the users that are allowed to use the content of the drive to a certain group and look up its id, then you do like above but replace the gid by its value (say 123 here) uid=0,gid=123,umask=007. this should make the files (and directories) owned by user root and the group that belongs to 123, makes it readable, executable and writable to them, but they are locked to the public.

Never the less you should actually read the manpages about mount, groups and umask, because I cannot guarantee for the above to work. But I still hope I could push you into the right direction.

oksage
Posts: 19
Joined: Fri Dec 01, 2017 6:44 am

Re: How to mount usb stick with ext4 for all users

Mon Oct 07, 2019 3:04 am

If you create a group, say mount4all or whatever, and make yourself part of that group, then you call look up all your gid numbers with

Code: Select all

id -G
I also have a pi with a usb mounted, and I explicitly list the uid (uid=paul) and gid (gid=paul). On that I don't use the umask options (dmask/fmask). Anyway, it seems to work by putting in the name of the group instead of having to look up its gid number.

But if you want/need to use the umask options, I'd look that up too.

I found a printed webpage in my docs, for the webpage
http://www.yolinux.com/TUTORIALS/LinuxT ... roups.html
Use Ctrl-f and search for fstab to find more info.
That page mentions using the chown command, but I don't think it says that it must be preceded with sudo.

User avatar
thagrol
Posts: 4041
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: How to mount usb stick with ext4 for all users

Mon Oct 07, 2019 9:41 am

Try the quick and dirty way:
  1. Mouint your USB drive.
  2. run

    Code: Select all

    sudo chmod -R a+rwx /media/usbstick 
This forces permissions of everything currently on the USB drive to allow read, write, and execute for all users. New files and directories will have whatever permissions their creator grants.

It's insecure but does acheive what you want.

If the USB device is currently empty you can omit the "-R".
Arguing with strangers on the internet since 1993.

Return to “Troubleshooting”