Project68.Mount and Unmount Local Disks


Project 68. Mount and Unmount Local Disks

"How do I manually mount and unmount local file systems?"

This project presents examples that mount and unmount volumes on locally attached disk drives, including removable media. It employs both the traditional Unix commands mount and umount, as well as Apple's diskutil command. The diskutil command is a command-line interface to Apple's Disk Management framework, providing the same functionality as its Disk Utility application. Project 67 uses diskutil to verify and repair the integrity of file systems.

Manage Volumes Manually

Mac OS X automatically mounts disk drives and removable media such as CDs as they become available, and unmounts them at the click of a mouse. Mounted volumes are accessible from the command line in a directory named /Volumes/name-of-drive.

You'll rarely want to mount a volume manually, but occasionally, the situation arises. You may need to remount an external drive after unmounting (ejecting) it. I have a backup disk that's permanently attached for nightly backups, but I want it mounted only during the backup process. At other times, I prefer that it remain hidden and out of harm's way.

We'll look at two methods to mount and unmount volumes manually: the Unix mount and umount commands, which let you choose where in the file system a volume is mounted, and Apple's diskutil, which requires less work but gives you no choice in where a volume is mounted.

Use the Unix mount Command

Before we can mount a disk, we must discover its device node. The easiest method is to use diskutil to list all attached devices. Fortunately, diskutil lists all attached devices, not just those that are mounted.

To mount the volume labeled Backup-sauron, invoke diskutil, and filter its output by using the grep command.

$ diskutil list | grep "Backup-sauron"    3:         Apple_HFS Backup-sauron    129.0  GB disk1s5


Learn More

Project 67 shows how to use diskutil to list attached drives and identify a drive or partition by its mount point, its device identifier, or its device node.


From this (and from reading Project 67), we conclude that the device node is /dev/disk1s5. We mount the volume by typing

$ mount /dev/disk1s5 /Backup /dev/disk1s5 on /Backup: No such file or directory


... but fail to do so! Because mount lets you choose the mount point, you are left to your own devices (pun intended) to create the mount point. You may mount at any point in the file system; choose a suitable one, and create an appropriately named directory. We choose to mount the drive at the root of the file system in a directory called /Backup.

$ mkdir /Backup $ mount /dev/disk1s5 /Backup /dev/disk1s5 on /Backup: Incorrect super block.


Foiled again! The mount command assumes that a volume is formatted as UFS (Unix File System), whereas Mac OS X uses Apple's HFS+. Let's try again, specifying the appropriate file system. Type either of the following.

$ mount -t hfs /dev/disk1s5 /Backup $ mount_hfs /dev/disk1s5 /Backup


Check that the mount was successful. The mount-point directory is replaced by the mounted volume and takes on the permissions assigned to the top level of that volume.

$ ls -ld /Backup drwxrwxr-t   31 root  admin  1156 Jun 24 13:21 /Backup $ ls /Backup Applications      Users          mach.sym Desktop DB        Volumes        mach_kernel Desktop DF        automount      private ...


To view all currently mounted file systems, invoke the mount command with no arguments. We should see our new mount at the end of the list.

$ mount /dev/disk0s3 on / (local, journaled) ... /dev/disk1s5 on /Backup (local, nodev, nosuid, journaled, mounted by saruman)


Tip

After you mount a volume, you'll notice that the mount command reports various options as disabled, such as nodev and nosuid. That's a security measure. To enable those options, you must mount the volume as the root user with the sudo command. Check the man page for mount to learn more about these options. The SUID bit is explained in "The s-bit" in Project 8.


Unmount Drives

To unmount a volume, specify either the device node or the mount point to the umount command.

$ umount /dev/disk1s5 $ umount /Backup


Don't forget to remove the directory too (although there's no harm in leaving it for the next time you mount the volume).

$ rmdir /Backup/


Show in the Finder

Mounting a volume from the Unix mount command does so behind the Finder's back. If you wish the mounted volume to show up in the Finder, tell it to refresh its list of mounted volumes by typing

$ disktool -r Refreshing Disk Arbitration...


