Linux mount check if is mounted

How to check if a filesystem is mounted with a script

I am new at scripting . I can do very basic stuff, but now I need a hand. I have a local filesystem that only will be mounted when I need to do a backup. I’m starting with this.

#!/bin/bash export MOUNT=/myfilesystem if grep -qs $MOUNT /proc/mounts; then echo "It's mounted." else echo "It's not mounted."; then mount $MOUNT; fi 

As I said, I’m very basic at scripting. I heard that you can check the status of the mount command by looking at the return codes.

RETURN CODES mount has the following return codes (the bits can be ORed): 0 success 1 incorrect invocation or permissions 2 system error (out of memory, cannot fork, no more loop devices) 4 internal mount bug 8 user interrupt 16 problems writing or locking /etc/mtab 32 mount failure 64 some mount succeeded 

8 Answers 8

Many Linux distros have the mountpoint command. It can explicitly used to check if a directory is a mountpoint. Simple as this:

#!/bin/bash if mountpoint -q "$1"; then echo "$1 is a mountpoint" else echo "$1 is not a mountpoint" fi 

This approach will fail if you try to check an encfs, since you will stumble into a permission denied error, when run by a non-root user.

You can check the status code of mount , and most well written executables, with the shell special parameter ? .

? Expands to the exit status of the most recently executed foreground pipeline.

After you run the mount command, immediately executing echo $? will print the status code from the previous command.

# mount /dev/dvd1 /mnt mount: no medium found on /dev/sr0 # echo $? 32 

Not all executables have well defined status codes. At a minimum, it should exit with a success (0) or failure (1) code, but that’s not always the case.

To expand on (and correct) your example script, I added a nested if construct for clarity. It’s not the only way to test the status code and perform an action, but it’s the easiest to read when learning.

#!/bin/bash mount="/myfilesystem" if grep -qs "$mount" /proc/mounts; then echo "It's mounted." else echo "It's not mounted." mount "$mount" if [ $? -eq 0 ]; then echo "Mount success!" else echo "Something went wrong with the mount. " fi fi 

For more information on «Exit and Exit Status», you can refer to the Advanced Bash-Scripting Guide.

Источник

How can I know if a partition is mounted or unmounted?

maybe this is a simple thing but I have the following doubt. If I perform fdisk -l, in the output I can find these devices that represent 2 partitions on the /dev/sdb device that is my SD card:

Dispositivo Boot Start End Blocks Id System /dev/sdb1 8192 122879 57344 c W95 FAT32 (LBA) /dev/sdb2 122880 15523839 7700480 83 Linux 

From this output can I know is these partitions are mounted or unmounted ? (I think no). What can I do to know if a specific partition is mounted on my system?

6 Answers 6

The mount command is the usual way. On Linux, you can also check /etc/mtab, or /proc/mounts.

Читайте также:  Adding user in linux with home directory

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). /proc/mounts is guaranteed to be accurate, but obviously only exists if the /proc filesystem is correctly mounted. df reads /etc/mtab via the functions in , so is no more reliable than this method.

You can also use df , which will give you a nicer printout and show the disk usage of the mounted file systems:

$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 27G 8.6G 17G 35% / dev 2.0G 0 2.0G 0% /dev run 2.0G 488K 2.0G 1% /run tmpfs 2.0G 456K 2.0G 1% /dev/shm tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup tmpfs 2.0G 738M 1.3G 38% /tmp /dev/sdb2 715G 515G 164G 76% /home tmpfs 396M 4.0K 396M 1% /run/user/1000 

lsblk is a nice way for humans to see devices and mount-points. See also this answer.

$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 7.3T 0 disk └─dataGB-dataVB 253:1 0 14.6T 0 lvm /mnt/dataB sdb 8:16 0 7.3T 0 disk └─dataGB-dataVB 253:1 0 14.6T 0 lvm /mnt/dataB sdc 8:32 0 7.3T 0 disk └─sdc1 8:33 0 7.3T 0 part └─dataG-data 253:0 0 7.3T 0 lvm /mnt/data sdd 8:48 0 7.3T 0 disk └─sdd1 8:49 0 7.3T 0 part sde 8:64 0 9.1T 0 disk └─sde1 8:65 0 9.1T 0 part /mnt/dataC nvme0n1 259:0 0 232.9G 0 disk └─nvme0n1p1 259:1 0 232.9G 0 part / 

