3

Whenever I try to use autocompletion in combination with pmount the terminal freezes until I abort via CTRL+C which leads to the autocompletion to actually be executed. Autocompletion works fine in any other respect and interestingly for unmounting pumount does directly and successfully autocomplete to any drives mounted via pmount independently of my working dir.

To be very clear a step-by-step process list:

#start pmount
pmount /de<TAB to autocomplete>
#terminal is frozen
<press CTRL+C once to unfreeze>
pmount /dev/s
pmount /dev/sd<TAB to autocomplete>
#frozen again - note the "d"
<press CTRL+C once to unfreeze>
pmount /dev/sdb

Note that in this case /dev/sdb is the only flash drive present, so the autocompletion is somewhat intelligent not to offer e.g. /dev/sda, but it still freezes.

I have tested a fresh, i.e. unconfigured install of zsh where autocompletion works fine with pmount, however does not behave as intelligently: at pmount /dev/s<TAB> the choice is not reduced to /dev/sdb, but also includes the "root hard drive" /dev/sda and pumount <TAB> autocompletes to entries of the current working directory.

So I assume that it is connected to the supposed intelligent autocompletion behaviour of BASH, but I cannot remotely solve the problem as I do not understand where this behaviour is even configured.

Any ideas how to solve the problem and where it even arises from?

System is Debian 8, pmount version is 0.9.23, BASH is 4.3.30

  • Bash intelligent autocompletes are stored in /usr/share/bash-completions/completions, on my system at least. If nothing else, you could move the file pmount in that directory elsewhere; that should disable the intelligent behavior and fall back on plain filename completion. – Tom Hunt Nov 18 '15 at 22:14
  • Thanks, I found such a file in /etc/bash_completion.d/ and moving it does result in a fall back on simple completion as you predicted. I'll do my very best to find an error there to solve the problem and post it here - for the time being unintelligent behaviour it is. – Fiximan Nov 24 '15 at 20:34
  • I wrote replacement completion functions (on Code Review) for pmount and pumount; it's also added to the Debian bug report as a patch. – Toby Speight Jan 15 at 11:14
3

The autocomplete reference file is stored in /etc/bash_completion.d/pmount on my system.

Solution 1 : (Re)moving/renaming the file

As suggested by Tom Hunt, this is the simplest solution. The hanging stops, however the autocompletion is unintelligent, i.e. sticking to filenames of the current directory.

Solution 2 : Repairing the autocompletion file

The problem with the reference file arises in the function _pmount() at line 62 of the file (added newlines for readability - not in original file):

devices="$( command ls $(grep -v '^[[:space:]]*#' /etc/pmount.allow )\
$(grep 1 /sys/block/*/removable |\
sed -e 's,/sys/block/,/dev/,;s,/removable:1,*,') 2>/dev/null |\
sort -u | sed -e 's,\(^/dev/\)\(.*\),\1\2 \2,' ; \
#this last line is of interest, as the errors occur here
grep $mdir /proc/mounts | sed -e 's,.*\($mdir/[^ ]*\).*,\1,' )"

with the error being that for

grep $mdir /proc/mounts

the variable $mdir is not defined previously and thus the script hangs.

So I added the definition of mdir

mdir="$(readlink -f /media)"

as taken from the _pumount() function of the same script (see line 75) at e.g. line 36 as mdir seems to be standing for media (or mount) directory, i.e. the standard mount point for pmount. Besides that I also added mdir to the local variables of the _pmount() function (line 25) for safety reasons.

Autocompletion now stops hanging, but if a removable drive's partition is mounted, it behaves oddly as it then shows the mounted drive, its mount point and all the information from /proc/mounts, e.g.:

/media/sdc1
rw,nosuid,nodev,noexec,relatime,uid=1000,gid=1000,fmask=0177,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,quiet,utf8,errors=remount-ro

This is due to the very same line 62, where the error occured, now the problem being this part:

grep $mdir /proc/mounts | sed -e 's,.*\($mdir/[^ ]*\).*,\1,'

where the sed command must fail (i.e. not work as intended, not fail as a command) as the single quotes suppress the evaluation of $mdir, thus I changed them to double quotes:

grep $mdir /proc/mounts | sed -e "s,.*\($mdir/[^ ]*\).*,\1,"

pmount autocompletion now skips information from /proc/mounts, but still shows already mounted partitions, including the /media/sdc1 mount point.

I cannot tell which is the standard behaviour of pmount, but I did the following to actually suppress the listing of any partitions that have already been mounted, again initial line 62 is now changed to (added newlines for readability - should be removed in autocompletion script):

 devices="$( command ls $(grep -v '^[[:space:]]*#' /etc/pmount.allow )\
 $(grep 1 /sys/block/*/removable |\
 sed -e 's,/sys/block/,/dev/,;s,/removable:1,*,') 2>/dev/null |\
 sort -u | sed -e 's,\(^/dev/\)\(.*\),\1\2 \2,' \
 #previous block identical to original script
 #the following pipe replaces a semicolon from the original script
 #and so does anything that follows 
 | grep -v "$( if [[ $( grep $mdir /proc/mounts ) ]] ;
               then grep $mdir /proc/mounts |\
                    sed -e "s,.*\($mdir/[^ ]*\).*,\1," \
                        -e "s,$mdir/,," -e 's, ,\|,g' ;
               else echo $mdir ; fi \
             )"
 )"

Now in contrast to the original script, the first two greps' output is piped to a grep where already mounted partitions are filtered versus any mounts in /media as taken from /proc/mounts. The if loop is needed to differentiate between the cases of an empty /media and one with already mounted partitions.

With these changes pmount will behave as below:

pmount <TAB to autocomplete>
/dev/sdc   /dev/sdc1  /dev/sdc2  /dev/sr0   sdc        sdc1       sdc       sr0
pmount sdc1
#successful mounting of sdc1
pmount <TAB to autocomplete>
/dev/sdc  /dev/sdc2  /dev/sr0  sdc       sdc2       sr0

I.e. the mounted partition is suppressed while other partition of the drive (and the drive itself) are still displayed.

Please note the importance of defining mdir as the first step!

Side note on pmount

Note that this autocompletion scans /sys/block/*/removable for 1, i.e. only list devices where the removable bit is set. However other drives may be mounted by entering them via e.g. pmount /dev/sdd1, they just won't show up in the autocompletion. AFAIK this is intended behaviour and I'll keep it as it is. In my case this happens for an external hard drive where I have an ext4 partition running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.