虚拟化技术:虚拟机管理(六)
六、虚拟机管理
1.进入virsh交互界面
[root@localhost ~]# virsh
Welcome to virsh, the virtualization interactive terminal.
Type: 'help' for help with commands
'quit' to quit
2. 查看虚拟机
virsh # list --all
Id Name State
----------------------------------------------------
31 window7 running
- linux-01 shut off
3.虚拟机启动、关闭、重启
virsh # start linux-01 #启动虚拟机
Domain linux-01 started
virsh # list --all
Id Name State
----------------------------------------------------
31 window7 running
32 linux-01 running
virsh # shutdown linux-01 #关闭虚拟机
Domain linux-01 is being shutdown
virsh # list --all
Id Name State
----------------------------------------------------
31 window7 running
- linux-01 shut off
virsh # reboot linux-01 #重启虚拟机
Domain linux-01 is being rebooted
virsh # list --all
Id Name State
----------------------------------------------------
31 window7 running
32 linux-01 running
4. 虚拟机挂起与恢复
virsh # suspend window7 #挂起虚拟机(暂停)
Domain window7 suspended
virsh # list --all
Id Name State
----------------------------------------------------
31 window7 paused
- linux-01 shut off
#虚拟机处于paused暂停状态,一般情况下是被root运行了virsh suspend才会处于这种状态,但是仍然消耗资源,只不过不被超级管理程序调度而已。
virsh # resume window7 #恢复虚拟机
Domain window7 resumed
virsh # list --all
Id Name State
----------------------------------------------------
31 window7 running
- linux-01 shut off
5.配置虚拟机自启动
virsh # list --all
Id Name State
----------------------------------------------------
1 centos_test running
- linux-01 shut off
- window7 shut off
virsh # autostart centos_test #开机自启动
Domain centos_test marked as autostarted
virsh # exit
[root@localhost ~]# ll /etc/libvirt/qemu/autostart/ #开启自启动会在这个目录建立一个链接,KVM会检查这个目录来实现自启动
total 0
lrwxrwxrwx 1 root root 33 Mar 12 10:56 centos_test.xml -> /etc/libvirt/qemu/centos_test.xml
[root@localhost ~]# virsh
Welcome to virsh, the virtualization interactive terminal.
Type: 'help' for help with commands
'quit' to quit
virsh # autostart --disable centos_test #关闭自启动
Domain centos_test unmarked as autostarted
virsh # exit
[root@localhost ~]# ll /etc/libvirt/qemu/autostart/ #虚拟机描述文件的软链接没了
total 0
6.常用查询命令
6.1 查看指定虚拟机的状态
[root@localhost ~]# virsh domstate centos_test
running
6.2 查看虚拟机的磁盘列表
[root@localhost ~]# virsh domblklist centos_test
Target Source
------------------------------------------------
vda /home/kvm/image/centos_test.qcow2
6.3 查看虚拟机的网络接口情况
[root@localhost ~]# virsh domiflist centos_test
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 network default virtio 52:54:00:4d:c6:26
6.4 查看虚拟机的网络接口IP
[root@localhost ~]# virsh domifaddr centos_test
Name MAC address Protocol Address
-------------------------------------------------------------------------------
vnet0 52:54:00:4d:c6:26 ipv4 192.168.122.174/24
6.5 查看虚拟机的基本信息
[root@localhost ~]# virsh dominfo centos_test
Id: 1
Name: centos_test
UUID: 7ad79c31-127b-4787-bdb8-7257597c47b9
OS Type: hvm
State: running
CPU(s): 1
CPU time: 168.1s
Max memory: 2097152 KiB
Used memory: 2097152 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
6.6 编辑KVM虚拟机配置文件
[root@localhost ~]# virsh edit window7
6.7 导出虚拟机配置
[root@localhost ~]# virsh dumpxml window7 > window7.xml
10. 制作虚拟机模板
虚拟机模板作用:
- 批量生成虚拟机
- 缩短部署时间
- 提升工作效率
模板制作过程:
- 删除 ssh 主机密钥:rm -rf /etc/ssh/ssh_host_*
- 在 /etc/hostname 中设置 HOSTNAME=localhost.localdomain。
- 从 /etc/sysconfig/network-scripts/ifcfg-ens*中删除 HWADDR 行和 UUID 行。
- 关机,备份模板机磁盘。
根据模板机生成虚拟机:
复制模板磁盘文件到存储虚拟机磁盘文件的目录下(文件名最好跟创建虚拟机一致,方便管理)
[root@localhost ~]# cp /home/kvm/backup_image/centos7.qcow2 /home/kvm/image/centos_test.qcow2
创建虚拟机
virt-install --name centos_test \
--memory 2048 \
--vcpus=1 \
--disk /home/kvm/image/centos_test.qcow2,bus=virtio,format=qcow2 \
--import \
--graphics vnc,port=6001,listen=192.168.15.15 \
--network network=default,model=virtio \
--noautoconsole
Tips:--import 为跳过操作系统安装过程,围绕一个存在的磁盘文件建立客户机。引导使用的设备是通过"--disk" or "--file"指定的第一个设备。
作者:YinJay
Email:szgetshell@163.com