Prevent nautilus showing partition mounted in bash script

In my bash script I mount partitions, check them, copy files to them, and unmount. When the script mounts the partition, Nautilus pops up with a Window showing the partition and stealing focus. This is something I want to avoid.

Note: I know I can change the behaviour of this in System settings, Details, Removable media, Never prompt or start programs on media insertion, but I don't want to change the behaviour e.g. if a USB stick is plugged in, I just want to prevent it in my bash script. Since this script runs on other users' computers I cannot modify these settings (in the script) because if it is terminated abnormally it will leave the modified settings in place (I also don't believe it should be necessary to do this).

Actually this auto display doesn't seem consistent. If I do the exact same command from the terminal, Nautilus doesn't show, and I know there are other mounts in my script that don't show. So what could be causing this? Here's an example of the code (both cause separate nautilus windows to pop):

mount $dev $target
mkdir -p $target/home
mount $homedev $target/home

I'm interested in understanding the process by which the mount signals Nautilus and why this is happening (when it doesn't if I mount the partition from the terminal). And what is the best way to prevent this from happening.

I noticed this behaviour starting on release 11.10 (if I recall correctly). I'm currently running 12.04, but the script has to work on all supported releases. I don't believe the solution should involve modifying system settings (that might differ between releases). For example, if you look at other things that mount partitions (like os-prober), there are no nautilus windows popping up when it mounts different partitions.

The script can be found here. And here's the part where Nautilus pops. I think it also pops here, but I've been focusing my testing on the first link. This is where it doesn't pop, called prior to those other mounts - note I've rewritten the first ones as --read-only and within an if statement and it makes no difference.

Ideally the answer will explain why this is happening and how to prevent it, in other words, explain why the in-script mount is treated differently to the one issued from a terminal. A good workaround will also be accepted if it is a fully explained solution and practical that can support all desktop environments (ubuntu, kubuntu, xubuntu, lubuntu etc) - I also want to avoid an exponential increase in testing to verify it.

Thanks in advance

============

Solution as per papashou's link:

add_udev_rules ()
{
    for i in "$dev" "$homedev" "$bootdev" "$usrdev"; do
      if [ -n "$i" ]; then
        block=${i#/dev/}
        echo "KERNEL==\"""$block""\",ENV{UDISKS_PRESENTATION_HIDE}=\"1\"" >> "$wubi_move_dir"/wubi_move.rules
      fi
    done
    cp "$wubi_move_dir"/wubi_move.rules /etc/udev/rules.d/wubi_move.rules
    udevadm trigger > /dev/null 2>&1
}

remove_udev_rules ()
{
    rm /etc/udev/rules.d/wubi_move.rules
    rm "$wubi_move_dir"/wubi_move.rules
    udevadm trigger > /dev/null 2>&1
}

This achieves the desired result, but feels like a sledgehammer approach. I believe there is probably a fine-tuning in udev that will allow the partition to still show in Nautilus (and other file browsers) without popping up when it's mounted. By the way if anyone wants to try this, the following script will show the pop-up effect of nautilus:

mkdir -p /tmp/testmount
mount /dev/sda1 /tmp/testmount
sleep 3
umount /dev/sda1 
mount /dev/sda1 /tmp/testmount
sleep 3
umount /dev/sda1 
mount /dev/sda1 /tmp/testmount
sleep 3
umount /dev/sda1 
mount /dev/sda1 /tmp/testmount
sleep 3
umount /dev/sda1 
rmdir /tmp/testmount

I'm going to keep playing with udev and I've also filed a bug, since this doesn't make any sense for a partition that's not inserted to act like an inserted USB/media card upon mount. Also the udevadm trigger is not an inexpensive operation that appears (on 12.04 at least) to temporarily freeze everything.

So, in summary, I have a workable solution (thanks to everyone who helped out), which I may or may not implement (not sure yet); but I'll keep looking for something else. Thanks


Solution 1:

Try this suggestion: http://www.worldofnubcraft.com/969/hide-your-disks-or-partitions-from-nautilus/

I'm fairly sure it has to do with udev signals and processing.

Solution 2:

Try wrapping your script:

#!/bin/bash
#

# disable nautilus automount
#
gconftool --type Boolean --set /apps/nautilus/preferences/media_automount  false

# put your script here  
#
...

# enable back nautilus automount
# 
gconftool --type Boolean --set /apps/nautilus/preferences/media_automount  true

Not tested cause I'm on 10.04

UPDATE:

#!/bin/bash
#

# disable nautilus automount
#
gconftool --type Boolean --set /apps/nautilus/preferences/media_automount  false

# put CALLING your script here to survive its crashes
#
/bin/bash -c "/whenever/it/is/placed/script.sh"

# enable back nautilus automount
# 
gconftool --type Boolean --set /apps/nautilus/preferences/media_automount  true

Solution 3:

You can prevent flash drive automounting by adding a new udev rule. In that rule you can specify a flash drive by its vendor, serial number, other attributes. See ATTRS{} lines in the output of:

udevadm info -a --name=sdb1

That script you should place into /etc/udev/rules.d folder with special file name, e.g. 81-usb-wubi-move.rules

The rule could look like

ACTION=="add", KERNEL=="sdb?", SUBSYSTEMS=="usb", ATTRS{serial}=="001CC07CEE5EFB91C91B235C", ENV{UDISKS_PRESENTATION_HIDE}="1", ENV{UDISKS_PRESENTATION_NOPOLICY}="1"

See also:

  • How to understand the flow of USB detection?
  • Writing udev rules
  • udev rule to hide partitions from thunar [solved]
  • HakAday - How to write udev rules