How do you create an APFS volume inside an ordinary file?

Solution 1:

You can use the Disk Utility application or a combination of the hdiutil and diskutil commands. The files can be either full images (.dmg) or sparse images (.sparseimage). You can also create a sparse bundle which is actually a folder (.sparsebundle) containing files. The sparse bundle can be used, for example, to create a 10 GB APFS drive on a physical partition that is FAT32 formatted. This would allow a person save a file greater than 4 GB on a drive/partition that was physically FAT32 formatted.

Below is an example of where the Disk Utility is used to create a 1 TB FAT32 formatted sparse image.

  1. Open the Disk Utility application.
  2. From the menu bar select File->New Image->Blank Image....
  3. Enter the following information, then click on Save.

An example of creating a 10 GB APFS sparse bundle is given below.

Note: This sparse bundle folder is created in the FAT32 sparse image from the previous example.

  1. Open the Disk Utility application.
  2. From the menu bar select File->New Image->Blank Image....
  3. Enter the following information, then click on Save.
  4. If the Disk Utility hangs after the pop up shown below appears, then Force Quit the Disk Utility.

Solution 2:

If you have a file that contains a raw image of a disk, you can use the following command in the Terminal:

hdiutil attach -imagekey diskimage-class=CRawDiskImage myimage.raw

If you haven't got an existing image, you can create a new, empty file named myimage.raw and then attach it without mounting:

hdiutil attach -imagekey diskimage-class=CRawDiskImage -nomount myimage.raw

Then you can create APFS volumes on there as you would have a physical disk and mount them. This can be done via the command line with a command such as:

diskutil apfs create disk5 MyNewVolumeName

where disk5 needs to be replaced with the device name, you've received from the attach command earlier.