Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to create an APFS partition using the terminal?

Hello everyone.


Preface:

I have an NVMe SSD and a box for connecting an SSD via USB. I want to make a multi-boot disk with different operating systems (including macOS) from this kit.

Also I need an EFI partition larger than the standard 200 MB.


That's what I was doing:

1. Connected the disk via USB


2. In the disk utility, the entire disk was formatted in APFS


3. Using the

diskutil list

command, I looked at the status of the partitions on the attached disk


4. Then, using the

sudo gpt -r show /dev/disk3

command, I looked at the state of the partitions in more detail


5. Using these commands, I deleted the EFI and APFS partitions

diskutil unmountDisk disk3
sudo gpt remove -i 1 /dev/disk3
diskutil unmountDisk disk3
sudo gpt remove -i 2 /dev/disk3

(yes, after deleting each partition, the entire disk had to be unmounted)


5.1 And looked at the disk information to make sure. that the partitions are deleted

diskutil list

sudo gpt -r show /dev/disk3


6. Using this command, I found out the size of the sector on the disk

diskutil info /dev/disk3 | grep "Block Size"

Device Block Size: 512 Bytes


7. I want to create an EFI partition with a size of 350 MB and it is 367001600 bytes or 716800 sectors

To create this EFI partition I run this command

sudo gpt add -b 40 -i 1 -s 716800 -t efi /dev/disk3


8. The next partition I want to create an APFS partition with a size of 100 GB is 107374182400 bytes or 209715200 sectors and for this I execute this command

sudo gpt add -b 716840 -i 2 -s 209715200 -t apfs /dev/disk3


9. Checked that both partitions are really created

diskutil list

sudo gpt -r show /dev/disk3


And after that, some kind of non-functional APFS container is created.

With this command, I tried to create a volume in the APFS container

sudo diskutil apfs addVolume /dev/disk3s2 APFS TestAPFS

but this error appears

Error starting APFS Container creation: The given disk is not an APFS Container Reference (-69630)


I am trying to create an APFS disk in place of the current APFS partition

sudo diskutil apfs createContainer /dev/disk3s2

but also got an error

Error: -69614: The disk is already in use by APFS


And even when I tried to delete this APFS container

sudo diskutil apfs deleteContainer /dev/disk3s2

I got an error

Unable to look up the APFS Container Reference for the given Physical Store


I also tried to reformat the APPS section to APFS, using this command

sudo diskutil eraseVolume APFS TestAPFS disk3s2

but it didn't help


After that, I opened the disk utility and in the hierarchy of the connected disk there was a dark gray name of the 100 GB partition and it was called, like, "Physical Location" (something like that). I formatted it in APFS and eventually formatted the entire disk into 1 APFS partition and the EFI partition became 200 MB again.


How to create a working APFS partition using the terminal correctly, so that it is the same as the disk utility does?


P.S.

Here are the articles I relied on:

https://apple.stackexchange.com/questions/280405/create-an-efi-partition

https://itectec.com/askdifferent/macos-how-to-fix-the-error-this-disk-doesnt-contain-an-efi-system-partition/

https://www.transscendsurvival.org/2019/02/27/mac-osx-fixing-gpt-and-pmbr-tables/

https://apple.stackexchange.com/questions/307780/macos-partition-startup-volume-type-ffffffff-ffff-ffff-ffff-ffffffffffff

https://eclecticlight.co/2017/04/08/how-to-make-your-own-apfs-volume/

https://gist.github.com/bzerangue/dca8fc2d63309ba2bd9f

Mac Pro

Posted on Dec 29, 2021 9:32 AM

Reply

Similar questions

4 replies

Dec 29, 2021 10:43 AM in response to Drovosek01

Here's what else I tried:

1. Tried to create a partition on the APFS volume that I created in step 8

I used this command:

sudo newfs_apfs -s 10g -v "testAPFS" -A /dev/disk3s2

But I got an error in response:

newfs_apfs: /dev/disk3s2 is not an APFS container

Then I looked at the information about the disks using

diskutil list

And I made sure that the volume that I originally created is still a container


2. I tried to reformat the APFS volume that I created in step 8.

I used this command:

sudo diskutil reformat /dev/disk3s2

But I got an error:

The target disk is imported by an APFS Container as a Physical Store; you probably want to use diskutil apfs verbs to delete and (re)-add its APFS Volume(s)


3. I tried to erase the APFS volume that I created in step 8.

I used this command:

sudo diskutil eraseVolume APFS "TestAPFS" /dev/disk3s2

But I got an error:

The target disk is in use by APFS as a Physical Store; use diskutil apfs deleteContainer


4. I tried to delete the APFS container that I created in step 8.

I used this command:

sudo diskutil apfs deleteContainer /dev/disk3s2

And upon completion of its work, the container was deleted and all the free space was reformatted into the HFS+ file system (although I did not ask to do this).

