Methods of payment Abuse

Partitioning a disk in Linux

11.10.2023, 21:17

Everything you are trying to learn requires practice. Working with hard drives in Linux is no exception. It is not worth practicing on a real disk, risking damaging the file system, but it is also too impractical to create a virtual machine for such purposes. What is the solution?

Instructions for splitting the disk

In Linux, everything is considered files, and the hard disk is also represented as a file. An interesting possibility emerges from this. We can use a regular file instead of a hard drive for our experiments.

Creating a test site with the command:

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

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

$ sudo parted /disk.img

If you need to work with a real disk, just pass the path to the file of its device 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 is empty because there is not even a partition table. While it is not there, disk partitioning in Linux cannot be performed, we have to create it. To do this, use the mktable command:

(parted) mktable gpt

We have a gpt partition table, 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 partition with an ext2 file system of 100 megabytes in size using the mkpart command. It needs to pass three parameters: partition type, file system and coordinates. The type of partition can be:

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

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

Creating a primary:

(parted) mkpart primary ext2 0 400M

Since this is the first partition, we start from scratch, and finish with the size we need - 400 megabytes.

Look at the list of sections again:

(parted) print

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

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

The -0 parameter means to count from the end of the partition, thus all available space for the third partition will be occupied. Let's see what happened:

Working with a disk in Linux is carried out by their numbers. Let's reduce the last section 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 the creation of the parted section:

mkpart primary ext4 1600M -0M

The partition of the disk into Linux partitions is complete. We will omit working with file systems in this article, since other commands are already used for this, which we will consider in one of the following articles.