Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Automating LVM Expansion for System Partitions Using Cloud-Init Configuration

Tech 2

Automating LVM Expansion for System Partitions Using Cloud-Init Configuration

Cloud-init is a widely used tool for initializing cloud instances, enabling automated configuration of system settings during boot. This article demonstrates how to configure cloud-init to automatically expand a system partition using LVM (Logical Volume Manager) on a virtual disk, specifically for scenarios where the root filesystem needs to grow to utilize available disk space.

Cloud-Init Configuration Overview

The primary configuraton file for cloud-init is /etc/cloud/cloud.cfg. Below is a sample configuration that includes settings for user management, SSH, and modules for system initialization. Key modules relevant to disk and filesystem management are highlighted.

# /etc/cloud/cloud.cfg
users:
  - default

disable_root: 0
ssh_pwauth: 1

mount_default_fields: [~, ~, 'auto', 'defaults,nofail,x-systemd.requires=cloud-init.service', '0', '2']
resize_rootfs_tmp: /dev
ssh_deletekeys: 0
ssh_genkeytypes: ~
syslog_fix_perms: ~
disable_vmware_customization: false

cloud_init_modules:
  - resolv-conf
  - disk_setup
  - migrator
  - bootcmd
  - write-files
  - growpart
  - resizefs
  - rsyslog
  - users-groups
  - ssh

cloud_config_modules:
  - mounts
  - locale
  - set-passwords
  - rh_subscription
  - yum-add-repo
  - package-update-upgrade-install
  - timezone
  - puppet
  - chef
  - salt-minion
  - mcollective
  - disable-ec2-metadata
  - runcmd

cloud_final_modules:
  - rightscale_userdata
  - scripts-per-once
  - scripts-per-boot
  - scripts-per-instance
  - scripts-user
  - ssh-authkey-fingerprints
  - keys-to-console
  - phone-home
  - final-message
  - power-state-change

system_info:
  default_user:
    name: centos
    lock_passwd: true
    gecos: Cloud User
    groups: [wheel, adm, systemd-journal]
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
    shell: /bin/bash
  distro: rhel
  paths:
    cloud_dir: /var/lib/cloud
    templates_dir: /etc/cloud/templates
  ssh_svcname: sshd

Modifying User Passwords

To set or change user passwords during initialization, use the chpasswd directive. This example sets passwords for the root and centos users.

#cloud-config
chpasswd:
  list: |
    root:123456
    centos:centos
  expire: False

Automating Partition Expansion with LVM

The core functionality for expanding the system partition involves using the growpart module to resize the partition and then using runcmd to manage LVM operations. This example targets /dev/vda2 as the partition to expand.

# Automatically expand the vda2 partition
growpart:
  mode: auto
  devices: [/dev/vda2]
  ignore_growroot_disabled: false

# Use runcmd to expand LVM
runcmd:
  - [pvresize, /dev/vda2]
  - [lvextend, -l, +100%FREE, /dev/mapper/centos-root]
  - [xfs_growfs, /dev/mapper/centos-root]

Explanation:

  • growpart: Automatically resizes the specified partition (/dev/vda2) to fill available disk space.
  • pvresize: Resizes the physical volume to include the newly expanded partition space.
  • lvextend: Extends the logical volume (centos-root) to use all available free space.
  • xfs_growfs: Expands the XFS filesystem to match the new logical volume size.

Alternative Approach Using bootcmd

For more granular control or one-time execution, you can use the bootcmd module with cloud-init-per to ensure commands run only once. This method is commented out in the example but provided for reference.

#bootcmd:
#  - [cloud-init-per, once, mygrowpart, growpart, /dev/vda, 2]
#  - [cloud-init-per, once, mypartprobe, partprobe, -s, /dev/vda2]
#  - [cloud-init-per, once, mypvresize, pvresize, /dev/vda2]
#  - [cloud-init-per, once, mylvextend, lvextend, -l, +100%FREE, /dev/mapper/centos-root]
#  - [cloud-init-per, once, myxfs_growfs, xfs_growfs, /dev/mapper/centos-root]

Final Message and Additional Configuration

After initialization, a final messsage can be displayed. Optionally, you can configure DNS settings or reboot the system, though these are commented out in the example.

final_message: "The system is finally up, after $UPTIME seconds. Default user and password: root 123456"

# Optional DNS configuration (commented out)
#manage_resolv_conf: true
#resolv_conf:
#  nameservers: ['114.114.144.114', '8.8.8.8']
#  searchdomains:
#    - localdomain
#  domain: localdomain
#  options:
#    rotate: true
#    timeout: 1

# Optional reboot configuration (commented out)
#power_state:
#  delay: now
#  mode: reboot
#  message: reboot now
#  timeout: 10
#  condition: true

Summary

This configuration enables automatic expansion of a system partition using LVM during cloud instance initialization. By leveraging cloud-init modules like growpart and runcmd, administrators can ensure that the root filesystem dynamically adjusts to available disk space, reducing manual intervention and improving system scalability. Adjust device names (e.g., /dev/vda2, /dev/mapper/centos-root) based on your specific environment and partition layout.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.