Mastodon

How to Partition an APFS Drive

Apple’s darling new file system, appropriately named Apple File System (APFS), has a lot to love about it, but it’s not without its hiccups, which tend to manifest through obscure and poorly-documented error codes, a lá Windows. One such feature that can easily cause headaches is partitioning. Partitions are logical sections on a storage medium upon which a filesystem is written to and that can be managed independently of one another. All storage devices must have at least one partition to be used. While partitioning is often used simply to segregate data on a drive (An operating system on one partition and user data on another), partitions can be used to install multiple operating systems on the same storage device. In most cases however, partitions are used to accomplish the former, rather than the latter. In light of this, APFS provides a nifty feature to solve a fraught problem with partitioning: deciding exactly how to slice the pie. Shrinking and expanding partitions can be a huge pain. APFS solves this with pseudo-partitions called Volumes that exist within a larger partition called a Container. Volumes share free space and can consume as much as they need from the pot (Or until its quota is reached, if one is set), and return it when no longer needed. The only downside to Volumes is that because they exist on a parent partition, they themselves cannot have a different file system and, as of time of writing, no operating system other than macOS supports installation on APFS. So if you were crazy enough to want to install Arch on your Mac, and don’t have a spare SSD lying around, you’re going to need to truly partition your drive.

Disk utility.

Step one: fire up Disk Utility, and select “Partition.” Disk Utility will ask you if you’d prefer to add a volume instead, which we do not. Next, select “+” under the pie chart to add a partition. If you’re unable to, look for a message under “Partition Information” stating that the “container cannot be split because the resulting containers will be too small.” Huh? Too small for what?

A not-so-helpful message.

Well if your drive is formatted with APFS and you use time machine like I do, partitioning it will make the container too small for its snapshots. Another awesome feature of APFS is its ability to create temporary restoration points, snapshots, which write-protect all blocks on the device that contain data belonging in a snapshot. This means that in order to partition our drive and format it with a new file system, we’ll have to remove any snapshots of the current partition.

To list snapshots on the current partition, you can use the cleverly-named listlocalsnapshots command:

$
tmutil listlocalsnapshots /

A typical output might look like so:

$
com.apple.TimeMachine.2018-12-23-005508 com.apple.TimeMachine.2018-12-23-015511 com.apple.TimeMachine.2018-12-23-123520 com.apple.TimeMachine.2018-12-23-201023 com.apple.TimeMachine.2018-12-23-211137 com.apple.TimeMachine.2018-12-23-221150 com.apple.TimeMachine.2018-12-23-231149 com.apple.TimeMachine.2018-12-24-001108

Deleting a snapshot is simple as well:

$
tmutil deletelocalsnapshots YYYY-MM-DD-HHMMSS

The final argument is the timestamp of the snapshot as given in the last portion of the snapshot’s filename.

Before we voluntarily remove our first line of defense against data loss and perform a destructive operation on our file system, make sure your data is backed up (Preferably to another drive). We’ll also want to disable Time Machine until we’ve completed partitioning:

$
tmutil disable

Afterward, re-enble it with:

$
tmutil enable

If you have more than a few snapshots, deleting all of them will be tedious to say the least. Let’s save ourselves the trouble with a short script:

#!/usr/bin/env bash
for d in $(tmutil listlocalsnapshotdates); do
sudo tmutil deletelocalsnapshots $d
done

Feel free to brew yourself a cup of coffee while you wait for it to finish, it might take a while.

Once all of the snapshots on your drive have been removed, we can try adding a new partition again. This time, we won’t have the constraint of any snapshots write-protecting blocks all over the drive.

Note that if you’re partitioning your boot drive (Which you more than likely are), your system will become temporarily unresponsive. Feel free to brew another cup while you wait, as it might be a while, depending on the size of your drive.

And there you have it! A shiny new partition on your drive. Be careful that you select the correct partition if you’re installing a new OS, as it will probably want to reformat it. But I wouldn’t worry too much if anything goes wrong. After all, you have everything backed up, right?