Linux administration

Create Partitions and Filesystems

Nguyen Hai Chau
Vietnam National University

Concepts: Disks and Partitions

  • A hard disk provides a single large storage space
  • Usually split into partitions
    • Information about partitions is stored in the partition table
    • Linux defaults to using partition tables compatible with Microsoft Windows
    • For compatibility with Windows, at most four primary partitions can be made
    • But they can be extended partitions, which can themselves be split into smaller logical partitions
      • Extended partitions have their own partition table to store information about logical partitions

Disk Naming

  • The device files for IDE hard drives are /dev/hda to /dev/hdd
    • hda and hdb are the drives on the first IDE channel, hdc and hdd the ones on the second channel
    • The first drive on each channel is the IDE 'master', and the second is the IDE 'slave'
  • Primary partitions are numbered from 1–4
  • Logical partitions are numbered from 5
  • The devices /dev/hda, etc., refer to whole hard disks, not partitions
    • Add the partition number to refer to a specific partition
    • For example, /dev/hda1 is the first partition on the first IDE disk
  • SCSI disks are named /dev/sda, /dev/sdb, etc

Using fdisk

  • The fdisk command is used to create, delete and change the partitions on a disk
  • Give fdisk the name of the disk to edit, for example:
# fdisk /dev/hda
  • fdisk reads one-letter commands from the user

    • Type m to get a list of commands
    • Use p to show what partitions currently exist
    • Use q to quit without altering anything
    • Use w to quit and write the changes
  • Use with caution, and triple-check what you're doing!

Making New Partitions

  • Create new partitions with the n command
    • Choose whether to make a primary, extended or logical partition
    • Choose which number to assign it
  • fdisk asks where to put the start and end of the partition
    • The default values make the partition as big as possible
    • The desired size can be specified in megabytes, e.g., +250M
  • Changes to the partition table are only written when the w command is given

Changing Partition Types

  • Each partition has a type code, which is a number
  • The fdisk command l shows a list of known types
  • The command t changes the type of an existing partition
    • Enter the type code at the prompt
  • Linux partitions are usually of type 'Linux native' (type 83)
  • Other operating systems might use other types of partition, many of which can be understood by Linux

Making Filesystems with mkfs

  • The mkfs command initializes a filesystem on a new partition
    • Warning: any old data on the partition will be lost
    • For example, to make an ext2 filesystem on /dev/hda2:
# mkfs -t ext2 -c /dev/hda2
  • -t sets the filesystem type to make, and -c checks for bad blocks on the disk
    • mkfs uses other programs to make specific types of filesystem, such as mke2fs and mkdosfs

Useful Websites