技术分享
linux虚拟机管理命令/安装/恢复
2020-10-30
一 使用虚拟化的意义在于节约资源,可以减少服务器的数量而达到同样的效果。而服务器的散热是消耗能源最主要的部分,使用虚拟化减少服务器数量达到节约成本的目的。
二 虚拟机管理命令
注意:在超级用户下对虚拟机进行管理。
virt-manager #开启虚拟机管理器
virsh list #列出正在运行的虚拟机
virsh list --all #列出所有虚拟机
virsh start 虚拟机名称 #开启虚拟机
virsh shutdown 虚拟机名称 #正常关闭虚拟机 关闭速度慢 并且有关机过程
virsh destroy 虚拟机名称 #强行关闭虚拟机
virt-viewer虚拟机名称 #显示虚拟机
三 使用镜像安装虚拟机。
准备:安装之前建议关闭所有系统中已经安装的虚拟机。确保顺利安装。以及查看cpu是否打开虚拟化功能 cat /proc/cpuinfo |grep vmx
以及存在镜像文件::rhel-server-7.3-x86_64-dvd.iso
(1)进入虚拟机管理界面选择创建新的虚拟机
(2)选择创建方式为镜像创建并添加镜像文件
(3)选择内存大小及CPU核数
(4)设置硬盘的大小
(5)设置虚拟机的名称
(6)点击Finish后进入安装界面,选择直接安装
(7)对语言坏境的选择,选择英文
(8)对时区、系统语言进行修改,添加系统磁盘并划分大小
(9)完成以上步骤点击Begin Instarllation 进行安装
(10)设置登录名及密码
(11)设置完成后进行虚拟机的重启
(12)重启后再次进入虚拟机中设置证书、语言、手动设置时区、密码
(13) 修改虚拟机的硬盘和网卡为虚拟化。
模拟虚拟机的修复
虚拟机在系统中以文件的形式存在,两个文件:
/ec/libvirt/qemu/xxx.xml #该文件文虚拟机前端配置文件。
/var/lib/libvirt/images/xxx.qcow2 #该文件为虚拟机硬盘文件。
1 准备好这两个文件。
2 将新安装的虚拟机删除。
3 恢复虚拟机
(1) 使用硬盘文件进行修复
打开虚拟机管理器——选择创建虚拟机——创建方式选择已经存在的硬盘文件——选择xxx.qcow2 文件。之后和使用镜像安装虚拟机一致。
(2)一次性恢复虚拟机
virsh create xxx.xml #例如: virsh create toto.xml
[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)永久恢复虚拟机
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 #指定其后端镜像文件,那么这个创建的镜像文件仅记录与后端镜像文件的差异部分。后端镜像文件不会被修改。
使用新创造出来的镜像文件创建虚拟机进行操作后,所有的操作都与原来的镜像文件无关,一旦产生异常可以删除该文件,使用源文件重新进行新建。
1 创建快照镜像文件node1.qcow2 文件
2 使用该文件创建虚拟机
3 删除该虚拟机之后 仅仅是删除了node1.qcow2 文件。
七 使用脚本进行快速的使用快照进行虚拟机创建。
就是将创建qcow2格式的镜像文件以及使用该文件进行新建虚拟机的过程全部写入脚本文件中进行自动的执行。
[root@foundation47 ~]# cd /opt/shell/ [root@foundation47 shell]# ls kuaizhao.sh virt_install.sh [root@foundation47 shell]# vim kuaizhao.sh #编辑脚本文件 [root@foundation47 shell]# cat kuaizhao.sh #查看脚本文件内容 #!/bin/bash #运行环境 qemu-img create -f qcow2 -b \ #创建新的镜像文件 /var/lib/libvirt/images/$1.qcow2 \ #源文件 /var/lib/libvirt/images/$2.qcow2 \ #新创造出来的文件 virt-install \ --memory 1024 \ --vcpus 1 \ --disk /var/lib/libvirt/images/$2.qcow2,bus=virtio \ --name $2 \ --import \ #输入为空 --network model=virtio,bridge=br0 [root@foundation47 shell]# sh kuaizhao.sh toto lele #运行脚本使用已经存在的虚拟机toto 新建一个虚拟机lele Formatting '/var/lib/libvirt/images/lele.qcow2', fmt=qcow2 size=8589934592 backing_file='/var/lib/libvirt/images/toto.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off WARNING No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results. Starting install... Creating domain... | 0 B 00:00 (virt-viewer:9264): GSpice-WARNING **: PulseAudio context failed Connection refused (virt-viewer:9264): GSpice-WARNING **: pa_context_connect() failed: Connection refused (virt-viewer:9264): GSpice-WARNING **: Warning no automount-inhibiting implementation available Domain creation completed. [root@foundation47 shell]#
运行成功
- 标签:
-
容灾备份