Libvirt初探和创建虚拟机

Libvirt初探和创建虚拟机

  • Libvirt初探
    • Libvirt配置文件
    • Libvirt的使用
  • 创建虚拟机
    • 使用virt-install创建虚拟机
    • 使用virsh创建虚拟机

Libvirt初探

Libvirt配置文件

libvirt相关的配置的配置文件一般存放在安装目录的 /etc/libvirt/ 目录之中,如果是默认安装路径,配置文件路径如下:

[root@localhost ~]# cd /usr/local/etc/libvirt/
[root@localhost libvirt]# ls
libvirt-admin.conf  libvirtd.conf  nwfilter  qemu.conf        secrets         virtlogd.conf
libvirt.conf        lxc.conf       qemu      qemu-lockd.conf  virtlockd.conf  virt-login-shell.conf

其中,qemu文件夹中存放网络配置文件和虚拟机的配置文件。

Libvirt的使用

libvirtd 是一个作为 libvirt 虚拟化管理系统中的服务器端的守护程序,如果要让某个节点能够用 libvirt 进行管理(无论是本地还是远程管理),都需要在这个节点上运行着 libvirtd 这个守护进程,以便让其他上层管理工具可以连接到该节点,libvirtd 负责执行其他管理工具发送它的虚拟化管理操作指令。
LIBVIRT、LIBVIRTD的配置和使用

1. 启动服务

[root@localhost ~]# service libvirtd start

2. 查看状态

[root@localhost ~]# service libvirtd status

3. 出现的错误及解决

  • 查看状态时发现服务并没有启动
[root@localhost ~]# service libvirtd status
Redirecting to /bin/systemctl status libvirtd.service
● libvirtd.service - Virtualization daemon
   Loaded: loaded (/usr/local/lib/systemd/system/libvirtd.service; disabled; vendor preset: enabled)
   //看到下面这行failed内心是有点崩溃的,服务没有正常启起来
   Active: failed (Result: start-limit) since Sat 2019-04-20 11:31:44 CST; 3h 41min ago
     Docs: man:libvirtd(8)
           https://libvirt.org
  Process: 19705 ExecStart=/usr/local/sbin/libvirtd $LIBVIRTD_ARGS (code=exited, status=1/FAILURE)
 Main PID: 19705 (code=exited, status=1/FAILURE)

Apr 20 11:31:44 localhost.VD systemd[1]: Failed to start Virtualization daemon.
Apr 20 11:31:44 localhost.VD systemd[1]: Unit libvirtd.service entered failed state.
Apr 20 11:31:44 localhost.VD systemd[1]: libvirtd.service failed.
Apr 20 11:31:44 localhost.VD systemd[1]: libvirtd.service holdoff time over, scheduling restart.
Apr 20 11:31:44 localhost.VD systemd[1]: Stopped Virtualization daemon.
Apr 20 11:31:44 localhost.VD systemd[1]: start request repeated too quickly for libvirtd.service
Apr 20 11:31:44 localhost.VD systemd[1]: Failed to start Virtualization daemon.
Apr 20 11:31:44 localhost.VD systemd[1]: Unit libvirtd.service entered failed state.
Apr 20 11:31:44 localhost.VD systemd[1]: libvirtd.service failed.

同时,重启libvirtd服务报错

[root@localhost ~]# service libvirtd restart
Redirecting to /bin/systemctl restart libvirtd.service
Job for libvirtd.service failed because the control process exited with error code. See "systemctl status libvirtd.service" and "journalctl -xe" for details.

解决1:更新或安装依赖

[root@localhost ~]# yum install librbd1-devel

上述方法没有解决我的问题。

解决2:将libvirtd服务添加到systemctl中
手动编译安装Libvirt之后利用systemctl管理libvirtd服务
根据链接提供的方法一通操作,依旧没有解决。

解决3:重启服务器

[root@localhost ~]# reboot

摩天营救电影里说的没错,“重启能解决90%的问题”,重启之后libvirtd服务的restart终于正常运行了,查看状态显示如下,为了凸显正常active的绿色,这里就放张截图了。
Libvirt初探和创建虚拟机_第1张图片
4. 进入virsh管理模式

输入virsh即可直接进入virsh模式,也可以直接virsh + 命令直接执行。

[root@localhost ~]# virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

该模式下输入help即可打印所有功能说明,在此就不列举了。

创建虚拟机

使用virt-install创建虚拟机

在另一篇博文中说的很详细,这里就不赘述了。
使用virt-install创建虚拟机

使用virsh创建虚拟机

1. 创建qcow2镜像
本文使用qemu的qemu-img命令来创建镜像文件,先看一下qemu-img的命令说明:

  • qemu-img命令说明
