Extending Disk Capacity for KVM Virtual Machines

From time to time, your data grows and a virtual machine is getting filled up. Here is a short tutorial for extending the disk capacity for KVM machines on LVM2:

First step: Poweroff the machine. This is a good situation to also update the kernel. Now you can extend the LVM2 volume in the host:

host@host01 ~ # lvextend -L 100G /dev/vg0/mysql01
  WARNING: lvmetad is running but disabled. Restart lvmetad before enabling it!
  Size of logical volume vg0/mysql01 changed from 50.00 GiB (12800 extents) to 100.00 GiB (25600 extents).
  Logical volume mysql01 successfully resized
root@host01 ~ #

Now you can boot up any live linux and determine the correct partition and disk:

root@rescue ~ # blkid
/dev/loop0: UUID="40c4ea95-0ecc-4c51-9f3e-e49d8f62f160" TYPE="ext2"
/dev/sda1: UUID="c8357796-3185-4f8f-a987-79652339bb0d" TYPE="swap" PARTUUID="00096cad-01"
/dev/sda2: UUID="c0b805e3-30d0-4af2-9724-58c651e41641" TYPE="ext4" PARTUUID="00096cad-02"
/dev/sda3: UUID="bb435f41-f0e4-438b-a4b0-b17874fd0504" TYPE="ext4" PARTUUID="00096cad-03"
root@rescue ~ # parted /dev/sda print
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  2149MB  2147MB  primary  linux-swap(v1)
 2      2150MB  2686MB  537MB   primary  ext4
 3      2688MB  53.7GB  51.0GB  primary  ext4

root@rescue ~ #

My VM has only one disk, sda1 is swap, sda2 is for /boot and sda3 is the rest, the Disk capacity amounts to 107GB, sda3 only has 51.0GB, this is the partition that I want to grow:

parted /dev/sda resizepart 3 100%

Than we need to do a fsck, otherwise we can not grow the filesystem:

root@rescue ~ # fsck.ext4 -fvC0 /dev/sda3
e2fsck 1.42.12 (29-Aug-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity                                        
Pass 4: Checking reference counts
Pass 5: Checking group summary information                                     
                                                                               
       67570 inodes used (2.18%, out of 3100800)
        4449 non-contiguous files (6.6%)
         136 non-contiguous directories (0.2%)
             # of inodes with ind/dind/tind blocks: 2603/46/0
             Extent depth histogram: 16320/165
     6130225 blocks used (49.23%, out of 12451072)
           0 bad blocks
           2 large files

       56433 regular files
        5856 directories
           2 character device files
           0 block device files
           2 fifos
          11 links
        5241 symbolic links (5030 fast symbolic links)
          27 sockets
------------
       67572 files
root@rescue ~ # 

# the grow:

root@rescue ~ # resize2fs -p /dev/sda3 
resize2fs 1.42.12 (29-Aug-2014)
Resizing the filesystem on /dev/sda3 to 25558272 (4k) blocks.
The filesystem on /dev/sda3 is now 25558272 (4k) blocks long.

root@rescue ~ #

Now you can reboot and your system should boot up and report the new size for sda3:

root@mysql01 ~ # dfc
FILESYSTEM  (=) USED      FREE (-) %USED AVAILABLE     TOTAL MOUNTED ON
rootfs      [======--------------]   29%     67.9G     95.8G /
udev        [--------------------]    0%     10.0M     10.0M /dev
tmpfs       [=-------------------]    1%      1.6G      1.6G /run
/dev/sda3   [======--------------]   29%     67.9G     95.8G /
tmpfs       [=-------------------]    0%      3.9G      3.9G /dev/shm
tmpfs       [--------------------]    0%      5.0M      5.0M /run/lock
tmpfs       [--------------------]    0%      3.9G      3.9G /sys/fs/cgroup
/dev/sda2   [===-----------------]   14%    419.3M    487.9M /boot
tmpfs       [--------------------]    0%    624.1M    624.1M /run/user/0
root@mysql01 ~ #
This entry was posted in 30in30, General, Linux, Short Tips. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.