Project74.System Tips


Project 74. System Tips

"How do I change the startup disk from the command line?"

This project gives you some handy tips and scripts for system administration.

Learn More

Project 23 shows you how to use the grep command.


Mount a Volume by Name

The diskutil tool (covered in Projects 67 and 68) mounts a volume identified by its disk identifier, such as disk1s5, or device node, such as /dev/disk1s5. If you wrote a script to mount a volume, you'd probably want to make life easier for whoever uses the script by letting him specify a friendlier volume name. To achieve this, you must translate a volume name into the disk identifier required by diskutil. Here's how we might go about this.

First, use diskutil to list all partitions and pipe its output to the grep command to search for a line containing the volume name. Here's an example.

$ diskutil list | grep -iw "macintosh hd"    2:       Apple_HFS Macintosh HD          19.8 GB    disk1s3


Naturally, in a script we'd expect the volume name to be passed as a parameter, so we wouldn't use the literal string "macintosh hd". The options -iw passed to grep make the search case insensitive and match whole words only; we don't want to match volume backup against both backup and backup2.

Next, we extract the disk identifier disk1s3 from this line. The awk command is an obvious choice to extract a particular field, but in this case, we don't know which field the device identifier is; volume names that contain spaces appear to awk as though they occupy multiple fields. The cut command, however, will extract a specific character range. Careful counting, and allowing for a disk name of the form disknnsmm, suggests the command

$ diskutil list | grep -iw "macintosh hd" | cut -c 59-67 disk1s3


This rather cavalier approach will fail for long volume names that overrun the allotted column width. An alternative uses the sed command. We employ clever regular-expression trickery to match any of disknsm, disknnsm, disknsmm, or disknnsmm by using the expression disk.\{1,2\}s.\{1,2\}, and also capture the matching text by enclosing the pattern with \(...\). The whole line is replaced by the captured text, which is represented by \1. The net effect is to extract the device identifier and display it onscreen. It could just as easily have been sent to the standard output for further use in a script.

$ diskutil list | grep -iw "macintosh hd" | sed -n ¬     "s/.*\(disk.\{1,2\}s.\{1,2\}\).*/\1/p" disk1s3


This isn't perfect, as it will match macintosh with the volume macintosh hd, but it's a good start.

Learn More

Projects 59 to 62 cover the awk and sed commands. Projects 77 and 78 show you how to form regular expressions.


Change the Startup Disk

To check or change the startup volume from the command line, Apple has blessed us with the bless command. Using bless with option --setBoot is equivalent to setting the Startup Disk in System Preferences.

To determine the current startup volume, type

$ sudo bless --getBoot /dev/disk0s3


The df command confirms that this is indeed the current system disk mounted on /.

$ df -h Filesystem       Size   Used   Avail Capacity Mounted on /dev/disk0s3     234G    43G    190G    18%     / ...


To obtain more information on the selected startup volume, type

$ bless --info finderinfo[0]: 3048 => Blessed System Folder is /System/Library/CoreServices ...


To choose another mounted volume as our startup volume, use bless in --mount mode, followed by the mount point of the new boot volume and the --setBoot operator. Applying --setboot to a volume writes the volume's ID to the Open Firmware boot-device variable.

To change the startup volume to another mounted volume, specify --mount followed by the volume's mount point and --setBoot to write that volume to the Open Firmware boot-device variable.

$ sudo bless --mount /Volumes/Macintosh\ HD/ --setBoot


Confirm that the startup volume has changed by typing

$ sudo bless --getBoot /dev/disk1s3


or by checking the Startup Disk in System Preferences.

Try running bless --info again. Note that it reports on the selected boot volumenot necessarily the one that's running.

View the Open Firmware boot-device variable directly by typing

$ nvram -p | grep "boot-device" boot-device fw/node@d04b5407023723/sbp-2@c000/@0:3,\\:tbxi


For a volume to be bootable, it must contain a Mac OS X system, and that system must be blessedthat is, the appropriate folder must be marked as a valid system folder. This will always be so when Mac OS X has been properly installed but might not be so on volumes created by cloning. To bless the appropriate folder, assuming that the potential boot volume is mounted at /Volumes/volname, type

$ sudo bless --folder ¬ /Volumes/volname/System/Library/CoreServices/


If the device you wish to make your boot volume is attached but not mounted, specify --device instead of --mount and the device node instead of the mount point.

$ sudo bless --device /dev/disk1s3 --setBoot


Learn More

Project 38 illustrates the nvram command.


Running bless --info confirms that the new boot volume has been selected and also reminds us that the device is offline.

$ bless --info Volume for OpenFirmware path fw/node@d04b5407023723/sbp- 2@c000/@0:3,\\:tbxi is not available


Set the Date and Time

To set the system date from the command line, issue the date command as the root user. In the following example, we set the date and time to November 17, 15:35, year 2005.

$ sudo date 111715352005


The format for specifying a full date and time is MMDDhhmmCCYY, where MM is the month, DD is the date, hhmm is the hours and minutes, and CCYY is the century and year.

You may omit any field except the minutes, but if you specify the larger unit in any pair (date, time, or year), you must also supply its smaller counterpart. You'd confuse date by furnishing the month (MM) without the date (DD), for example.

To set the time to 15:31 without changing the date, issue the command

$ sudo date 1531 Wed Aug 17 15:31:00 BST 2005





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