使用libvirt 擴展虛擬機磁盤的方法:
libvirt 工具支持KVM/Qemu,xen, lxc,openvz, esx, hyperv 等虛擬化,是一個使用及其廣泛的虛擬化管理工具.
vrish , libvirt 的客戶端管理工具,常用到的命令有:
virsh list --all 列出所有的虛擬機,注意當(dāng)前的user, 可能root 并不能列出已經(jīng)存在的虛擬機,因為權(quán)限的問題;
virsh shutdown VM_NAME 通過call ACPI接口關(guān)閉虛擬機,如果VM沒有開啟ACPI 接口,那么不會成功;
virsh destroy VM_NAME 強行通過掉電的方式進(jìn)行關(guān)機,不推薦,除非系統(tǒng)出現(xiàn)問題;
virsh start VM_NAME 開啟虛擬機
virsh suspend VM_NAME 休眠虛擬機
virsh resume VM_NAME 恢復(fù)虛擬機
virsh dominfo VM_NAME 列出虛擬機的基本信息
virsh dumpxml VM_NAME 列出虛擬機的配置文件,以xml格式的形式,在該配置文件中,可以找到cpu, mem, disk的相關(guān)配置,所以比較有用;
virsh edit VM_NAME 編輯虛擬機的配置文件,一般是剛創(chuàng)建的時候使用;
virsh setmem VM_NAME 512000 #給不活動虛擬機設(shè)置內(nèi)存大小為512000
virsh setvcpus VM_NAME 4 #給不活動虛擬機設(shè)置cpu個數(shù)為4
值得注意的是,virsh 命令本身支持交互模式,這一點非常好用;
磁盤擴容的步驟(linux 環(huán)境):
A. 備份原來的磁盤文件, 參考命令如下:
$ sudo cp /var/lib/libvirt/images/vm-gitlab6/ubuntu-kvm/vm-gitlab6-img1.qcow2 /var/lib/libvirt/images/vm-gitlab6/ubuntu-kvm/vm-gitlab6-img1-back.qcow2
B. 查看磁盤的使用情況, 參考命令如下:
$ sudo virt-df -h vm-gitlab6-img1.qcow2
W: kvm binary is deprecated, please use qemu-system-x86_64 instead
Filesystem Size Used Available Use%
vm-gitlab6-img1.qcow2:/dev/sda1 18G 1.1G 16G 6%
C. 創(chuàng)建新的虛擬磁盤,指定 磁盤大小,這個磁盤將用來替換原來的磁盤(這個過程并不會檢查系統(tǒng)中的空間是否足夠分配指定的大小,所以確保系統(tǒng)有足夠空間是我們需要關(guān)注的,這個過程基本不會失敗,速度非???以下指定創(chuàng)建一個40GB的新磁盤):
$ sudo qemu-img create -f qcow2 vm-gitlab6-img1-big.qcow2 40G
D. 通過virt-resize 命令對需要擴容的分區(qū)進(jìn)行擴容,其過程基本是這樣的:
從原來的磁盤獲取分區(qū)信息,然后把用戶指定的分區(qū)進(jìn)行邏輯上的擴容,其余分區(qū)保持不變;最后把這個新的分區(qū)表寫入到新的磁盤;從而在新磁盤上實現(xiàn)對分區(qū)的擴容操作;以下是擴展原來磁盤的/dev/sda1分區(qū),并保留其他分區(qū)不變的操作過程,以及相應(yīng)的提示;
$ sudo virt-resize --expand /dev/sda1 vm-gitlab6-img1.qcow2 vm-gitlab6-img1-big.qcow2
Examining vm-gitlab6-img1.qcow2 ...
W: kvm binary is deprecated, please use qemu-system-x86_64 instead
**********
Summary of changes:
/dev/sda1: This partition will be resized from 18.6G to 32.5G. The
filesystem ext4 on /dev/sda1 will be expanded using the 'resize2fs'
method.
/dev/sda2: This partition will be left alone.
**********
Setting up initial partition table on vm-gitlab6-img1-1.qcow2 ...
Copying /dev/sda1 ...
100% ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 00:00
Copying /dev/sda2 ...
100% ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 00:00
W: kvm binary is deprecated, please use qemu-system-x86_64 instead
Expanding /dev/sda1 using the 'resize2fs' method ...
Resize operation completed with no errors. Before deleting the old
disk, carefully check that the resized disk boots and works correctly.
E.
以上完成了新磁盤的分區(qū)分配,然后用 virsh-df 命令查看新磁盤分區(qū)情況:
$ sudo virt-df -h vm-gitlab6-img1-big.qcow2
W: kvm binary is deprecated, please use qemu-system-x86_64 instead
Filesystem Size Used Available Use%
vm-gitlab6-img1-big.qcow2:/dev/sda1 32G 1.1G 29G 4%
F.
最后是重命名這個新的磁盤和老的磁盤名稱,這樣就無需從系統(tǒng)層面做變更, 然后進(jìn)行開機驗證; 如果一切都OK, 那么就可以直接刪除原來的老磁盤;
$ sudo rm vm-gitlab6-img1.qcow2 $ sudo mv vm-gitlab6-img1-big.qcow2 vm-gitlab6-img1.qcow2 $ sudo chmod 755 vm-gitlab6-img1.qcow2