1

I've recently made 2 script that take care of mounting and dismounting a harddrive in linux. The thing that I could not figure out is the following. I am currently mounting /dev/sdc1 to /home/media/externalHardDrive. The thing is, when I insert another usb device (like usb stick) while the harddrive is not being inserted it will most likely put the usb-stick on /dev/sdc1. I would like to learn a way to identify the device before mounting it, so I can make sure that only the harddrive is affected by this script.

These are my scripts: unmount_script.sh:

#!/bin/bash

MOUNT="/home/media/externalHardDrive"

if grep -qs "$MOUNT" /proc/mounts; then
  umount "$MOUNT"
  if [ $? -eq 0 ]; then
    echo "HardDrive kan veilig worden verwijderd :D"
  else
    echo "Er is iets mis gegaan, blijf overal vanaf :("
  fi
else
  echo "Er is geen HardDrive gemount op $MOUNT, deze kan daarom niet verwijderd worden!"
fi

mount_script.sh

#!/bin/bash

MOUNT="/home/media/externalHardDrive"

if grep -qs "$MOUNT" /proc/mounts; then
  echo "HardDrive is al gemount op $MOUNT ;)"
else
  mount /dev/sdc1 "$MOUNT"
  if [ $? -eq 0 ]; then
    echo "HardDrive is succesvol gemount :D"
  fi
fi

Can somebody point me in the right direction? I am running these scripts on a debian server.

2

At a point in time where you know your USB drive is on /dev/sdc0 do the following:

ls -l /dev/disk/by-uuid | grep -F /dev/sdc1

and note the number between the time (HH:MM) and the ->, lets assume its xyz. That xyz is the drives UUID it normally doesn't change (unless you make a new filesystem and don't provide this number again as an option).

You can use that UUID to mount instead of the device when mounting:

mount UUID=xyz /home/media

or insert that UUID=xyz in the first column of an /etc/fstab entry, then you can just do mount /home/media.

Alternatively you can assign a label to a filesystem (e.g. using the -L option for mkfs.ext4 or mkfs.btrfs and use LABEL=thelabelyouspecified in a similar fashion.

  • Thank you very much for the answer, I am going to try this tonight :D – Sander Koenders Nov 7 '14 at 8:54
  • Just a quick thing I noticed, when I try: ls -l /dev/disk/by-uuid | fgrep /dev/sdc1 (fdisk -l shows that it's on /dev/sdc1) I don't get any output. – Sander Koenders Nov 7 '14 at 8:58
  • 1
    @SanderKoenders You should put that in your /etc/fstab as that makes solving the problem in your other question more easy. But you can try first with a manual mount UUID=xyz /home/media. – Anthon Nov 7 '14 at 9:12
  • 1
    The fifth field is for dump and nowadays seldom used. The sixth for the order of mounting (i.e. / needs to be mounted before /home etc). – Anthon Nov 7 '14 at 9:19
  • 1
    No 0 2 should be fine, especially since noauto is specified and the drive not mounted on bootup anyway ;-) – Anthon Nov 7 '14 at 9:21
2

Use udev for mounting the hard drives. It will automate the task and make identification a lot easier. If connected device is a hard drive, there will be a $ID_ATA=1 variable in your script.

  • Well the problem is that it has to be easy for my father to dismount. So I made a simple web interface which runs these scripts. – Sander Koenders Nov 7 '14 at 8:50
  • 1
    Mouting will be done automatic – just connect the drive. If it is USB and is a hard drive - then mount. Unmounting you can make as your father like. Multiple drives are supported. – Scyld de Fraud Nov 7 '14 at 9:25

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.