You are not logged in.

#1 2024-11-17 15:48:08

SeagullFish
Member
Registered: 2023-08-10
Posts: 78

Bash script: How to check if partition if mounted or not

Hello.

I want to write a bash shell script that checks if a certain partition has been mounted at a specific mount point, and then executes a task if the partition is mounted. But in my case, the checking seems to be a bit difficult.

The partition in question is stored on a removable USB drive, so it will not allways be present. But every time the partition is present, it is desirable that it will allways be mounted at the same mount point. So to achieve this, I have listed the partition in /etc/fstab, like this:

UUID=xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx	/path/to/mountpoint	ext4	noatime,user,nofail,x-systemd.device-timeout=100ms,x-systemd.automount,x-systemd.idle-timeout=20min    0   0

(Note: It is possible that I am not using the best configuration of /etc/fstab. I would appreciate input on this, if a better configuration could solve the issue in my question.)

Anyway, the basic behaviour of the script should analogously correspond to this:

if /path/to/mountpoint != mounted; then
	# Error!
else
	# Do something
fi

I have browsed the web to find a solution, but the suggestions that I have found does not quite seem to work:

Suggestion 1:

From this site:

#!/bin/bash
if mount | grep /path/to/mountpoint > /dev/null; then
    echo "yay"
else
    echo "nay"
fi

In my case, this code will allways return “yay”, regardless of whether the partition in question is actually mounted or not.

The most probable explanation for this behaviour could be derived from to the first comment to the answer to this thread:

Jules wrote:

Note that mount simply displays the contents of /etc/mtab, which is a static file that can become out-of-date (most notably if the root fs is mounted read-only, but also if mounts are changed via direct syscalls rather than using the mount and umount utilities).

In my case, it seems that the mount point is allways mentioned in /etc/mtab (which in Arch Linux seems to be a symbolic link to /proc/self/mount), regardless of whether the partition is mounted or not. When the partition is not mounted, the related line in /etc/mtab looks like this:

systemd-1 /path/to/mountpoint autofs rw,relatime,fd=62,pgrp=1,timeout=1200,minproto=5,maxproto=5,direct,pipe_ino=5441 0 0
Suggestion 2:

From this site:

$ mountpoint /path/to/mountpoint

In my case, this code will allways return with the following result, regardless of whether the partition is mounted or not (assumingly for the same reason as in Suggestion 1):

/path/to/mountpoint is a mountpoint
Suggestion 3:

From this site:

#!/bin/bash
if [[ $(findmnt -M /path/to/source) ]]; then
    echo "Mounted"
else
    echo "Not mounted"
fi

In my case, this code will allways return “Mounted”, regardless of whether the partition is mounted or not (assumingly for the same reason as in Suggestion 1).

Suggestion 4:

From this site:

#!/bin/bash
if awk -v status=1 '$2 == "/path/to/mountpoint" {status=0} END {exit status}' /proc/mounts; then
  echo "yes"
else
  echo "no"
fi

In my case, this code will allways return “yes”, regardless of whether the partition is mounted or not (assumingly for the same reason as in Suggestion 1).

Question:

Any other suggestions, anyone?

Offline

#2 2024-11-17 16:18:59

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,732

Re: Bash script: How to check if partition if mounted or not

It's because you're using systemd-x-automount, just grep mtab for the correct pattern, ie. w/o the device being systemd-1

Offline

#3 2024-11-17 18:35:49

SeagullFish
Member
Registered: 2023-08-10
Posts: 78

Re: Bash script: How to check if partition if mounted or not

seth wrote:

[...] just grep mtab for the correct pattern, ie. w/o the device being systemd-1

Well, filtering output from grep is not something that I have much experience with. After reading the examples on this page, I made my first attempt. Unfortunately, I failed.

This is what I tried:

#!/bin/bash

t=false

grep "/path/to/mountpoint" /etc/mtab | awk '{print $1}' | while read -r line; do
  if [ "$line" != "systemd-1" ]; then
    t=true
  fi
done

if [ "$t" != "true" ]; then
  echo "Error: Partition not mounted."
else
  echo "OK."
fi

So I mounted the partition, and gave the script a go. This is the output:

[foo@bar ~] ./test.sh
Error: Partition not mounted.

In other words, this didn’t quite work out the way I thought it would.
In order to figure out what’s going on, I added some debugging-echo’s to the code:

#!/bin/bash

t=false

#echo "DEBUG 1: $t"
grep "/path/to/mountpoint" /etc/mtab | awk '{print $1}' | while read -r line; do
  echo "DEBUG 2: $line"
  if [ "$line" != "systemd-1" ]; then
    echo "DEBUG 3: $line"
    t=true
    echo "DEBUG 4: $t"
  fi
done

echo "DEBUG 5: $t"

if [ "$t" != "true" ]; then
  echo "Error: Partition not mounted."
else
  echo "OK."
fi

This is the output:

[foo@bar ~] ./test.sh
DEBUG 1: false
DEBUG 2: systemd-1
DEBUG 2: /dev/sda3
DEBUG 3: /dev/sda3
DEBUG 4: true
DEBUG 5: false
Error: Partition not mounted.

It seems that the variable $t changes from "false" to "true" as expected, but then back to "false" again without there being any line telling it to.

Q1: What’s wrong here?
Q2: Is there a better way of doing this?

