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?
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:
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:
If you need to work with a real disk, just pass the path to the file of its device to the utility:
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:
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:
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:
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:
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:
Let's create some more partitions, for example, for the root of the system and for the home folder:
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:
You can specify the desired size as a percentage:
And now the creation of the parted section:
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.