This guide explains how to set up udev rules to automatically mount your phone at /mnt/phone
and synchronize music files from a local directory to the phone when it's connected to your computer.
-
Create Udev Rule for Mounting Phone at
/mnt/phone
-
Create a new udev rule file:
sudo touch /etc/udev/rules.d/9-local.rules
-
Add the following rule to the newly created file:
ACTION=="add", SUBSYSTEM=="block", ATTRS{idVendor}=="your_phone_vendor_id", ATTRS{idProduct}=="your_phone_product_id", RUN+="/usr/bin/systemd-mount --no-block --automount --collect /dev/%k /mnt/phone"
Replace
your_phone_vendor_id
andyour_phone_product_id
with the actual vendor and product IDs of your phone. See Identify Phone Attributes for instructions on how to find these. -
Save the file.
-
-
Create Udev Rule for Running Command When Phone is Mounted
-
Create a new udev rule file:
sudo touch /etc/udev/rules.d/10-local.rules
-
Add the following rule to the newly created file:
ACTION=="add", ENV{DEVTYPE}=="partition", ENV{ID_FS_UUID}=="your_phone_uuid", ENV{ID_FS_MOUNT_POINT}=="/mnt/phone", RUN+="/path/to/your/script.sh"
Replace
your_phone_uuid
with the actual UUID of your phone's filesystem, and/path/to/your/script.sh
with the path to your desired script. -
Save the file.
-
-
Reload Udev Rules
After saving the udev rule files, reload udev rules to apply the changes:
sudo udevadm control --reload-rules && sudo udevadm trigger
Before setting up the udev rules, you need to identify the attributes of your phone that udev can use to reliably identify it when it's connected. This could include attributes like the device name, vendor ID, product ID, or serial number.
-
Plug in Your Phone: Connect your phone to your computer using a USB cable.
-
List USB Devices: Use the
lsusb
command to list all connected USB devices:lsusb
Example Output:
Bus 001 Device 002: ID 1234:5678 YourPhoneManufacturer Phone Model
-
Note Vendor and Product IDs: Note down the Vendor ID and Product ID of your phone. In the example output above,
1234
is the Vendor ID, and5678
is the Product ID. -
Verify Mount Point: Ensure that your phone's filesystem is mounted at the expected mount point. You can use the
mount
command to list mounted filesystems:mount
Example Output:
/dev/sdc1 on /mnt/phone type ext4 (rw,relatime)
Ensure that
/mnt/phone
is the correct mount point for your phone's filesystem.
After modifying or adding new udev rules, you need to reload the udev rules to apply the changes. Use the following command:
sudo udevadm control --reload-rules && sudo udevadm trigger
With these udev rules in place, your phone will be automatically mounted at /mnt/phone
when connected to your computer, and the specified script will synchronize music files from a local directory to your phone. This provides an automated solution for keeping your music library updated on your device without manual intervention. Adjust the attributes in the rules as needed to match your phone's specific characteristics.