TranslateProject/sources/tech/20150302 How to Manage KVM Virtual Environment using Commandline Tools in Linux.md
2015-03-24 11:52:15 +08:00

6.8 KiB
Raw Blame History

如何在Linux中用命令行工具管理KVM虚拟环境

在我们KVM系列专题的第四部分我们将会一起讨论下在命令行界面下来管理KVM环境。我们分别用virt-install和virsh命令行工具来创建并配置虚拟机和存储池用qemu-img命令行工具来创建并管理磁盘映像。

KVM Management in Linux Linux系统的KVM管理

在这篇文章里没有什么新的概念,我们只是用命令行工具重复之前所做过的事情,也没有什么前提条件,都是相同的过程,之前的文章我们都讨论过。

第一步: 配置存储池

Virsh命令行工具是一款管理virsh客户域的用户界面。virsh程序能在命令行中运行所给的命令以及它的参数。

本节中我们要用它给我们的KVM环境创建存储池。想知道关于这个工具的更多信息用以下这条命令。

# man virsh

1. 用virsh带pool-define-as的命令来定义新的存储池你需要指定名字、类型和类型参数。

本例中我们将名字取为Spool1类型为目录。默认情况下你可以提供五个参数给该类型

  • source-host
  • source-path
  • source-dev
  • source-name
  • target

对于目录类型我们需要用最后一个参数“target”来指定存储池的路径其它参数项我们可以用“-”来填充。

# virsh pool-define-as Spool1 dir - - - - "/mnt/personal-data/SPool1/"

Create New Storage Pool 创建新存储池

2. 查看环境中我们所有的存储池,用以下命令。

# virsh pool-list --all

List All Storage Pools 列出所有存储池

3. 现在是时候来构造存储池了,用以下命令来构造我们刚才定义的存储池。

# virsh pool-build Spool1

Build Storage Pool 构造存储池

4. 用virsh带pool-start的命令来激活并启动我们刚才创建并构造完成的存储池。

# virsh pool-start Spool1

Active Storage Pool 激活存储池

5. 查看环境中存储池的状态,用以下命令。

# virsh pool-list --all

Check Storage Pool Status 查看存储池状态

你会发现Spool1的状态变成了已激活。

6. 对Spool1进行配置让它每次都能被libvirtd服务自启动。

# virsh pool-autostart Spool1

Configure KVM Storage Pool 配置KVM存储池

7. 最后来看看我们新的存储池的信息吧。

# virsh pool-info Spool1

Check KVM Storage Pool Information 查看KVM存储池信息

恭喜你Spool1已经准备好待命接下来我们试着创建存储卷来使用它。

第二步: 配置存储卷/磁盘映像

现在轮到磁盘映像了using qemu-img to create new disk image from Spool1. For more details about qemy-img, use the man page.

# man qemu-img

8. We should specify the qemu-img command “create, check,….etc”, disk image format, the path of disk image you want to create and the size.

# qemu-img create -f raw /mnt/personal-data/SPool1/SVol1.img 10G

Create Storage Volume Create Storage Volume

9. By using qemu-img command info, you could get information about your new disk image.

Check Storage Volume Information Check Storage Volume Information

Warning: Never use qemu-img to modify images in use by a running virtual machine or any other process; this may destroy the image.

Now its time to create virtual machines in the next step.

第三步: 创建虚拟机

10. Now with the last and latest part, we will create virtual machines using virt-istall. The virt-install is a command line tool for creating new KVM virtual machines using the “libvirt” hypervisor management library. For more details about it, use:

# man virt-install

To create new KVM virtual machine, you need to use the following command with all the details like shown in the below.

  • Name: Virtual Machines name.

  • Disk Location: Location of disk image.

  • Graphics : How to connect to VM “Usually be SPICE”.

  • vcpu : Number of virtual CPUs.

  • ram : Amount of allocated memory in megabytes.

  • Location : Specify the installation source path.

  • Network : Specify the virtual network “Usually be vibr00 bridge”.

    virt-install --name=rhel7 --disk path=/mnt/personal-data/SPool1/SVol1.img --graphics spice --vcpu=1 --ram=1024 --location=/run/media/dos/9e6f605a-f502-4e98-826e-e6376caea288/rhel-server-7.0-x86_64-dvd.iso --network bridge=virbr0

Create New Virtual Machine Create New Virtual Machine

11. You will find also a pop-up virt-vierwer window appears to communicate with virtual machine through it.

Booting Virtual Machine Booting Virtual Machine

Installation of Virtual Machine Installation of Virtual Machine

结论

This is the latest part of our KVM tutorial, we havent covered everything of course. It a shot to scratch the KVM environment so its your turn to search and keep hands dirty using this nice resources.


via: http://www.tecmint.com/kvm-management-tools-to-manage-virtual-machines/

作者:Mohammad Dosoukey 译者:ZTinoZ 校对:校对者ID

本文由 LCTT 原创翻译,Linux中国 荣誉推出