11.1. Disks and PartitionsA common medium for storing user data is a hard[1] disk drive. The storage space on a disk is divided at the hardware level into fundamental units called sectors. In a typical hard drive, each sector holds 512 bytes[2] of user data. A sector may also hold some additional data used internally by the drivesuch as data for error correction and synchronization. A disk may also have some number of spare sectors that are not exposed through its interface. If a regular sector goes bad, the disk can attempt to transparently replace it with a spare one. Modern drives deprecate the geometric Cylinder-Head-Sector (CHS) addressing model for accessing a sector. The preferred model is Logical Block Addressing (LBA), in which addressable storage on a drive appears as a linear sequence of sectors.
The program in Figure 111 uses disk I/O control (ioctl) operations to retrieve and display basic information about a disk device on Mac OS X. Figure 111. Using ioctl operations to display information about a disk device
The two capacity numbers listed in Figure 111 are calculated using the metric and computer definitions of the giga prefix. The metric definition (1 gigabyte = 109 bytes; abbreviated as GB) leads to a larger capacity than a traditional computer science definition (1 gigabyte = 230 bytes; abbreviated as GiB). A disk may be logically divided into one or more partitions, which are sets of contiguous blocks that can be thought of as subdisks. A partition may contain an instance of a file system. Such an instancea volumeis essentially a structured file residing on the partition. Besides user data, its contents include data structures that facilitate organization, retrieval, modification, access control, and sharing of the user data, while hiding the disk's physical structure from the user. A volume has its own block size that is usually a multiple of the disk block size. The mount command, when invoked without any arguments, prints the list of currently mounted file systems. Let us determine the partition that corresponds to the root file system.[3]
$ mount /dev/disk0s10 on / (local, journaled) ... In this case, the root file system is on the tenth partitionor sliceof disk0. This system uses the Apple partitioning scheme (see Section 11.1.1), and therefore, the partition table for disk0 can be viewed using the pdisk command, which is a partition table editor for this scheme.
A similar list of disk0's partitions can be obtained using the diskutil command. $ diskutil list disk0 /dev/disk0 #: type name size identifier 0: Apple_partition_scheme *12.0 GB disk0 1: Apple_partition_map 31.5 KB disk0s1 ... 8: Apple_Patches 256.0 KB disk0s8 9: Apple_HFS Macintosh HD 11.9 GB disk0s10 11.1.1. The Apple Partitioning SchemeThe disk in Figure 112 uses the Apple partitioning scheme, with a specific partition layout called UNIVERSAL HD, which includes several legacy partitions. Let us analyze the pdisk output for disk0.
Figure 112. Listing a disk's partitions
A variant partition layout called UNIVERSAL CD would have partitions containing ATAPI drivers and SCSI Manager for CD. Figure 113 shows details of the Apple partitioning scheme. Although the disk shown in this case has a simpler layout, with no patch or driver partitions, the on-disk data structures follow similar logic regardless of the number and types of partitions. Figure 113. A disk partitioned using the Apple partitioning schemeThe first physical block's first two bytes are set to 0x4552 ('ER'), which is the Apple partitioning scheme signature. The next two bytes represent the disk's physical block size. The total number of blocks on the disk is contained in the next four bytes. We can use the dd command to examine the contents of these bytes for disk0. $ sudo dd if=/dev/disk0 of=/dev/stdout bs=8 count=1 2>/dev/null | hexdump 0000000 4552 0200 0180 0000 ... We see that the block size is 0x200 (512), and that the disk has 0x1800000 (25165824) 512-byte blocks. The next 63 512-byte blocks constitute the partition map. Each block represents a single partition map entry that describes a partition. Each map entry contains 0x504D ('PM') as its first two bytes, followed by information that includes the partition's starting offset, size, and type. The pdisk command lets you view, edit, and create Apple partitionsboth interactively and otherwise. Another command-line tool, diskutil, uses the Mac OS X Disk Management framework to let you modify, verify, and repair disks. You can also use the GUI-based Disk Utility application (/Applications/Utilities/Disk Utility.app) to manage disks, partitions, and volumes. Disk Utility allows creation of up to 16 partitions on a disk. One could create as many partitions as would fit in a given partition mapsay, using pdisk. However, some programs may not be able to handle more than 16 partitions properly. 11.1.2. PC-Style PartitioningIn contrast with the Apple partitioning scheme, PC partitions may be primary, extended, or logical, with at most four primary partitions allowed on a disk. The first 512-byte sector of a PC disk, the master boot record (MBR), has its space divided as follows: 446 bytes for bootstrap code, 64 bytes for four partition table entries of 16 bytes each, and 2 bytes for a signature. Therefore, the size of a PC partition table is rather limited, which in turn limits the number of primary partitions. However, one of the primary partitions may be an extended partition. An arbitrary number of logical partitions can be defined within an extended partition. The Mac OS X command-line program fdisk can be used to create and manipulate PC-style partitions. 11.1.3. GUID-Based PartitioningWe discussed GUID-based partitioning in Section 4.16.4.4 in the context of the Extensible Firmware Interface (EFI). x86-based Macintosh computers use the GUID-based scheme instead of the Apple partitioning scheme. In particular, although x86-based Macintosh computers support the Apple partitioning scheme, they can boot only from a volume partitioned using the GUID-based scheme. Figure 423 shows the structure of a GPT-partitioned disk. The gpt command-line program can be used on Mac OS X to initialize a disk with a GUID Partition Table (GPT) and to manipulate partitions within it. Section 11.4.4 provides an example of using gpt. The diskutil command also works with GPT disks. $ diskutil list disk0 # GPT disk /dev/disk0 #: type name size identifier 0: GUID_partition_scheme *93.2 GB disk0 1: EFI 200.0 MB disk0s1 2: Apple_HFS Mini HD 92.8 GB disk0s2 |