Creating a disk partition in Linux

Disk Partitioning in Linux

Creating and deleting partitions in Linux is a regular practice because storage devices (such as hard drives and USB drives) must be structured in some way before they can be used. In most cases, large storage devices are divided into separate sections called partitions. Partitioning also allows you to divide your hard drive into isolated sections, where each section behaves as its own hard drive. Partitioning is particularly useful if you run multiple operating systems.

Creating a Disk Partition in Linux

This procedure describes how to partition a storage disk in Linux using the parted command.

Procedure

  1. List the partitions using the parted -l command to identify the storage device you want to partition. Typically, the first hard disk (/dev/sda or /dev/vda) will contain the operating system, so look for another disk to find the one you want. For example:

    1. sudo parted -l
    2. Model: ATA RevuAhn_850X1TU5 (scsi)
    3. Disk /dev/vdc: 512GB
    4. Sector size (logical/physical): 512B/512B
    5. Partition Table: msdos
    6. Disk Flags:
    7. Number Start End Size Type File system Flags
    8. 1 1049kB 525MB 524MB primary ext4 boot
    9. 2 525MB 512GB 512GB primary lvm
  2. Open the storage device. Use the parted command to begin working with the selected storage device. For example:

    1. sudo parted /dev/vdc
    2. GNU Parted 3.3
    3. Using /dev/vdc
    4. Welcome to GNU Parted! Type 'help' to view a list of commands.
    5. (parted)

    Be sure to indicate the specific device you want to partition. If you just enter parted without a device name, it will randomly select a storage device to modify.

  3. Set the partition table type to gpt, then enter Yes to accept it.

    1. (parted) mklabel gpt
    2. Warning: the existing disk label on /dev/vdc will be destroyed
    3. and all data on this disk will be lost. Do you want to continue?
    4. Yes/No? Yes

    The mklabel and mktable commands are both used for making a partition table on a storage device. At the time of writing, the supported partition tables are: aix, amiga, bsd, dvh, gpt, mac, ms-dos, pc98, sun, atari, and loop. Use help mklabel to get a list of supported partition tables. Remember mklabel will not make a partition, rather it will make a partition table.

  4. Review the partition table of the storage device.

    1. (parted) print
    2. Model: Virtio Block Device (virtblk)
    3. Disk /dev/vdc: 1396MB
    4. Sector size (logical/physical): 512B/512B
    5. Partition Table: gpt
    6. Disk Flags:
    7. Number Start End Size File system Name Flags
  5. Create a new partition using the following command. For example, 1396 MB on partition 0:

    1. (parted) mkpart primary 0 1396MB
    2. Warning: The resulting partition is not properly aligned for best performance
    3. Ignore/Cancel? I
    4. (parted) print
    5. Model: Virtio Block Device (virtblk)
    6. Disk /dev/vdc: 1396MB
    7. Sector size (logical/physical): 512B/512B
    8. Partition Table: gpt
    9. Disk Flags:
    10. Number Start End Size File system Name Flags
    11. 1 17.4kB 1396MB 1396MB primary

    Providing a partition name under GPT is a must; in the above example, primary is the name, not the partition type. In a GPT partition table, the partition type is used as partition name.

  6. Quit using the quit command. Changes are automatically saved when you quit parted.

    1. (parted) quit
    2. Information: You may need to update /etc/fstab.

Help command for creating a new partition

To get help on how to make a new partition, type: help mkpart.

  1. (parted) help mkpart
  2. mkpart PART-TYPE [FS-TYPE] START END make a partition
  3. PART-TYPE is one of: primary, logical, extended
  4. FS-TYPE is one of: udf, btrfs, nilfs2, ext4, ext3, ext2, fat32, fat16, hfsx, hfs+, hfs, jfs, swsusp,
  5. linux-swap(v1), linux-swap(v0), ntfs, reiserfs, hp-ufs, sun-ufs, xfs, apfs2, apfs1, asfs, amufs5,
  6. amufs4, amufs3, amufs2, amufs1, amufs0, amufs, affs7, affs6, affs5, affs4, affs3, affs2, affs1,
  7. affs0, linux-swap, linux-swap(new), linux-swap(old)
  8. START and END are disk locations, such as 4GB or 10%. Negative values count from the end of the
  9. disk. For example, -1s specifies exactly the last sector.
  10. 'mkpart' makes a partition without creating a new file system on the partition. FS-TYPE may be
  11. specified to set an appropriate partition ID.
  • Setting filesystem type (FS-TYPE) will not create an ext4 filesystem on /dev/vdc1. You still have to create the ext4 filesystem with mkfs.ext4.

  • A DOS partition table’s partition types are primary, logical, and extended.

  • Providing a partition name under GPT is a must. In a GPT partition table, the partition type is used as the partition name.