To use storage devices such as USB sticks and hard drives in Linux, you also need to understand how to structure them when using the Linux operating system. Storage devices are often divided into discrete parts called partitions. This allows you to create a file system by dividing your hard drive into multiple virtual parts.
A Linux disk partition is like an edge device that tells each filesystem how much space it can use. This is useful when creating shared drives and allows you to more efficiently allocate and edit disk space.
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 different sizes. Each Linux disk partition acts like a separate hard drive. This is especially useful if you are using multiple operating systems on the same computer.
Use the Partition Command
Ubuntu comes with parted preinstalled. If you are using a different distribution, install it using the following command:
To see the hard drives on your system, enter: sudo parted -l. See the list of devices in the screenshot below:
You can see above that there are three Ubuntu partitions on Disk / dev / sda. Let’s use a partition named / dev / sda5 to create a new partition.
The next step is to run parted. But make sure you are using root privileges. Select the drive you want to partition. We will be using / dev / vdc.
Enter the following command:
(parted) select / dev / vdc
To see what is on the Linux disk partition, type: print. You will see a summary of your hard drive, size, and partition table.
The example below uses Model: Virtio Block Device hard drive, size is 1396 MB, partition table is gpt.
To set up your Ubuntu partition disk, you must first exit by typing quit. The next step is to open the selected storage device with parted. In this tutorial, we will be using the / dev / vdc device.
If you do not specify a specific device that you want to use, your system will randomly select the device. Use the command below which includes the device name (vdc):
sudo parted / dev / vdc
To install the partition table, enter GPT, then Yes to accept. You should only do this with sections that do not contain the data you want to keep.
View the partition table to display information about the storage device with the following command:
To see instructions for creating a new partition, type (parted) help mkpart.
In this tutorial, we will create a new Linux disk partition using the following command:
(parted) mkpart primary 0 1396 MB
0 means you want to create a partition at the beginning of the disk. From the screenshot above, we know that there is 1396 MB of disk space. The above command tells your system to start the partition at 0 and end it at 1396 MB.
To be able to use a section, it must be formatted. First, you need to exit parted by typing quit. Then, using the ext4 filesystem, enter the following command to format the disk:
mkfs.ext4 / dev / vdc
Confirm by typing sudo parted / dev / vdc. To exit parted, enter quit. When you exit parted, your changes are saved automatically.
In command mode, use the one letter command to show you a list of actions you can take. Type m and press Enter.
Create disk partitions using cfdisk
Cfdisk is a Linux utility used to create, delete, and modify partitions on a disk device. To use it to create a section, enter the following command:
# cfdisk / dev / sda
The drive name in this example is sda.
In the screenshot above, you can see the summary of the disk device. The partition table is shown in the middle of the window. The brackets at the bottom show the commands to be selected.
Use the up and down arrow keys to select a section from the list. Select a command using the right and left arrows.
The example above shows three main sections (1, 2 and 3). Pay attention to the type of partition with free space.
Create a new section by choosing New in the bottom window. We’ll call this partition / dev / sdb. Enter the command # cfdisk / dev / sdb. Then select primary as the partition type on the next screen.
On the next screen, you will specify the size of the partition. We will create an 800KB partition. You will now be prompted to determine where to start the section. Select the beginning of the free space.
On the next screen, select “Burn” to save your changes and write the partition data to disk. Test the new section by printing it out using the following command:
fdisk -l / dev / sdb
Final tips for creating Linux disk partitions
You should always back up your data. Even the smallest mistake can destroy a partition on a critical disk.
Also, be sure to check and double-check that you are using the correct drive when creating the partition. Otherwise, you may lose data.
Let us know your questions in the comments below.