(The disktool command is supposedly obsolete, but I don't know of any other way to refresh the Finder.) One problem you may encounter after letting the Finder know about a mount: When you unmount the volume, it might be busy.

$ umount /Backup umount: unmount(/Backup): Resource busy


If this is the case, you'll have to unmount it by using either the Finder or diskutil (examples of which are given later in this project).

Mount a CD or DVD

Mounting a CD or DVD is similar to mounting any other volume. Here's an example that mounts an audio CD.

First, pop in a CD, which will be mounted automatically by Mac OS X. Before we mount the CD, we have to unmount it manually, but without ejecting it. To do this, use the df command to determine its device node, which in this example is /dev/disk2, and unmount it. Also, tell the Finder to refresh its list of mounted volumes.

$ df Filesystem    512-blocks ... Capacity   Mounted on /dev/disk0s3   489972528 ...      22%   / ... /dev/disk1        965307 ...     100%   /Volumes/Hot Fuss $ sudo umount /dev/disk1 Password: $ disktool -r Refreshing Disk Arbitration...


CD Formats

A CD can be formatted for any of several file systems: audio, enhanced audio with multiple partitions, CD-ROM, data CDs formatted as Apple's HFS+ file system, and so on. You'll use different mount types for the different file systems.

Type $ ls /sbin/mount*


to display a list of the mount variants.


To mount the CD, we note that the file system on audio CDs is CD-DA, and we choose the mount command mount_cddafs (or mount -t cddafs).

$ mkdir /Audio_CD $ mount_cddafs /dev/disk1 /Audio_CD $ ls /Audio_CD 1 Jenny Was A Friend Of Mine.aiff   5 All These Things... 10 Midnight Show.aiff               6 Andy, You're A St... 11 Everything Will Be Alright.aiff  7 On Top.aiff 2 Mr. Brightside.aiff               8 Glamourous Indie... 3 Smile Like You Mean It.aiff       9 Believe Me Natali... 4 Somebody Told Me.aiff


Unmount the CD in the normal manner, and use diskutil to eject it.

$ umount /Audio_CD $ ls /Audio_CD $ $ diskutil eject /dev/disk1 Disk /dev/disk1 ejected $ rmdir /Audio_CD/


To mount a DVD, we perform the same steps as those for mounting a CD, except that we specify the file system as cd9660.

$ sudo umount /dev/disk1 Password: $ disktool -r Refreshing Disk Arbitration... $ mkdir /DVD $ mount_cd9660 /dev/disk1 /DVD $ ls /DVD VIDEO_TS $ umount /DVD $ ls /DVD $ diskutil eject /dev/disk1


Manage On-Disk Permissions

After mounting a volume, it's wise to ensure that Unix permissions are enabled. They are not necessarily enabled on a non-system volume, especially on an external drive. Select a volume in the Finder, and press Command-i (or choose File > Get Info). At the bottom of the info window, you'll see a check box labeled Ignore Ownership on This Volume. There's a command-line alternative to checking and unchecking this box, called vsdbutil. For instructions on using this (badly documented) command, specify option -h.

$ vsdbutil -h Usage: vsdbutil [-a path] | [-c path ] [-d path] [-i]   -a adopts (activates) on-disk permissions on the spec...   -c checks the status of the permissions usage on the ...   -d disowns (deactivates) the on-disk permissions on t...   -i initializes the permissions database to include al...


Switch on (adopt) permissions by typing

$ sudo vsdbutil -a /The-mount-point-here


Use the diskutil Tool

Using diskutil to mount and unmount volumes is easier than using the Unix mount and umount commands. It takes care of creating and deleting the mount-point directory. You do not have the flexibility to choose where a particular volume is mounted, however. It will always be mounted at /Volumes/name-of-volume.

Tip

Manually mounting and unmounting volumes can confuse the Finder. Issue the command disktool -r, or relaunch the Finder (force quit) to unconfuse it.


Here's an example in which we use diskutil to mount and unmount the volume Backup-sauron. First, we determine the device identifier. (We could also specify the device node.)

$ diskutil list | grep "Backup-sauron"    3:      Apple_HFS Backup-sauron   129.0 GB  disk1s5


Mount the volume by typing

$ diskutil mount disk1s5 Volume disk1s5 mounted $ ls /Volumes Backup-sauron OSX-sauron


To unmount the volume, type

$ diskutil unmount /Volumes/Backup-sauron Volume /Volumes/Backup-sauron unmounted


Note

The older disktool command is depreciated in favor of diskutil.


The diskutil command can mount and unmount entire disks in a single command, in which every volume (partition) on the disk is mounted or unmounted. Here's an example in which we mount and then unmount both partitions on disk1.

$ diskutil mountDisk /dev/disk1 $ diskutil unmountDisk /dev/disk1 Disk /dev/disk1 unmounted


This technique is also applicable to CDs and DVDs. Additionally, to eject a CD or DVD, type a command such as

$ diskutil eject disk2 Disk disk2 ejected





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net