Skip to content

Latest commit

 

History

History
147 lines (104 loc) · 4.27 KB

Grow a KVM virtual machine.adoc

File metadata and controls

147 lines (104 loc) · 4.27 KB

Assumption:

  • The virtual machine name is rhel7

  • The virtual machine is using qcow2 disk

  • The virtual machine disk path is /var/lib/libvirt/images/rhel7.qcow2

Resize the Virtual machine disk

  1. Stop the virtual machine

    virsh shutdown rhel7
  2. Create a new qcow2 image

    qemu-img create -f qcow2 -o preallocation=metadata /root/rhel7-new.qcow2 10G
  3. Resize the image

    virt-resize /var/lib/libvirt/images/rhel7.qcow2 /var/lib/libvirt/images/rhel7-new.qcow2
  4. Edit the virtual machine meta data pointing to the new image

    virsh edit rhel7
  5. Start the virtual Machine

    virsh start rhel7
  6. Find the IP address of the virtual machine

    # virsh domifaddr rhel7
     Name       MAC address          Protocol     Address
    -------------------------------------------------------------------------------
    vnet0      52:54:00:19:5b:9e    ipv4         192.168.122.23/24

Resize the LVM in the machine

  1. SSH into the virtual machine

    ssh root@<ip>
  2. Find out what the disk name is using fdisk

    fdisk -l
    
    Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x0002ff53
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048     1026047      512000   83  Linux
    /dev/vda2         1026048     4194303     1584128   8e  Linux LVM
    /dev/vda3         4194304    20971455     8388576   83  Linux
    
    Disk /dev/mapper/rhel-root: 9948 MB, 9948889088 bytes, 19431424 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    
    
    Disk /dev/mapper/rhel-swap: 213 MB, 213909504 bytes, 417792 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
  3. Change system type for the new partition

    From the previous command we found out that the current disk is /dev/vda and that the new partition create by virt-resize is /dev/vda3. However the type of the new disk is Linux. To change the type we issue the following commands:

    # fdisk /dev/vda
    Welcome to fdisk (util-linux 2.23.2).
    
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    
    Command (m for help): t
    Partition number (1-3, default 3): 3
    Hex code (type L to list all codes): 8e
    Changed type of partition 'Linux LVM' to 'Linux LVM'
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    
    WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
    The kernel still uses the old table. The new table will be used at
    the next reboot or after you run partprobe(8) or kpartx(8)
    Syncing disks.
  4. Sync the disk using partprobe

    # partprobe
  5. Create Physical Volume

    # pvcreate /dev/vda3
      Physical volume "/dev/vda3" successfully created
    # pvdisplay
      --- Physical volume ---
      PV Name               /dev/vda2
      VG Name               rhel
      PV Size               1.51 GiB / not usable 3.00 MiB
      Allocatable           yes
      PE Size               4.00 MiB
      Total PE              386
      Free PE               11
      Allocated PE          375
      PV UUID               d9PV1O-XsrD-wSvM-OAhV-p9oY-ciXY-KBJs3t
    "/dev/vda3" is a new physical volume of "8.00 GiB"
    --- NEW Physical volume ---
    PV Name               /dev/vda3
    VG Name
    PV Size               8.00 GiB
    Allocatable           NO
    PE Size               0
    Total PE              0
    Free PE               0
    Allocated PE          0
    PV UUID               HXnFD0-ky0p-GByZ-Fm8k-UGQ6-hrMM-NPqyrh
  6. Add the new physical volume to the volume group

    # vgextend rhel /dev/vda3
  7. Extend the logica volume matching the size of the new physical volume

    # lvextend -L+8G /dev/rhel/root
  8. Resize the file system using either resize2fs for ext4 etc and xfs_growfs for xfs file system (default in RHEL 7)

    # xfs_growfs /dev/rhel/root

DONE