Ferk / udev-media-automount

A simple automount mechanism using udev rules
Other
121 stars 59 forks source link

Replace " with ' to handle " " (space) character in partition labels #12

Closed timsummerer closed 1 year ago

timsummerer commented 1 year ago

When automounting an exFAT Disk with a space in the partition label, a duplicate mount was created because the space in the awk command was not escaped. By using '' instead of "", the space is escaped properly and only one mount point is created for the affected partition/thumb drive.

Ferk commented 1 year ago

Thanks for the PR! But are you sure that it works?

I think the '' is just closing the quote and then opening it.. so '$2==''$dir''{exit 1}' is equivalent to just writing '$2==$dir{exit 1}' (the string $dir is being sent to awk instead of the content of the variable, I'm not sure if awk can use environment variables directly that way).

dir='this is a dir' && echo '$2=="'$dir'"{exit 1}'
# result: $2=="this is a dir"{exit 1}

dir='this is a dir' && echo '$2==''$dir''{exit 1}'
# result: $2==$dir{exit 1}

dir='this is a dir' && echo '$2==$dir{exit 1}'
# result: $2==$dir{exit 1}

If you want to send ' instead of " to awk, you could escape the inside ' so it actually sends it and the content of the variable can be surrounded by single quotes instead of double quotes, like below.

dir='this is a dir' && echo '$2=='\'$dir\''{exit 1}'
# result: $2=='this is a dir'{exit 1}

But it's likely switching the quotes like this won't make a difference. Most likely there's other reason for the problem. Like how awk might be taking only the first word as the $2 element.

Also, I don't understand why this would be the line that would be the cause of the problem, since that line is just for preventing cleaning folders that have been manually configured into /etc/fstab, and that shouldn't be your case, since those are explicitly prevented from being automounted at all by the script in the first place.

I'm not at my home computer right now, but once I return and get the chance I'll run some tests with a removable device with spaces in the label, to try and reproduce your problem.

timsummerer commented 1 year ago

You are probably right, there might be another reason for the duplicate mounting problem that I have experienced. I am very much not familiar with awk. Edit: The issue has reappeared so the quotation is not the issue. Perhaps the find: ‘/media/USB 128GB.exfat*’: Datei oder Verzeichnis nicht gefunden line in the log might be related to the issue.

The following is an example log that I looked at, where the duplicate mount occurred (some parts are in german; any translation app should be fine if needed):

Dez 26 11:32:16 pi4b media-automount[450]: device doesn't exist anymore or is not a block device: /dev/sdio
Dez 26 11:32:16 pi4b media-automount[477]: awk: line 1: runaway string constant "/media/USB ...
Dez 26 11:32:16 pi4b media-automount[474]: awk: line 1: runaway string constant "/media/USB ...
Dez 26 11:32:16 pi4b media-automount[457]: device doesn't exist anymore or is not a block device: /dev/bcm2835
Dez 26 11:32:16 pi4b media-automount[488]: awk: line 1: runaway string constant "/media/USB ...
Dez 26 11:32:16 pi4b media-automount[472]: /dev/sda has no known filesystem type, ignoring mount request
Dez 26 11:32:16 pi4b media-automount[492]: awk: line 1: runaway string constant "/media/USB ...
Dez 26 11:32:16 pi4b media-automount[444]: device doesn't exist anymore or is not a block device: /dev/sdhci
Dez 26 11:32:16 pi4b media-automount[494]: awk: line 1: runaway string constant "/media/USB ...
Dez 26 11:32:16 pi4b media-automount[505]: awk: line 1: runaway string constant "/media/USB ...
Dez 26 11:32:16 pi4b media-automount[507]: awk: line 1: runaway string constant "/media/USB ...
Dez 26 11:32:16 pi4b media-automount[515]: awk: line 1: runaway string constant "/media/USB ...
Dez 26 11:32:16 pi4b media-automount[516]: rmdir: konnte '/media/tim' nicht entfernen: Das Verzeichnis ist nicht leer
Dez 26 11:32:16 pi4b media-automount[504]: rmdir: konnte '/media/tim' nicht entfernen: Das Verzeichnis ist nicht leer
Dez 26 11:32:16 pi4b media-automount[508]: rmdir: konnte '/media/tim' nicht entfernen: Das Verzeichnis ist nicht leer
Dez 26 11:32:16 pi4b media-automount[522]: rmdir: konnte '/media/tim' nicht entfernen: Das Verzeichnis ist nicht leer
Dez 26 11:32:16 pi4b media-automount[512]: mounting device /dev/sda1 in /media/USB 128GB.exfat
Dez 26 11:32:17 pi4b media-automount[583]: find: ‘/media/USB 128GB.exfat*’: Datei oder Verzeichnis nicht gefunden
Dez 26 11:32:17 pi4b media-automount[586]: mounting device /dev/sda1 in /media/USB 128GB.exfat_1
Dez 26 11:32:17 pi4b media-automount[599]: Device successfully mounted: /media/USB 128GB.exfat_1
Dez 27 17:25:19 pi4b media-automount[5193]: /dev/sdb has no known filesystem type, ignoring mount request
Dez 27 17:25:19 pi4b media-automount[5202]: loading configuration for fs type ntfs
Dez 27 17:25:19 pi4b media-automount[5205]: mounting device /dev/sdb1 in /media/Elements.ntfs
Dez 27 17:25:54 pi4b media-automount[5218]: Device successfully mounted: /media/Elements.ntfs

As you can see awk was complaining about a runaway string constant which I assumed to be caused by the space in the partition label "USB 128GB". The idea I had for changing from " to ' was that IIRC bash needs less characters to be escaped inside a single quote than a double quote.

Currently with the quotation changed, the log looks like this: (I also created a config for exfat filesystems after I worked around the duplicate mount)

Dez 29 18:20:00 pi4b media-automount[452]: device doesn't exist anymore or is not a block device: /dev/sdhci
Dez 29 18:20:00 pi4b media-automount[458]: /dev/sda has no known filesystem type, ignoring mount request
Dez 29 18:20:00 pi4b media-automount[444]: /dev/sdb has no known filesystem type, ignoring mount request
Dez 29 18:20:00 pi4b media-automount[474]: device doesn't exist anymore or is not a block device: /dev/bcm2835
Dez 29 18:20:00 pi4b media-automount[499]: loading configuration for fs type ntfs
Dez 29 18:20:00 pi4b media-automount[480]: device doesn't exist anymore or is not a block device: /dev/sdio
Dez 29 18:20:00 pi4b media-automount[513]: rmdir: konnte '/media/Elements.ntfs' nicht entfernen: Datei oder Verzeichnis>
Dez 29 18:20:00 pi4b media-automount[517]: rmdir: konnte '/media/Elements.ntfs' nicht entfernen: Datei oder Verzeichnis>
Dez 29 18:20:00 pi4b media-automount[527]: rmdir: konnte '/media/USB 128GB.exfat' nicht entfernen: Datei oder Verzeichn>
Dez 29 18:20:00 pi4b media-automount[529]: mounting device /dev/sdb1 in /media/Elements.ntfs
Dez 29 18:20:01 pi4b media-automount[592]: loading configuration for fs type exfat
Dez 29 18:20:01 pi4b media-automount[596]: loading configuration for fs type ntfs
Dez 29 18:20:01 pi4b media-automount[598]: mounting device /dev/sda1 in /media/USB 128GB.exfat
Dez 29 18:20:01 pi4b media-automount[600]: mounting device /dev/sdb1 in /media/Elements.ntfs
Dez 29 18:20:01 pi4b media-automount[638]: Device successfully mounted: /media/USB 128GB.exfat
Dez 29 18:20:03 pi4b media-automount[683]: Device successfully mounted: /media/Elements.ntfs

The USB 128GB partition is mounted only once here as it should be.

I really just wanted to share my workaround in case someone else runs into the same issue and also hoping that this might be fixed in a future version :)

PS I just remembered that changing the partition label to "USB_128GB" did not make a difference. I tried this before changing the quotation marks, so perhaps the issue might be something else than the space.

Ferk commented 1 year ago

Thank you very much @Summerer for checking on this. In 2d176deca16dcd4444fa89ed93d7759dedcf4a6c I've replaced awk with grep. I think it should be working more universally that way, without quirks due to awk's interpretation of quotes. grep is probably better suited for this, and it also avoids the $2 problem I was talking about before.