📜 ⬆️ ⬇️

Expansion of sections without data loss

The essence


Developed a program for simple expansion of the partition and file system (xfs, ext3,4) without data loss. github.com/rekby/fsextender/releases/latest

Initial problem


After expanding the disk of the linux virtual machine from the linux family, expand the data partition inside it.

The file system occupies the entire disk, without a partition table.


In the simplest case (one file system for the whole disk, without partitions), it is enough to call xfs_growfs or resize2fs and it's done.

LVM file system


If the file system runs on top of LVM — the extension is a bit more complicated, but still safe: add a new partition, create an LVM physical volume, add it to the LVM group, expand the logical volume and then expand the file system. A side effect here will be a jumble of sections and physical volumes, if space is added gradually several times. in portions. And if you use msdos-partition table, then you still need to remember about the use of extended partitions, otherwise their number is limited to 4 pieces and then you have to shamanize and rewriting the partition table.
')

File system in a regular partition


The most dangerous option for manual execution: you need to manually delete and re-create the partition so that it starts in the same place as the previous one, and ends on the last free sector of the disk.
It requires increased attentiveness and accuracy - with an error in the numbers, you can easily lose all the data. In addition, as a side effect, the GUID of the GPT partitions is changed, to which there may be some bindings in the system settings.

Solution


In all cases, when possible, I decided to rewrite the partition table. In automatic mode, this is not as dangerous as manually (in the msdos table, the entire table fits in one sector, and the sector record is atomic, and in the GPT tables there is a checksum and a backup copy in case of damage to the first table).

The implementation language is go, because it is very easy to get a static binary at the output, without external dependencies, this is important because the same binary should run on different versions of the OS from centos 5.4 x86 to the latest ubuntu 14.04 x64 and further - as the templates are updated.

Total


Two libraries have been written for working directly with partition tables: github.com/rekby/mbr — for working with the msdos partition table (the external interface is not very good, but it works fine) and github.com/rekby/gpt — for working with GPT tables.

The extender of sections and file systems is written. He can:
1. Expand the main (primary, non-logical) sections of the msdos table and GPT sections in place, without loss of data, flags, identifiers, etc. (if the kernel supports, then without rebooting, as in ubuntu 14.04, for example).
2. Expand the LVM physical volumes (LVM-PV) in place without losing data, if it is possible to expand the partition under LVM-PV.
2. Create new partitions for the LVM extension (in order to avoid misunderstandings, partitions are created only on disks where there is already a ready partition table).
3. Filter disks to create new partitions (by default, new partitions are created only on those disks where this LVM group is already located; filters can be disabled).
4. Expand file systems ext3, ext4, xfs.
5. Automatically determines whether a reboot is necessary after overwriting the partition table.

Work with partition tables is directly rewriting data in the service areas of the disk. It turns out to be safer than working through tools like parted, because the time of violation of the integrity of the partition table is less, and in some cases is absent altogether.
Working with LVM and file system extensions is done through external calls to the appropriate commands (pvcreate, xfs_growfs, etc.), so these commands must be installed and present in the PATH, i.e. be available to call just by name. In the overwhelming majority of cases, this condition is satisfied in a natural way.

As a result, now the file system extension and the underlying partitions look like this:
fsextender /home --do 


or so:
 fsextender /dev/sda2 --do 


or so:
 fsextender /dev/lvm-group/lvm-volume --do 

Source: https://habr.com/ru/post/274451/


All Articles