Partition expansion without data loss
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
Original problem
After expanding the disk of the virtual machine with the linux family, expand the data section inside it.
The file system occupies the entire disk, without a partition table
In the simplest case (one file system on the entire disk, without partitions), it is enough to call xfs_growfs or resize2fs and the thing is done.
File System on LVM Volume
If the file system runs on top of LVM, the extension becomes a little complicated, but it’s still safe: add a new partition, create a physical LVM volume there, add it to the LVM group, expand the logical volume, and then expand the file system. A side effect here will be a heap of partitions and physical volumes, if the place is added gradually several times. in portions. And if you use the msdos partition table, then you need to remember about using extended partitions, otherwise their number is limited to 4 pieces and then you need to shaman and rewrite the partition table.
File system in a regular partition
The most dangerous option for manual execution: you need to manually delete and recreate the partition so that it starts in the same place as the previous one, and ends on the last free sector of the disk.
This requires increased care and accuracy - with an error in the numbers, you can easily lose all the data. In addition, as a side effect, the GUIDs of GPT partitions are changing, to which there may be some kind of binding in the system settings.
Solution method
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 whole 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 the first table is damaged).
The implementation language is go, because the output is very simple to get a static binary, 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 were 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 properly) and github.com/rekby/gpt - for working with GPT tables.
An extension of partitions 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 losing data, flags, identifiers, etc. (if the kernel supports, then without rebooting - as for example in ubuntu 14.04).
2. Expand LVM physical volumes (LVM-PV) in place without data loss, 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 prepared 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. Extend file systems ext3, ext4, xfs.
5. The machine determines whether a reboot is needed after overwriting the partition table.
Partition tables are directly overwritten by data in the service areas of the disk. This is safer than working through tools like parted, because Partition table integrity violation time is shorter, and in some cases is completely absent.
Work with LVM and file system expansion is done through external calls to the corresponding commands (pvcreate, xfs_growfs, etc.), so these commands must be installed and present in PATH, i.e. be available for calling just by name. In the vast majority of cases, this condition is satisfied naturally.
As a result, now the file system extension and the underlying partitions look like this:
fsextender /home --door so:
fsextender /dev/sda2 --door so:
fsextender /dev/lvm-group/lvm-volume --do