Methods of payment Abuse

Partitioning a disk in Linux

11.10.2023, 21:17

Everything you try to learn takes practice. Working with hard disks in Linux is no exception. Practicing on a real disk at the risk of damaging the file system is not worth it, but it is also too impractical to create a virtual machine for such purposes. So what is the solution?

Instructions on how to partition a disk

In Linux, everything is considered a file, and the hard disk is represented as a file. This brings up an interesting possibility. We can use a regular file instead of a hard disk for our experiments.

Let's create a test site with a team:

sudo dd if=/dev/zero of=/disk.img count=2000 bs=1M

We have created a 2000 megabyte file filled with zeros that we can safely work with. Let's run parted, passing it our file instead of the device:

$ sudo parted /disk.img

If you want to work with a real disk, just pass the path to its device file to the utility:

$ sudo parted /dev/sda

The utility will start in interactive mode and you will be able to execute the necessary commands. Now let's try to see the list of partitions on the device:

(parted) print

It's empty because there isn't even a partition table. As long as there is no partition table, Linux disk partitioning cannot be performed, so we have to create one. To do this, we will use the mktable command:

(parted) mktable gpt

We have a partition table of type gpt, but you can choose one of these: aix, amiga, bsd, dvh, gpt_sync_mbr, gpt, mac, msdos, pc98, sun, loop

Now let's create a new ext2 file system partition of 100 megabytes using the mkpart command. It needs three parameters: partition type, file system and coordinates. The partition type can be:

  • primary
  • logical
  • extended (primary, logical and extended).

In gpt, you can create as many primary partitions as you want and not have to think about their type. This was all created for MBR, in this table there is a limitation on the number of primary partitions - only four.

Creating a primary partition:

(parted) mkpart primary ext2 0 400М

Since this is the first partition, we start with zero and end with the size we want - 400 megabytes.

Let's look at the list of partitions again:

(parted) print

Let's create some more partitions, for example for the system root and for the home folder:

(parted) mkpart primary ext4 400 1000M
(parted) mkpart primary ext4 1000M -0M

The parameter -0 means counting from the end of the partition, so all available space for the third partition will be taken. Let's see what we got:

Working with disks in Linux is done by their numbers. Let's shrink the last partition and create another one after it:

(parted) resizepart 3 1600M

You can specify the desired size as a percentage:

(parted) resizepart 3 60%

And now creating a parted partition:

mkpart primary ext4 1600M -0M

Partitioning the disk into Linux partitions is complete. We will omit working with file systems in this article, as there are other commands for this purpose, which we will cover in a future article.