Offline

#4 2024-11-17 19:03:58

frostschutz
Member
Registered: 2013-11-15
Posts: 1,647

Re: Bash script: How to check if partition if mounted or not

Changing variables in a subshell does not affect the parent shell.

You could check for existence, of /your/mount/point/some-file-on-it, of /dev/disk/by-uuid/your-usb-stick, ...

Sorry if I missed it, you posted above what /proc/self/mounts looks like when it's not mounted. Does it change when it is mounted? I don't use autofs much, so...

Offline

#5 2024-11-17 19:48:16

SeagullFish
Member
Registered: 2023-08-10
Posts: 78

Re: Bash script: How to check if partition if mounted or not

frostschutz wrote:

Changing variables in a subshell does not affect the parent shell.

Hmm... interesting. What's making the sub-shell? Is it the while-loop? How can I transfer a value from the subshell to the parent shell? Or is this generally a bad approach?

frostschutz wrote:

You could check for existence, of /your/mount/point/some-file-on-it, of /dev/disk/by-uuid/your-usb-stick, ...

Those are indeed possible solutions. Though, the former one could fail if the target file should somehow be accidentally deleted. The latter one will probably work well, as long as the partition is never manually mounted at the wrong mounting point by accident.

frostschutz wrote:

Sorry if I missed it, you posted above what /proc/self/mounts looks like when it's not mounted. Does it change when it is mounted? I don't use autofs much, so...

Sorry, my fault! I forgot to mention that. When the partition is mounted, the mounting point is mentioned twice in /etc/mtab. This is what it looks like :

systemd-1 /path/to/mountpoint autofs rw,relatime,fd=62,pgrp=1,timeout=1200,minproto=5,maxproto=5,direct,pipe_ino=5441 0 0
/dev/sds3 /path/to/mountpoint ext4 rw,nosuid,nodev,noexec,noatime 0 0

Offline

#6 2024-11-17 20:35:35

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,732

Re: Bash script: How to check if partition if mounted or not

You could check for existence, of /your/mount/point/some-file-on-it, of /dev/disk/by-uuid/your-usb-stick, ...

Accessint that file will trigger the systemd-x-automount.

Well, filtering output from grep is not something that I have much experience with.

grep -P '^(?!.*systemd-1).* /path/to/mountpoint /etc/mtab && echo yay

Offline

#7 2024-11-17 20:39:38

frostschutz
Member
Registered: 2013-11-15
Posts: 1,647

Re: Bash script: How to check if partition if mounted or not

or something like

awk '$2 == "/path/to/mountpoint" && $3 != "autofs"'

(would only print non-autofs entries)

mountpoint -d might also work

Offline

#8 2024-11-18 07:41:40

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 9,003
Website

Re: Bash script: How to check if partition if mounted or not

[ -e /dev/disk/by-uuid/$uuid ] && stuff

Jin, Jîyan, Azadî

Offline

#9 2024-11-18 07:44:21

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,732

Re: Bash script: How to check if partition if mounted or not

The presence of the partition doesn't say whether it's mounted, does it?

Offline

#10 2024-11-18 07:46:44

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 9,003
Website

Re: Bash script: How to check if partition if mounted or not

In this case it is for a USB drive, which the OP claims will always be mounted at the same point in the filesystem.

EDIT: if the OP uses systemd-automount then the partition would only be mounted when access is attempted rather than when the device is plugged, which might not be what the OP wants. Perhaps a udev rule to mount the stick conventionally would be better.

And apologies to Frostshutz for repeating their earlier advice, I should have read the thread more carefully.

Last edited by Head_on_a_Stick (2024-11-18 07:53:23)


Jin, Jîyan, Azadî

Offline

#11 2024-11-18 07:55:00

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,732

Re: Bash script: How to check if partition if mounted or not

But the question was how to detect whether it's *mounted* and not whether it's present.
Also the OP uses systed-x-automount, so it'll only be mounted after access (and maybe umounted on idle), not on attachment.

If this is "I want to trigger foo whenever I plug this device" the correct answer is a udev rule.

Offline

#12 2024-11-18 10:14:26

archlynovice
Member
Registered: 2023-10-22
Posts: 32

Re: Bash script: How to check if partition if mounted or not

This might not be the shortest way to achieve it, but it uses 'blkid' and 'lsblk' - commands we (as newbies) know from the install guide.

(1) Checks if a device with the UUID of your choice is currently present. This command transforms your UUID into the device name. No output if such device is not present.

blkid -U XXXX-XXXX | awk -F '/' '{print $NF}'

(2) Outputs the device name of mounted device at your /path/to/mountpoint. No output if such mountpoint is not found.

lsblk -o NAME,MOUNTPOINT | awk '$2 == "/path/to/mountpoint" {print $1}' | grep -o '[a-zA-Z0-9-]*'

Compare those strings. If it's a match, you have confirmation.

[!] There is an edge case where it falls apart though: It greps only the last part of a device name. LVM volumes can be named by the user. Strangly named volumes (why would you) can produce false positives / negatives.

The more I use scripts, the more I realize how important 'awk' and 'grep' is. It's worth learning. I'm switching back from Waybar to Swaybar and thus have to pull lots of system parameters with scripts for my status bar.

Last edited by archlynovice (2024-11-18 12:06:29)

Offline

Board footer

Powered by FluxBB