Setting the size and resizing disk images
In Disk Utility, one can create disk images from arbitrary folders; even encrypting them if you like. However, there doesn't seem to be a way of setting the image size, so once you run out of space, you need to start over. Disk Utility has a "Resize Image" option, but it appears to be quite limited (I can't get it to work on rw images).
So far, the only procedure I've found to create a disk image of an arbitrary size is:
-
Create a big, empty file in your otherwise clean directory:
dd if=/dev/zero of=zeros bs=1024M count=5
-
Create a disk image of that folder.
-
Mount the image, erase its contents (i.e., the
zeros
file) and copy the actual data you want in there.
Is there a simpler way?
Solution 1:
Often using the Graphic User Interface (GUI) offered by the Disk Utility application is more convenient than using a Command Line Interface (CLI) required by macOS commands. However, the Disk Utility application only offers a limited subset of the capabilities of various macOS commands. In your case, to resize an image you would need to use the hdiutil
command. Below is an example of the syntax.
hdiutil resize -size size_spec image
The size specifiers (size_spec
) can be the following.
-size ??b|??k|??m|??g|??t|??p|??e
Here ??
needs to be replaced by a number. The letters represent the following multipliers.
b
is bytes (not blocks) where the multiplier is 1.
k
is power of two kibibytes where the multiplier is 1024 (1 x 2^10).
m
is power of two mebibytes where the multiplier is 1048576 (1 x 2^20).
g
is power of two gibibytes where the multiplier is 1073741824 (1 x 2^30).
t
is power of two tebibytes where the multiplier is 1099511627776 (1 x 2^40).
p
is power of two pebibytes where the multiplier is 1125899906842624 (1 x 2^50).
e
is power of two exbibytes where the multiplier is 1152921504606846976 (1 x 2^60).
or
-sectors sector_count | min
Specify the number of 512-byte sectors to which the partition should be resized. If this falls outside the minimum valid value or space remaining on the underlying file system, an error will be returned and the partition will not be resized. min
automatically determines the smallest possible size.
For example, the following command could be applied to an ejected image.
hdiutil resize -size 3m sample.dmg
Afterwards, the command gpt -r show sample.dmg
could be used to verify the result. In this case, the command would produce the following output.
start size index contents
0 1 PMBR
1 1 Pri GPT header
2 32 Pri GPT table
34 6
40 6144 1 GPT part - 48465300-0000-11AA-AA11-00306543ECAC
6184 3
6187 32 Sec GPT table
6219 1 Sec GPT header
Where the partition holding the volume is shown as exactly 6144 sectors = 3 x 2^20 bytes / 512 bytes per sector.