25. October 2014 4 min read

Static mounting USB storage in Linux terminal

I am running Ubuntu Linux server on Odroid and I wanted additional space that eMMC cannot offer. I have decided to use USB storage key with 16 GB of storage space. Then I needed to statically mount the USB storage to /web directory which will give me additional freedom when wanting to transfer the whole static content of websites.
So to get started you need to format your USB key to fat or ext4 extended partition. You can do that with plugging in the USB storage and execute:

sudo fdisk /dev/sda1

You can find to which /dev/sd your key is mounted by running

dmesg

or

lsusb
# then to view if you have successfully linked it
ls -l /dev/bus/usb/001/002

You need to create udev rules. you can do this with creating a file in /etc/udev/rules.d/ with .rules suffix. To find out which attributes your USB key has you run

sudo udevadm info --attribute-walk /dev/sda1

Once you find out the attributes you need to write them into the file. Remember that SUBSYSTEM should be block otherwise you will get Error: Unable to mount. Not block. Also keep in mind that you cannot put NAME to KERNEL attribute (error is logged into dmesg) and that is why you create a SYMLINK

# mount usb storage (all in one line)
# this code will execute a script (RUN) on add (ACTION)
# ATTR attributes are to enable unique indentification of device
# SYMLINK is the name you will see in /dev/
SUBSYSTEM=="block", ATTRS{idVendor}=="abcd", ATTRS{idProduct}=="1234", MODE="0666", SYMLINK+="webhdd", 
ACTION=="add", RUN+="/etc/udev/scripts/mount_web.sh"

# unmount usb storage (all in one line)
# this code will execute a script (RUN) on remove (ACTION)
SUBSYSTEM=="block", ATTRS{idVendor}=="abcd", ATTRS{idProduct}=="1234", MODE="0666", SYMLINK+="webhdd", 
ACTION=="remove", RUN+="/etc/udev/scripts/unmount_web.sh"


/etc/udev/scripts/unmount_web.sh

#!/bin/sh 
# Mount drive on udev 'ACTION==add' 
mount /dev/webhdd /web


/etc/udev/scripts/unmount_web.sh

#!/bin/sh 
# Unmount drive on udev 'ACTION==remove' 
umount /web


To execute udev rules without reboot you run:

sudo udevadm trigger


Now you want to edit /etc/fstab and add

/dev/webhdd /web ext4 rw,users,uid=33,gid=33,noauto,sync  0 0

Newest from this category: