To use storage devices such as USB drives and hard drives in Linux, you need to also understand how to structure them when using Linux operating system. Storage devices are often split into separate portions called partitions. This enables you to create a file system by splitting your hard drive into multiple virtual parts.
A Linux disk partition is like a boundary device that tells each file system how much space it can use. It’s handy when creating shared drives and enables you to allocate and edit drive space more effectively.
For example, if you have a 2GB USB drive, you can create a partition that takes up the entire drive, two partitions of 1GB each, or variations of sizes. Each Linux disk partition acts as its own hard drive. It is especially useful if you are using more than one operating system on the same computer.
Use The Parted Command
Ubuntu comes preinstalled with parted. If you are using a different distribution, install it using the following command:
To see the hard drives on your system, type: sudo parted -l. See the list of devices in the screenshot below:
You can see above that there are three Ubuntu partition disks on Disk /dev/sda. Let’s use the partition called /dev/sda5 to create a new partition.
The next step is to launch parted. But be sure you are using root privileges. Choose the drive you want to partition. We will be using /dev/vdc.
Type the following command:
(parted) select /dev/vdc
To see what is in the Linux disk partition, type: print. You will see a summary of your hard drive, the size, and the partition table.
In the example below, the hard drive is Model: Virtio Block Device, the size is 1396MB, and the partition table is gpt.
To configure the Ubuntu partition disk, you must exit first by typing quit. The next step is to open the selected storage device using parted. In this article, we will use the /dev/vdc device.
If you don’t specify the specific device you want to use, your system will randomly select a device. Use the command below that includes the device name (vdc):
sudo parted /dev/vdc
To set the partition table, type GPT, then Yes to accept it. You should only do this on partitions that contain no data you want to keep.
Review your partition table to show information about the storage device with the following command:
To see instructions on how to make a new partition, type (parted) help mkpart.
For this article, we will create a new Linux disk partition using the command below:
(parted) mkpart primary 0 1396MB
The 0 means you want to start the partition at the beginning of the drive. We know from the screenshot above that the drive has 1396MB. The command above tells your system to start the partition at 0 and end it at 1396MB.
To be able to use the partition, it must be formatted. First, you need to exit parted by typing quit. Then, using the ext4 file system, type the command below to format the disk:
Verify by typing sudo parted /dev/vdc. To exit parted, type quit. When you exit parted, the changes save automatically.
In command mode, use a single letter command to show you a list of the actions you can take. Type m and press Enter.
Create Disk Partitions Using cfdisk
Cfdisk is a Linux utility program used to create, delete, and modify partitions on a disk device. To use it to create a partition, enter the following command:
# cfdisk /dev/sda
The name of the drive for this example is sda.
In the screenshot above, you can see summary information for the disk device. The middle of the window shows the partition table. The brackets on the bottom show selectable commands.
To select a partition from the list, use the up and down arrow keys. Select a command by using the right and left arrows.
The example above shows three primary partitions (1,2 & 3). Notice the free space partition type.
Create a new partition by selecting New from the bottom window. We will call this partition /dev/sdb. Type the command # cfdisk /dev/sdb. Next select primary as the partition type from the next screen.
On the next screen you will specify the size of the partition. We will create a partition that is 800 KB. Now you will be asked to determine where to start the partition. Choose the beginning of free space.
From the next screen, select Write to save your changes and write the partition data to disk. Verify the new partition by printing it using the following command:
fdisk -l /dev/sdb
Concluding Tips for Creating Linux Disk Partitions
You should always back up your data. Even the smallest mistake can destroy the partition of a critical drive.
Also, be sure to verify and re-verify that you are using the correct drive when creating your partition. Otherwise, you could lose data.
Let us know your questions in the comments below.