Execute files from a USB thunb drive

Archives Forums/Linux Discussion/Execute files from a USB thunb drive

dawlane(Posted 2013) [#1]
Some asked about how could they run an executable from a USB stick the other day as they also need to use this stick for windows and macs it had to be formatted as vfat (think it was Tricky).
As vfat and NTFS don't support execute permissions and Linux by nature won't let you execute any file without them. As it stands there are three ways to do this.

1) Manually mount the device with mount or pmount with the relevant permission set using a command line in a terminal. Problem here is you would have to unmount the device first so you can remount it.

2) Edit the fstab file in /etc to add a permanent mount point with the relevant permissions set. This isn't a problem as long as the USB device is plugged in.

3) Add a script into /etc/udev/d.rules that will dynamically mount and unmount the USB device.

First a description of how to script for the udev daemon.
One thing to note the last update for this site was in 2008. udevinfo,udevtest etc should now be combined into udevadm.

The next thing I would like to mention here is that some file browsers (caja for mint mate) will still try to mount any removable USB device in /media/$HOME/$DEVICE after the device has been mounted using the script below. $HOME/$DEVICE being your user name and the name of the device. I did find a topic about this yesterday but I can't find the dam thing now and it was hard to find in the first place. But it has something to do the the fact that USB detection was moved into the udev script udisks2. And many file browsers use a script for auto mounting that points to this file. I will update this information when I do some more research into it. But for now you just get a pop up saying the device is already mounted just click ok and fire up a file browser and you should see that the USB device is mounted and ready to use and you will be able to run executable files of the device.

NOTE: I haven't tested this with multiple usb devices yet. So I don't know what will happen. So if you want to try carry on reading.
Edit:Ok test works as normal.

As Super user open a text editor.
For Mint/Ubuntu press Alt+F2 to open a run program dialog.
Depending on the OS distribution: type
gksu pluma
for mint or
gksu gedit
Enter the root password in the next dialog.

Then copy and past the code be low into the editor. You will need to save this as file in /etc/udev/rules.d give it the name it 11-usb-flash.rules.
#Assume that the USB devices are linked to /dev/sd* use mount or dmesg to list devices
KERNEL!="sd[a-z]*", GOTO="auto_mount_end"
SUBSYSTEMS=="usb",ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="auto_mount_end"

# Set environment
SUBSYSTEMS=="usb",ACTION=="add", IMPORT{program}="/sbin/blkid -o udev -p -s TYPE -s LABEL %N"

# Global mount options
SUBSYSTEMS=="usb",ACTION=="add", ENV{mount_options}="relatime,users,umask=0"

# Filesystem specific options
SUBSYSTEMS=="usb",ACTION=="add", ENV{ID_FS_TYPE}=="vfat", ENV{mount_options}="%E{mount_options},users,exec,noatime,umask=000"
SUBSYSTEMS=="usb",ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", ENV{mount_options}="%E{mount_options},utf8,users,exec,noatime,umask=000"

# Get mount point
# use basename to correctly handle labels such as ../mnt/foo
SUBSYSTEMS=="usb",ACTION=="add", ENV{ID_FS_LABEL}=="?*", PROGRAM="/usr/bin/basename '%E{ID_FS_LABEL}'", ENV{dir_name}="%c"
SUBSYSTEMS=="usb",ACTION=="add", ENV{dir_name}!="?*", ENV{dir_name}="usbhd-%k"

# Main action
SUBSYSTEMS=="usb",ACTION=="add", ENV{dir_name}=="?*", RUN+="/bin/mkdir -p '/media/usb/%E{dir_name}'", RUN+="/bin/mount -o %E{mount_options} /dev/%k '/media/usb/%E{dir_name}'"
SUBSYSTEMS=="usb",ACTION=="remove", ENV{dir_name}=="?*", RUN+="/bin/umount -l '/media/usb/%E{dir_name}'", RUN+="/bin/rmdir '/media/usb/%E{dir_name}'"

LABEL="auto_mount_end"


Once done save it and open and type in a terminal
sudo udevadm control --reload-rules