1. 程式人生 > >KVM虛擬機的創建和常用功能及命令

KVM虛擬機的創建和常用功能及命令

信息 cdr graph sna res ole 新的 熱添加內存 efault

安裝:
yum install libvirt virt-install qemu-kvm -y

介紹:
libvirt服務:管理kvm虛擬機的生命周期
virt-install工具:創建安裝虛擬機
qemu-kvm工具:使用qemu-img為虛擬機提供硬盤

新創建一個虛擬機:
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7
--memory 1024 --vcpus 1 --disk /opt/centos2.raw,format=raw,size=10
--cdrom /opt/CentOS-7-x86_64-DVD-1708.iso

--network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole


創建虛擬磁盤
qemu-img create test.raw 10G
qemu-img create -f qcow2 test.qcow2 10G

查看虛擬磁盤信息
qemu-img info test.raw

調整虛擬磁盤容量大小
qemu-img resize test.raw +5G

磁盤格式轉換
qemu-img convert -f raw -O qcow2 test.raw oldboy.qcow2


創建快照
virsh snapshot-create centos7

查看快照
virsh snapshot-list centos7

還原快照
virsh snapshot-revert centos7 --snapshotname 1516574134

刪除快照
virsh snapshot-delete centos7 --snapshotname 1516636570


完整克隆
實現方法:
virt-clone -o web01 --auto-clone

鏈接克隆

1:克隆虛擬磁盤文件
cp centos7.qcow2 web03.qcow2
2:生成新的虛擬機配置文件
name修改
uuid刪掉
disk路徑/opt/web03.qcow2
mac地址刪除
3:測試啟動
virsh define web01.xml

qemu-img create -f qcow2 -b cetnos7.qcow2 web04.qcow2

virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web02 --memory 1024 --vcpus 1 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole


創建橋接網絡
1:virsh iface-bridge eth0 br0

基於橋接網絡創建虛擬機
2:virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 1024 --vcpus 1 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

1:virsh edit centos7

<interface type=‘bridge‘>
  <mac address=‘52:54:00:55:aa:fa‘/>
  <source bridge=‘br0’/>

2:修改虛擬機ip地址
/etc/sysconfig/network-scripts/ifcfg-eth0


創建硬盤
qemu-img create -f qcow2 centos7-add01.qcow2 5G

熱添加硬盤
virsh attach-disk web01 /opt/centos7-add01.qcow2 vdb --live --cache=none --subdriver=qcow2

添加網卡
virsh attach-interface web04 --type bridge --model virtio --source br0

virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web01 --memory 1024 --vcpus 1,maxvcpus=4 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

熱添加cpu
virsh setvcpus web04 --count=2

virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 512,maxmemory=2048 --vcpus=1,maxvcpus=2 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

熱添加內存
virsh setmem web04 1G

KVM虛擬機的創建和常用功能及命令