Disk partition in Unix

From Exterior Memory
(Redirected from Format disk in Linux)
Jump to: navigation, search

Each hard disk is partitioned into multiple partitions: logical disks. The information where each partition is stored on the disk is store in a partition map. The format of the partition map is dictated by the partition scheme. The most-often used partition schemes are:

  • GUID Partitioning Table (GPT), a modern and extremely common partition scheme.
  • Master Boot Record (MBR), an older format popular on older DOS/Windows disks. It can only be used for disk smaller than 2 TByte.
  • Apple Partition Map (APM), an old (pre-2006) format used on PowerPC-based Apple disks.

In addition, some removable media (such as floppies, CDs, DVDs and USB sticks) might not use a partitioning, but instead have a single file system fill the whole disk.

If you have the choice, use GPT.

Block Alignment

With MBR, the first 512-byte sector of a disk is reserved to store information for up to 4 partitions and a 446-byte bootloader. Usually, the only job of this small bootloader is to find the location of a second bootloader (typically 512kByte) and start booting from there.

GPT uses 34 512-byte sectors, #0 tot #33, with #0 used for backwards compatibility with MBR.

Thus the first actual partition is usually placed at block #34. However, this does not align with the 4096-byte logical blocks of current diskdrives, so it is recommend to add 6 "free" sectors, and let the first actual partition start at block #40.

List partition tables

On BSD and Linux, use gpart:

pakhuis# gpart show
=>       34  117231341  ada0  GPT  (56G)
         34       1024     1  bios-boot  (512K)
       1058          6        - free -  (3.0K)
       1064  117230304     2  freebsd-zfs  (56G)
  117231368          7        - free -  (3.5K)

=>        34  5860533101  ada1  GPT  (2.7T)
          34          94        - free -  (47K)
         128     4194304     1  freebsd-swap  (2.0G)
     4194432  5856338696     2  freebsd-zfs  (2.7T)
  5860533128           7        - free -  (3.5K)

on macOS, use diskutil:

[freek@MLT0079-2] ~% diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage Untitled                250.1 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3

/dev/disk1 (internal, virtual):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                  Apple_HFS LittleBit              +249.8 GB   disk1
                                 Logical Volume on disk0s2
                                 23422256-E72D-4272-B75D-2C3BA6A8C762
                                 Unlocked Encrypted

Mount disks on Linux

Create a file system:

mkfs -t ext3 /dev/hda1

Mount a partition:

sudo mount -t ext3 /dev/hda9 /backup

It is advisable to add this information to /etc/fstab so you don't have to specify the mount point and disk type manually:

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>               <dump>  <pass>
/dev/hda1       /               ext3    errors=remount-ro       0       1
/dev/hdc7       /mnt/hdc7       ext3    noauto                  0       2

or using UUID:

# <file system>                            <mount point>  <type>  <options>           <dump>  <pass>
UUID=8b4ea5ed-d138-4077-93fe-67de4e997eaa       /         ext4    errors=remount-ro       0       1
UUID=d8071118-283f-41ca-8c26-1dcc76e02021       /data     ext4    noauto                  0       2

These UUID can be found with the `blkid` or `lsblk -a` tool in the util-linux package.

# blkid
/dev/sda1: UUID="91BD-6366" TYPE="vfat" PARTLABEL="efi" PARTUUID="20804065-e679-48e2-948d-abb697e2fe46"
/dev/sda2: LABEL="root" UUID="8b4ea5ed-d138-4077-93fe-67de4e997eaa" TYPE="ext4" PARTLABEL="root" PARTUUID="a8de86b9-362f-448a-af13-6b7fbf75d119"
/dev/sda3: LABEL="data" UUID="d8071118-283f-41ca-8c26-1dcc76e02021" TYPE="ext4" PARTLABEL="data" PARTUUID="952f4878-5bfc-4db5-ae79-35815a25b84f"
/dev/sda4: UUID="7e32e298-d42e-4d91-85bc-d80ad7d4201f" TYPE="swap" PARTLABEL="swap" PARTUUID="8893b517-372f-46b3-963b-a34e830f7896"

Note that while fstab is mostly replaced by udev, it is still commonly used to specify mount points for static disks. The only thing that fstab does not handle properly is mounting is unmounting of removable media (such as USB drives) by regular (non-root) users.

Old MBR tools

List partition table:

sudo fdisk -l /dev/hda

Interactively partition disk:

sudo fdisk /dev/hda