0

I want to create a script to backup data for casuals. They have to insert a usb key and just have to click on a script to backup data on it.

Unfortunately, in Centos 6.6 auto mount change the mount point everytime, depending of the name of the usb key /media/workkey /media/mykey /media/minikey ....

How can i set a static point for the auto mount ?

  • I may not be able to give you exact answer but you will need to use autofs command to auto-mount usb. Please follow below link to assign static name to your usb drive: http://linuxconfig.org/automatically-mount-usb-external-drive-with-autofs Use of autofs to auto mount the drive: http://blog.zwiegnet.com/linux-server/configure-automount-centos-6/ – Amir Apr 23 '15 at 09:56

2 Answers2

1

1.Make USB device base name permanent(eg: Iomega usb device)

To avoid any confusion whether base name for your USB block device is /dev/sdb1, /dev/sdd1 or /dev/sdXn we make it permanently /dev/Iomega anytime you plug it in. This can be done with help of udev device manager. You should have udev already installed on your system, otherwise install it with:

apt-get install udev

Next, search for a current base name of your external USB disk using fdisk command:

fdisk -l

This will return something like this:

OUTPUT: Disk /dev/sdc: 2000.3 GB, 2000396746752 bytes 255 heads, 63 sectors/track, 243201 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x001425a0

Device Boot Start End Blocks Id System /dev/sdc1 1 243201 1953512001 b W95 FAT32

Where the base name for your external USB device is /dev/sdc. Next, use udevinfo command with /dev/sdc as an argument to get model attribute: $ udevinfo -a -p /sys/block/sdc/ | grep model ATTRS{model}=="Ext HDD 1021 "

Now, that we have model attribute, we can add it to /etc/udev/rules.d/custom.rules by following line: SUBSYSTEM=="scsi", ATTRS{model}=="Ext HDD 1021 ", SYMLINK+="Iomega%n"

At this point all we need to do is restart udev device manager:

/etc/init.d/udev restart

Stopping the hotplug events dispatcher: udevd. Starting the hotplug events dispatcher: udevd.

Plug in external USB and your new base name is: ls -l /dev/Iomega* lrwxrwxrwx 1 root root 3 2011-02-23 12:36 /dev/Iomega -> sdc lrwxrwxrwx 1 root root 12 2011-02-23 12:36 /dev/Iomega0 -> bsg/14:0:0:0 lrwxrwxrwx 1 root root 4 2011-02-23 12:36 /dev/Iomega1 -> sdc1 lrwxrwxrwx 1 root root 3 2011-02-23 12:36 /dev/Iomega3 -> sg3

Please note that /dev/Iomega1 points to a /dev/sdc1, which is exactly the partition we are interested in and we use it next to configure autofs. 2.Setting up autofs

First we need to install autofs:

apt-get install autofs

Configuring autofs is rather simple task. All we need to do is to edit two simple files.

Let's start with master file by appending a following line: /media/ /etc/auto.ext-usb --timeout=10,defaults,user,exec,uid=1000

Next, edit /etc/auto.ext-usb file which we included in a master configuration file configuration: Iomega -fstype=auto :/dev/Iomega1

Restart autofs:

/etc/init.d/autofs restart 3.Testing autofs configuration

We are done! Every time you now plug in your external Iomega USB drive aufosf will add your device to a list of Active Mount Points. Plugin your external USB drive now and execute: /etc/init.d/autofs status

OUTPUT:

Configured Mount Points:

/usr/sbin/automount --timeout=10 /media file /etc/auto.ext-usb ,defaults,user,exec,uid=1000

Active Mount Points:

/usr/sbin/automount --pid-file=/var/run/autofs/_media.pid --timeout=10\ /media file /etc/auto.ext-usb ,defaults,user,exec,uid=1000

and

mount

OUTPUT: automount(pid7124) on /media type autofs (rw,fd=4,pgrp=7124,minproto=2,maxproto=4)

Please note, although our drive is now listed as an active mount point the disk is not mounted yet! autofs only waits for user to access specified mount point directory and once that happens it will mount the filesystem.

Example: $ cd /media/ $ ls $ cd Iomega $ ls lost.dir music picture ps3 video mystuff $ cd .. $ ls Iomega

From the output above you can see that Iomega directory was created only when I tried to access it. Every time you now plug in you USB external disk you can instantly access it via some sort of Desktop or Bookmark shortcut. I hope this helps to create and run a script as the name of the usb will remain same everytime you plug it in.

Amir
  • 193
  • 1
  • 3
  • 13
0

You can use part of the answer here which deals with adding udev rules: https://unix.stackexchange.com/questions/74123/what-is-the-service-thats-responsible-for-automounting-a-usb-drive-in-centos-6

  • Add a file to /etc/udev/rules.d
  • Add lines as per the answer, but add the label argument to the pmount command, eg:

automount.rules

# automounting usb flash drives
# umask is used to allow every user to write on the stick
# we use --sync in order to enable physical removing of mounted memory sticks -- this is OK for fat-based sticks
# I don't automount sda since in my system this is the internal hard drive
# depending on your hardware config, usb sticks might be other devices than sdb*
ACTION=="add",KERNEL=="sdb*", RUN+="/usr/bin/pmount --sync --umask 000 %k usbstick"
ACTION=="remove", KERNEL=="sdb*", RUN+="/usr/bin/pumount %k"
ACTION=="add",KERNEL=="sdc*", RUN+="/usr/bin/pmount --sync --umask 000 %k usbstick"
ACTION=="remove", KERNEL=="sdc*", RUN+="/usr/bin/pumount %k"
  • Reload the udev rules: sudo udevadm control --reload-rules

This will ensure the stick is mounted in the same place every time. There is probably a more elegant way of writing the udev rule to detect USB sticks.

Note that you can also get udev to automatically fire your backup script after the mount.

James Yale
  • 5,042
  • 1
  • 16
  • 20