What are the sector sizes on Mac OS X?

I read on the HFS Plus Wikipedia page that the default sector size on a Mac is 512 b. However, I also read that Macs support 4kb sectors. How can I determine which I have, 512 b or 4 kb sectors? I think 512 is the right answer, but I would like to be sure.


Solution 1:

In terminal, you can use diskutil to get information about the drive, including the device block size:

$ diskutil info / | grep "Block Size"
  Device Block Size:        512 Bytes

Solution 2:

First, note that Device Block Size is different from the block size in use by the filesystem. The former value as reported by diskutil refers to the raw block size used by the hardware. I haven't found an easy way to check the latter value by the command line, but you can just create a zero-byte file then do Get Info from the Finder. It will say 0 bytes, but 4k used on disk.

Second, you can create an HFS+ filesystem with larger than 4k block sizes using the command line program newfs_hfs. The easiest way is to use Disk Utility to partition the drive and create a partition with the default formatting, then use /bin/df to determine the block device (an example only: /dev/disk0s2). Then unmount that partition (using umount /dev/diskXXX or Disk Utility), and to reformat as HFS+ with 64k blocks do:

newfs_hfs -v VolumeName -b 65536 /dev/disk0s2

Use the Get Info tip above to verify that a small file now occupies 64k on disk (it may say 65k for powers-of-10 units).

Performance is the main reason you might want to do this, if most of the data to be stored are large files (such as MP3s, photo, video, .zip files, etc), and it also helps with keeping disk fragmentation low. Obviously don't bother if you plan on storing mostly small files.

I have found that on large drives (> 1 TB) formatted as HFS with the default 4k block size, when the drive nears capacity, write performance degrades terribly. I'm guessing that is due to the partition being fragmented and having to hunt and peck for free blocks in order to write out the last 1% of data. I'm hoping that larger block sizes will alleviate this problem somewhat.

Solution 3:

The OS X device block size can be determined by executing the following command from a Terminal window prompt:

diskutil info / | grep "Block Size"

Which will output the following information:

Device Block Size: 512 Bytes

The file system block size can be determined by using the stat utility:

stat -f %k .

Which will show you the Optimal file system I/O operation block size in bytes:

4096

Solution 4:

With the older HFS file system, Apple's second attempt at a file system for the Macintosh, there could only be up to 65,535 allocation blocks on a disk. The block size was a function of the disk size in bytes divided by the maximum number of allocation blocks, 65,535. For small disks this was fine, but when disk sizes started to approach 1GB in size the minimum allocation block size was 16 KB (1073741824 / 65535) = 16384.25, but you need to truncate to 16384.

What this means is that a 1 byte file on a 1GB HFS disk required 16,384 bytes to store on the hard drive, so there was too much wasted space. So Apple created the HFS+ wrapper to reduce the wasted space.

There may be a way to set the block size higher, but why would you want to?

Here is a quick bash script that will list all of the diskinfo output for all of your currently connected disks.

#!/bin/bash

for disk in /dev/disk*s*
do
    diskutil info $disk
    echo "**************************************************************************************"
    echo " "
done