用脚本建立虚拟机:

给大家讲解一个不容易排出来的错误,当我们在指令后面打换行符时,应该注意在换行符后面没有空格,同时换行符号与指令要有一定距离,不然会发生明显的报错,也会相应的无法执行。我们经常会忽略这个问题,切忌敲指令要用心,复制粘贴总会发生意想不到的问题!

[kiosk@foundation21 Desktop]$ su -  切换到超级用户

Password:

Last login: Sat Apr 21 13:08:27 CST 2018 on tty2

[root@foundation21 ~]# virt-manager   打开所有虚拟机界面

[root@foundation21 ~]# cd /mnt

[root@foundation21 mnt]# ls

file  mnt  vm_create.sh

[root@foundation21 mnt]# rm -fr *    删除所有文件

[root@foundation21 mnt]# vim westos.sh   编辑文件

[root@foundation21 mnt]# chmod +x /mnt/westos.sh   给一个执行权限

[root@foundation21 mnt]# /mnt/westos.sh test       调用

如下图利用脚本打开虚拟机已经完成。

基于Linux下的脚本控制虚拟机_desktop

[root@foundation21 mnt]#

Starting install...

Allocating 'test.qcow2'                                  | 9.0 GB     00:00     

Creating domain...                                       |    0 B     00:00     

 

(virt-viewer:7093): GSpice-CRITICAL **: spice_session_set_shared_dir: assertion 'dir != NULL' failed

 

(virt-viewer:7093): GSpice-WARNING **: PulseAudio context failed Connection refused

 

(virt-viewer:7093): GSpice-WARNING **: pa_context_connect() failed: Connection refused

 

(virt-viewer:7093): GSpice-WARNING **: Error calling 'org.gnome.SessionManager.Inhibit': GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.SessionManager was not provided by any .service files

我们可以看到虚拟机已经被打开!

westos.sh文件内容:

#!/bin/bash  

virt-install   \       \代表换行,安装虚拟机        

--cdrom /home/kiosk/Desktop/rhel-server-7.0-x86_64-dvd.iso \   安装源地址  

--ram 1024      \     内存

--cpus 1 \     

--disk /var/lib/libvirt/images/$1.qcow2,size=9,bus=virtio  \     硬盘位置,大小,bus硬盘类型

--name $1 \  随机名字,用传参变量

--network source=br0,model=virtio &    网络资源接口,&代表释放后台资源

基于Linux下的脚本控制虚拟机_desktop_02

用脚本直接进入虚拟机:

[kiosk@foundation21 Desktop]$ su -

Password:

Last login: Sat Apr 21 15:46:54 CST 2018 from content.example.com on pts/1

[root@foundation21 ~]# cd /mnt/

[root@foundation21 mnt]# vim vm_sanpshoot.sh

[root@foundation21 mnt]# chmod +x /mnt/vm_sanpshoot.sh   赋予执行权限

[root@foundation21 mnt]# cd /var/lib/libvirt/images/      进入硬盘目录

[root@foundation21 images]# ls                            查看我们所建立的虚拟机在硬盘位置

xfl.qcow2              rh124-desktop-vdb.qcow2  rh124-server-vdb.ovl

rh124-desktop-vda.ovl    rh124-desktop.xml        rh124-server-vdb.qcow2

rh124-desktop-vda.qcow2  rh124-server-vda.ovl     rh124-server.xml

rh124-desktop-vdb.ovl    rh124-server-vda.qcow2

基于Linux下的脚本控制虚拟机_desktop_03

[root@foundation21 images]# /mnt/vm_sanpshoot.sh xfl westos

Formatting '/var/lib/libvirt/images/westos.qcow2', fmt=qcow2 size=9663676416 backing_file='/var/lib/libvirt/images/jay-1.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off

[root@foundation21 images]#

Starting install...

Creating domain...                                       |    0 B     00:00     

 

(virt-viewer:5950): GSpice-CRITICAL **: spice_session_set_shared_dir: assertion 'dir != NULL' failed

 

(virt-viewer:5950): GSpice-WARNING **: PulseAudio context failed Connection refused

 

(virt-viewer:5950): GSpice-WARNING **: pa_context_connect() failed: Connection refused

 

(virt-viewer:5950): GSpice-WARNING **: Error calling 'org.gnome.SessionManager.Inhibit': GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.SessionManager was not provided by any .service files

如下图所示,虚拟机成功被打开:

基于Linux下的脚本控制虚拟机_xml_04

vm_sanpshoot.sh文件内容:

 #!/bin/bash

  qemu-img create -f qcow2 -b \

  /var/lib/libvirt/images/$1.qcow2 \    原来的硬盘名字

  /var/lib/libvirt/images/$2.qcow2


  virt-install \

  --name $2 \

  --cpus 1 \

  --ram 1024 \

  --disk /var/lib/libvirt/images/$2.qcow2,bus=virtio \    

  --network source=br0,model=virtio \   

  --import &      已经存在的镜像

