24

I use VirtualBox on my MBP and often need to mount my USB flash drive inside the guest. The problem I'm running into is that the host OS (OS X Mavericks) immediately mounts the USB flash drive when I insert it. When I try to unmount it (gracefully, through Finder), OS X complains that the device is in use. Further research into lsof reveals that Spotlight and AVG are doing their thing with the flash drive.

Rather than fiddling with individual app settings, I'd like to just modify the host OS behavior so that the USB device doesn't auto-mount. Any suggestions?

AJ.
  • 1,612
  • If someone has a modification, awesome, but in the mean time you can use terminal to force an unmount. diskutil unmount force disk1s2 Also, if you choose a filesystem that spotlight won't index or set that drive to be excluded in Spotlight preferences, you can reduce that delay and overhead with a quick setting change. – bmike Feb 12 '14 at 18:01
  • 1
    I advise you to test Disk Arbitrator: http://apple.stackexchange.com/a/39360/22003 . This will protect you against Spotlight, anti-viruses and attacks. – dan Jan 29 '15 at 10:03
  • See also: http://apple.stackexchange.com/a/164976/22003 . – dan Jan 29 '15 at 10:11

5 Answers5

25

OS X has a mechanism, called the Disk Arbitration framework, through which programs can control what happens when disks are inserted or ejected. It also provides an opportunity to intercept a disk insertion event and prevent the usual automatic mounting behaviour.

To change the behaviour, though, requires a third-party utility, since the Disk Arbitration framework is just a programming interface. One good utility is Disk Arbitrator, a free (BSD-licensed) tool by Aaron Burghardt.

Screenshot of Disk Arbitrator

While Disk Arbitrator is running, you can choose to either veto the mounting of newly inserted disks, mount them read-only, or just let the normal automatic mounting happen. After Disk Arbitrator exits, the normal mounting behaviour is restored.

200_success
  • 983
  • 1
  • 8
  • 18
  • 2
    This app also gave me much more information about volumes and the status (or failure) of partitions when using Apple's Disk Utility it practically instantly updates the mount status vs Disk Utility's sluggish responses. Thanks for sharing it. – Nilloc Jul 22 '22 at 14:03
17

According to Topher Kessler article on CNET, you can add an entry to /etc/fstab, specifying the noauto option.

Such an entry might look like this:

UUID=791E37B4-82A3-37E7-9F15-3C39359126B4   none  hfs  rw,noauto

The UUID may be obtained by looking for the Volume UUID line that is printed when you run diskutil info /Volumes/volume name.

200_success
  • 983
  • 1
  • 8
  • 18
