APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Home > foo-mac > Union Mounts
Printer Friendly Version






Union Mounts

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        
 /dev/disk2 
 $ hdid -nomount /tmp/dir2.dmg 
 /dev/disk3         
 
 

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
 
 
cartoon

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 (http://www.jay.fm).:

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            DOS_FAT_32
 

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)
 

Instead:

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.
 

Technorati tags:


If this page was useful to you, please click to help others find it:  
Your +1's can help friends, contacts, and others on the web find the best stuff when they search.


1 comment




More Articles by Tony Lawrence - Find me on Google+



Click here to add your comments





Fri Apr 11 22:38:22 2008:   TonyLawrence

gravatar
Jay Levitt provided some updated Leopard info. Thanks Jay!



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide




Buy Kerio from a dealer who knows tech: I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals
pavatar.jpg

This post tagged:

       - Disks/Filesystems
       - MacOSX




Unix/Linux Consultants

Skills Tests

Guest Post Here