一.虚拟机的管理命令
注意:切换到root超级用户下进行实验
命令:
virt-manager ##开启虚拟机的管理器
注意:必须保持其余虚拟机处于关闭状态,以防在安装过程中电脑卡死
virsh list ##显示正在运行的虚拟机
[root@foundation5 ~]# virsh list ##显示正在运行的虚拟机
Id Name State
----------------------------------------------------
1 desktop running
2 server running
[root@foundation5 ~]# virsh list --all ##显示所有虚拟机
Id Name State
----------------------------------------------------
1 desktop running
2 server running
virsh shutdown正常关闭虚拟机
[root@foundation5 ~]# virsh shutdown desktop ##正常关闭虚拟机,速度慢,需要关闭过程
Domain desktop is being shutdown
virsh start ##开启虚拟机
[root@foundation5 ~]# virsh start desktop ##开启虚拟机
Domain desktop started
virsh destroy ##强行关闭虚拟机
[root@foundation5 ~]# virsh destroy desktop ##强行关闭虚拟机
Domain desktop destroyed
virt-viewer ##显示虚拟机
[root@foundation5 ~]# virt-viewer desktop ##显示虚拟机
二.镜像安装虚拟机
1.选择创建方式为镜像并添加文件
2.选择内存大小及cpu核数
3.设置硬盘
4.给虚拟机命名
Finish 后进入安装界面,此时选择直接安装即可
5.语言环境的选择
6.以下4处的更改
(1)系统时区的修改
(2)系统语言的修改
(3)添加系统磁盘并划分大小
对磁盘进行大小的
7.完成上述所有配置更改后,点击Begin Instarllation
8.设置登录名及密码
9.安装重启
10.重启再次进入虚拟机中
三.模拟虚拟机的恢复
虚拟机在系统中以文件的形式存在,两个文件:
/ec/libvirt/qemu/xxx.xml #该文件文虚拟机前端配置文件
/var/lib/libvirt/images/xxx.qcow2 #该文件为虚拟机硬盘文件
1.准备文件
2.对新安装的虚拟机进行删除
3.虚拟机的恢复
(1)硬盘文件恢复虚拟机
打开虚拟机管理器——选择创建虚拟机——创建方式选择已经存在的硬盘文件——选择xxx.qcow2 文件。之后和使用镜像安装虚拟机一致。
(2)虚拟机的一次性恢复
[root@foundation47 ~]# cd /mnt
[root@foundation47 mnt]# ls
rhel-server-7.3-x86_64-dvd.iso toto.qcow2 toto.xml
[root@foundation47 mnt]# virsh create toto.xml ##一次性恢复虚拟机
error: Failed to create domain from toto.xml
error: Cannot access storage file '/var/lib/libvirt/images/toto.qcow2' (as uid:107, gid:107): No such file or directory # #报错由于/var/lib/libvirt/images/toto.qcow2该文件没有
[root@foundation47 mnt]# mv toto.qcow2 /var/lib/libvirt/images/ ##移动该文件到相应的位置
[root@foundation47 mnt]# virsh create toto.xml ##再次恢复虚拟机
Domain toto created from toto.xml ## 显示恢复成功
[root@foundation47 mnt]# virt-manager
注意:使用此方式恢复的虚拟机是一次性的,当虚拟机关闭后会自动消失
(3)虚拟机的永久恢复
[root@foundation47 mnt]#virsh define xxx.xml ##永久恢复虚拟机
(4)删除虚拟机
virsh undefine 虚拟机名称 ##在虚拟机关闭状态下删除xxx.xml文件
rm -fr /var/lib/libvirt/images/xxx.qcow2 ##删除硬盘文件
四.脚本快速创建虚拟机
[root@foundation47 ~]# ls /opt
rh shell
[root@foundation47 ~]# cd /opt/shell/
[root@foundation47 shell]# touch virt_install.sh ##创建脚本文件
[root@foundation47 shell]# ls
virt_install.sh
[root@foundation47 shell]# vim virt_install.sh ##编辑脚本文件
[root@foundation47 shell]# cat virt_install.sh ##查看脚本文件内容
#!/bin/bash ##规定运行环境
virt-install \ ##命令 '\'为换行符 后面不能有空格等
--cdrom /mnt/rhel-server-7.3-x86_64-dvd.iso \ ##参数,使用镜像创建虚拟机并给出镜像文件路径
--memory 1024 \ ##规定内存大小
--vcpus 1 \ ##规定cpu个数
--disk /var/lib/libvirt/images/$1.qcow2,bus=virtio,size=8 \ ##规定硬盘文件以及硬盘总线类型和硬盘大小;‘$1’表示运行脚本命令后的第一串字符 此处是新建虚拟机的名字
--name $1 \ ##虚拟机名称
--network model=virtio,bridge=br0 ##规定设置网卡
[root@foundation47 shell]# sh virt_install.sh lala ##运行脚本并且新建虚拟机取名lala
[root@foundation47 shell]# WARNING No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results.
Starting install...
Allocating 'lala.qcow2' | 8.0 GB 00:00
Creating domain... | 0 B 00:00
(virt-viewer:7477): GSpice-WARNING **: PulseAudio context failed Connection refused
(virt-viewer:7477): GSpice-WARNING **: pa_context_connect() failed: Connection refused
(virt-viewer:7477): GSpice-WARNING **: Warning no automount-inhibiting implementation available
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
^C
[root@foundation47 shell]# vim virt_install.sh #编辑脚本文件
[root@foundation47 shell]# cat virt_install.sh
#!/bin/bash
virt-install \
--cdrom /mnt/rhel-server-7.3-x86_64-dvd.iso \
--memory 1024 \
--vcpus 1 \
--disk /var/lib/libvirt/images/$1.qcow2,bus=virtio,size=8 \
--name $1 \
--network model=virtio,bridge=br0 \
&>/dev/null #不需要显示返回信息
[root@foundation47 shell]# sh virt_install.sh hahaha
[root@foundation47 shell]# ^C #无信息返回
脚本运行完之后进入安装界面,正常进行安装即可
五.快照进行虚拟机的快速安装
qemu-img create -f qcow2 -b /var/lib/libvirt/images/toto.qcow2 /var/lib/libvirt/images/node1.qcow2
-f qcow2 ##表示新创建的镜像文件的格式为 qcow2
-b /var/lib/libvirt/images/toto.qcow2 ##指定其后端镜像文件,那么这个创建的镜像文件仅记录与后端镜像文件的差异部分。后端镜像文件不会被修改。
使用新创造出来的镜像文件创建虚拟机进行操作后,所有的操作都与原来的镜像文件无关,一旦产生异常可以删除该文件,使用源文件重新进行新建。