Creating/modifying partitions

We will look at creating partitions using the example of working with real partitions running Linux. In the case of a virtual system installed under a Virtualbox type virtual machine, all actions and commands will be identical.

General concepts

Illustration 1 — Creating/modifying partitions
  • Partitioning programs are programs for partitioning a hard drive.

  • A mount point is a folder in which the contents of a device, such as a hard drive, will be visible.

  • Device - any device, whether a hard drive, a flash drive, a CD-disk or something else, is seen in linux as a file. This is usually displayed in the style /dev/sda.

  • Partition table - The partition table stores information about the type of partition and its location on the hard drive.

  • File system - data about the partition file system.

Schematic diagram of actions

Illustration 2 — Creating/modifying partitions

Warning

Be careful during the following operations. They permanently delete information from the disk. If you make a mistake and select the wrong hard drive, you will lose the data accumulated there.

Note

If you are more comfortable working with a graphical interface, you can download Live-CD Gparted, boot from it, and partition the disk as you need.

  1. Select the device (hard drive) that we will mark with the command:

sudo parted -a optimal /dev/sdX

We get to the command line interface of the program.

  1. We create a partition table with a command like

mklabel gpt

Note

because We are already in the text interface of the parted command, launched with superuser rights, there is no need to enter sudo.

  1. Create a section with the command

mkpart primary 0% 100%

This means that we create one partition that will occupy the entire disk, from beginning to end, and is primary. If you need a partition to occupy part of the disk, you can change the start or end of the partition.

  1. Creating a file system

There are several types of file systems for OS linux, the default is ext4. If you don’t have a clear idea of what the strengths and weaknesses of each file system are, then using ext4 is your best bet. It is created by a team

sudo mkfs.ext4 /dev/sdX1
  1. We register the device in /etc/fstab so that it is mounted automatically at every boot.

The line will look something like this:

/dev/sdX1 /folder ext4 rw,auto 0 2
  1. We check the correctness of mounting with the command

sudo mount -a

This command mounts all devices registered in /etc/fstab, but not mounted.

Increase the size without unmounting the disk

Warning

Remember - any operations with partitions are potentially dangerous and jeopardize the integrity of your data on the partition being modified.

This operation is only possible on Ubuntu 18 and higher. You can check the version with the following command:

lsb_release -a
  1. If the version is Ubuntu 18 and higher, then continue. First you need to turn off the services:

ibatyr stop
systemctl stop postgresql
  1. Launch cfdisk and select Resize

sudo cfdisk
Illustration 3 — Creating/modifying partitions
  1. We agree with the new size

Illustration 4 — Creating/modifying partitions
  1. We write down the changes (Write) and exit cfdisk

Illustration 5 — Creating/modifying partitions
  1. Rereading the partition table and expanding the file system

partprobe
resize2fs /dev/sdX1
  1. We enable services

systemctl start postgresql
ibatyr start

Increasing disk size

Warning

Remember - any operations with partitions are potentially dangerous and jeopardize the integrity of your data on the partition being modified.

Illustration 6 — Creating/modifying partitions

Sometimes it is necessary to change the disk size. You can do this as follows:

  1. Turn off services:

ibatyr stop
systemctl stop postgresql
  1. Let’s unmount the partition being changed.

Attention

Doesn’t work for boot partition. We recommend using other methods “live-cd” or “gparted”. We recommend downloading from the link above and booting from the disk.

umount /dev/sdX1.
  1. Next, run parted and resize the partition.

sudo parted /dev/sdX1
resizepart №part 100%

This command will stretch the partition to cover the entire free remainder of the disk, provided there is one.

  1. The disk has been enlarged, but if we do not complete the next step, the new space will not be available to our system. Exit parted and execute the command:

sudo resize2fs /dev/sdX1
  1. After changing a partition, it is useful to check it:

sudo fsck.ext4 -f /dev/sdX1

Warning

Do not run fsck on a mounted partition! This may result in data loss.

  1. We enable services

systemctl start postgresql
ibatyr start

Also to resize your partition you can boot from livecd or gparted and get a GUI to work with your partitions.

Link to gparted: https://gparted.org/download.php