8

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

9
  • For others answering, could you add your Ubuntu Version? e.g. 9.04, 10.10, 12.04. There was a fair bit of evolution from one to another.
    – Paul
    Apr 15, 2012 at 5:37
  • Good point. I'll edit my question to add that.
    – bcbc
    Apr 15, 2012 at 5:45
  • if you mount -i does it keep nautilus from popping up? It works for me for keeping gnome-shell from popping up with the little dialog at the bottom of the screen. This was tested on 12.04 Ubuntu using gnome-shell.
    – Gary
    Apr 15, 2012 at 16:29
  • That makes no difference for me.
    – bcbc
    Apr 15, 2012 at 20:12
  • <p>_If I do the exact same command from the terminal, Nautilus doesn't show_ - How do you launch your script? </p> <p> and I know there are other mounts in my script that don't show - What differences between these mounts?</p>
    – zuba
    Apr 17, 2012 at 6:38

5 Answers 5

3
+100

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.

3
  • 3
    Can you post the link's content concisely here? An answer with just a link is not very welcome in AU community.
    – Mahesh
    Apr 17, 2012 at 7:42
  • This works. At the moment there's too little detail to accept the answer. I have confirmed manually that it works to create the file and call udevadm trigger. Just want to confirm it works in the script. I'll post the details in the question if you haven't added more here.
    – bcbc
    Apr 18, 2012 at 1:25
  • I didn't want to re-summarize what the link has already laid out clearly. Basically, it requires adding a rule in /etc/udev/rules.d/ that tells udev to not process specific devices -- each device would get their own line specifying: KERNEL==”sda1″, ENV{UDISKS_PRESENTATION_HIDE}=”1″. Doing so doesn't modify any system or kernel behavior. It just tells UDEV to hide the presentation of the disk. Nautilus relies on udev rule processing for precisely this behavior.
    – papashou
    Apr 28, 2012 at 6:00
1

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
6
  • Thanks for the suggestion... I don't want to modify the settings because I'm not sure it will take effect immediately, it requires different code for each release, it is inelegant, and if the script is aborted it leaves the user in an undesired state - I also don't believe it should be necessary. What I am after is the mechanism by which nautilus is notified of the mount, and how to bypass it. e.g. udev? dbus? that sort of thing.
    – bcbc
    Apr 16, 2012 at 17:04
  • + I don't want to modify the settings because I'm not sure it will take effect immediately - That is the question to be checked out (easy enough). + it requires different code for each release - it may require or may not 50|50 as if I could meet a dyno at the street )) + it is inelegant - comparing to what alternative?
    – zuba
    Apr 16, 2012 at 18:14
  • + if the script is aborted it leaves the user in an undesired state - many packages (aptitude e.g) when aborted do so. There are approaches to avoid that side effect. E.g. you can charge cron to check every 1min if script is running, and if not to change the setting back. Or you can run you script from another which controls the setting.
    – zuba
    Apr 16, 2012 at 18:15
  • My point is that it shouldn't be required to change a desktop specific setting. I should also have added that this script can run on ubuntu/xubuntu/kubuntu/lubuntu etc. so it adds a high level of complexity and risk.
    – bcbc
    Apr 16, 2012 at 19:12
  • The alternative of blacklisting the partition with udisk(see papashous link) require sudo and is not portable across machines, so it would be even more complex and risky.
    – Egil
    Apr 17, 2012 at 8:32
1

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:

2
  • This seems like it should be potential workaround - I need time to review this and experiment, and won't have a chance until later today (will give feedback then). Ideally the answer will explain why this is happening (as compared to running mount from the terminal) but if I get a good workaround I'll accept that too.
    – bcbc
    Apr 17, 2012 at 18:24
  • This is the one Apr 18, 2012 at 4:39
0

Untested.... just a hunch.

Mount the filesystem in a tree inaccessible to the nautilus user, e.g. in a subdirectory of /root or /home/someoneelse or a /tmp subdirectory owned and secured by someone else

Keep in mind it might be a bad idea to mount the same hardware device at two mountpoints and modify it from both.

Some human configuration is possible in (12.04beta) Ubuntu->System Settings->Details->Removable Media . Some of the options could be set to "do nothing" and that might avoid the popup.

The documentation at https://help.ubuntu.com/community/Mount/USB might be helpful for 9.10 but did not work with 12.04. The keys for gconf-editor that they mention are no longer used.

4
  • It is mounted in a directory under /tmp (and nowhere else).
    – bcbc
    Apr 15, 2012 at 3:41
  • But the logged in user can still access /tmp. The suggestion was to use a directory that the logged-in user can not access.
    – Paul
    Apr 15, 2012 at 4:19
  • I used chmod 700 on the dir... looked promising and then up popped an message window (with no window title): "Unable to open a folder for 10 GB Filesystem", "No application is registered as handling this file"
    – bcbc
    Apr 15, 2012 at 5:33
  • Fascinating. It sure tries hard. Maybe I'll have time to play with this later.
    – Paul
    Apr 15, 2012 at 5:38
0

Try mount with the -n and -i flags.

1
  • I've tried that - pretty much been through all the mount options that I can. The trigger is happening elsewhere.
    – bcbc
    Apr 17, 2012 at 18:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .