虚拟机管理、虚拟机传输、虚拟机快照的制作
1.虚拟机的管理
shut down #正常关机
forceoff #强制关机
virsh list #列出正在运行的虚拟机
virsh list --all #列出所有的虚拟机
virsh start desktop #开启desktop
virt-viewer desktop #显示desktop
virsh shutdown desktop #正常关闭desktop
virsh destroy desktop #强制关闭desktop
virsh create rhel7.2.xml #一次性恢复
virsh define rhel7.2.xml #永久性恢复
virsh undefine rhel7.2 #永久性删除
实验:
##列出正在运行的虚拟机
[[email protected] ~]# virsh list
##列出所有的虚拟机
[[email protected] ~]# virsh list --all
##正常关闭虚拟机(不会关闭图形)
[[email protected] ~]# virsh shutdown rhel7.2
Domain rhel7.2 is being shutdown
##开启虚拟机
[email protected] ~]# virsh start rhel7.2
Domain rhel7.2 started
##强制退出虚拟机(会关闭图形)
[[email protected] ~]# virsh destroy rhel7.2
Domain rhel7.2 destroyed
##开启虚拟机
[email protected] ~]# virsh start rhel7.2
Domain rhel7.2 started
##显示虚拟机
[[email protected] ~]# virt-viewer rhel7.2
2.虚拟机的传输
html #超文本编辑文件
xml #可扩展性编辑语言
/etc/libvirt/qemu/westos1.xml 包含虚拟机硬盘信息
/var/lib/libvirt/images/westos1.qcow2 包含虚拟机系统信息(在虚拟机图形中可以查看到)
实验:将42主机的虚拟机rhel7.2拷贝到本机
@1查看42主机的硬盘信息与系统信息位置
[[email protected] mnt]# ssh [email protected] -X
[email protected]'s password:
Last login: Sun Oct 21 13:56:52 2018 from foundation34.ilt.example.com
[[email protected] ~]# virt-manager
[[email protected] ~]#
** (virt-manager:17892): WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-WF050MidiS: Connection refused
^C
[[email protected] ~]# cd /etc/libvirt/qemu/
[[email protected] qemu]# ls
desktop.xml networks rhel7.2.xml server.xml
硬件信息
[[email protected] qemu]# pwd
/etc/libvirt/qemu
@2拷贝42主机的硬盘信息与系统信息到本机
[[email protected] ~]# cd /mnt
##拷贝虚拟机硬盘信息
[[email protected] mnt]# scp [email protected]:/etc/libvirt/qemu/rhel7.2.xml .
[email protected]'s password:
rhel7.2.xml 100% 4265 4.2KB/s 00:00
##拷贝虚拟机系统信息
[[email protected] mnt]# scp [email protected]5.254.42:/var/lib/libvirt/images/rhel7.2-2.qcow2 .
[email protected]'s password:
rhel7.2-2.qcow2 100% 9218MB 29.5MB/s 05:12
[[email protected] mnt]# ls
rhel7.2-2.qcow2 rhel7.2.xml vm_create.sh
@3管理刚拷贝过来的虚拟机
[[email protected] mnt]# virsh create rhel7.2.xml
error: Failed to create domain from rhel7.2.xml
error: Cannot access storage file '/var/lib/libvirt/images/rhel7.2-2.qcow2' (as uid:107, gid:107): No such file or directory
[[email protected] mnt]# mv rhel7.2-2.qcow2 /var/lib/libvirt/images/
##打开虚拟机
[[email protected] mnt]# virsh create rhel7.2.xml
Domain rhel7.2 created from rhel7.2.xml
将虚拟机关机(force off)后;会发现虚拟机消失了
##恢复虚拟机
[[email protected] mnt]# virsh define rhel7.2.xml
Domain rhel7.2 defined from rhel7.2.xml
##永久性删除硬件信息;不是删除虚拟机
[[email protected] mnt]# virsh undefine rhel7.2
[[email protected] mnt]# cd /var/lib/libvirt/images/
[[email protected] images]# ls
rh124-desktop-vda.ovl rh124-server-vda.ovl rhel7.2-2.qcow2 #
rh124-desktop-vdb.ovl rh124-server-vdb.ovl westos2.qcow2
rh124-desktop-vdb.qcow2 rh124-server-vdb.qcow2
rh124-desktop.xml rh124-server.xml
##一次性恢复
rh[[email protected] mnt]# virsh create rhel7.2.xml
Domain rhel7.2 created from rhel7.2.xml
##永久性恢复
[[email protected] mnt]# virsh define rhel7.2.xml
Domain rhel7.2 defined from rhel7.2.xml124-desktop-vda.qcow2 rh124-server-vda.qcow2 westos1.qcow2
##删除虚拟机系统信息(磁盘文件),即删除虚拟机;此时虚拟机便无法再恢复
[[email protected] images]# rm -rf /var/lib/libvirt/images/rhel7.2-2.qcow2
注意:你所拷贝的虚拟机不能和你已有的虚拟机名称重名,我这里是因为前面的实验主机与此实现主机不同,所以没有影响。
3.虚拟机快照的制作
qemu-img create -f qcow2 -b rhel7.2.qcow2 node1.qcow2
具体参数含义:
create #创建
-f #类型
-b #备份
方法1:命令的方式
@1制作快照
[[email protected] ~]# cd /var/lib/libvirt/images/
[[email protected] images]# ls
##制作快照
[[email protected] images]# qemu-img create -f qcow2 -b rhel7.2.qcow2 node1.qcow2
##查看快照所占磁盘空间大小
[[email protected] images]# du -sh node1.qcow2
[[email protected] images]# ls
@2导入快照
[[email protected] images]# virt-manager
如果虚拟机快照node1误删了/ 此时虚拟机便会起不来
删除/之后,reboot命令都无法使用;此时用图形强制关机再开机发现系统仍然无法使用
解决方案:
@1删除坏掉的虚拟机快照node1
##关闭虚拟机
[[email protected] ~]# virsh destroy node1
##永久性删除虚拟机
[[email protected] ~]# virsh undefine node1
##删除虚拟机系统信息
[[email protected] ~]# rm -rf /var/lib/libvirt/images/node1.qcow2
@2重新创建快照,再导入即可
[[email protected] ~]# cd /var/lib/libvirt/images/
##创建快照
[[email protected] images]# qemu-img create -f qcow2 -b rhel7.2.qcow2
因为坏掉的是快照,而它的母盘完好无损,所以删除坏掉的快照,重新创建即可
(2).方法2:脚本的方式
[[email protected] ~]# vim vm_snapshoot.sh
######################
#!/bin/bash #幻数
qemu-img create \ #1制作快照(同命令方式)
-f qcow2 \
-b /var/lib/libvirt/images/$1.qcow2 \
/var/lib/libvirt/images/$2.qcow2 &> /dev/null \
virt-install \ #2导入快照(同图形界面时设定硬件信息)
--name $2 \
--ram 1024 \
--vcpus 1 \
--disk /var/lib/libvirt/images/$2.qcow2,bus=virtio \
--network source=br0,model=virtio \
--import &> /dev/null & # --import 表示导入
[[email protected] ~]# chmod +x vm_snapshoot.sh
##执行脚本
[[email protected] ~]# ./vm_snapshoot.sh rhel7.2 node1
排错思想:
1.先将重定向 &>/dev/null 删掉
2.执行脚本,查看报错信息
3.根据报错信息,更正脚本内容