Category Image mounting a volume in a script


diskutil requires the use of a device name to mount a disk....

For example, do this:

$ diskutil list

Besides the boot disk, you can unmount a disk by knowing its volume name, as in:
$ diskutil unmount /Volumes/name_of_volume

However to mount it you need to know the device name and use something like
$ diskutil mount /dev/disk1s2

The caveat is that the device name is assigned dynamically by OS X at startup time or when a disk is plugged into the computer.

In my case, I have backup firewire drives that are rotated offiste regularly, but not necessarily every day of the week. It is inconvenient to log into the server and unmount the backup drive manually. So we want our backup script to unmount the backup drive after backup each night and to test for presence and mount before backup each night. In this way, our tech can just switch off and rotate in backup drives each day without needing to log into the server to unmount it before turning the drive off.

I found this script named 'disk' (also shown below) by mhenders which usefully mounts a drive based on volume name if the drive is not already mounted. The key to the script is the use of grep and awk to extract the device name from the output of diskutil list. Also it unmounts based on volume name if the volume is already mounted, so in my case, I only try to mount the disk in my script if the volume is not present.

#!/bin/bash
if [ -z $1 ] ; then
echo "usage : disk <diskname>"
echo "Mounts <diskname> if it's not mounted, and"
echo "unmounts it if it is already mounted."
exit 1
fi
NAME=$1
PART=`diskutil list|grep $NAME|awk '{print $6}'`

if [ -z `ls -1 /Volumes/ | grep $NAME` ] ; then
# check that PART appears to be a disk partition
echo Checking $NAME $PART
if [ `file /dev/$PART | awk '{print $2}'` = "block" ] ; then
echo Mounting $NAME $PART
diskutil mount /dev/$PART
else
echo /dev/$PART does not appear to be a disk partition - exiting
exit 1
fi
else
echo unmounting $NAME
diskutil unmount /Volumes/$NAME
fi

Posted: Wednesday - November 22, 2006 at 02:47 PM        


Published by