Thursday, December 29, 2016

resize qemu kvm image size


On HOST MACHINE:
==========================
// adding 2 G space to qemu image
$ qemu-img resize /var/lib/libvirt/images/centos7.0_ceph3.qcow2  +2G
Image resized.


NOW INSIDE VM:
=========================
// Now delete the root / partition and add new partition with updated size.
[root@marvelcentos ~]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).

Command (m for help): p

Disk /dev/vda: 5368 MB, 5368709120 bytes, 10485760 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: 0x0009f32f

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200     6291455     2096128   8e  Linux LVM

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

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): 2
First sector (2099200-10485759, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-10485759, default 10485759):
Using default value 10485759
Partition 2 of type Linux and of size 4 GiB is set

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): 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@marvelcentos ~]#
---------------------------
REBOOT VM
# reboot
=========================++
Now in VM MACHINE :
++++++++++++++++++
// extend the physical volume
[root@marvelcentos ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               cl_supercentos
  PV Size               2.00 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              511
  Free PE               0
  Allocated PE          511
  PV UUID               ngQJZP-cOEp-S0DE-7JJX-1MN9-btqJ-zZ8pIH
[root@marvelcentos ~]#
[root@marvelcentos ~]# pvresize /dev/vda2
  Physical volume "/dev/vda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized
[root@marvelcentos ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               cl_supercentos
  PV Size               4.00 GiB / not usable 2.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              1023
  Free PE               512
  Allocated PE          511
  PV UUID               ngQJZP-cOEp-S0DE-7JJX-1MN9-btqJ-zZ8pIH
[root@marvelcentos ~]#
[root@marvelcentos ~]#
-------------------------------------
// Extend the logical volume with *updated* physical volume
[root@marvelcentos ~]# lvextend  /dev/mapper/cl_supercentos-root  /dev/vda2 
  Size of logical volume cl_supercentos/root changed from 1.70 GiB (434 extents) to 3.70 GiB (946 extents).
  Logical volume cl_supercentos/root successfully resized.
[root@marvelcentos ~]#
[root@marvelcentos ~]# df -h
Filesystem                       Size  Used Avail Use% Mounted on
/dev/mapper/cl_supercentos-root  1.7G  1.4G  318M  82% /
devtmpfs                         910M     0  910M   0% /dev
tmpfs                            920M     0  920M   0% /dev/shm
tmpfs                            920M   17M  904M   2% /run
tmpfs                            920M     0  920M   0% /sys/fs/cgroup
/dev/vda1                       1014M  184M  831M  19% /boot
tmpfs                            184M     0  184M   0% /run/user/0
[root@marvelcentos ~]#
[root@marvelcentos ~]#

// Extend the underlying partition too
[root@marvelcentos ~]# xfs_growfs /dev/mapper/cl_supercentos-root  
meta-data=/dev/mapper/cl_supercentos-root isize=512    agcount=4, agsize=111104 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=444416, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 444416 to 968704

// See the results :)
[root@marvelcentos ~]# df -h
Filesystem                       Size  Used Avail Use% Mounted on
/dev/mapper/cl_supercentos-root  3.7G  1.4G  2.4G  38% /
devtmpfs                         910M     0  910M   0% /dev
tmpfs                            920M     0  920M   0% /dev/shm
tmpfs                            920M   17M  904M   2% /run
tmpfs                            920M     0  920M   0% /sys/fs/cgroup
/dev/vda1                       1014M  184M  831M  19% /boot
tmpfs                            184M     0  184M   0% /run/user/0
[root@marvelcentos ~]#

====================================

Note: On Centos, file system is mounted by xfs file system , you cannot use resize2fs,
rather you should use xfs_growfs.

reboot after partition update is quite important ( you may call partprobe too instead of reboot).


No comments:

Post a Comment