Fix permissions issues with archaic filesystems (ExFAT) on Linux
Here we are in the sixth decade of the Unix Epoch and the lowest common denominator supported filesystem (between Android, Linux, and the BSDs) is the archaic FAT family of DOS filesystems. The least shitty of the FAT family, ExFAT, is what most of my sdcards are formatted with, and its complete lack of Unix-native metadata occasionally poses problems when mounting such partitions on Linux.
Since these filesystems have no concept of users, groups, and r/w/x
permissions, by default when you mount a partition formatted with
ExFAT using mount(8)
, Linux provides some simulation of them
inherited respectively from the user, group, and umask of the process
which calls mount(2)
. Since mount must run as root and the
filesystem doesn't technically support permissions, everything below
the mountpoint will be owned as root and cannot be chown(1)
'd. This
is the case even if the mountpoint is owned by your user account. As
you might imagine, this makes copying files to and from the disk
rather annoying.
When mounting an ExFAT partition on my machine (OpenSuse Tumbleweed
with Linux 6.9.1), mount(8)
defaults to using the ExFAT module built
into the kernel, which from what I can tell doesn't provide a way to
specify ownership. Fortunately, there is an alternative ExFAT driver
available that makes use of Fuse (this is the preferred one on FreeBSD
and OpenBSD so this should apply to them too) which accepts options
for setting custom uid and gid fields:
sudo mount -t exfat-fuse /dev/whence /mnt/whither -o uid=$(id -u),gid=$(id -g),fmask=137,dmask=027
This will mount the /dev/whence partition on the /mnt/whither directory with all files and directories in it being readable and writable and owned by the current user rather than root.
I've found this solution also works for VFAT (with the -t vfat
driver on Linux) but haven't tested it with other FAT derivatives,
nor on BSDs or other systems with different kernels.
To understand the foregoing mount options I'd recommend checking out
the man pages for fstab(5)
, umask(1p)
, and mount(8)
, and this
handy umask calculator.