0

I'm trying to look into the innards of FAT32, and towards that I'm trying to create a FAT32 image, mount it and do a few file operations on the command line. Per the question here, I know there's no way around using sudo to mount the image. I'm still wondering though why I end up needing sudo in order to do file operations within the mountpoint. A small bash script follows which demonstrates what works and what doesn't. Could someone show me how to do this without root?

DISK=/tmp/disk1.raw
MOUNTPOINT=/tmp/mount1
SIZE=512M
rm -f $DISK
rm -rf $MOUNTPOINT

# create disk image
qemu-img create -f raw $DISK $SIZE
# format disk image
mkfs.fat -v -F 32 $DISK
# make mountpoint
mkdir -p $MOUNTPOINT

# can't be helped
sudo mount -o loop -t msdos $DISK $MOUNTPOINT
# should work but doesn't
mkdir -p $MOUNTPOINT/tmp/
# actually works
sudo mkdir -p $MOUNTPOINT/tmp/
# should work but doesn't
dd of=$MOUNTPOINT/tmp/ticket2.txt if=/dev/zero bs=512 count=9
# actually works
sudo dd of=$MOUNTPOINT/tmp/ticket2.txt if=/dev/zero bs=512 count=9

ls -lR $MOUNTPOINT
sudo umount $MOUNTPOINT
  • What matters are permissions and ownership inside the filesystem. Could the uid and gid options for fat filesystems help here? Try adding -o uid=n to the mount command, where n is your user id. – njsg Apr 16 '18 at 17:36
  • Thanks @njsg, the answer below expanded on your point and the advice worked for me. – airbornemihir Apr 16 '18 at 18:56
4

The MS-DOS filesystem variants do not support file permissions or owners (stored on disk). So instead, the kernel defaults them to the mounting user — in this case, root.

You can override this by passing the uid= and gid= options. E.g., sudo mount -o loop,uid=1000,gid=1000 -t msdos "$DISK" "$MOUNTPOINT". (I added quoting there, which is a good habit to get in to). You can check what your uid/gid is with id; it may well be something other than 1000, or alternatively do the following:

UID=`id -u`
GID=`id -g`
sudo mount -o loop,uid=$UID,gid=$GID -t msdos "$DISK" "$MOUNTPOINT"

These options are documented in man 8 mount, at least.

PS: There are several options for mounting w/o sudo mentioned in that question; e.g., udisks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.