KVM管理虚拟机:常用命令
1.a,创建虚拟机1.b,运行/停止虚拟机1.c,查看虚拟机虚拟机配置调整:内存,磁盘,cpu数1, 虚拟机:生命周期管理a, 创建虚拟机#内存:1G,硬盘:40G,cup: 2###执行命令后,满足条件就会弹出centos的安装界面virt-install \--name cent7 \--ram 1024 \--disk path=/var/lib/li...
·
安装kvm
文档参考:
CentOS7 :http://www.linux-kvm.org/page/HOWTO#RHEL.2FCentOS_7
CentOS6 :https://wiki.centos.org/zh/HowTos/KVM --> 白皮书: 第一册, 第二册
- 1.a,创建虚拟机
- 1.b,运行/停止虚拟机
- 1.c,查看虚拟机
- 虚拟机配置调整:内存,磁盘,cpu数
- 虚拟机的两大主要文件:image, xml文件
## 1, image路径:
root@controller:/var/lib/libvirt/images# ls
cent7-clone.qcow2 cent7.qcow2 centos7.0.qcow2
## 2, 虚拟机xml配置文件路径:
root@controller:/etc/libvirt/qemu# ls
cent7-clone.xml cent7.xml centos7.0.xml networks
1, 虚拟机:生命周期管理
- a, 创建虚拟机 (virt-install)
#内存:1G, cup: 2, 硬盘:30G
###执行命令后,满足条件就会弹出centos的安装界面
virt-install \
--name c6 \
--ram=1024 \
--vcpus=2 \
--network=bridge:br0 \
--disk path=/var/lib/libvirt/images/c6.img,size=30 \
--cdrom /CentOS-6.10-x86_64-minimal.iso
- b,查看/运行/停止虚拟机 (virsh list/ start/ shutdown,destroy)
root@controller:/home/wang# virsh list
Id Name State
----------------------------------------------------
6 centos7.0 running
9 cent7 running
root@controller:~/openstack# virsh dominfo cent7
Id: 20
Name: cent7
UUID: a566d8e4-ec6f-42d7-9d31-8d0d3ce5f09a
OS Type: hvm
State: running
CPU(s): 2
CPU time: 197.0s
Max memory: 2097152 KiB
Used memory: 1048576 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: apparmor
Security DOI: 0
Security label: libvirt-a566d8e4-ec6f-42d7-9d31-8d0d3ce5f09a (enforcing)
################# autostart, start, shutdown, reboot //pause, resume
root@controller:/etc/network# virsh shutdown cent7
Domain cent7 is being shutdown
root@controller:/etc/network# virsh list --all
Id Name State
----------------------------------------------------
6 centos7.0 running
- cent7 shut off
- 3, 删除虚拟机
root@controller:/ # cat /usr/local/bin/kvm-delete.sh
read -p '输入(要删除的虚拟机):' name
echo "....going to delete ..$name"
#1,destroy
virsh destroy $name
#2,undefine
virsh undefine $name
#3,del image
rm -f /var/lib/libvirt/images/$name.qcow2
echo "delete kvm $name --> ok...."
2, kvm登录虚拟机
- virsh console xx
#virt-viewer -c qemu:///system VMachinename
virt-viewer cent7
3, 虚拟机内存调整
## setmaxmem change maximum memory limit
## setmem change memory allocation
#虚拟机开机后,动态内存扩展(加上--config表示写入到配置文件,永久生效)
virsh setmaxmem centos7 9GB --config
virsh setmem centos7 8GB --config
## setvcpus change number of virtual CPUs
## setvcpu attach/detach vcpu or groups of threads
4, 复制/克隆虚拟机
virt-clone命令: 需要关闭虚拟机再操作
root@controller:~/openstack# virt-clone -o cent7 -n cent7_2 --auto-clone
正在分配 'cent7_2.qcow2' | 5.0 GB 00:00:23
成功克隆 'cent7_2'。
root@controller:~/openstack# virsh list --all
Id Name State
----------------------------------------------------
18 stack running
- cent7 shut off
- cent7_2 shut off
root@controller:~/openstack# ls /var/lib/libvirt/images/
cent7_2.qcow2 cent7.qcow2 centos7.0.qcow2 stack.qcow2
root@controller:~/openstack# ls /etc/libvirt/qemu
cent7_2.xml cent7.xml networks stack.xml
若没有关闭虚拟机来克隆就会报以下错误:
root@controller:~# virsh list
Id Name State
----------------------------------------------------
11 centos7.0 running
12 cent7 paused
root@controller:~# virt-clone -o cent7 --auto-clone
WARNING 设置图形设备端口为自动端口,以避免相互冲突。
ERROR Couldn't create storage volume 'cent7-clone.qcow2': 'internal error: Child process (/usr/bin/qemu-img convert -f qcow2 -O qcow2 -o compat=1.1,lazy_refcounts /var/lib/libvirt/images/cent7.qcow2 /var/lib/libvirt/images/cent7-clone.qcow2) unexpected exit status 1: qemu-img: Could not open '/var/lib/libvirt/images/cent7.qcow2': Failed to get shared "write" lock
Is another process using the image?
5,虚拟机重命名
把上面的cent7-clone 这个刚克隆的虚拟机重命名(先停止虚拟机)–> cent7_2
root@controller:~# virt-clone -o cent7 --auto-clone
正在分配 'cent7-clone.qcow2' | 40 GB 00:00:47
成功克隆 'cent7-clone'。
#启动克隆好的虚拟机
root@controller:~# virsh start cent7
Domain cent7 started
root@controller:~# virsh start cent7-clone
Domain cent7-clone started
root@controller:~# virsh list
Id Name State
----------------------------------------------------
11 centos7.0 running
13 cent7 running
14 cent7-clone running
# 1, xml修改
root@controller:~# virsh dumpxml cent7-clone > /etc/libvirt/qemu/cent7_2.xml
root@controller:/etc/libvirt/qemu# ls
cent7_2.xml cent7-clone.xml cent7.xml networks
####修改旧的名字, 为新的名字
root@controller:/etc/libvirt/qemu# grep cent7-clone cent7_2.xml
<name>cent7-clone</name>
<source file='/var/lib/libvirt/images/cent7-clone.qcow2'/>
root@controller:/etc/libvirt/qemu# sed -i s/cent7-clone/cent7_2/g cent7_2.xml
# 2, image文件重命名
root@controller:/var/lib/libvirt/images# ls
cent7-clone.qcow2 cent7.qcow2
root@controller:/var/lib/libvirt/images# mv cent7-clone.qcow2 cent7_2.qcow2
# 3, 废弃旧的虚拟机,使用新的虚拟机
root@controller:~# virsh list
Id Name State
----------------------------------------------------
13 cent7 running
14 cent7-clone running
root@controller:~# virsh shutdown cent7-clone
Domain cent7-clone is being shutdown
root@controller:/etc/libvirt/qemu# virsh undefine cent7-clone
Domain cent7-clone has been undefined
root@controller:/etc/network# ls /etc/libvirt/qemu/
cent7_2.xml cent7.xml centos7.0.xml networks/
root@controller:/etc/libvirt/qemu# virsh list --all
Id Name State
----------------------------------------------------
13 cent7 running
root@controller:/etc/libvirt/qemu# virsh define cent7_2.xml
Domain cent7_2 defined from cent7_2.xml
root@controller:/etc/libvirt/qemu# virsh start cent7_2
viDomain cent7_2 started
root@controller:/etc/libvirt/qemu# virsh list
Id Name State
----------------------------------------------------
13 cent7 running
15 cent7_2 running
6, kvm导出virtualbox/vmware虚拟机
- 把kvm的虚拟机磁盘文件导出为vdi,vmdk即可在virtualbox或vmware中使用此虚拟机
sudo qemu-img convert /var/lib/libvirt/images/win7.qcow2 -O vdi ./win7.vdi
更多推荐
已为社区贡献8条内容
所有评论(0)