Storage devices (such as hard drives or USB drives) need to be structured before they can be used. Partitioning is the process of cutting a disk into a separate area called a partition. Most importantly, a partition is required to install the operating system. The second common reason is that you can install multiple operating systems.
Basically, this is the first step of installing a new disk. After the partition is created, the partition will be formatted using the file system.
1) List the partitions in Linux
To list existing partitions or blocked devices on the system, you can use
parted -l Either
fdisk -l Either
lsblk command. From this list, you can determine which disk to partition. Disks connected from storage devices (SANS) are usually visible in cat
$ sudo parted -l
you can see
unrecognized disk label Errors indicate new and unpartitioned disks.
$ sudo fdisk -l
$ sudo lsblk
Our system has two disks, the first disk
/dev/xvda The operating system is installed, the second is
2) Disk partition in Linux
In this section, we will use the entire disk to create a partition. Fdisk and parted are two tools used to create disk partitions in Linux. fdisk does not support the creation of partitions larger than 2 TB.
Here, we will check how to use the parted tool to create partitions.
Set partition type
The two most common partition types are MBR (msdos) and GPT. GPT uses more modern standards, and many operating systems support MBR.
If you have no special requirements, you can choose the GPT standard.
$ sudo parted /dev/xvdb mklabel gpt
Output [email protected]:~$ sudo parted /dev/xvdb mklabel gpt Information: You may need to update /etc/fstab.
In addition to single-line commands, you can also run parted commands interactively. In the example, I changed the disk label to
msdos This is the 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
Create a new partition
Even if space has been allocated for the new partition and written to disk, you still need to create a file system on this new free space, so the next step is to format the partition with the required file system.
$ sudo parted -a opt /dev/xvdb mkpart primary ext4 0% 100%
If you check
lsblk, You can see a new partition
Note: Parted defaults to “1000 KB = 1 megabyte” instead of “1024 KB = 1 megabyte”.
If you run the parted command again to list the partitions, you will no longer see any errors:
To cut the entire disk into multiple partitions, run
mkpart Specify the desired size as follows:
(parted) mkpart primary ext4 0 1024MB (parted) print
You can add more partitions as needed, as follows:
(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
Therefore, we created two main partitions with the names shows
Note: For the MBR scheme, you have a limit of 4 primary partitions, but there is no such limit in GPT.
You can also use the following parted command to list the partition 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 drop out
parted Tip, any changes made will be saved.
3) Create a file system
Linux supports different file system types, such as Ext2, Ext3, Ext4, BtrFS and GlusterFS. In the previous section, we created two new partitions, and now we can format them as Ext4 file system.
Linux has a built-in utility called
mkfs.ext4, You can format the partition as Ext4 file system.
$ sudo mkfs.ext4 -L databackup /dev/xvdb1
you can use it
-L Option to set the partition label when formatting the partition.
Use the 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, please 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 the file system
Finally, we will now mount the file system that can write data to the mount point.
The following command will temporarily mount the file system:
$ sudo mount -t auto defaults /dev/xvdb1 /mnt/data
It is important to ensure that the /etc/fstab file is updated so that the newly created partition is automatically mounted at boot time.
The fstab file should have any of the following entries:
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” indicates the order in which the device was first found. For example, sda1 represents the first partition on the first drive. To illustrate, I used a Xen virtual disk, which shows the device as “/dev/xvd”.
To verify that the file system is available, use the df command to list the mounted partitions and view their size.
$ df -h -x tmpfs -x devtmpfs -x squashfs
In this tutorial, we learned how to partition, format and mount a raw hard drive connected to a Linux system.
If you have any questions or feedback, please feel free to comment.
Related Read: How to Create Swap File (Partition) on Linux