暗无天日

=============>DarkSun的个人博客

体验libvirt虚拟机

安装虚拟化工具

sudo pacman -S qemu virt-manager virt-viewer libvirt --noconfirm
resolving dependencies...
looking for conflicting packages...

Packages (4) libvirt-4.5.0-1  qemu-2.12.0-2  virt-manager-1.5.1-1
             virt-viewer-6.0-1

Total Download Size:    0.22 MiB
Total Installed Size:  67.46 MiB
Net Upgrade Size:       1.90 MiB

:: Proceed with installation? [Y/n] 
:: Retrieving packages...
 virt-viewer-6.0-1-x...     0.0   B  0.00B/s 00:00 [----------------------]   0%
 virt-viewer-6.0-1-x...    40.4 KiB  67.4K/s 00:02 [###-------------------]  17%
 virt-viewer-6.0-1-x...   107.7 KiB   155K/s 00:00 [##########------------]  47%
 virt-viewer-6.0-1-x...   165.2 KiB   139K/s 00:00 [################------]  73%
 virt-viewer-6.0-1-x...   225.4 KiB   217K/s 00:01 [######################] 100%
(0/4) checking keys in keyring                     [----------------------]   0%
(1/4) checking keys in keyring                     [#####-----------------]  25%
(2/4) checking keys in keyring                     [###########-----------]  50%
(3/4) checking keys in keyring                     [################------]  75%
(4/4) checking keys in keyring                     [######################] 100%
(0/4) checking package integrity                   [----------------------]   0%
(1/4) checking package integrity                   [##########------------]  46%
(2/4) checking package integrity                   [#####################-]  98%
(3/4) checking package integrity                   [#####################-]  98%
(4/4) checking package integrity                   [######################] 100%
(0/4) loading package files                        [----------------------]   0%
(1/4) loading package files                        [##########------------]  46%
(2/4) loading package files                        [#####################-]  98%
(3/4) loading package files                        [#####################-]  98%
(4/4) loading package files                        [######################] 100%
(0/4) checking for file conflicts                  [----------------------]   0%
(1/4) checking for file conflicts                  [#####-----------------]  25%
(2/4) checking for file conflicts                  [###########-----------]  50%
(3/4) checking for file conflicts                  [################------]  75%
(4/4) checking for file conflicts                  [######################] 100%
(0/4) checking available disk space                [----------------------]   0%
(1/4) checking available disk space                [#####-----------------]  25%
(2/4) checking available disk space                [###########-----------]  50%
(3/4) checking available disk space                [################------]  75%
(4/4) checking available disk space                [######################] 100%
:: Processing package changes...
(1/4) reinstalling qemu                            [----------------------]   0%
(1/4) reinstalling qemu                            [----------------------]   0%
(1/4) reinstalling qemu                            [----------------------]   0%
(1/4) reinstalling qemu                            [----------------------]   0%
(1/4) reinstalling qemu                            [----------------------]   0%
(1/4) reinstalling qemu                            [----------------------]   0%
(1/4) reinstalling qemu                            [----------------------]   0%
(1/4) reinstalling qemu                            [----------------------]   0%
(1/4) reinstalling qemu                            [----------------------]   0%
(1/4) reinstalling qemu                            [#############---------]  60%
(1/4) reinstalling qemu                            [######################] 100%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [----------------------]   0%
(2/4) reinstalling libvirt                         [#######---------------]  36%
(2/4) reinstalling libvirt                         [##############--------]  67%
(2/4) reinstalling libvirt                         [####################--]  95%
(2/4) reinstalling libvirt                         [######################] 100%
>>> You may need to run 'rm -rf ~/.libvirt'
(3/4) reinstalling virt-manager                    [----------------------]   0%
(3/4) reinstalling virt-manager                    [######################] 100%
(4/4) installing virt-viewer                       [----------------------]   0%
(4/4) installing virt-viewer                       [######################] 100%
:: Running post-transaction hooks...
(1/8) Updating icon theme caches...
(2/8) Reloading system manager configuration...
(3/8) Applying kernel sysctl settings...
(4/8) Creating system user accounts...
(5/8) Reloading device manager configuration...
(6/8) Arming ConditionNeedsUpdate...
(7/8) Updating the desktop file MIME type cache...
(8/8) Updating the MIME type database...
Unknown media type in type 'all/all'
Unknown media type in type 'all/allfiles'

安装好后,将当前用户加入 libvirt 组中

sudo gpasswd -a $USER libvirt

创建虚拟机

使用 virt-install 来创建虚拟机,例如

virt-install --name=arch --ram=1024 --vcpus=2 --cdrom=/home/lujun9972/Downloads/archlinux-2018.03.01-x86_64.iso --os-type=linux --os-variant=archlinux --disk path=/home/lujun9972/arch.dsk,size=4
开始安装......
域安装仍在进行。您可以重新连接
到控制台以便完成安装进程。

这会创建并启动一个名为 arch 的虚拟机,内存为 1024M, 2 个CPU, 加载 /home/lujun9972/Downloads/archlinux-2018.03.01-x86_64.iso 作为光盘, 4G 硬盘,虚拟硬盘文件为 /home/lujun9972/arch.dsk

Virt-viewer_1533034216.png

管理虚拟机

一般通过 virsh 来管理虚拟机,常见的操作包括:

显示所有的虚拟机列表

virsh list --all
 Id    名称                         状态
----------------------------------------------------
 1     arch                           running

启动已经创建好的虚拟机

virsh start ${name}

获取虚拟机的信息

virsh dominfo ${name}
Id:             1
名称:       arch
UUID:           fac60be1-a248-440b-bd71-bec5e0379dd5
OS 类型:    hvm
状态:       running
CPU:          2
CPU 时间:   1.4s
最大内存: 1048576 KiB
使用的内存: 1048576 KiB
持久:       是
自动启动: 禁用
管理的保存: 否
安全性模式: none
安全性 DOI: 0

编辑虚拟机信息

virsh edit ${name}

设置主机启动后是否自动启动虚拟机

virsh autostart ${name}
域 arch标记为自动开始

virsh autostart --disable ${name}
域 arch取消标记为自动开始

暂停虚拟机

virsh suspend ${name}

回复暂停的虚拟机

virsh resume ${name}

关闭虚拟机

virsh shutdown ${name}
域 arch 被关闭

克隆虚拟机

关闭虚拟机后就可以通过 virt-clone 来拷贝虚拟机了

virt-clone --original ${name} --auto-clone --name ${new_name}

删除虚拟机

virsh destroy ${name}
virsh undefine ${name}

常用虚拟化管理应用工具

virt-manager
虚拟机图形化管理工具
virt-install
虚拟机配给工具
virt-clone
虚拟机镜像克隆工具
virt-image
从一个XML描述文件中构造虚拟机
virt-viewer
虚拟机图形化控制台
virsh
virsh guest域的交互式终端