Sample Header Ad - 728x90

Shrink disk image to eliminate free space

1 vote
1 answer
516 views
I created a larger-than-necessary writable disk image, copied content to it, then shrunk the partition with diskutil resizeVolume. This left a big gap at the end of the partition table:
MEDIA: ""; Size 20 GB [41943040 x 512]; Max Transfer Blocks 2048
SCHEME: 1 APM, "Apple Partition Scheme" 
SECTION: 1 Type:'MAP'; Size 20 GB [41943040 x 512]; Offset 1 Blocks (41943039 + 1) x 512
ID Type                 Offset       Size         Name                      (4)
-- -------------------- ------------ ------------ -------------------- --------
 1 Apple_partition_map             1           63 Apple               
 2 Apple_Free                     64       262144 Explicit Record     
 3 Apple_HFS                  262208     25941040 disk image          
 4 Apple_Free               26203248     15739792 Explicit Record
I cannot figure out how to eliminate this gap and shrink the disk image down to the minimal size. The obvious command does nothing (with or without -nofinalgap): hdiutil resize -sectors min image.dmg I can even edit the file with a hex editor to change the partition count, which results in this partition table (note Free not Apple_Free), but even then I cannot get figure out how to eliminate the free space.
ID Type                 Offset       Size         Name                      (3)
-- -------------------- ------------ ------------ -------------------- --------
 1 Apple_partition_map             1           63 Apple               
 2 Apple_Free                     64       262144 Explicit Record     
 3 Apple_HFS                  262208     25941040 disk image          
   Free                     26203248     15739792
Asked by rgov (641 rep)
Apr 3, 2022, 04:02 PM
Last activity: May 3, 2022, 10:00 PM