findmnt is useful for scripting or to query a specific device:

$ findmnt /dev/sde1 TARGET SOURCE FSTYPE OPTIONS /mnt/dataC /dev/sde1 xfs rw,relatime,attr2,inode64,noquota 

I suppose you could use the command blkid to list what is mounted (DQMOT). I would suggest setting up your sudo gedit /etc/fstab — if you didn’t know of it — with the outputs for the hard drives blkid picks up. The UUIDs «universally unique identifier» are a better way of mounting than other methods.

#      UUID=9ee10f9f-c7fa-4c94-93dc-d8ca02db9c2f / ext4 errors=remount-ro 0 1 UUID=48ee8-657-3154044569-d52005b00ded-68 none swap sw 0 0 UUID=C8CE6F14CE6EF9D8 /media/john/windows ntfs defaults 0 0 UUID=F4644D2D644CF3C0 /media/john/e ntfs defaults 0 0 

You can also often see in the file manager GUI: win+e and look at whether or not the disks are mounted with the up-turned arrows. You can also mount/un-mount from this menu.

enter image description here

Thanks, but this shows what’s mounted, but not the device node, so it wouldn’t answer the original question — how can I see which device nodes are actually mounted? Is there a way of showing device nodes in this GUI?

The simplest way is use the command mount:

 $ mount /dev/sda1 on / type ext4 (rw,errors=remount-ro) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) none on /sys/fs/cgroup type tmpfs (rw) none on /sys/fs/fuse/connections type fusectl (rw) none on /sys/kernel/debug type debugfs (rw) none on /sys/kernel/security type securityfs (rw) udev on /dev type devtmpfs (rw,mode=0755) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620) tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755) none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880) none on /run/shm type tmpfs (rw,nosuid,nodev) none on /run/user type tmpfs (rw,noexec,nosuid,nodev,size=104857600,mode=0755) none on /sys/fs/pstore type pstore (rw) /dev/sda6 on /home type ext4 (rw) binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev) rpc_pipefs on /run/rpc_pipefs type rpc_pipefs (rw) systemd on /sys/fs/cgroup/systemd type cgroup (rw,noexec,nosuid,nodev,none,name=systemd) 

This information is stored in /etc/mtab, you can see by yourself that the output of mount is nearly identical to that of /etc/mtab

Источник

How to check whether a particular directory is a mount point? [duplicate]

How to check whether or not a particular directory is a mount point? For instance there is a folder named /test that exists, and I want to check if it is a mount point or not.

Читайте также:  What is drm linux

You don’t mount directories on Linux. You mount devices to particular directories. Checking to see if something is mounted is as simple as looking at the output of the mount command.

The answers below will show you how to examine the mount table, but a simpler solution is to create a file in the mount point directory before anything is mounted on it. Call it anything you like, but one example is NOTMOUNTED . When you can see the file, the directory is not a mount point, and when you don’t, it is.

6 Answers 6

If you want to check it’s the mount point of a file system, that’s what the mountpoint command (on most Linux-based systems) is for:

if mountpoint -q -- "$dir"; then printf '%s\n' "$dir is a mount point" fi 

It does that by checking whether . and .. have the same device number ( st_dev in stat() result). So if you don’t have the mountpoint command, you could do:

perl -le '$dir = shift; exit(1) unless (@a = stat "$dir/." and @b = stat "$dir/.." and ($a[0] != $b[0] || $a[1] == $b[1]))' "$dir" 

Like mountpoint , it will return true for / even if / is not a mount point (like when in a chroot jail), or false for a mount point of a bind mount of the same file system within itself.

Contrary to mountpoint , for symbolic links, it will check whether the target of the symlink is a mountpoint.