基于Linux下的脚本控制虚拟机_xml_05

虚拟机的快照:

虚拟机坏掉之后删除照片从母盘再次进行快照(读的时候是读母盘,写的时候是照片)

[root@foundation21 mnt]# cd /var/lib/libvirt/images  切换路径

[root@foundation21 images]# ls

rh124-desktop-vda.ovl    rh124-server-vda.qcow2  xfl-1.qcow2

rh124-desktop-vda.qcow2  rh124-server-vdb.ovl    xfl-2.qcow2

rh124-desktop-vdb.ovl    rh124-server-vdb.qcow2  xfl-3.qcow2

rh124-desktop-vdb.qcow2  rh124-server.xml        xfl.qcow2

rh124-desktop.xml        test.qcow2

rh124-server-vda.ovl     westos.qcow2

[root@foundation21 images]# qemu-img create -f qcow2 -b xfl.qcow2 xyy.qcow2    生成快照

Formatting 'xyy.qcow2', fmt=qcow2 size=9663676416 backing_file='xfl.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off

[root@foundation21 images]# ls

rh124-desktop-vda.ovl    rh124-server-vda.ovl    test.qcow2    xfl.qcow2

rh124-desktop-vda.qcow2  rh124-server-vda.qcow2  westos.qcow2  xyy.qcow2

rh124-desktop-vdb.ovl    rh124-server-vdb.ovl    xfl-1.qcow2

rh124-desktop-vdb.qcow2  rh124-server-vdb.qcow2  xfl-2.qcow2

rh124-desktop.xml        rh124-server.xml        xfl-3.qcow2

基于Linux下的脚本控制虚拟机_sed_06

输入virt-manager打开虚拟机界面,点击电视图样的图标,选最后一个

基于Linux下的脚本控制虚拟机_xml_07

找到我们刚才快照的文件xyy.qcow2

基于Linux下的脚本控制虚拟机_xml_08

添加快照

基于Linux下的脚本控制虚拟机_desktop_09

默认

基于Linux下的脚本控制虚拟机_sed_10

可以更改名字

基于Linux下的脚本控制虚拟机_sed_11

如下图利用快照直接打开了虚拟机

基于Linux下的脚本控制虚拟机_xml_12

管理虚拟机命令:

[root@foundation21 images]# cd /var/lib/libvirt/images/

[root@foundation21 images]# ls

jay-1.qcow2              rh124-desktop-vdb.qcow2  rh124-server-vdb.ovl

rh124-desktop-vda.ovl    rh124-desktop.xml        rh124-server-vdb.qcow2

rh124-desktop-vda.qcow2  rh124-server-vda.ovl     rh124-server.xml

rh124-desktop-vdb.ovl    rh124-server-vda.qcow2

[root@foundation21 images]# mv jay-1.qcow2 /mnt/

[root@foundation21 images]# ls

rh124-desktop-vda.ovl    rh124-desktop.xml       rh124-server-vdb.qcow2

rh124-desktop-vda.qcow2  rh124-server-vda.ovl    rh124-server.xml

rh124-desktop-vdb.ovl    rh124-server-vda.qcow2

rh124-desktop-vdb.qcow2  rh124-server-vdb.ovl

[root@foundation21 images]# cd /etc/libvirt/qemu/

[root@foundation21 qemu]# ls

desktop.xml  jay.xml  networks  server.xml

[root@foundation21 qemu]# mv jay.xml /mnt/

mv: overwrite ‘/mnt/jay.xml’? y

[root@foundation21 qemu]# cd /mnt/

[root@foundation21 mnt]# ls

jay-1.qcow2  jay.xml  westos.sh

[root@foundation21 mnt]# mv jay-1.qcow2 /var/lib/libvirt/images/

[root@foundation21 mnt]# ls

jay.xml  westos.sh

基于Linux下的脚本控制虚拟机_xml_13

[root@foundation21 mnt]# virsh create jay.xml  一次性恢复使用force all就会消失

基于Linux下的脚本控制虚拟机_xml_14

Domain jay created from jay.xml

[root@foundation21 mnt]# virsh define jay.xml   永久恢复

Domain jay defined from jay.xml

基于Linux下的脚本控制虚拟机_desktop_15

[root@foundation21 mnt]# virsh define jay.xml

Domain jay defined from jay.xml

[root@foundation21 mnt]# virsh list --all      显示所有虚拟机

 Id    Name                           State

----------------------------------------------------

 -     desktop                        shut off

 -     jay                            shut off

 -     server                         shut off

基于Linux下的脚本控制虚拟机_xml_16