Enlarge file system

Question:

The file-system (of a virtual machine) is too small and fills up. How can it be enlarged?

Answer:

To enlarge the file-system several steps must be performed. They depend on several conditions:

  • Depending on which virtualization technology and environment, the steps to grow the backing image differs.
  • Different releases of UCS use different partition table formats
  • When LVM is used, the PV and LV must be resized.
  • Finally the file-system must be resized, which can be ext2/ext3/ext4 or XFS.

Disclaimer

Modifying the partition-table, logical volume manager and file-system is dangerous and may lead to data loss. Make sure to have a up-to-date backup! Train the following steps in a test environment before going live on production systems!

Enlarge the partition table

The partition table must be re-sized. Since UCS-3 GPT is used by default, while MBR was used before.

Start the virtual machine in single user mode and perform the following actions as the user root. The device name differs between the virtualization environment: QEMU/KVM with VirtIO uses /dev/vda, Xen/Amazon EC2 uses /dev/xvda for PV disks, while others use /dev/sda for SCSI/SATA or /dev/hda for IDE/PATA disk devices.

Amazon EC2 / Xen

Para-virtualized instances by default don’t use a partition table, so this step can be skipped.

OpenStack

The Univention provided images use cloud-initramfs-growroot by default, which automatically re-sizes the file-system on next start.

GUID Partition Table - GPT

The following example assumes the disk to be named /dev/vda and uses the partition number 4.

  1. Install gdisk:
    univention-install gdisk

  2. Create a backup of the GPT:
    sgdisk /dev/vda --backup=/root/gpt.backup
    This can be restored by running
    sgdisk /dev/vda --load-backup=/root/gpt.backup
    if anything goes wrong.

  3. Move the backup partition table to the end of the disk:
    sgdisk /dev/vda --move-second-header

  4. Print the current partition table:
    sgdisk /dev/vda --print
    Find the partition at the end of the disk.

  5. Print the detailed partition information:
    sgdisk /dev/vda --info=4
    Partition GUID code: E6D6D379-F507-44C2-A23C-238F2A3DF928 (Linux LVM) Partition unique GUID: 2B8DD459-A490-41BA-A90B-AB6F16454E17 First sector: 5206016 (at 2.5 GiB) Last sector: 41680895 (at 19.9 GiB) Partition size: 36474880 sectors (17.4 GiB) Attribute flags: 0000000000000000 Partition name: 'LVMPV'

  6. Delete the old partition:
    sgdisk /dev/vda --delete=4

  7. Re-create the partition using the old partition number and start sector, but 0 for the end sector, as this represents the largest available block:
    sgdisk /dev/vda --new=4:5206016:0

  8. Change the name:
    sgdisk /dev/vda --change-name=4:'LVMPV'

  9. Change the type:
    sgdisk /dev/vda --typecode=4:8E00

  10. Change the UUID:
    sgdisk /dev/vda --partition-guid=4:2B8DD459-A490-41BA-A90B-AB6F16454E17

  11. Verify the changes by printing the new partition:
    sgdisk /dev/vda --info=4

  12. Verify the consistency of the new partition table:
    sgdisk /dev/vda--verify

Master Boot Record - MBR

The following example assumes the disk to be named /dev/sda and uses the partition number 3.

  1. Create a Backup of the MBR:
    sfdisk /dev/sda --dump >/root/mbr.backup
    This can be restored by running
    sfdisk /dev/sda </root/mbr.backup
    if anything goes wrong.
  2. Print the current partition table:
    sfdisk /dev/sda -u S --list
    to determine the partition number at the end of the disk (3 in this example).
  3. Re-size the last partition to extend to the end of the new disk:
    echo ',+,,' | sfdisk /dev/sda -u S -N 3

Enlarge the Logical Volume Manager - LVM

By default UCS uses LVM to manage its disk space for all file-systems expect the root file-system. If running the command
pvs
returns any physical volumes on the enlarged disk, the following steps need to be performed:

  1. Enlarge the physical volume, which uses the enlarged partition:
    pvresize /dev/sda3
  2. Enlarge the logical volume, which should use all newly allocated space:
    lvresize -l +100%FREE /dev/mapper/vg_ucs-rootfs

Enlarge the file-system

Finally the file-system can be enlarged:

ext2/ext3/ext4

resize2fs /dev/mapper/vg_ucs-rootfs

XFS

xfs_growfs /dev/mapper/vg_ucs-rootfs

Final check

Run
df
and
df -i

to verify everything has worked as expected. Re-boot the system to make sure all changes are persistent and work on the next re-boot.

1 Like
Mastodon