Alpine Linux is a lightweight and secure operating system commonly used in embedded systems and containers. If you are using Alpine Linux and need to automatically mount a USB drive when it is plugged in, this article will guide you through the process. Please note that some basic knowledge of Linux command line is required.
Step 1: Check USB Drive
Before proceeding, make sure your USB drive is properly connected to your Alpine Linux system. You can use the following command to check if the USB drive is detected:
$ lsblk
This command will list all the available block devices, including your USB drive. It will show the device name, size, and other information. Take note of the device name, as we will need it in the next steps.
Step 2: Install Required Packages
We need to install two packages to enable automatic mounting of USB drives:
$ apk add udev usbutils
This command will install the udev package, which handles device management, and the usbutils package, which provides utilities for USB devices.
Step 3: Create udev Rule
Now we need to create a udev rule that will trigger the automatic mounting of the USB drive. Create a new file using your preferred text editor (e.g., nano) with the following command:
$ nano /etc/udev/rules.d/10-usb-mount.rules
In the newly created file, add the following content:
ACTION=="add", SUBSYSTEMS=="usb", KERNEL=="sd[a-z][0-9]", RUN+="/bin/mount /dev/%k /mnt"
This udev rule will match any USB device that is added to the system and has a device name starting with sd followed by a letter (a to z) and a number (0 to 9). It will then run the mount command to mount the device on the /mnt directory.
Save the file and exit the text editor.
Step 4: Reload udev Rules
After creating the udev rule, we need to reload the udev rules to apply the changes. Use the following command:
$ udevadm control --reload-rules
This command will reload the udev rules without requiring a system restart.
Step 5: Test Automatic Mounting
Now it's time to test if the automatic mounting works. Simply unplug and then plug in your USB drive. After plugging it back in, you should see the USB drive automatically mounted on the /mnt directory.
You can check the mount status using the following command:
$ mount | grep /mnt
This command will show the mounted devices on the /mnt directory. If your USB drive is listed, it means the automatic mounting is working correctly.
Step 6: Configure Mount Options (Optional)
By default, the USB drive will be mounted with the default mount options. If you want to modify the mount options, you can do so by editing the udev rule file created earlier.
Open the udev rule file using your preferred text editor:
$ nano /etc/udev/rules.d/10-usb-mount.rules
Find the line that contains the mount command and modify it according to your needs. For example, if you want to mount the USB drive with read-only access, you can use the following command:
ACTION=="add", SUBSYSTEMS=="usb", KERNEL=="sd[a-z][0-9]", RUN+="/bin/mount -o ro /dev/%k /mnt"
Save the file and exit the text editor.
Conclusion
By following the steps outlined in this article, you should now have an Alpine Linux system that automatically mounts USB drives when they are plugged in. This can greatly simplify the process of accessing and managing data on USB drives.
References
| Reference | Link |
|---|---|
| Alpine Linux | https://alpinelinux.org/ |
| udev - ArchWiki | https://wiki.archlinux.org/index.php/Udev |