Month: November 2021

  • Command Line Knowledge for macOS: software update

    One of the leading reasons to dive into the command-line tools in macOS is automation.   Writing scripts that link with the Shortcuts stuff adapted from iOS means you can automate some things.

    For instance, there is a command-line tool that you can use to run software updates: software update.

    Consider:

    
    softwareupdate -l
    
    

    This gets you a list of available software just like what happens in System Preferences when you launch the Software Update preference pane. In both cases, the utilities are talking the softwareupdate daemon in the operating system.

    Next up, getting stuff installed:

    
    softwareupdate -I NAME
    softwareupdate --install name
    
    

    You replace NAME with one of the items from the list you asked for in the first step. Be careful there as macOS is very sensitive about names and format of names. You should quote the names and watch out for cases where the name has trailing spaces.

    The “-d” option will just download an update while including the “-a” option will install all available updates. One of the useful options for this command-line tool is “–install-rosetta”. This option tells macOS on Apple silicon Macs to install the Rosetta 2 hypervisor/emulator for Intel macOS applications. Include the –agree-to-license” option to agree to the software license agreement without user interaction.

    Selah.

  • Command Line Knowledge for macOS: diskutil

    I have found over the years that the Disk Utility app in macOS has become less and less useful to me over the years.   My guess has been the dev teams at Apple have trying to cut back on the ability of less informed to do unpleasant things to themselves using the tool.

    But with a little effort we find the macOS diskutil utility. This is also where we begin to see some the FreeBSD heritage in macOS as this is follows the FreeBSD “noun verb” UX for commands where you enter diskutil followed by a number of verbs that do the work.

    Let’s start with the list verb. Issuing the command:

    diskutil list
    

    Gets you a listing of currently mounted disks, partitions, and mount points. This is fun as you get a lot more detail about the internals of how APFS is blatting stuff through your disk. For instance, on my machine I have /dev/disk0 as the physical disk with an APFS container disk in a partition. That logic disk is mounted as /dev/disk3 with the multiple volumes in the container. This is far more detailed information than what you get from the user interface.

    The info verb gets you the details for a specific disk. Again, lots of detail but good for troubleshooting. The umount and umountDisk verbs are for un-mounting partitions and disks out of a file system while mount goes the other way. It’s important to understand that the eject verb is for removable devices and is the same action as ejecting a drive in Finder.

    All of the formatting and partition things you do in the GUI have corresponding verbs in the command-line tool. Do be careful as with great power comes with great responsibility. Think twice and then again before hitting that enter key.

    The jobby-job thing that I do to pay the bills requires me to do a bunch of system administration things. So, I’m often needing to “burn” a Linux installer to a USB key. Here we can use a combination of the diskutil and hdiutil command-line tools to automate that process.

    First, use the list verb to find the mount point for the USB device that’s your target device:


    diskutil list

    Now we can write a Bash script that will do the heavy lifting for us:

    
    ISONAME=$1
    DESTDISK=$2
    TMPIMG=“~/tmp/copytarget.img”
    hdiutil convert UDRW -o $TMPIMG $DESTDISK
    diskutil unmountDisk /dev/$DESTDISK
    dd if=$TMPIMG of=/dev/r$(DESTDISK) bs=1m
    diskutil eject $DESTDISK
    rm $TMPIMG 
    

    So, our little script takes two parameters: the filename of the Linux ISO and the base name of the disk mount point. This script first uses hdiutil to convert the ISO into a macOS disk image. I keep a temp folder in my home folder for these sort of things. We then unmount the external device and use the classic UNIX dd command to do a byte-by-byte copy to raw version of the mount point. After doing this, you need to eject the device.

    And that’s a quick summary of diskutil.

    Selah.