TonyCr
  • 201
  • I've got this chmod +x (executable) in my ~/bin/ directory.

    http://gist.github.com/voltechs/fc48c9683d50c7c03cab2f0a6477d8da

    also see (https://apple.stackexchange.com/a/310669/43769)

    – Volte Dec 30 '17 at 22:30
  • uh... /etc/fstab doesn't exist! – Michael Apr 29 '19 at 17:38
  • It's ok to create the file if it doesn't exist, but create it as root. BTW, the command sudo vifs is recommended instead of direct editing, and it will create the file for you if it doesn't exist. – Thomas Albright Mar 24 '20 at 14:51
4

This is how I disable auto mounting of APFS volumes on macOS Catalina.

For macOS volumes, both Macintosh HD - Data and Macintosh HD type volumes need to be referenced in /etc/fstab.

Find volume(s) using diskutil list and set VOLUME_PATH accordingly.

# Disable auto mounting of "macOS - Data" volume
VOLUME_PATH="/Volumes/macOS - Data"
VOLUME_UUID=`diskutil info "$VOLUME_PATH" | awk '/Volume UUID:/ { print $3 }'`
cat << EOF | sudo tee -a /etc/fstab
UUID=$VOLUME_UUID none apfs rw,noauto
EOF

Disable auto mounting of "macOS" volume

VOLUME_PATH="/Volumes/macOS" VOLUME_UUID=diskutil info &quot;$VOLUME_PATH&quot; | awk '/Volume UUID:/ { print $3 }' cat << EOF | sudo tee -a /etc/fstab UUID=$VOLUME_UUID none apfs rw,noauto EOF

Here is what is going when running above commands:

Set VOLUME_PATH variable to /Volumes/macOS - Data

VOLUME_PATH="/Volumes/macOS - Data"

Set VOLUME_UUID variable to volume UUID of /Volumes/macOS - Data volume

Running diskutil info "$VOLUME_PATH" outputs volume details of /Volumes/macOS - Data (which includes its UUID).

Piping (|) these details to awk '/Volume UUID:/ { print $3 }' extracts the UUID.

VOLUME_UUID=`diskutil info "$VOLUME_PATH" | awk '/Volume UUID:/ { print $3 }'`

Append UUID=$VOLUME_UUID none apfs rw,noauto to /etc/fstab and output to console (see tee)

cat << EOF | sudo tee -a /etc/fstab
UUID=$VOLUME_UUID none apfs rw,noauto
EOF
  • 2
    The question asks about disabling the automounting of USB/external drives. Your answer might be more useful if you describe how this can be accomplished (and what the commands you propose do in general). – nohillside Aug 12 '20 at 14:22
  • Thanks for the feedback. These commands are for an external USB drive running a separate macOS installation. The same logic applies to any volume. Thought I would contribute a power user alternative to the other answers. I can delete it if you think it doesn’t add value. – sunknudsen Aug 12 '20 at 14:43
  • It does, and with the context you've given in the comment I understand what the code is doing. But having an external drive with Catalina partitions is probably not the typical case most users have, so maybe updating the script to show how it works for a device called USBSTICK or similar may make the answer more useful to people not working with Terminal every day. – nohillside Aug 12 '20 at 14:49
  • Also, as a minor detail, you can combine grep/awk into awk '/Volume UUID:/ { print $3 }' – nohillside Aug 12 '20 at 14:52
  • 1
    Cool didn’t know about this syntax. Thanks for sharing! – sunknudsen Aug 12 '20 at 15:06
2

I think that modifying fstab to turn off automount is a bit excessive for what you are trying to do. It's sort of like removing the doors on your car because every now and then you need to get in or out.

For this, I would use VirtualBox's built in USB Filters. This allows you to automatically mount a particular USB device upon insertion. You can find the USB filters under the "Settings" menu of the particualr VM you wish to assign it to.

For instance, my USB devices are are listed below that are available if I want to automatically attach it to my Windows 10 Pro VM. I can attach my Seagate FreeAgent Go, or my SanDisk Cruzer or any of the attached USB devices. However, attaching the Logitech USB receiver would ultimately make the mouse inoperable on the host, so be careful what you attach.

enter image description here,

How do I know this works? Well, I accidentally attached a FreeAgent Go drive and it wouldn't (as I perceived) mount in OS X. See my original "problem": USB Drive Will Not Mount, Not Listed in Disk Utilities, but Found In System Profiler

It does it by serial number or by UUID (I haven't investigated enough to figure out which) but I had several FreeAgent Go drives of the exact same model and size and only one was auto attaching to the VM. At the time, I thought my drive had gone bad.

The bottom line is that automount still worked, and I was able to attach a USB device directly to a VM without having to go through the trouble of unmounting it first

Allan
  • 101,432
0

I found a official guide at https://discussions.apple.com/docs/DOC-7942 "Prevent a volume from mounting at startup"

BB9z
  • 129
  • 3
  • 1
    Answers on Ask Different need to be more than just a link. We expect answers to be self contained, since if your link goes down, your answer will be useless. Paraphrasing the source and including any relevant information in the answer will ensure it stays relevant – nohillside Aug 11 '21 at 10:21