How to Mount Drives Using udisksctl and Automate the Process with a Bash Script

How to Mount Drives Using udisksctl and Automate the Process with a Bash Script

Created by: laczi.ostr

How to Mount Drives Using `udisksctl` and Automate the Process with a Bash Script

Subtitle: Learn how to use `udisksctl` for mounting drives and automate the process using Bash scripting.

Introduction

When working with Linux, mounting drives is a common task that allows access to external storage devices like hard drives and USB drives. While there are multiple ways to mount a drive, using the `udisksctl` command is one of the most user-friendly and straightforward methods available. This guide will walk you through how to manually mount a drive using `udisksctl` and how to automate the process with a simple Bash script.

The manual mounting method is great for one-off scenarios, but if you frequently connect and disconnect drives such as `/dev/sda`, `/dev/sdb`, or `/dev/sdc`, automation can save you significant time and effort. Automating this process will ensure that drives are automatically mounted when they are connected, without requiring manual intervention.

By the end of this article, you will understand how to manually mount drives using `udisksctl` and how to write a Bash script that automates the task for unmapped drives.

Manual Mounting with `udisksctl`

The `udisksctl` command is a part of the udisks package, which provides a command-line interface for working with storage devices on Linux. It offers various options for mounting, unmounting, and querying the status of drives. To manually mount a drive, the basic syntax is as follows:

udisksctl mount -b /dev/sda

Here, the `-b` flag indicates that you are specifying the block device, in this case, `/dev/sda`. Once executed, this command mounts the drive at the default location, usually under `/run/media/username/`. To confirm that the drive has been mounted, you can use the following command:

udisksctl status

This will display the status of all connected and mounted drives.

Automating the Mounting Process with a Bash Script

Manually mounting drives every time they are disconnected can become tedious, especially when dealing with multiple drives like `/dev/sda`, `/dev/sdb`, and `/dev/sdc`. By automating this process using a Bash script, you can ensure that the drives are mounted automatically whenever they are connected.

The following Bash script checks for unmapped drives and mounts them using `udisksctl`. This script can be run at system startup or triggered whenever drives are connected.

#!/bin/bash # List of drives to check drives=(/dev/sda /dev/sdb /dev/sdc) # Loop through each drive for drive in "${drives[@]}" do # Check if the drive is mounted mountpoint=$(udisksctl info -b $drive | grep "MountPoints" | awk '{print $2}') if [ -z "$mountpoint" ]; then # If not mounted, mount the drive echo "Mounting $drive..." udisksctl mount -b $drive else echo "$drive is already mounted." fi done

Explanation

The script works as follows:

Running the Script Automatically

To automate the execution of this script, you can add it to your system's startup routine. This can be done by adding the script to the `/etc/rc.local` file or creating a systemd service. Here’s how to run it via systemd:

[Unit] Description=Automount Drives After=network.target [Service] ExecStart=/path/to/your/script.sh Restart=always [Install] WantedBy=default.target

Once the service file is created and placed in `/etc/systemd/system/`, you can enable the service to start at boot with the following commands:

sudo systemctl enable your-service-name sudo systemctl start your-service-name

Conclusion

Mounting drives manually using `udisksctl` is simple and efficient for occasional use, but automating the process with a Bash script saves time and effort, especially when dealing with multiple drives. The script provided ensures that drives are automatically mounted upon connection, making your workflow more efficient and hassle-free. Additionally, integrating the script with systemd enables the automation to run at boot, ensuring that drives are ready to use without manual intervention.

References