Ubuntu Server Disk Expansion Guide: LVM and XFS Mastery

author
By PS

10 September 2024

Ubuntu Server Disk Expansion Guide: Optimizing Storage with LVM and XFS

Master Ubuntu Server storage management with our comprehensive guide on using LVM and XFS for efficient disk expansion and organization.

Objectives

  • Expand an existing logical volume (e.g., /home)
  • Create a new logical volume (e.g., for /var/www/html)
  • Utilize XFS filesystem for optimal performance
  • Reserve space for future use

Current System Configuration

  • Ubuntu Server (applicable to various versions)
  • Existing logical volumes (sizes may vary)
  • Additional disk space available (size may vary)

Data Protection Considerations

  1. Always create a full backup of your important data before modifying partitions or filesystems.
  2. Local Backups:
    • For /home directory:
    • bash
      sudo cp -rp /home /home_$(date +%d%b%Y)
    • For /etc/fstab:
    • bash
      sudo cp -p /etc/fstab /etc/fstab_$(date +%d%b%Y)
  3. Implement regular backups using tools like rsync or xfsdump, which work well with XFS volumes.
  4. For web directories like /var/www/html, consider using Git or another version control system to track changes to your files.

Additional Notes

  • The steps in this guide can be adapted for different volume sizes and purposes. Adjust the sizes and names as needed for your specific requirements.
  • If you're expanding an existing directory (like /var/www/html) that already contains data, remember to transfer it to the new logical volume after mounting.
  • Adjust file permissions and ownership on newly created or expanded volumes to match your specific needs and security requirements.
  • XFS is an excellent choice for most volumes due to its superior performance and scalability. However, you can use other filesystems if your use case requires it.
  • Always monitor your disk usage regularly and plan for future expansions to ensure smooth operation of your server.

Step-by-Step Guide

  1. Assess Current Storage Situation

    Before making changes, check your current storage layout:

    bash
    df -h
    sudo lvdisplay
    sudo vgdisplay

    Note the sizes of your existing logical volumes and the available space in your volume group.

  2. Prepare the New Disk (if applicable)

    Why: Preparing a new disk is crucial when you're adding physical storage to your system. This step initializes the disk for use with LVM (Logical Volume Management), allowing you to incorporate it into your existing storage pool.

    How: Use the following command to initialize a disk or partition for use as a physical volume in LVM:

    bash
    sudo pvcreate /dev/sdX  # Replace X with the appropriate letter

    To determine the correct letter:

    1. Before connecting the new disk, run:
    2. bash
      lsblk
      or
      bash
      sudo fdisk -l
    3. Connect the new disk and run the command again. The new entry is your new disk.
    4. The naming convention typically follows this pattern: /dev/sda (first disk), /dev/sdb (second disk), /dev/sdc (third disk), and so on.

    Important notes:

    • Always double-check the device name to avoid initializing the wrong disk.
    • If the new disk has been used before, you might need to remove existing partitions first using:
    • bash
      fdisk
      or
      bash
      parted
    • For larger disks (>2TB), you might need to use a GPT partition table instead of MBR. In this case, create a GPT partition table first using:
    • bash
      gdisk
      before running
      bash
      pvcreate
  3. Extend the Volume Group (if needed)

    If you've added a new disk, add it to the existing volume group:

    bash
    sudo vgextend your_volume_group /dev/sdX
  4. Expand an Existing Logical Volume

    To increase the size of an existing logical volume (e.g., /home):

    bash
    sudo lvextend -L +SizeG /dev/mapper/your_volume_group-lv_name
    # Example: sudo lvextend -L +15G /dev/mapper/vg_os-lv_home

    Replace SizeG with the amount of space you want to add, and adjust the volume group and logical volume names as necessary.

  5. Grow the XFS Filesystem

    Expand the XFS filesystem to use the newly allocated space:

    bash
    sudo xfs_growfs /mount_point
    # Example: sudo xfs_growfs /home
  6. Create a New Logical Volume

    To create a new logical volume (e.g., for /var/www/html):

    bash
    sudo lvcreate -L SizeG -n lv_name your_volume_group
    # Example: sudo lvcreate -L 70G -n lv_www vg_os

    Adjust the size and names according to your needs.

  7. Format the New Logical Volume with XFS

    Create an XFS filesystem on the new logical volume:

    bash
    sudo mkfs.xfs /dev/mapper/your_volume_group-lv_name
  8. Mount the New Logical Volume

    Create the mount point and mount the new volume:

    bash
    sudo mkdir -p /mount_point
    sudo mount /dev/mapper/your_volume_group-lv_name /mount_point
  9. Update /etc/fstab

    Add the new mount to /etc/fstab to ensure it's mounted on boot:

    bash
    sudo xfs_growfs /mount_point
    # Example: sudo xfs_growfs /home
    bash
    echo '/dev/mapper/your_volume_group-lv_name /mount_point xfs defaults 0 2; | sudo tee -a /etc/fstab'
  10. Verify Changes

    After making changes, verify your new storage layout:

    bash
    df -h
    sudo lvdisplay
    sudo vgdisplay

Conclusion

By following this guide and adapting it to your specific needs, you can efficiently manage your Ubuntu Server's storage using LVM and XFS. This flexible approach allows you to expand existing volumes, create new ones, and prepare for future growth. Remember to always back up your data before making significant changes to your storage configuration.

Share this post :