How to set a consistent location for drives mounted on boot?

Each time I reboot my computer, when I run diskutil list the order of the mounted drives changes: /dev/disk0 becomes /dev/disk1 and vice versa. This creates problems for applications and scripts that depend on finding specific information in a consistent location. So far, the only (unreliable) solution is to reboot the computer hoping the paths correct themselves.

I have also seen this question but none of the answers seemed to be a reliable long-term fix: Can I set a mount order via stab

Is there a way to set how the drives mount on boot so they are always at the same mounting location/path?

Update: I have found a similar question for Ubuntu here where the solution is to update the reference to drives to use their UUID. However, I have not found a similar UUID-based reference available in Mac: VirtualBox raw drive using UUID instead of device name


Is there a way to set how the drives mount on boot so they are always at the same mounting location/path?

You don't need to specify the order in which they boot; you use the UUID. To get the UUID of a drive in macOS use the command:

% diskutil info diskXsY | grep -i UUID

where X is the disk number (i.e. /dev/disk5) and Y is the slice or partition number (i.e. disk5s1)

For example, I inserted a random USB flash disk that identified as /dev/disk7. Issuing the command as detailed above

% diskutil info disk7s1 | grep -i UUID
Volume UUID:               0E239BC6-F960-3107-89CF-1C97F78BB46B
Disk / Partition UUID:     DF8CB142-B426-4F62-841C-5D26904CF54C

The Disk UUID is the second entry.

So, to identify this disk anytime it's been attached to the Mac, you can use the UUID. For example:

% diskutil list DF8CB142-B426-4F62-841C-5D26904CF54C
/dev/disk8 (external, physical):
#:                       TYPE NAME                    SIZE       IDENTIFIER
0:      GUID_partition_scheme                        *16.0 GB    disk8
1:                        EFI EFI                     209.7 MB   disk8s1
2:          Apple_CoreStorage MyTest                  15.7 GB    disk8s2
3:                 Apple_Boot Boot OS X               134.2 MB   disk8s3

If you notice, it now has an identifier of disk8. I had inserted another USB flash forcing the drive identifier to change proving that this is a valid method.

If you must use the drive identifier, create a function that determines the identifier via the UUID. For example, the following command will get your the disk identifier of the attached drive with the UUID:

% diskutil list DF8CB142-B426-4F62-841C-5D26904CF54C | awk '/dev/ { print $1 } '

/dev/disk8

You could create a Bash/Zsh function to return this info for you on demand.

Regarding non-removable media...

Each time I reboot my computer, when I run diskutil list the order of the mounted drives changes: /dev/disk0 becomes /dev/disk1 and vice versa

I can't see this happening. Non-removable media does not change. Your boot device is specified in NVRAM meaning /disk0 will be what you boot from and since it's defined in the preboot environment disk0 and disk1 will not swap places. The only time this can happen is during Recovery (you're booting from a different volume) but then, when booting from Recovery, you're not loading anything there, especially your VB app.

Now, if you're referring to removable devices, yes, it is possible for the identifier to change (I did it in my example). This is why you use the disk UUID to ensure you're always access the same drive regardless of when it gets attached.