[CENTOS] [LVM] Extend virtual disk without adding partition

1.TL;DR

When RHEL / CentOS is installed in the hypervisor environment of VMware or KVM, it defaults to LVM (logical volume management). If you try to increase the number of virtual disks, you will usually add partitions after increasing the size of the virtual disks in the hypervisor environment. You can only create up to 4 primary partitions (you can add more by using extended partitions, but I will omit them here). I don't like adding partitions every time I increase the disk size, so I tried another method. LVMこっちじゃない.png

** Increased the size of the partition and finally increased the size of the logical volume recognized by the OS. ** **

2. Work flow

LVMの説明.png

** This is the worst thing you can do to destroy a virtual machine. Please do it at your own risk. ** **

Prerequisite environment

3. State before change

Check the state before disk expansion. This is the information that can be seen from the hypervisor side first. The size of the virtual disk is recognized as 24GB. The actual size reserved is 12GB.

[root@hypervisor images]# qemu-img info vm1.img 
image: vm1.img
file format: qcow2
virtual size: 24G (25769803776 bytes)
disk size: 12G
cluster_size: 65536
Format specific information:
    compat: 0.10

Next is the information that can be seen from inside the virtual machine. The second partition to be expanded is 24.7GB.

[root@vm1 ~]# parted /dev/vda print
model: Virtio Block Device (virtblk)
disk/dev/vda: 25.8GB
Sector size(logic/Physics): 512B/512B
Partition table: msdos
Disk flag: 

Number Start End Size Type File system flag
 1    1049kB  1075MB  1074MB  primary  xfs               boot
 2    1075MB  25.8GB  24.7GB  primary                    lvm

Check the file system. The root partition / dev / mapper / cl_centos7template-root is 22GB.

[root@vm1 ~]# df -h
File system size used Remaining used%Mount position
:
/dev/mapper/cl_centos7template-root    22G  7.4G   15G   35% /
/dev/vda1                            1014M  245M  770M   25% /boot
:

4. Expanding the size of the virtual disk

Log in to the hypervisor server as root. Shut down the virtual machine before adding more virtual disks.

Increase 10GB with the qemu-img resize command.

[root@hypervisor images]# qemu-img resize  vm1.img +10G
Image resized.

The size of the virtual disk has increased from 24GB to 34GB.

[root@hypervisor images]# qemu-img info vm1.img 
image: vm1.img
file format: qcow2
virtual size: 34G (36507222016 bytes)
disk size: 12G
cluster_size: 65536
Format specific information:
    compat: 0.10

5. Partition size expansion

The work from here is done by logging in to the virtual machine as root. Check the partition allocation status with the parted command. At this point, the second partition is still 24.7GB and has not increased.

[root@vm1 ~]# parted  /dev/vda print
model: Virtio Block Device (virtblk)
disk/dev/vda: 36.5GB
Sector size(logic/Physics): 512B/512B
Partition table: msdos
Disk flag: 

Number Start End Size Type File system flag
 1    1049kB  1075MB  1074MB  primary  xfs               boot
 2    1075MB  25.8GB  24.7GB  primary                    lvm

Partitions are increased with the resizepart subcommand of the parted command. Enter here in an interactive way. The first option 2 indicates the second partition. -1s means to increase to the last sector. There is no response after running the resizepart subcommand, but don't worry. The end is the q subcommand.

[root@vm1 ~]# parted /dev/vda
GNU Parted 3.1
/dev/Use vda
Welcome to GNU Parted! To see the command list'help'Please enter.

(parted) resizepart 2 -1s                                                 

(parted) q   

The second partition has increased from 24.7GB to 35.4GB.

[root@vm1 ~]# parted  /dev/vda print                                   
model: Virtio Block Device (virtblk)
disk/dev/vda: 36.5GB
Sector size(logic/Physics): 512B/512B
Partition table: msdos
Disk flag: 

Number Start End Size Type File system flag
 1    1049kB  1075MB  1074MB  primary  xfs               boot
 2    1075MB  36.5GB  35.4GB  primary                    lvm

6. Physical volume size expansion

The details of the physical volume have not increased yet.

[root@vm1 ~]# pvdisplay /dev/vda2
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               cl_centos7template
  PV Size               <23.00 GiB / not usable 2.00 MiB
:

Increase the size of the physical volume with the pvresize command.

[root@vm1 ~]# pvresize /dev/vda2
  Physical volume "/dev/vda2" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

The size of the physical volume has been increased from 23GB to 33GB.

[root@vm1 ~]# pvdisplay /dev/vda2
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               cl_centos7template
  PV Size               <33.00 GiB / not usable 2.00 MiB
:

The size of the volume group increases with the increase of the physical volume (33GB). You can see that there is 10GB of unallocated (FREE) space.

[root@vm1 ~]# vgdisplay cl_centos7template
  --- Volume group ---
  VG Name               cl_centos7template
:
  VG Size               <33.00 GiB
  PE Size               4.00 MiB
  Total PE              8447
  Alloc PE / Size       5887 / <23.00 GiB
  Free  PE / Size       2560 / 10.00 GiB
  VG UUID               XZleoU-FvcU-pNs9-rIc7-4PrH-pZZ6-KfoyZm

7. Expanding the size of the logical volume

Finally, increase the logical volume with the lvextend command. -l + 100% FREE refers to all free space. -r is an option to resize the file system as well (synonymous with the resize2fs command).

[root@vm1 ~]# lvextend  -l +100%FREE  -r  /dev/cl_centos7template/root
  Size of logical volume cl_centos7template/root changed from <21.50 GiB (5503 extents) to <31.50 GiB (8063 extents).
  Logical volume cl_centos7template/root successfully resized.
meta-data=/dev/mapper/cl_centos7template-root isize=512    agcount=7, agsize=818944 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=5635072, 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 5635072 to 8256512

The logical volume has increased to 31.5GB.

[root@vm1 ~]# lvdisplay /dev/cl_centos7template/root
  --- Logical volume ---
  LV Path                /dev/cl_centos7template/root
  LV Name                root
  VG Name                cl_centos7template
:
  LV Status              available
  # open                 1
  LV Size                <31.50 GiB
:

You can see that the file system has also increased to 32G.

[root@vm1 ~]# df -h
File system size used Remaining used%Mount position
:                             1000M     0 1000M    0% /sys/fs/cgroup
/dev/mapper/cl_centos7template-root    32G  7.4G   25G   24% /
/dev/vda1                            1014M  245M  770M   25% /boot
:

8. Finally

I introduced this troublesome procedure, but in the first place, the virtual machine created in KVM or VMware does not need to be LVM. If it is a physical machine, by using LVM, you can increase the logical volume without partitioning when adding an HDD. Is it an LVM snapshot? I've never used it because I'm scared.

Disk expansion should be a little easier for non-LVM (simple partitions).

Recommended Posts

[LVM] Extend virtual disk without adding partition
Extend virtual box + Linux (Ubuntu 19.04) disk