APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Union Mounts

© December 2005 Tony Lawrence
December 2005

Mac OS X lets you use "union" mounts. Imagine you have a directory with files in it, and you then mount some device on that directory. Ordinarily, the original files would no longer be available, but a union mount leaves them visible: you can see both the files from the device you mounted and the files that were originally in the directory you mounted it on.

Yes, you can do this on Linux also: see Unionfs -Union mounts for Linux

This mixing raises interesting problems - for example what happens if there are identically named files? You only get to see the one in the "topmost" mount - everything below is invisible.

Let's play. First, you need some disk images:

 hdiutil create /tmp/dir1 -volname dir1 -size 512k -fs MS-DOS
 hdiutil create /tmp/dir2 -volname dir2 -size 512k -fs MS-DOS

I used MS-DOS as the file system type just to make things simple. You might need larger .dmg's if you put real files in the directories; I just use empty files to show the concept.

Next you run hdid with nomount to get an attach point:

 $ hdid -nomount /tmp/dir1.dmg        
 $ hdid -nomount /tmp/dir2.dmg 

and then you can mount /dev/disk3 or /dev/disk2. This is similar to using the loopback device from Unix and Linux.

You probably need to be root for the next part just because the ms-dos driver isn't ordinarily loaded:

 $ mkdir /tmp/testing
 $ sudo su -
 root# mount -t msdos /dev/disk2 /tmp/testing
 kextload: /System/Library/Extensions/msdosfs.kext loaded successfully
 root# cd /tmp/testing
 root# touch a b c d
 mount -t msdos /dev/disk3 /tmp/testing
 root# cd /tmp/testing
 root# ls
 root# touch d e f g 
 root# umount /tmp/testing

You can exit sudo after that if you want.

We're finally ready for the union mount:

 $ mount -t msdos  -o union /dev/disk2 /tmp/testing
 $ mount -t msdos  -o union /dev/disk3 /tmp/testing
 $ ls /tmp/testing
 ls /tmp/testing 
 a       b       c       d       e       f       g

Seven files from both file systems are available. We don't see the "underneath" file named "d". Let's remove some things:

 $ rm /tmp/testing/a /tmp/testing/d /tmp/testing/f
 $ $ ls /tmp/testing
 b       c       d       e       g

Looks like "d" is still there, but that's the "underneath" one. So which "d" got wiped out? Let's see:

 $ mount -t msdos /dev/disk3 /tmp/testing
 $ ls /tmp/testing
 e       g
 $ umount /dev/disk3
 $ mount -t msdos /dev/disk2 /tmp/testing
 $ ls /tmp/testing
 b       c       d

As expected, it was the "top" one - the last one mounted.

Although I can't find mention of it in Mac OS X, union mounts can also have a concept of "whiteouts". If we had that option above, the "lower" of the "d" files would not have been visible after we deleted the upper - a "whiteout" would have kept us from seeing it.

Leopard Update

The following information was provided by Jay Levitt https://www.jay.fm (link dead, sorry)

I found your Union Mounts article very helpful! I'm setting up a Mac Pro for the first time, using a non-bootable RAID array, and so I'm planning to use them to get /Applications, etc. on my RAID drive while fooling Leopard into thinking they're on the boot drive.

I guess a few things have changed in Leopard. I don't know if you plan to go back and update anything, but if you do, here's what you'll need to fix:

1. Different name/output for hdid

$ hdid -nomount /tmp/dir1.dmg       /dev/disk2

hdid is now more properly (and intuitively) "hdiutil attach", and Leopard creates partition schemes by default. So now it's:

$ hdiutil attach -nomount /tmp/dir1.dmg
/dev/disk2              FDisk_partition_scheme            /dev/disk2s1          

2. Must mount the partition, not the disk

root# mount -t msdos /dev/disk2 /tmp/testing

That won't work; you get the confusing error:

mount_msdos: Unsupported sector size (0)


root# mount -t msdos /dev/disk2s1 /tmp/testing

3. umount doesn't work like it used to (?)

I'm not sure why - and it could be something on my system - but the following:

root# umount /tmp/testing

gives an error:

umount: unmount(/private/tmp/testing): Resource busy

Instead, this works:

root# cd
root# hdiutil unmount /tmp/testing
"/tmp/testing" unmounted successfully.
root# hdiutil unmount /tmp/testing
"/tmp/testing" unmounted successfully.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Union Mounts

1 comment

Inexpensive and informative Apple related e-books:

El Capitan: A Take Control Crash Course

Sierra: A Take Control Crash Course

iOS 10: A Take Control Crash Course

Take Control of High Sierra

Take Control of Pages

More Articles by © Tony Lawrence

Fri Apr 11 22:38:22 2008: 4031   TonyLawrence

Jay Levitt provided some updated Leopard info. Thanks Jay!


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. (Arthur Conan Doyle)

Linux posts

Troubleshooting posts

This post tagged:


Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode