Linux下 如何通过命令行使用现有的镜像创建、启动kvm虚拟机

这里假定已经创建好了相应的镜像:

eg:我这里制作的镜像名称为zu1-centos7.img

# ls
zu1-centos7.img

1、拷贝这个镜像到某一个目录

cp zu1-centos7.img /data2/

2、编写镜像的配置文件,当然一般情况是从其他镜像的配置文件拷贝一份,重新命名在修改

# cd /etc/libvirt/qemu   进入到镜像配置文件目录
# cp test2.xml test3.xml test2.xml是其他镜像存在的配置文件,test3.xml是当前准备启动的镜像的配置文件

test2.xml配置文件大体如下:

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh edit test2
or other application using the libvirt API.
--> <domain type='kvm'>
<name>test2</name> ---虚拟机名字,记得修改,随便起什么名字都行,只要不跟其他虚拟机名字一样就行
<uuid>92047c95-c5b8-4e4d-916c-fb2218ca055f</uuid> --uuid 记得修改,一般来说,把最好的四位数字重新修改一下,例如我把最后四位改成0001,具体效果看下个test3.xml文件
<memory unit='KiB'></memory> ---当前虚拟机使用内存,看自己情况是否需要修改
<currentMemory unit='KiB'></currentMemory> ---当前虚拟机可以使用的最大内存,使用内存不得超过这个内存,当使用内存不足的时候,可以在虚拟机里面动态调整内存。
<vcpu placement='static'></vcpu>
<os>
<type arch='x86_64' machine='rhel6.6.0'>hvm</type> ---架构,一般不用修改
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/> ---driver类型,需要跟镜像的文件格式一致。以前老版本默认是raw类型,现在默认是qcow2类型。查看镜像的文件格式在该配置文件的下面:
<source file='/data1/iso/zu1-centos7.img'/> --- 镜像文件路径
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</disk>
<disk type='block' device='cdrom'>
<driver name='qemu' type='qcow2'/>
<target dev='hdc' bus='ide'/>
<readonly/>
<address type='drive' controller='' bus='' target='' unit=''/>
</disk>
<controller type='ide' index=''>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='usb' index='' model='piix3-uhci'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='' model='pci-root'/>
<interface type='bridge'>
<mac address='52:54:00:5a:07:de'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> ---这里的slot号记得修改 把最后两位改成其他的就行,但不要跟其他的虚拟机配置的值一样,同时,这个值不超过 0x0f
</interface>
<serial type='pty'>
<target type='isa-serial' port=''>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port=''/>
</console>
<input type='tablet' bus='usb'>
<address type='usb' bus='' port=''/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</memballoon>
</devices>
</domain>

3、查看镜像的driver类型

# qemu-img info /data1/zu1-centos7.img
image: /data1/virtcentos/pool/zu1-centos7.img
file format: qcow2 ----这里就是镜像的文件格式
virtual size: 20G ( bytes)
disk size: 18G
cluster_size:
Format specific information:
compat: 1.1
lazy refcounts: true

4、修改test3.xml文件,如下:

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh edit test2
or other application using the libvirt API.
--> <domain type='kvm'>
<name>yjt</name> ---这里改成了yjt
<uuid>92047c95-c5b8-4e4d-916c-fb2218ca0001</uuid>
<memory unit='KiB'></memory>
<currentMemory unit='KiB'></currentMemory>
<vcpu placement='static'></vcpu>
<os>
<type arch='x86_64' machine='rhel6.6.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/data2/zu1-centos7.img'/> ---这里修改了镜像的路径
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</disk>
<disk type='block' device='cdrom'>
<driver name='qemu' type='qcow2'/>
<target dev='hdc' bus='ide'/>
<readonly/>
<address type='drive' controller='' bus='' target='' unit=''/>
</disk>
<controller type='ide' index=''>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='usb' index='' model='piix3-uhci'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='' model='pci-root'/>
<interface type='bridge'>
<mac address='52:54:00:5a:07:de'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x10' function='0x0'/> 这里改成了0x10,注意,该值不得超过0x0f
</interface>
<serial type='pty'>
<target type='isa-serial' port=''>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port=''/>
</console>
<input type='tablet' bus='usb'>
<address type='usb' bus='' port=''/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</memballoon>
</devices>
</domain>

5、定义虚拟机

# virsh define test3.xml  ---定义虚拟机跟上对应的文件即可

6、启动虚拟机

virsh start yjt   ---yjt这个名字在配置文件定义的,也就是虚拟机的名字。

到这里就ok了。

如果虚拟机不要用了,可以使用如下方法销毁

7、停止虚拟机

