blog advertising is good for you


blog advertising is good for you
User login

CLI Disk Management

Mac OS X is a hybrid of the behaviors of Mac OS 9 and its predecessors and the BSD-based Unix variants of yore. As such, the disk system was forced to handle two completely different methods of mounting disks. For those users that live in the GUI and use physical media (and mount network volumes in the Finder) all disk mounts are instant and come up just as soon as the media is ready, like Mac OS 9 did. Then for those that live in the CLI and mount remote volumes that way (or go re-partitioning their iPod) Mac OS X can use the traditional mount & unmount commands.

The Layers

When a disk is inserted into the computer, we’ll say a FireWire hard drive, /usr/sbin/diskarbitrationd is notified of the event by the kernel and looks for mountable partitions on the drive. If found, the proper Unixy things are called to initiate the mount and then the internal disk arbitration tables are updated with the proper information which causes a cascade notification that eventually updates the Finder and any other program that subscribed to notifications.

Now this works great for the average user living in the Finder with physical media, but what about doing all this from the command line? Apple gave us disktool for just that reason, and it’s well-worth looking into, especially if you want to manage a remote or headless Mac.

Manual Page

disktool:  Disk Arbitration Command Tool
disktool:  disktool -[r][a][u][e][m][p][n][d][s][g][A][S][D][l]
                deviceName [options]
You can use disktool to rename, eject, mount or unmount disks and volumes
The acceptable command line parameters are:

Information about disks:
        Usage:  -l -- List Disks.  Lists the disks currently known and available
                      on the system.

Controlling arbitration:
        Usage:  -r -- Refresh Disk Arbitration.  Causes arbitration to refresh
                       its internal tables and look for new mounts/unmounts.

