Resizing a Guest’s Disk (KVM) – Extending

Resizing the disk on a KVM system is rather straight-forward, but it sure does require that you get the steps right!

The Steps

  1. Remove snapshots
  2. Stop the VM
  3. Enlarge the disk itself
    1. In GUI, find your VM.  Choose Hardware, then click on the disk and then Resize Disk
    2. From CLI, run one of the following:
      1. qemu-img resize <disk> +<size>G
        1. Example: qemu-img resize /vm/images/158/vm-158-disk-1.qcow2 +32G to increase image size by 32GB
      2. qm resize <vmid> <disk> +<size>G
        1. Example: qm resize 158 /vm/images/158/vm-158-disk-1.qcow2 +32G to increase VM #158’s disk by 32GB
  4. Start the VM and resize the partition then LVM
    1. Run fdisk inside your guest and remove the partition that you want to extend
      [root@centos7 ~]# lsblk | grep disk
      sda                                 8:0    0   64G  0 disk
      [root@centos7 ~]# fdisk /dev/sda
      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): p
      
      Disk /dev/sda: 68.7 GB, 68719476736 bytes, 134217728 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: 0x000be40f
      
         Device Boot      Start         End      Blocks   Id  System
      /dev/sda1   *        2048     2099199     1048576   83  Linux
      /dev/sda2         2099200    67108863    32504832   8e  Linux LVM
      
      Command (m for help): d
      Partition number (1,2, default 2): 2
      Partition 2 is deleted
      
      Command (m for help): p
      
      Disk /dev/sda: 68.7 GB, 68719476736 bytes, 134217728 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: 0x000be40f
      
         Device Boot      Start         End      Blocks   Id  System
      /dev/sda1   *        2048     2099199     1048576   83  Linux
      
      Command (m for help): n
      Partition type:
         p   primary (1 primary, 0 extended, 3 free)
         e   extended
      Select (default p): p
      Partition number (2-4, default 2):
      First sector (2099200-134217727, default 2099200):
      Using default value 2099200
      Last sector, +sectors or +size{K,M,G} (2099200-134217727, default 134217727):
      Using default value 134217727
      Partition 2 of type Linux and of size 63 GiB is set
      
      Command (m for help): p
      
      Disk /dev/sda: 68.7 GB, 68719476736 bytes, 134217728 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: 0x000be40f
      
         Device Boot      Start         End      Blocks   Id  System
      /dev/sda1   *        2048     2099199     1048576   83  Linux
      /dev/sda2         2099200   134217727    66059264   83  Linux
      
      Command (m for help): t
      Partition number (1,2, default 2): 2
      Hex code (type L to list all codes): 8e
      Changed type of partition 'Linux' to 'Linux LVM'
      
      Command (m for help): p
      
      Disk /dev/sda: 68.7 GB, 68719476736 bytes, 134217728 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: 0x000be40f
      
         Device Boot      Start         End      Blocks   Id  System
      /dev/sda1   *        2048     2099199     1048576   83  Linux
      /dev/sda2         2099200   134217727    66059264   8e  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.
      [root@centos7 ~]#
    2. Reboot the VM
    3. Resize the LVM physical volume
      [root@centos7 ~]# pvdisplay
        --- Physical volume ---
        PV Name               /dev/sda2
        VG Name               cl_vm-07
        PV Size               <31.00 GiB / not usable 3.00 MiB
        Allocatable           yes
        PE Size               4.00 MiB
        Total PE              7935
        Free PE               1
        Allocated PE          7934
        PV UUID               8dCBQY-gbR5-xy4k-U0Ap-fDfv-4idB-J3rD5V
      
      [root@centos7 ~]# pvresize /dev/sda2
        Physical volume "/dev/sda2" changed
        1 physical volume(s) resized / 0 physical volume(s) not resized
      [root@centos7 ~]# pvdisplay
        --- Physical volume ---
        PV Name               /dev/sda2
        VG Name               cl_vm-07
        PV Size               <63.00 GiB / not usable 2.00 MiB
        Allocatable           yes
        PE Size               4.00 MiB
        Total PE              16127
        Free PE               8193
        Allocated PE          7934
        PV UUID               8dCBQY-gbR5-xy4k-U0Ap-fDfv-4idB-J3rD5V
      
      
    4. Resize the logical volume
      [root@pete-dev-centos7 ~]# lvdisplay
        --- Logical volume ---
        LV Path                /dev/cl_vm-07/swap
        LV Name                swap
        VG Name                cl_vm-07
        LV UUID                6E7b0t-0q8s-Bm6m-LqRI-jwfY-GQcr-eMmERY
        LV Write Access        read/write
        LV Creation host, time vm-07, 2017-05-23 08:22:40 -0400
        LV Status              available
        # open                 2
        LV Size                2.00 GiB
        Current LE             512
        Segments               1
        Allocation             inherit
        Read ahead sectors     auto
        - currently set to     8192
        Block device           253:1
      
        --- Logical volume ---
        LV Path                /dev/cl_vm-07/root
        LV Name                root
        VG Name                cl_vm-07
        LV UUID                aeVwKb-Th7A-al03-aKAq-HONA-mciK-Nsew02
        LV Write Access        read/write
        LV Creation host, time vm-07, 2017-05-23 08:22:40 -0400
        LV Status              available
        # open                 1
        LV Size                28.99 GiB
        Current LE             7422
        Segments               1
        Allocation             inherit
        Read ahead sectors     auto
        - currently set to     8192
        Block device           253:0
      
      [root@centos7 ~]# lvresize /dev/cl_vm-07/root -l +8193
        Size of logical volume cl_vm-07/root changed from 28.99 GiB (7422 extents) to <61.00 GiB (15615 extents).
        Logical volume cl_vm-07/root successfully resized.
        ### NOTE:
        ### You can also use a one-liner:
        ### lvextend /dev/cl_vm-07/root /dev/sda2
        ### Without any options, it will use the maximum size in that group
      
      [root@centos7 ~]# lvdisplay
        --- Logical volume ---
        LV Path                /dev/cl_vm-07/swap
        LV Name                swap
        VG Name                cl_vm-07
        LV UUID                6E7b0t-0q8s-Bm6m-LqRI-jwfY-GQcr-eMmERY
        LV Write Access        read/write
        LV Creation host, time vm-07, 2017-05-23 08:22:40 -0400
        LV Status              available
        # open                 2
        LV Size                2.00 GiB
        Current LE             512
        Segments               1
        Allocation             inherit
        Read ahead sectors     auto
        - currently set to     8192
        Block device           253:1
      
        --- Logical volume ---
        LV Path                /dev/cl_vm-07/root
        LV Name                root
        VG Name                cl_vm-07
        LV UUID                aeVwKb-Th7A-al03-aKAq-HONA-mciK-Nsew02
        LV Write Access        read/write
        LV Creation host, time vm-07, 2017-05-23 08:22:40 -0400
        LV Status              available
        # open                 1
        LV Size                <61.00 GiB
        Current LE             15615
        Segments               1
        Allocation             inherit
        Read ahead sectors     auto
        - currently set to     8192
        Block device           253:0
    5. Grow the filesystem
      ### NOTE: 
      ### In Centos 7 default filesystem is xfs.
      ### Also noteworthy: xfs file system supports extend not reduce
      ### If you want to resize the filesystem use xfs_growfs rather than resize2fs.
      [root@centos7 ~]# xfs_growfs /dev/vm-07/root
      meta-data=/dev/mapper/vm--07-root isize=512 agcount=4, agsize=1900032 blks
               =                sectsz=512     attr=2, projid32bit=1
               =                crc=1          finobt=0 spinodes=0
      data     =                bsize=4096     blocks=7600128, imaxpct=25
               =                sunit=0        swidth=0 blks
      naming   =version 2       bsize=4096     ascii-ci=0 ftype=1
      log      =internal        bsize=4096     blocks=3711, version=2
               =                sectsz=512     sunit=0 blks, lazy-count=1
      realtime =none            extsz=4096     blocks=0, rtextents=0
      data blocks changed from 7600128 to 15989760
      
      ### Note: For ext4 filesystem use
      [root@centos7 ~]#resize2fs /dev/vm-07/root
      
      [root@centos7 ~]# df -h
      Filesystem              Size Used Avail Use% Mounted on
      /dev/mapper/vm--07-root  61G  23G   39G  37% /
      devtmpfs                909M    0  909M   0% /dev
      tmpfs                   920M    0  920M   0% /dev/shm
      tmpfs                   920M 8.4M  912M   1% /run
      tmpfs                   920M    0  920M   0% /sys/fs/cgroup
      /dev/sda1              1014M 279M  736M  28% /boot
      tmpfs                   184M    0  184M   0% /run/user/1002
      tmpfs                   184M    0  184M   0% /run/user/1001

Other Useful References

Convert vmdk to iso

Convert vmdk to raw