0

I would like to mount a partition of an external hard drive from a script using

udisksctl mount --block-device /dev/sda1

and in the end of the script I would like to unmount it.

However, it may happen that the partition has already been mounted (through the file manager GUI, for example). In this case the script prints an error message:

Error mounting /dev/sda1: GDBus.Error:org.freedesktop.UDisks2.Error.AlreadyMounted: Device /dev/sda1 is already mounted at `/media/alexey/[...]'.

I can live with it, but I am looking for a better way. (In particular, currently I cannot use set -e in the script.)

I didn't find any option for udisksctl to return without errors if the partition is already mounted.

Should I use if? How should I test if the partition is mounted? Is there an idiomatic solution?

4 Answers 4

2

First, I wonder re your use of udiskctl in a script. This udiskctl man page has the following caution:

Additionally, this program is not intended to be used by scripts or other programs...

It seems to be designed for use by non-root users... is this what you needed?

Second, I wonder if findmnt might not work for you - in terms of checking if your device is mounted? Rather than speculate further, I'll just add a simple script that you could use as a stand-alone, or incorporate in your script if you think it will resolve your issue:

   #!/usr/bin/env bash
   # My $0 is bfw-verify.sh; I am run as root! 
   if ! findmnt /dev/sda >/dev/null; then
      # note we depend upon $? (exit status), so can discard output
      echo "/dev/sda in not mounted"
   else
      echo "/dev/sda is already mounted"
      # do something else?; maybe 'udisksctl mount --block-device /dev/sda1' ??
   fi 
2
  • 1
    Thanks, I'll look into what you suggest. I am using udiskctl because I do not know how else to replicate from a script the usual automatic or GUI-operated mounting of external drives. I believe to understand that it is done through udisks. The stability of the CLI is not a big issue for my use case. Commented Nov 12 at 23:07
  • @Alexey: "I do not know how else to replicate from a script the usual automatic or GUI-operated mounting of external drives."... If you have root privileges, you could simply use mount. If you don't, you could read this article to understand your options for mounting as a regular user. In either case, the script above should work for you to determine beforehand (and avoid errors) whether or not the device is mounted. Commented Nov 12 at 23:41
2

Your assumption/premise is wrong. You can use set -e in a script where a program might return an error as long as you catch the error and preferably deal with it (although a no-op like : works just as well as real code for the purpose of preventing early termination).

For example, consider these two trivial examples, which both use false to trigger a non-zero exit code (i.e. an error):

  1. uncaught.sh:

    #!/bin/bash
    
    set -e
    
    false
    
    echo "the script will never reach this point"
    
  2. caught.sh:

    #!/bin/bash
    
    set -e
    
    if false ; then
     : do something about the error here
    fi
    
    echo "still running, this line will execute"
    

The first script will not produce any output. The second will.

As long as you catch the error, e.g. with an if statement, or by using && or ||, the script will not terminate even if the script uses set -e.

Here's another trivial example, caught2.sh, this time using false twice:

#!/bin/bash

set -e

false || echo error

false && echo no error

echo "still running, this line will execute"

Like the second script, this one will have printed output, a line containing only "error" and the "still running..." line, because the "errors" were caught.

If you don't want to or don't know how to deal with the error correctly, or if it doesn't matter whether a command succeeds or not, it's not uncommon to just use true to discard the exit code. e.g.

udisksctl mount --block-device /dev/sda1 || true

The script will continue on to the next statement whether the mount succeeded or failed. For a mount fs operation in particular, this is bad practice (you really should capture the exit code and take appropriate actions depending on the nature of the error¹ - e.g. permissions problem, fs already mounted, device is missing or what was known as sda1 is now called sdb1 after the last reboot, cosmic horror flipped my bits, or whatever), but it "works".


¹ Unfortunately, the udisksctl man page doesn't document the exit codes. You'll have to examine the source or search for other documentation.


BTW, you should check the exit code when you unmount the fs too, there are many reasons why an unmount might fail even though you were able to successfully mount the fs. Including most of those that might prevent a successful mount plus a few others like: a file being open on that fs, or a process having its CWD inside the mounted fs.

1

The mountpoint command (check man mountpoint) will tell you if a directory has something mounted on it.

1
  • 1
    How will this tell whether or not /dev/sda1 is mounted? Commented Nov 12 at 23:48
0

I always create a mount point with any new install, I used to test for that & create it, but easier just to have it. Or use the defaults in /media/fred as I label all partitions.

I have never tested an unmount as often it says done, when external flash drive is still flashing. And manual safely remove does not immediately work.

And then in my scripts I do this;

DEST="/media/fred/data-a"
if grep -qs "$DEST" /proc/mounts; then
  echo "It's mounted."
else
  echo "It's not mounted."
  mount -L data-a -t ext4 /media/fred/data-a
  if [ $? -eq 0 ]; then
   echo "Mount success!"
  else
   echo "Something went wrong with the mount..."
   exit $?
  fi

fi

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.