At least on Ubuntu, the output of mountpoint already says «$dir is a mountpoint» ,so you don’t need the if part around it.

@SergiyKolodyazhnyy the -q means quiet output, so in this case, the output is being suppressed and then replaced by custom output. Unnecessary, but yeah.

@Wyrmwood I know what the options are. What I’m saying is that mountpoint «$dir» already does exactly the same as the 3-line if statement does here. Functionally they’re the same. Such use of mountpoint -q in if statement can be used when you want to perform some action based on the exit status, but for printing the message to user its unnecessary — it’s already the default behavior of the program.

As HalosGhost mentions in the comments, directories aren’t necessarily mounted per se. Rather they’re present on a device which has been mounted. To check for this you can use the df command like so:

$ df -h /boot/ Filesystem Size Used Avail Use% Mounted on /dev/hda1 99M 55M 40M 59% /boot 

Here we can see that the directory /boot is part of the filesystem, /dev/hda1 . This is a physical device, on the system, a HDD.

You can also come at this a little bit differently by using the mount command to query the system to see what devices are currently mounted:

$ mount | column -t /dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) /dev/hda1 on /boot type ext3 (rw) tmpfs on /dev/shm type tmpfs (rw) /dev/mapper/lvm--raid-lvm0 on /export/raid1 type ext3 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) nfsd on /proc/fs/nfsd type nfsd (rw) 

Here you can see the type of device and the type of filesystems are currently mounted on your system. The 3rd column shows where they’re mounted on the system within its filesystem.

Читайте также:  Linux настройка uefi bios

Источник

How to Check if a Filesystem is Mounted in Linux?

In Linux, a “mount” is the process of making a file system accessible at a certain point in the directory tree. This directory or the location is known as the “mount point”. The purpose of mounting a file system is to make it accessible to the system and its users so that files and directories within the file system can be read, written, and manipulated.

This tutorial will illustrate different methods to check if the filesystem is mounted in Linux.

Method 1: Using mount Command

The “mount” command is utilized to list all the currently mounted file systems. It is a method to discover all filesystems in the system. For instance, type the “mount” command without any argument as below:

The output will list all currently mounted filesystems, including “sysfs”, “proc”, “devtmpfs”, “ext4”, and many more in the terminal.

Method 2: Using df Command

The “df” command is utilized to check whether the filesystem is mounted. It shows the file systems and their mount points, as well as usage statistics:

The output returns the available disk space on all mounted filesystems.

Method 3: Using lsblk Command

The “lsblk” command shows a tree-like representation of the file systems and their mount points. If the file system you’re interested in is listed, execute the “lsblk” command:

This output lists all block devices and their associated filesystems. If the filesystem you want to check is mounted, it will be listed under the “MOUNTPOINT” column.

Method 4: Using the “/proc/mounts” File

The “/proc/mounts” file has a list of mounted file systems and their mount points. Users can utilize the “cat” command to display the content in the file system to check the mounted filesystem:

The outcome of the above command represents all currently mounted filesystems.

Method 5: Using the findmnt Command

The “findmnt” command is similar to the “mount” command but more flexible and user-friendly. It can filter, search and list the mounted filesystems as below:

The output lists all mounted file systems and other information.

Method 6: Using Disks Application (GUI-based)

To check if a file system is mounted in Linux, use the “Disks” application. It is a GUI tool for managing storage devices on Linux. The Disks application can be accessed through the main menu or by searching for “Disks” in the launcher. Once the Disks application is opened, it will show all the connected storage devices and their partitions as below:

The mounted partitions will have a mount point (/boot/efi) highlighted in the above figure.

Conclusion

Linux offers “mount”, “df”, “cat /proc/mounts”, “lsblk”, and “findmnt” commands to check the mounted filesystem. All these commands will show the current file systems mounted in the system and the path where they are mounted. Additionally, users can visualize through the “Disks” application. This article has briefly explained different methods to check the mounted filesystem in Linux.

Источник

Оцените статью
Adblock
detector