3

Often, I boot to macOS Recovery only to find one of commands I wish to enter has not been included. For example, hexdump and nano are not included. However, if the volume containing macOS is mounted, the same version and not damaged, then there exists the possibility of accessing the missing commands from that volume. This requires knowing the path to the command. Therefore, I find myself locating the command by manually searching through the various directories or using the find command, which can be tedious and/or time consuming. Additionally, some commands can not be executed because the root directory is not the mount point of the volume containing macOS. An example would be the zsh command. Is there a better way to access the macOS commands from macOS Recovery? Here, macOS commands is meant to be a commands included with an installation of macOS.

1 Answer 1

4

This answer was tested using OS X Mavericks, macOS High Sierra and macOS Monterey. OS X Mavericks was installed on a JHFS+ volume and macOS High Sierra was installed on a APFS volume. The Monterey release of macOS was used in the examples below.

Note: In the examples below, the commands were entered in a Terminal window while booted to macOS Recovery for Monterey. When booting to macOS Recovery, the APFS volumes containing macOS Monterey are mounted read‑only on /Volumes/Macintosh HD and read/write on /Volumes/Macintosh HD - Data.

A Method of Accessing macOS Commands

Below is the synopsis for getting location (location) of desired macOS commands (macOScommand ...). To be located, each command needs be in the search path determined by the supplied mount point (macOSmountpoint) and the value of the PATH variable. Additionally, the volume containing the macOS commands needs to be mounted on the supplied mount point.

chroot macOSmountpoint which macOScommand ...

The search path is determined by prepending each directory in the colon‑separated list of directories output by echo $PATH command with the macOSmountpoint. The output from echo $PATH command is given below.

/usr/bin:/bin:/usr/sbin:/sbin

For example, the /Volumes/Macintosh HD mount point and above PATH would make the search path to be the directories in the following order.

/Volumes/Macintosh\ HD/usr/bin
/Volumes/Macintosh\ HD/bin
/Volumes/Macintosh\ HD/usr/sbin
/Volumes/Macintosh\ HD/sbin

Below is a example of how to get the location of the hexdump command.

chroot /Volumes/Macintosh\ HD which hexdump

Below is the output from the above command. This output is the location (location) of the hexdump command. Since the chroot command changes its root directory to the supplied mount point /Volumes/Macintosh HD, the supplied mount point does not appear in the output.

/usr/bin/hexdump

Below is the synopsis for entering the located command.

macOSmountpoint/location [argument ...]

Below is a example of applying the hexdump command to the test.txt in the current working directory.

/Volumes/Macintosh\ HD/usr/bin/hexdump test.txt

An Alternative Method of Accessing macOS Commands

Below is an alternate synopsis for entering the macOS command which does not require first locating the command.

chroot macOSmountpoint macOScommand [argument ...]

For example, this alternative allows the previous two example commands given above to be replaced with the single command given below.

chroot /Volumes/Macintosh\ HD hexdump "$PWD/test.txt"

Note the following.

  • Using $PWD works in the above command, because /Volumes/Macintosh HD/Volumes/Macintosh HD is a symbolic link to /.

  • If the current working directory resides outside of the /Volumes/Macintosh\ HD mount point, then remounting could be required. See the section below titled "How to Remount When Necessary to Access One or More Files".

  • Some macOS commands may only work as expected if this alternative form is used. The zsh command is one such command.

Below is an example of how to invoke a Z shell.

chroot /Volumes/Macintosh\ HD zsh

A shell invoked as shown above will not have access to the /dev directory. However, redirections can be used. For example, below involkes a Z shell which allows access to /dev/null through field descriptor 8.

chroot /Volumes/Macintosh\ HD zsh 8>/dev/null

How to Remount When Necessary to Access a File

Note: Any remounting only persists until the Mac restarts.

If a file to be accessed does not reside in the volume containing the macOS commands, then the corresponding mount point for volume containing the file may have to be moved. The directory of the new mount point will need to begin with the directory of the mount point assigned to the volume containing the macOS commands. For example, if the aforementioned test.txt file is stored in an APFS volume name MyAPFS, then the default mount point would be /Volumes/MyAPFS. Since this directory does not begin with /Volumes/Macintosh HD, the MyAPFS volume will need to be remounted. First, enter the command below to unmount the MyAPFS volume.

diskutil unmount /Volumes/MyAPFS

Example output is shown below.

Volume MyAPFS on disk2s7 unmounted

Next, use the commands below to insure the new mount point exists, then mount the MyAPFS volume at that point. Here, the new mount point was chosen to be the same as when booted to Monterey. The identifier that is entered should be the same as the disk2s7 shown in the above output.

Note: The commands below must be entered in a shell where the root directory is /.

mkdir -p /Volumes/Macintosh\ HD/Volumes/MyAPFS
diskutil mount -mountpoint /Volumes/Macintosh\ HD/Volumes/MyAPFS disk2s7

Note: Do to the design of Monterey, the contents of the /Volumes/Macintosh HD/Volumes directory is actual stored in the Macintosh HD - Data volume.

Example output is shown below.

Volume MyAPFS on disk2s7 mounted

 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .