raspberry pi automount usb pendrive

If you are using any graphical window manager, like lxde, xfce – then usb pendrive is mounted automatically. No need to follow these instructions. But, if you are using any stripped down, minimal OS without window manager, then these might be useful on your VT. USB disks would be mounted in “/mnt” folder. Like, /mnt/sda1, /mnt/sdb1 etc. By checking “usb-last-attached.txt” (mentioned bellow) this can be determined – path of the last disk attached.

 

* Install these, run commands:

apt-get update
apt-get install ntfs-3g ntfsprogs ntfs-config
ln -s /bin/ntfs-3g /usr/bin/ntfs-3g [ optional ]

Make a file as “61-usb-disk.rules” inside folder “/etc/udev/rules.d/”

 

######### [ 61-usb-disk.rules ] #########

SUBSYSTEMS==”scsi”, ENV{ID_FS_TYPE}==”vfat|ext2|ext3|ext4″ RUN+=”/root/rabiul/scripts/rabiul_notify_usb.sh %k”
SUBSYSTEMS==”scsi”, ENV{ID_FS_TYPE}==”ntfs” RUN+=”/root/rabiul/scripts/rabiul_notify_usb.sh %k”

#########################################

 

* I added a script – “rabiul_notify_usb.sh” inside the folder “/root/rabiul/scripts/” is as bellow for now:

######### [ rabiul_notify_usb.sh ] #########

#!/bin/bash

DISK=”${1}”

fdisk -l | grep -i “$DISK”

if (( $? != 0 ))
then
exit 1
fi

mkdir -p “/mnt/$DISK”

mount “/dev/$DISK” “/mnt/$DISK”

mount | grep -i “/mnt/$DISK”

if (( $? == 0 ))
then
echo “/mnt/$DISK” > /root/rabiul/scripts/usb-last-attached.txt
exit 0
fi

############################################

* Finally run the following command:

/etc/init.d/udev restart

* You may also reboot system if necessary.

** PS: To “kill” ntfs-3g or mount.ntfs from “ps aux”:

kill -9 3245

or

umount -a
mount -a

********************************************************************
********************************************************************

Another way –
———–

Make a file as “61-usb-disk.rules” inside folder “/etc/udev/rules.d/”

######### [ 61-usb-disk.rules ] #########

SUBSYSTEMS==”scsi”, ENV{ID_FS_TYPE}==”vfat|ext2|ext3|ext4″ RUN+=”/bin/mkdir -p /data/media/%k”, RUN+=”/bin/mount /dev/%k /data/media/%k”
SUBSYSTEMS==”scsi”, ENV{ID_FS_TYPE}==”ntfs” RUN+=”/bin/mkdir -p /data/media/%k”, RUN+=”/usr/bin/ntfs-3g /dev/%k /data/media/%k”

#########################################

Now, run the following command:

/etc/init.d/udev restart

Complete.

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s