[root@localhost ~]# qemu-img --help
qemu-img version 3.1.0
Copyright (c) 2003-2018 Fabrice Bellard and the QEMU Project developers
usage: qemu-img [standard options] command [command options]
QEMU disk image utility

    '-h', '--help'       display this help and exit
    '-V', '--version'    output version information and exit
    '-T', '--trace'      [[enable=]][,events=][,file=]
                         specify tracing options
//......省略部分,具体看create的格式......
Command syntax:
  create [--object objectdef] [-q] [-f fmt] [-b backing_file] [-F backing_fmt] [-u] [-o options] filename [size]
//下为参数具体说明
Command parameters:
  'filename' is a disk image filename
  'objectdef' is a QEMU user creatable object definition. See the qemu(1) manual page for a description of the object properties. The most common object type is a 'secret', which is used to supply passwords and/or encryption keys.  //对象定义
  'fmt' is the disk image format. It is guessed automatically in most cases.  //磁盘镜像格式,创建时一般都要自己定义好
  'cache' is the cache mode used to write the output disk image, the valid options are: 'none', 'writeback' (default, except for convert), 'writethrough', 'directsync' and 'unsafe' (default for convert).
  'src_cache' is the cache mode used to read input disk images, the valid options are the same as for the 'cache' option.
  'size' is the disk image size in bytes. Optional suffixes 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M), 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P)  are supported. 'b' is ignored.  //磁盘镜像文件大小,各种单位都可以设置
  'output_filename' is the destination disk image filename.
  'output_fmt' is the destination format.
  'options' is a comma separated list of format specific options in a name=value format. Use -o ? for an overview of the options supported by the used format.  //查看选项
  'snapshot_param' is param used for internal snapshot, format is 'snapshot.id=[ID],snapshot.name=[NAME]', or '[ID_OR_NAME]'.
  '-c' indicates that target image must be compressed (qcow format only).
  '-u' allows unsafe backing chains. For rebasing, it is assumed that old and new backing file match exactly. The image doesn't need a working backing file before rebasing in this case (useful for renaming the backing file). For image creation, allow creating without attempting to open the backing file.  //一般不选此项
  '-h' with or without a command shows this help and lists the supported formats
  '-p' show progress of command (only certain commands).
  '-q' use Quiet mode - do not print any output (except errors).  //使用静默模式(不打印任何输出,只打印报错),一般不选择开启此项
  '-S' indicates the consecutive number of bytes (defaults to 4k) that must contain only zeros for qemu-img to create a sparse image during conversion. If the number of bytes is 0, the source will not be scanned for unallocated or zero sectors, and the destination image will always be fully allocated.
  '--output' takes the format in which the output must be done (human or json).
  '-n' skips the target volume creation (useful if the volume is created prior to running qemu-img).
  • 创建qcow2格式的磁盘镜像文件
    根据 create [–object objectdef] [-q] [-f fmt] [-b backing_file] [-F backing_fmt] [-u] [-o options] filename [size] 格式创建一个磁盘镜像文件,文件名为engine,大小为10G。
[root@localhost ~]# qemu-img create -f qcow2 engine.qcow2 10G
Formatting 'engine.qcow2', fmt=qcow2 size=10737418240 cluster_size=65536 lazy_refcounts=off refcount_bits=16

2. 安装虚拟机操作系统

  • 在libvirt配置文件夹中qemu文件夹下创建虚拟机xml配置文件,每一台虚拟机对应一个xml配置文件。
[root@localhost ~]# cd /usr/local/etc/libvirt/qemu
[root@localhost ~]# vim engine.xml

本文配置文件的具体内容如下,创建的虚拟机名称为engine,网络选择默认的虚拟网络NAT方式,图形交互选用spice。
附上一个配置文件说明的具体说明贴:通过Libvirt创建KVM虚拟机

在老版本中有单独的qemu-kvm模块存在,结合qemu一起做虚拟机工作。在后续新版本中,已经将qemu-kvm模块完全合并到qemu中去,不再有qemu-kvm的说法了。一般创建x86的虚拟机需要用到qemu-system-x86_64这个命令,并需要加上–enable-kvm来支持kvm加速。
qemu/qemu-kvm/qemu-system-x86_64/qemu-x86_64命令的区别


    engine
    8
    4
    4
    
        hvm
        
        
    
    
        
        
        
    
    
    destroy
    restart
    destroy
    
        /usr/libexec/qemu-kvm				//前面说到,此处qemu-kvm已经启用,因此肯定是找不到文件的,需要做个链接
        
            
                //此处为该虚拟机镜像文件路径
            
        
        
                //此处为用来安装操作系统的镜像路径
            
        
        
            
        
        
        
            
            
        
    

编辑好之后,为qemu-kvm做个链接

[root@localhost qemu]# ln -s /usr/local/bin/qemu-system-x86_64 /usr/libexec/qemu-kvm
  • 该方法后续出现一些问题,后续更新。^ ^

你可能感兴趣的:(虚拟化)