# virsh shutdown yjt   ---正常停止
# virsh destroy yjt ---强制停止

8、取消虚拟机的定义,也就是删除

# virsh undefine yjt   ---默认会删除/etc/libvirt/qemu下的test3.xml文件

kvm安装:

https://blog.51cto.com/zero01/2083896

kvm 学习(二)镜像的更多相关文章

  1. Docker入门之二镜像

    Docker大部分的操作都是围绕三大核心概念:镜像.容器.仓库.学Docker首先得了解这几个词.这几个词可能平时也会有涉及,但Docker中可能不是同样得概念. 一.三大核心概念 镜像:可能在安装软 ...

  2. emberjs学习二(ember-data和localstorage_adapter)

    emberjs学习二(ember-data和localstorage_adapter) 准备工作 首先我们加入ember-data和ember-localstorage-adapter两个依赖项,使用 ...

  3. ReactJS入门学习二

    ReactJS入门学习二 阅读目录 React的背景和基本原理 理解React.render() 什么是JSX? 为什么要使用JSX? JSX的语法 如何在JSX中如何使用事件 如何在JSX中如何使用 ...

  4. TweenMax动画库学习(二)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  5. Hbase深入学习(二) 安装hbase

    Hbase深入学习(二) 安装hbase This guidedescribes setup of a standalone hbase instance that uses the local fi ...

  6. Struts2框架学习(二) Action

    Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...

  7. Python学习二:词典基础详解

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...

  8. Quartz学习--二 Hello Quartz! 和源码分析

    Quartz学习--二  Hello Quartz! 和源码分析 三.  Hello Quartz! 我会跟着 第一章 6.2 的图来 进行同步代码编写 简单入门示例: 创建一个新的java普通工程 ...

  9. SpringCloud学习(二):微服务入门实战项目搭建

    一.开始使用Spring Cloud实战微服务 1.SpringCloud是什么? 云计算的解决方案?不是 SpringCloud是一个在SpringBoot的基础上构建的一个快速构建分布式系统的工具 ...

  10. DjangoRestFramework学习二之序列化组件、视图组件 serializer modelserializer

      DjangoRestFramework学习二之序列化组件.视图组件   本节目录 一 序列化组件 二 视图组件 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 序列化组 ...

随机推荐

  1. 如何编写snort的检测规则

    如何编写snort的检测规则 2013年09月08日 ⁄ 综合 ⁄ 共 16976字 前言 snort是一个强大的轻量级的网络入侵检测系统.它具有实时数据流量分析和日志IP网络数据包的能力,能够进行协 ...

  2. 四、eureka服务端同步注册操作

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 在eureka服务端注册服务一文中,我们提到register方法做了两件事 1)注册服务 ...

  3. PHP原生EXCEL导出带样式无插件无乱码实现

    PHP原生EXCEL导出 经测试 带样式 无插件 无乱码,不需要引入任何插件,不需要修改任何编码 (使用时只需要修改引入php数据库配置文件.修改thead tbody中的数据即可.根据自己的需要去接 ...

  4. How to change SAPABAP1 schema password In HANA

    Symptom How to change SAPABAP1 schema password Environment HANA 1.x HANA 2.x Resolution Shutdown the ...

  5. [#Linux] CentOS 7 安装微信详细过程

    微信安装 微信安装过程如下: 1,下载最新版本tar.gz压缩包 wget https://github.com/geeeeeeeeek/electronic-wechat/releases/down ...

  6. 详解介绍Selenium常用API的使用--Java语言(完整版)

    参考:http://www.testclass.net/selenium_java/ 一共分为二十个部分:环境安装之Java.环境安装之IntelliJ IDEA.环境安装之selenium.sele ...

  7. Linux学习笔记之二

    vim编辑器 :三种工作模式 vim /tmp/xueying.txt 命令模式 a.i.o/esc     \  :wq  保存并退出 /              \ 输入模式           ...

  8. Go 性能分析之案例一

    思考 相信大家在实际的项目开发中会遇到这么一个事,有的程序员写的代码不仅bug少,而且性能高:而有的程序员写的代码能否流畅的跑起来,都是一个很大问题.而我们今天要讨论的就是一个关于性能优化的案例分析. ...

  9. jmap与jstat工具实战分析

    在上一节[https://www.cnblogs.com/webor2006/p/10662363.html]最后其实是抛出了infoq关于元空间介绍的文章中所涉及到JDK自带的一些工具的使用,这次咱 ...

  10. 回归分析特征选择(包括Stepwise算法) python 实现

    # -*- coding: utf-8 -*-"""Created on Sat Aug 18 16:23:17 2018 @author: acadsoc"& ...