Store a user's Home folder on a USB drive

Apr 29, '05 07:02:00AM

Contributed by: juz10mac

This hint is for you if you want to put your home folder on a USB flash drive to take between Macs, or maybe you are just interested in the novelty of being able to carry all of your 'stuff' around. Please note, this hint is not for the faint of heart.

The following instructions make it so you can take your home folder with you wherever you go. All you need is an admin password on each machine you want to use it on and, of corse, a flash drive. The roomier the flash drive the better, but I got this to work on a 128 megabyte flash drive. I won't be able to store much music or many pictures in this home folder (because of the size), but it would be great for getting to my documents and settings anywhere. I have a friend who has a 1-gigabyte flash drive. He would be able to use this for music, pictures, and maybe even small video files.

To see how my hint relates to this earlier hint on moving your home folder to an external hard disk, see the information at the bottom of this hint.

Behavior:
When you login to your computer with your flash drive connected, your user environment will be loaded from it. When you login to your computer without your flash drive connected, your user environment will be what it has always been.

Warning:
With these modifications, it is possible to mess up your ability to login to your user account. While I don't think you can lose your data this way, you can certainly lose your ability to log on to the right place (when I was first figuring this out I had some moments of panic, myself). I recommend creating a new user to try this on first, before trying it on your normal user. Also, when your flash drive is connected, it will look as if all your data and settings are gone. Don't Panic, you can still access them at /Users/short_username on your hard disk (where short_username is your Mac OS X short user name). Or simply logout and remove your flash drive (you will probably have to shut down, because it could be dangerous for the data on your flash drive to remove it, if it should happen to be mounted). Once you get this working and are happy with it, then you can simply copy across whatever you want from your normal user to your flash drive's user.

Also, I don't know how this modification would behave if your Mac OS X short user name and passwords on the different machines don't match. Also-also, I don't know how the different filesystems behave, should you use something other than the DOS filesystem that comes with most flash drives. This is open for experimentation. There is also the slight potential for an error to develop among the workings of this hint. I did put a fail-safe mechanism in the script (it will try to remove empty folders), but this mechanism is untested. If the flash drive is removed after it is detected (during boot) and before you login, the Mac OS will think that you want it to create a new home folder inside the invisible Volumes folder. The solution to this is to remove the directories it creates inside the Volumes folder if the script, for some reason, does not. I have NOT tested this hint with FileVault, and it will probably cause problems if you try it with FileVault on!

Here is what you do:
First of all, all of my samples are for my system, so Justin is my short user name and ATTACHE is the name of my flash drive.

  1. Place the following script in /Library/Hooks/FlashDriveHome. You may have to create the folders Hooks and FlashDriveHome is the filename of the script. If you use TextEdit to make this script, make sure you choose Make Plain Text from the Format menu first. Here's the code:
    #! /bin/bash
    
    # This script checks to see if your flash drive is connected. If it is it will
    # change your home folder reference to the flash drive. If your flash
    # drive is not connected it will change the reference back to your
    # usual home folder.
    
    # Change 'Justin' to your Mac OS X short user name. (7 occurrences)
    # Change 'ATTACHE' to the name of your flash drive. (5 occurrences)
    # You can find the flash drive's name by typing 'df' in the terminal, but
    # this should be the same name you see in the Finder.
    
    # Remove empty directories that will confuse this script (this will not work
    # if the directories are not empty):
    rmdir /Volumes/ATTACHE/Users/Justin
    rmdir /Volumes/ATTACHE/Users
    rmdir /Volumes/ATTACHE
    
    # Check to see if your flash drive is connected (if there is a folder for
    # it in Volumes):
    if [ -s /Volumes/ATTACHE ]
    then
       niutil -destroyprop . /users/Justin home
       niutil -createprop . /users/Justin home /Volumes/ATTACHE/Users/Justin
    else
       niutil -destroyprop . /users/Justin home
       niutil -createprop . /users/Justin home /Users/Justin
    fi
    
  2. In System Preferences, under Accounts, make a new user called 'user template.' 'usertemplate' is fine for the short user name, and you don't need to worry about a password or anything else.

  3. Run these commands from a Terminal (you will be prompted for your admin password):
    sudo mv /Library/Hooks/FlashDriveHome.txt /Library/Hooks/FlashDriveHome
    sudo chmod +x /Library/Hooks/FlashDriveHome
    sudo /usr/bin/defaults write /Library/Preferences/com.apple.loginwindow LoginHook /Library/Hooks/FlashDriveHome
    sudo defaults write /Library/Preferences/SystemConfiguration/autodiskmount AutomountDisksWithoutUserLogin -bool true
    mkdir /Volumes/ATTACHE/Users/
    Some explanations:
  4. Note: before you do this next step, you may need to login as 'user template' to trash extra stuff to make room. For me, there wasn't enough room for the Garage Band demos, so this is what I did -- if you do and need to logout, make sure to unmount and remove your flash drive, as we have not finished setting it up yet! Also, aliases will be followed when copying this user to your flash drive.
    sudo cp -r /Users/usertemplate /Volumes/ATTACHE/Users/Justin
    sudo chown -R Justin /Volumes/ATTACHE/Users/Justin 
    Some explanations:
  5. With your flash drive connected, reboot, log out, then log back in. At this point, you can delete the user 'user template' if everything is working. I also recommend choosing a different desktop picture for your flash drive's user. That way, you can tell at a glance which home folder you are using.

  6. Copy the contents of your old Library/Keychains to the Library/Keychains in the flash drive's home folder (overwrite existing files).
This will work across a logout and log back in, but it always seems to behave more smoothly if you treat your flash drive as a non-hot-swappable device (shutting down the computer before connecting or removing the flash drive). If you set this up on another machine, you can skip the parts about making a user template and copying it to the flash drive, as you would have already done this. Go ahead and save something to your 'new' home folder (flash drive connected). Try this on another Mac: connect your flash drive and login, and you should see it there, as if your home folder was there to begin with!

This would be great if you have Macs at your work or school, so you can still get to all of your stuff (emails, Safari bookmarks, Documents, settings, etc). Good luck explaining why you need admin passwords in the terminal to the administrator, though.

I do have about two minor issues/quirks to report, but they are probably something one of you bright readers out there can figure out. But I think you'll all agree that they are not a big deal.
  1. On my machine, I had to replace the Finder sidebar icons with the appropriate ones (from my home folder on the flash drive, not those from 'user template') each time. A good way to get around this would be to drag your new home folder on your flash drive to your dock, and get to those things from there.
  2. For me, when the flash drive is first connected each time, I need to log out then back in.
To uninstall this modification:
  1. Very important: Remove your flash drive and reboot (you need to be on your normal user)
  2. Trash Library -> Hooks -> FlashDriveHome
Here is what I've tested this on: Attache 128-megabyte USB 2 flash drive formatted for DOS and an iBook G4 running 10.3.9. One other thing: I tested this with "ignored permissions" on the flash drive, but I don't think it matters. And, I have tried it with auto login on and off. Also, I got some information and the idea from the older hint linked above. That hint was set up to use an external hard drive (not a flash drive) for your user, but the computer wouldn't automatically determine which home folder to use. So, while my hint is similar, I've built upon the general concept of the earlier hint and I've added automation. I've also taken a different approach. The earlier hint uses the NetInfo Manager to change the home folder of a user, whereas I've written a script that determines what to change it to.

Enjoy!

Comments (2)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050423203849332