Storage devices such as a hard disk or USB drives need to structured before start using it. Partitioning is the process of slicing the disk into one more separate region called a partition. Most importantly a partition is required to install an operating system. The second common reason is that you can install multiple operating systems.
It is basically the first step for a newly installed disk. After creating a partition, the partition is formatted with a file system.
In this tutorial, I will go through steps to create a new partition on Linux distribution such as CentOS, Ubuntu, RHEL, and Debian.
1) List the partitions in Linux
To list the existing partition or block devices on your system you can use
parted -l or
fdisk -l or
lsblk command. From this list, you can identify which disk you like to partition. The disk which is attached from storage devices (SANS) is normally visible in cat
$ sudo parted -l
You can see
unrecognized disk label error which indicates the new and unpartitioned disk.
$ sudo fdisk -l
$ sudo lsblk
We have two disks attached our system, first disk
/dev/xvda have the Operating system installed and the second is
2) Disk partitioning in Linux
In the section, we'll create a single partition using the entire disk. Fdisk and parted are two tools used in Linux to create a disk partition. For creating partitions greater than 2 TB is not supported by fdisk.
Here, we will check how to create a partition using parted tool.
Set a partition type
The most common two partition types are MBR (msdos) and GPT. GPT uses more modern standard and MBR is supported across many operating systems.
If you don't have a special requirement, then you can opt for GPT standard.
$ sudo parted /dev/xvdb mklabel gpt
Output ubuntu@linoxide:~$ sudo parted /dev/xvdb mklabel gpt Information: You may need to update /etc/fstab.
Instead of a one-liner command, you can also run parted command in an interactive way. In the example, I am changing disk label to
msdos which is MBR format.
$ sudo parted /dev/xvdb GNU Parted 3.2 Using /dev/xvdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mklabel msdos Warning: The existing disk label on /dev/xvdb will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? Y (parted)
For MBR type, run the following command:
$ sudo parted /dev/sda mklabel msdos
Creating a new partition
Even though the space for the new partition has been allocated and written to disk you still need to create a filesystem on that newly available space, so the next step is formatting the partition with the required filesystem.
$ sudo parted -a opt /dev/xvdb mkpart primary ext4 0% 100%
If you check
lsblk, can see a new partition
Note: Parted defaults to '1000 kilobytes = 1 megabyte' and not '1024 kilobytes = 1 megabyte'.
If you run parted command again to list partition, you no longer see any error:
To slice the whole disk into multiple partitions, run
mkpart specifying the size you want as follows:
(parted) mkpart primary ext4 0 1024MB (parted) print
You can add more partitions based on your requirement as shown below:
(parted) mkpart primary ext4 1024MB 2048MB (parted) print Model: Xen Virtual Block Device (xvd) Disk /dev/xvdb: 8590MB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 1024MB 1024MB ext4 primary 2 1024MB 2048MB 1023MB ext4 primary
So we have created two primary partitions which are named shows as
Note: For the MBR scheme you have a limit of 4 primary partitions but in GPT there are no such limits.
You may also use the following parted command to list partitions information on the disk.
$ sudo parted /dev/xvdb print
Model: Xen Virtual Block Device (xvd) Disk /dev/xvdb: 8590MB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 1024MB 1024MB ext4 primary 2 1024MB 2048MB 1023MB ext4 primary
quit to exit out from
parted prompt and whatever changes made will be saved.
3) Create a filesystem
Linux supports different filesystem type such as Ext2, Ext3, Ext4, BtrFS, and GlusterFS. In the previous section, we have created two new partitions and now we can format it as an Ext4 filesystem.
Linux has an inbuilt utility called
mkfs.ext4, which can format a partition to Ext4 filesystem.
$ sudo mkfs.ext4 -L databackup /dev/xvdb1
You can use
-L option to set a partition label while formatting the partition.
Use e2label command to change the partition label as follows:
$ sudo e2label /dev/xvdb2 storagedata
To print all partition table information such as Name, FSTYPE, LABEL, UUID and MOUNTPOINT use
$ lsblk --fs NAME FSTYPE LABEL UUID MOUNTPOINT loop0 squashfs /snap/ssm-agent/1566 loop1 squashfs /snap/core/8935 xvda └─xvda1 ext4 cloudimg-rootfs 6156ec80-9446-4eb1-95e0-9ae6b7a46187 / xvdb ├─xvdb1 ext4 databackup 86d249af-ead2-41d4-9acd-296e36c63ec4 └─xvdb2 ext4 storagedata beae745b-188f-41d2-a133-7c4212da0a34
4) Mount Filesystem
Finally, we are now going to mount the filesystem that enables to write data to mount point.
The following command will temporarily mount the filesystem:
$ sudo mount -t auto defaults /dev/xvdb1 /mnt/data
It's important to make sure that /etc/fstab file is updated to have newly created partitions get mounted automatically at boot time.
The fstab file should have any entry as follows:
LABEL=databackup /mnt/data ext4 defaults 0 2
/dev/xvdb1 /mnt/data ext4 defaults 0 2
Note: SCSI devices are identified as 'sd' and the letter immediately after 'sd' signifies the order in which it was first found. For example, sda1 means the first partition on the first drive. For explaining I have used Xen virtual disk which show devices as '/dev/xvd'.
To verify the filesystem is available use df command to list mounted partition and see its size.
$ df -h -x tmpfs -x devtmpfs -x squashfs
In this tutorial, we learned how to partition, format, and mount a raw hard drive attached to the Linux system.
If you have any questions or feedback, feel free to leave a comment.
Related Read: How to Create Swap Partition on Linux