
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:
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:
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.
Explanation
The script works as follows:
- We define a list of drives (`/dev/sda`, `/dev/sdb`, `/dev/sdc`) to be checked for mounting.
- The script loops through each drive and checks if it is already mounted by using the `udisksctl info -b $drive` command.
- The `grep "MountPoints"` command extracts the mount point, and `awk '{print $2}'` retrieves its value. If the mount point is empty (i.e., the drive is not mounted), the script proceeds to mount the drive using the `udisksctl mount -b $drive` command.
- If the drive is already mounted, the script skips to the next one and informs the user that the drive is mounted.
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:
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:
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.