KVM မှာ virtual disk တွေကို ဘယ်လို resize လုပ်လို့ရသလဲ
အခုနောက်ပိုင်းမှာ စာရေးသူ QEMU/KVM အကြောင်းတွေကိုပြန်လည်လေ့လာဖြစ်နေပြန်ပါတယ်။ Vagrant နဲ့ Virtualbox ကိုတွဲပြီးအသုံးပြုရတာ အဆင်ပြေပေမယ့်လည်း performance ပိုင်းမှာ QEMU/KVM ကိုမမှီတဲ့ အပိုင်းတွေအများကြီးရှိနေပြန်ပါတယ်။ QEMU/KVM တခုတည်းအသုံးပြုမယ်လို့ ဆိုရင်လည်း Virtualbox မှာလောက် မလွယ်ကူပြန်ပါ။ Vagrant နဲ့ KVM ကိုတွဲပြီးအသုံးပြုပြန်ရင်လည်း Vagrant box အများစုက Virtualbox အတွက် ပြင်ဆင်ထားတာများတာကြောင့် အဆင်မပြေပြန်ပါ။ နောက်ဆုံးတော့ LXD ကိုမျက်စိကျပြီးနောက် LXC container တွေကိုထဲထဲဝင်ဝင် အသုံးပြုနေရင်းမှ LXD မှာလည်း VM တွေကို ဖန်တီးနိုင်တယ်ဆိုတာ ထပ်မံလေ့လာရပြန်ပါတယ်။ လက်ရှိမှာတော့ LXD ဟာစာရေးသူအတွက် LXC container နဲ့ပြီးမယ့် lab environment တွေအတွက်သုံးဖြစ်ပါတယ်။ Vagrant နဲ့ Virtualbox ကိုတော့ VM နဲ့မှ စမ်းသပ်လို့ရမယ့် lab environment တွေမှာသုံးဖြစ်နေပါသေးတယ်။ တပြိုင်တည်းမှာ QEMU/KVM ကို စာရေးသူရဲ့ local machine ပေါ်မှာပဲ virt-manager နဲ့ဖြစ်ဖြစ်၊ Vagrant နဲ့ပဲဖြစ်ဖြစ်၊ LXD နဲ့ VM တွေကိုဖန်တီးဖြစ်တဲ့အခါမှာပဲဖြစ်ဖြစ်၊ Proxmox ပေါ်မှာ VM တွေအလွယ်တကူဖန်းတီးဖြစ်ရင်လည်း KVM ကိုသုံးဖြစ်နေပြီးသားမို့၊ bare-bone KVM အကြောင်းသိထားတာ စာရေးသူအတွက်အရှုံးမရှိပါ။
Proxmox မှာဆိုရင် VM ရဲ့ virtual disk ကို WebUI ကနေပဲဖြစ်ဖြစ်၊ CLI ကနေပဲဖြစ်ဖြစ် အလွယ်တကူ resize လုပ်လို့ရနိုင်ပါတယ်။ LVM ကိုသုံးရင် ကိုယ်ကြိုက်တဲ့ disk ကို expand လုပ်ပြီး သက်ဆိုင်ရာ filesystem မှာ grow လုပ်လိုက်ရုံပါပဲ။ အဲ့ဒီ process ကို bare-bone KVM မှာအဆင့်ဆင့်ဘယ်လိုမျိုး လုပ်သလဲဆိုတာကို ဒီ post မှာရှင်းလင်းလိုပါတယ်။ Demo အတွက် စာရေးသူ VM ထဲမှာ Almalinux 8 ကိုအသုံးပြုထားပါတယ်။ ဘယ် Linux distribution မှာဖြစ်ဖြစ် လုပ်ရတဲ့လုပ်ငန်းစဉ်က အတူတူပါပဲ။
21 GB ရှိတဲ့ virtual disk ကို 25 GB ဖြစ်အောင် လုပ်ပြီး လိုအပ်တဲ့ partition မှာထည့်သုံးသွားမှာဖြစ်ပါတယ်။ ဘယ်လိုလုပ်သလဲဆိုတာ ကြည့်လိုက်ရအောင်။ သတိပြုရမှာက ကိုယ်က home lab မှာစမ်းသပ်ဖို့အတွက်ဆိုရင်တော့ စိတ်ကြိုက်လုပ်နိုင်ပေမယ့်၊ production environment မှာဆိုရင် data တွေကို backup အရင်လုပ်ထားဖို့မှာချင်ပါတယ်။ စာရေးသူ အပါအဝင် နည်းပညာသမားတွေဟာ အလွယ်ဆုံး၊ အမြန်ဆုံးလမ်းကို လိုက်နေကြမို့ သတိကပ်ဖို့ ကြိုတင်အသိပေးတာဖြစ်ပါတယ်။
ပထမဆုံးအနေနဲ့ VM ကို shutdown လုပ်ပြီး ကိုယ်လိုချင်တဲ့ size ကိုအရင်ဆုံးပြောင်းပေးရပါ့မယ်။ ဒီနေရာမှာ... စာရေးသူ 1 GB ကိုပထမဆုံးတခေါက်မှာ အရင်ပေါင်းထည့်ပြီးသားမို့ အခုတခါ 4 GB ပဲထပ်တိုးပေးလိုက်ရအောင်။ qemu-img reszie ဆိုတဲ့ command ကိုအသုံးပြုပါတယ်။ size ကိုတိုးပြီးတဲ့အခါမှာလည်း ကိုယ်လိုချင်တဲ့ size မှန်မမှန်ကိုလည်း qemu-img info ဆိုတဲ့ command နဲ့စစ်ကြည့်လို့ရပါတယ်။ အဲ့လို စစ်ကြည့်လိုက်ရင် virtual size ဆိုတဲ့ နေရာမှာ 25 GB ဆိုပြီးပြနေပါလိမ့်မယ်။ စိတ်တိုင်းကျပြီဆိုရင် VM ကိုပြန်စလို့ရပါပြီ။ Terminal ထဲမှာ ဘယ်လိုမျိုးလုပ်သလဲဆိုတာကြည့်လိုက်ရအောင်။
## shutdown the VM first before resize the virtual disk
tyla@e32:~$ virsh shutdown almalinux8
Domain 'almalinux8' is being shutdown
## resize the .qcow2 file on host machine
tyla@e32:~$ sudo qemu-img resize /data/kvm/almalinux8.qcow2 +4G
Image resized.
## verify if the virtual size is now 25 GB
tyla@e32:~$ sudo qemu-img info /data/kvm/almalinux8.qcow2
image: /data/kvm/almalinux8.qcow2
file format: qcow2
virtual size: 25 GiB (26843545600 bytes)
disk size: 2.63 GiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: true
refcount bits: 16
corrupt: false
extended l2: false
Child node '/file':
filename: /data/kvm/almalinux8.qcow2
protocol type: file
file length: 20 GiB (21478375424 bytes)
disk size: 2.63 GiB
## start the VM
tyla@e32:~$ virsh start almalinux8
Domain 'almalinux8' started
## verify if the VM is running
tyla@e32:~$ virsh list
Id Name State
----------------------------
9 almalinux8 running
VM ရဲ့ status က running လို့ပြနေရင် ssh နဲ့ပြန်ဝင်ပြီး fdisk command နဲ့ လုပ်အပ်တဲ့ အချက်အလက်တွေကို သေချာပြန်ပြီး verify လုပ်ပါ။ အောက်ဖော်ပြမြင်ရတဲ့အတိုင်းပဲ vda1 က 1 GB နဲ့ vda2 က 19 GB လို့ပြနေပါလိမ့်မယ်။
## ssh into the VM
tyla@e32:~$ ssh [email protected]
[email protected]'s password:
Last login: Thu Sep 12 12:02:02 2024 from 192.168.122.1
## become root before the disk operation inside VM
[tyla@almalinux8 ~]$ sudo -i
[sudo] password for tyla:
## check the current disk layout and size
[root@almalinux8 ~]# fdisk -l
Disk /dev/vda: 25 GiB, 26843545600 bytes, 52428800 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
Disklabel type: dos
Disk identifier: 0xa8460c45
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 2099199 2097152 1G 83 Linux
/dev/vda2 2099200 41943039 39843840 19G 8e Linux LVM
Disk /dev/mapper/almalinux-root: 17 GiB, 18249416704 bytes, 35643392 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/almalinux-swap: 2 GiB, 2147483648 bytes, 4194304 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
fdisk အပြင်နောက်ထပ်အသုံးဝင်တဲ့ commands က lsblk နဲ့ df ပါ။ ဘယ် mount point က ဘယ် partition ပေါ်မှာ setup လုပ်ထားသလဲဆိုတာ အောက်မှာပြထားသလိုမျိုး စစ်ဆေးကြည့်နိုင်ပါတယ်။ မြင်တွေ့ရတဲ့အတိုင်း /boot ဆိုတဲ့ mount point ကို vda1 ပေါ်မှာ 1 GB ပေးထားပြီးတော့ / နဲ့ SWAP ကို vda2 ပေါ်မှာ 17 GB နဲ့ 2 GB ဆိုပြီးပေးထားပါတယ်။ df ရဲ့ output ကိုကြည့်မယ်ဆိုရင်လည်း xfs ဆိုတဲ့ filesystem ကိုအသုံးထားတာကိုတွေ့ရမှာဖြစ်ပါတယ်။
## also check the partition layout and size with both lsblk and df commands
[root@almalinux8 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 25G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 19G 0 part
├─almalinux-root 253:0 0 17G 0 lvm /
└─almalinux-swap 253:1 0 2G 0 lvm [SWAP]
[root@almalinux8 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 870M 0 870M 0% /dev
tmpfs tmpfs 890M 0 890M 0% /dev/shm
tmpfs tmpfs 890M 8.6M 881M 1% /run
tmpfs tmpfs 890M 0 890M 0% /sys/fs/cgroup
/dev/mapper/almalinux-root xfs 17G 2.2G 15G 13% /
/dev/vda1 xfs 1014M 232M 783M 23% /boot
tmpfs tmpfs 178M 0 178M 0% /run/user/1000
လိုအပ်တဲ့ အခြေခံအချက်အလက်တွေကို စုဆောင်းပြီးနောက် LVM ကိုအသုံးပြုပြီး partition ရဲ့ size တွေကိုစတင် resize လုပ်လို့ရနိုင်ပါပြီ။ pvs ဆိုတဲ့ LVM command ကိုအသုံးပြုတဲ့အခါမှာ /dev/vda2 တခုတည်းကိုသာ တွေ့ရမှာဖြစ်ပါတယ်။ /dev/vda1 မှာ LVM ကိုမသုံးထားတာကို သတိပြုရမှာဖြစ်ပါတယ်။
## check LVM physical volume size
[root@almalinux8 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 almalinux lvm2 a-- <19.00g 0
fdisk ကိုပဲအသုံးပြုပြီး /dev/vda disk ရဲ့ /dev/vda1 partition ကိုအရင်ဆုံး resize လုပ်ဖို့လိုပါလိမ့်မယ်။ ကိုယ်ပြင်ချင်တဲ့ partition ကိုပထမဆုံး ဖျက်ပစ်ပြီး ကိုယ်လိုချင်တဲ့ size ကိုပြောင်းလဲယူရမှာဖြစ်ပါတယ်။ ဒီအဆင့်မှာ သတိပြုရမှာက ဖျက်ပစ်တယ်လို့ဆိုပေမယ့် partition table ထဲက entry ကိုပဲဖျက်တာဖြစ်ပြီး၊ အဲ့ဒီ partition ထဲက data တွေကို ဖျက်ပစ်မှာမဟုတ်ပါဘူး။ သို့သော်... backup အရင်လုပ်ထားနိုင်တယ်ဆိုရင်တော့ အကောင်းဆုံးပါ။ နောက်တချက်က LVM type ကို ပြန်လည် setup လုပ်တဲ့အခါမှာ Do you want to remove the signature? [Y]es/[N]o: လို့မေးတဲ့နေရာရောက်ရင် N သို့မဟုတ် No လို့ဖြေပေးရပါ့မယ်။ ဒီနေရာမှာ သတိထားပြီးတော့ တဆင့်ပြီးတဆင့် လုပ်သင့်ပါတယ်။
## start working on the desired disk with fdisk
[root@almalinux8 ~]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
## type 'p' here to print the current disk layout to double check
## take note of 'Start' column for /dev/vda2
## value is 2099200 here.
Command (m for help): p
Disk /dev/vda: 25 GiB, 26843545600 bytes, 52428800 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
Disklabel type: dos
Disk identifier: 0xa8460c45
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 2099199 2097152 1G 83 Linux
/dev/vda2 2099200 41943039 39843840 19G 8e Linux LVM
## type 'd' here to delete the partition /dev/vda2
## note that it does not delete data on the disk but the entry in partition table
Command (m for help): d
## if no input and enter, the default is partition number 2 (/dev/vda2 here)
Partition number (1,2, default 2):
Partition 2 has been deleted.
## type 'n' for new to create new partition
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
## if no input and enter, the default is 'p' for primary partition type
Select (default p):
Using default response p.
## if no input and enter, the default is '2' for second primary partition
Partition number (2-4, default 2):
## if no input and enter, the default should be the same value as before
## value is 2099200 here
First sector (2099200-52428799, default 2099200):
## if no input and enter, the default is the last sector of the disk after resize
Last sector, +sectors or +size{K,M,G,T,P} (2099200-52428799, default 52428799):
Created a new partition 2 of type 'Linux' and of size 24 GiB.
Partition #2 contains a LVM2_member signature.
## !!!CAUTION!!! - type 'N' for No to keep the LVM2_member signature
Do you want to remove the signature? [Y]es/[N]o: N
## type 't' for type to set the partition type
Command (m for help): t
## type '8e' for LVM Hex code for the partition
Partition number (1,2, default 2):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
## type 'w' for write to apply the changes to the disk
Command (m for help): w
The partition table has been altered.
Syncing disks.
lsblk နဲ့ နောက်တခါထပ်စစ်ကြည့်လိုက်ပါ။ မြင်တွေ့ရတဲ့အတိုင်း vda2 partition ဟာ 24 GB ဖြစ်နေတာကိုတွေ့ရမှာပါ။ ဒီဆိုရင်တော့ LVM ရဲ့ physical volume ကို pvresize command နဲ့စတင်ပြောင်းလဲနိုင်ပါပြီ။ Resize လုပ်ပြီးနောက် pvs နဲ့ သေချာအောင် verify လုပ်လုပ်လိုက်ပါ။
## check if the vda2 disk partition size
[root@almalinux8 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 25G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 24G 0 part
├─almalinux-root 253:0 0 17G 0 lvm /
└─almalinux-swap 253:1 0 2G 0 lvm [SWAP]
## verify the LVM physical volume before resize
[root@almalinux8 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 almalinux lvm2 a-- <19.00g 0
## resize the physical volume with pvresize
[root@almalinux8 ~]# pvresize /dev/vda2
Physical volume "/dev/vda2" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
## verify the LVM physical volume after resize
[root@almalinux8 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 almalinux lvm2 a-- <24.00g 5.00g
Physical volume ကို resize လုပ်ပြီးနောက် LVM ရဲ့ volume group မှာဘယ်လောက်ထပ်တိုးလာသလဲဆိုတာကို vgs ဆိုတဲ့ command နဲ့ စစ်ကြည့်နိုင်ပါတယ်။ အောက်မှာမြင်တွေ့ရတဲ့အတိုင်း 5 GB ထပ်တိုးလာတာကိုတွေ့မြင်နိုင်ပါတယ်။ ဒါဆိုရင်တော့ ထပ်တိုးလာတဲ့ 5 GB အကုန်လုံးကို LVM ရဲ့ logical volume ထဲမှာတိုးပေးဖို့အတွက် lvextend -l +100%FREE /dev/almalinux/root ဆိုတဲ့ command ကိုသုံးရပါ့မယ်။ ဒီလိုတိုးပြီးရင် lvs ဆိုတဲ့ command နဲ့ logical volume size ကို verify လုပ်ကြည့်နိုင်ပါတယ်။
## also verify the LVM volume group for free space
[root@almalinux8 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
almalinux 1 2 0 wz--n- <24.00g 5.00g
## now extend the LVM logical volume with lvextend to extend /dev/almalinux/root
[root@almalinux8 ~]# lvextend -l +100%FREE /dev/almalinux/root
Size of logical volume almalinux/root changed from <17.00 GiB (4351 extents) to <22.00 GiB (5631 extents).
Logical volume almalinux/root successfully resized.
## verify the logical volume size of root
[root@almalinux8 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root almalinux -wi-ao---- <22.00g
swap almalinux -wi-ao---- 2.00g
နောက်တခါ lsblk နဲ့ / mount point ကိုစစ်ကြည့်တဲ့အခါမှာ 22 GB ဖြစ်နေတာကိုတွေ့ရမှာပါ။ အရှေ့မှာ စစ်ကြည့်ထားလို့ xfs ဆိုတဲ့ filesystem ကိုအသုံးပြုထားတာသိရှိပြီးတဲ့အတွက် xfs_growfs ဆိုတဲ့ command နဲ့ / mount point ရဲ့ filesystem ကို grow လုပ်ပေးရပါလိမ့်မယ်။ နောက်ဆုံးအနေနဲ့ df command ကိုအသုံးပြုပြီး / mount point ကိုစစ်ကြည့်လိုက်ရင် ကိုယ်လိုချင်တဲ့ size ကိုတိုးပြီးမြင်နိုင်မှာဖြစ်ပါတယ်။
## also verify it with lsblk
[root@almalinux8 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 25G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 24G 0 part
├─almalinux-root 253:0 0 22G 0 lvm /
└─almalinux-swap 253:1 0 2G 0 lvm [SWAP]
## grow the / mount with xfs_growfs because the filesystem is xfs here
## note that resize2fs is used when the filesystem is ext4
[root@almalinux8 ~]# xfs_growfs /
meta-data=/dev/mapper/almalinux-root isize=512 agcount=4, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=0 inobtcount=0
data = bsize=4096 blocks=4455424, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log 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 4455424 to 5766144
## verify the disk free space at / mount point now
[root@almalinux8 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 870M 0 870M 0% /dev
tmpfs tmpfs 890M 0 890M 0% /dev/shm
tmpfs tmpfs 890M 8.6M 881M 1% /run
tmpfs tmpfs 890M 0 890M 0% /sys/fs/cgroup
/dev/mapper/almalinux-root xfs 22G 2.2G 20G 10% /
/dev/vda1 xfs 1014M 232M 783M 23% /boot
tmpfs tmpfs 178M 0 178M 0% /run/user/1000
အထက်မှာရှင်းပြသွားတဲ့အတိုင်း အရမ်းကြည့်တော့မခက်ခဲ့တဲ့ process တခုဖြစ်ပေမယ့်လည်း တခါမှာမလုပ်ဘူးရင် KVM မှာ virtual disk တွေကို non-destructive နည်းနဲ့ ဘယ်လိုလုပ်ရသလဲဆိုတာ အလွယ်တကူ ရှာကြည့်ပြီး စမ်းသပ်ဖို့အနည်းငယ်လက်ဝင်ပါလိမ့်မယ်။ ကိုယ်တိုင်မလုပ်ဘူးရင် သေချာရှင်းရှင်းလင်းလင်း နားလည်ဖို့အတွက် ခက်ခဲ့နိုင်တာကြောင့် lab environment မှာအနည်းဆုံး တစ်ခေါက်လောက်တော့ အစအဆုံး စမ်းသပ်ဖို့တိုက်တွန်းလိုပါတယ်။
Last updated
Was this helpful?