Dec 29, 2021 1:53 PM in response to HWTech

Thank you for trying to help, but this is not the solution I need.

After executing the command you specified, the disk was completely reformatted and now it has a standard EFI partition with a standard size of 200 MB, as well as a FAT16 partition with the name EFI (but this name does not make ESP out of it), and all the remaining space was formatted in APFS.


First of all. as far as I know, it is more correct to format the ESP partition in FAT32 and to do this, instead of MS-DOS, you need to specify MS_DOS FAT32."


Secondly, another problem arose. I don't want to format the entire disk in APFS, I want, for example, an APFS volume with a size of 100GB and mark up the remaining unmarked space sometime later or even in another OS (for example, Linux or Windows). In this command, I specified the APFS size of 100 GB, but it still filled all the free space.

How do I create an APFS partition exactly 100 GB in size?

Dec 29, 2021 12:06 PM in response to Drovosek01

You are making things extremely complicated here by mixing "diskutil" and "gpt". Most of the things you did with "gpt" can be done with "diskutil". I ended up just skimming both of your posts after a while.


I'm not entirely sure if you can do this simply because I'm not certain whether "diskutil" will create an APFS Container automatically which is required if you are going to be installing macOS to the external drive. You can try to use "diskutil" to partition the drive, but if this does not create the APFS Container, then you may need to use a second "diskutil" command to add the Container. A second option is to create another separate EFI/ESP partition (in addition to having the small default EFI/ESP partition and APFS volume the Disk Utility GUI app creates -- it depends on why you are doing all this and it can be done using the Disk Utility GUI app to make things simple).


I've rarely used "diskutil" to partition or format the drives so I'm offering this as an example (assuming "disk3" is still the correct drive ID for the external drive):

diskutil  partitionDisk  disk3  2  GPT    MS-DOS  EFI  350M     APFS  TestAPFS  R  


You may first need to unmount all the volumes on the external drive (this is much simpler than the method you used):

diskutil  unmountDisk  disk3


Dec 29, 2021 5:54 PM in response to Drovosek01

I did forget that even the "diskutil" command will not do exactly as a user requests when specifying manual partitions. I also only gave the command as an example and I did mention I don't normally use "diskutil" in this manner plus you can in theory leave the unused area unpartitioned if you specify a specific size for the APFS partition. The rest of the drive should remain unpartitioned (in theory).


Like I said in my other post, depending on how/why you need such a large ESP partition, the EFI partition I created can work perfectly fine to boot a Mac especially if the bootloader files are named "boot_x64.efi" contained within a "boot" folder, but custom names could possibly work (sometimes depends on the Mac and its firmware). I have a custom boot drive containing multiple volumes which boot third party utilities and even some old Apple diagnostics without using the main ESP partition. Sometimes it does depend on what you are trying to boot. You should also be able to change the partition ID to become a true ESP partition, but I don't think any of the macOS utilities can do this.


Plus you may be able to use the rEFInd boot manager to assist you in booting other items since rEFInd can be easily customized if it doesn't detect the items on the standard MS-DOS partition by default.


I would suggest using the Disk Utility GUI or "diskutil" to partition the drive as you want including a 350MB dummy partition. Then use "gpt" to remove that dummy partition and replace it with an ESP partition. Then use "gpt" to remove the 200MB Apple ESP partition created by "diskutil". You should only be using the the Disk Utility GUI or "diskutil" for partitioning a macOS boot drive. Using any other utility is risky and should be avoided and this is true of any boot drive for any OS (yes other utilities may work, but you never know if there are any negative side effects). This is something similar to what you first attempted, but you made the other partition an APFS container. I would try the following:


diskutil  partitionDisk  disk3  2  GPT    MS-DOS  EFI2  350M     APFS  TestAPFS  100G


diskutil  unmountDisk  disk3


Make sure to get the correct values for "<start-sector>" and "<sectors>" for the "EFI2" partition before using "gpt" to remove these two partitions so you can utilize the space from "EFI2" as a proper ESP partition.

sudo  gpt  remove  -i  2  -i  1  /dev/disk3
sudo  gpt  add  -i  1   -b  <start-sector>   -s  <sectors>  -t  efi   /dev/disk3


I assume "gpt" actually formats the partition, but I don't know if it also provides the "EFI" label. If it does not:

sudo  gpt  label  -i  1  -l  "EFI"  /dev/disk3


FYI, I don't think it matters if the ESP is FAT16 or FAT32, but it may also depend on the system.


Please double check my examples here since I'm not actually testing them out. I've also never used most of these utilities in this manner.


Also, if you are going to be using Windows BootCamp who knows what Apple will do to your custom partitions here (at least the custom ESP partition). As we've already seen macOS utilities tend to do what they want and macOS tends to misbehave when it encounters something unexpected that deviates from Apple defaults (not saying it is certain, just that it can happen).

How to create an APFS partition using the terminal?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.