Managing disks:
        Usage:  -u -- Unmount a disk (ex. disktool -u disk2)
        Usage:  -p -- Unmount a partition (ex. disktool -p disk1s2)
        Usage:  -e -- Eject a disk (ex. disktool -e disk2)
        Usage:  -m -- Mount a disk (ex. disktool -m disk2).  Useful when a disk
                       has been unmounted by hand (using -p or -u parameters
        Usage:  -a -- Notify of mount.  Adds the disk to the Disk Arbitrations
                       internal tables.
                       Useful when you have already forced the mount and want 
                       to let applications know it.
                       (ex. disktool -a disk1 AFPVolName AFPFlags)
        Usage:  -d -- Notify of dismount.  Removes the disk from Disk
                       Arbitration's internal tables.
                       Useful when you have already forced the unmount and 
                       want to let applications know it.
                      (ex. disktool -d disk1)

Controlling disk parameters:
        Usage:  -n -- Rename volume.  Renames the volume specified as the first
                       argument. (ex. disktool -n disk1s2 newName)
        Usage:  -g -- Get the hfs encoding on a volume.
                      (ex. disktool -g disk1s2)
        Usage:  -s -- Set the hfs encoding on a volume.
                      (ex. disktool -s disk1s2 4)
        Usage:  -A -- Adopts the given device into the volinfo database.
        Usage:  -D -- Disowns the given device from the volinfo database.
        Usage:  -S -- Displays the status of the device in the volinfo database.

Disk Mounting

Say you have a partition that didn’t mount properly for whatever reason and you did your duty with fsck and want to get it going in one command, use:

$ disktool -m disk0
disk0 device will attempt to be mounted ...
***Disk Appeared ('disk0',Mountpoint = '', fsType = '', volName = '')
Disk Mounting Completed
$

The argument is the whole disk in question. Any mountable filesystems will be mounted and the rest ignored. This is basically what it does on initial insertion of the media.

Disk Unmounting

Especially useful for remote or headless situations, disktool can initiate an unmount of a disk. If you plan on taking a disk offline to format it or the like, use this as it also causes Mac OS X programs to know the disk is offline (versus unmount).

$ disktool -u disk1
disk1 device will be unmounted ...
***Notifications Complete for type 1
***Responding yes to unmount - disk1s1s2
***Disk Unmounted('disk1s1s2')
***Disk Unmounted('disk1')
$

Note the line Responding yes to unmount in there? Every partition is checked to see if there are files in use on it. If so, the disk may refuse to unmount. Run the lsof command to check for open files and close any that show up on that volume. If none exist, an open shell may be in a directory on the disk, so cd to another disk on all shells you control and try again. If you still fail, kill all shells you do not own. If you still fail then kill the Finder (if it is open).

Ejecting Removable Media

Again, especially useful for headless operations, disktool lets you eject removable media from the system, optionally unmounting it if it’s mounted.

disktool -e disk1
disk1 device will attempt to be ejected ...
***Notifications Complete for type 1
***Responding yes to unmount - disk1s1s2
***Disk Unmounted('disk1s1s2')
***Disk Unmounted('disk1')
***Notifications Complete for type 4
***Responding yes to eject - disk1
***Responding yes to eject - disk1s1
***Responding yes to eject - disk1s1s1
***Responding yes to eject - disk1s1s2
***Disk Ejected('disk1')
$

You may run into the same problems with drives refusing to unmount. Refer to the Disk Unmounting section above for more on that problem.

Automatic Disk Synchronization

When you perform a mount or unmount in the CLI you may notice that Mac OS X doesn’t respond by showing the disk on the desktop. This is because the Unixy underbelly of the OS doesn’t talk to the GUI when mounting volumes directly; normal flow is the GUI talks to the core. To get the proper chain of notifications sent that updates the Finder, use:

$ disktool -r
Refreshing Disk Arbitration ...
$

The disk should show up on the desktop and in /Volumes at this point.

Manual Disk Synchronization

So if automatic syncronization fails, you’re feeling particularly OCD today, or you’re trying to get a manually-mounted network volume to show up in the Finder, you can use the remainder of disktool’s commands to manually edit the arbitration table. You will first need to mount the disk using traditional mount commands. Mac OS X comes with mount commands for the following filesystems: AFP, ISO 9660, CD-DAFS, FTP (read-only), HFS/HFS+, MSDOS (FAT 12, 16 & 32), NFS, NTFS (read-only), SMB, SynthFS, UDF, and WebDAV.

Only physical media will be added during automatic arbitration; network volumes (such as NFS) must be manually added. More specifically, any mounted volume that does not have a corresponding /dev/disk# entry will need to be mounted from the Finder or using this method in order to be accessible as a volume in the GUI (the Finder or open/save dialog boxes). However, you can still access the mount point directly and have access to the files; this method merely presents them as real volumes in the GUI.

I’ll demo this with an NFS share.

# mount -t nfs solstice.local:/Library/NetBoot/NetBootSP0 /mnt
# mount
/dev/disk0s3 on / (local, journaled)
devfs on /dev (local)
fdesc on /dev (union)
<volfs> on /.vol
solstice.local:/Library/NetBoot/NetBootSP0 on /mnt
# disktool -l
***Disk Appeared ('disk0',Mountpoint = '', fsType = '', volName = '')
***Disk Appeared ('disk0s1',Mountpoint = '', fsType = '', volName = '')
***Disk Appeared ('disk0s3',Mountpoint = '/', fsType = 'hfs', volName = 'Europa')
# disktool -a solstice.local:/Library/NetBoot/NetBootSP0 /mnt 0
solstice.local:/Library/NetBoot/NetBootSP0 afp mount will attempt to be mounted ...
# disktool -l
***Disk Appeared ('/mnt',Mountpoint = '/mnt', fsType = '', volName = '')
***Disk Appeared ('disk0',Mountpoint = '', fsType = '', volName = '')
***Disk Appeared ('disk0s1',Mountpoint = '', fsType = '', volName = '')
***Disk Appeared ('disk0s3',Mountpoint = '/', fsType = 'hfs', volName = 'Europa')

Now, in the Finder, there’s a “NetBootSP0” item in my sidebar. Hitting eject on it does nothing, of course, because we’ve tricked it into displaying it in the Finder. To really get rid of it unmount it and then resync the arbitration table.

# umount /mnt; disktool -r
Refreshing Disk Arbitration ...

And now it’s gone from disktool, mount, and the Finder.

Summary

You can use the standard Unixy methods to mount and unmount disks and call disktool -r to make them show up in the Finder when you’re done. You can make non /dev/disk# devices show up in the Finder by manually adding them to the arbitration table that the Finder lists devices from.

Average rating
(0 votes)
About Adam Knight
Adam Knight's picture

Author Biography

Adam Knight is one of the founders of Mac Geekery and is a geek at heart. Programmer by day, hacker by night, his daily life revolves around the Macintosh platform, which he has been a user and programmer for since the early days of System 7 when his LCII replaced his Apple //c.

In-between tech jobs, he’s managed to learn the basics of any web hacker: PHP, MySQL, Perl, Apache, Linux, *BSD, and the intricacies of ./configure —prefix=~/bombshelter/. Today, codepoet is concentrating on blogging again, writing some software for the Mac by himself (including Notae) and for his company (such as Switchblade) and has a few other toys coming out soon.

Bug him over AIM or email [link fixed].

Post new comment
The content of this field is kept private and will not be shown publicly.
13 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.