目录

一、Libvirt

二、安装

三、常用操作


记录下libvirt的知识点。

一、Libvirt

libvirt是为了更方便地管理各种Hypervisor而设计的一套虚拟化库,libvirt作为中间适配层,让底层Hypervisor对上层用户空间的管理工具(virsh,virt-manager)做到完全透明,因为libvirt屏蔽了底层各种Hypervisor的细节,为上层管理工具提供了一个统一的、较稳定的接口(API)。

二、安装

在centos7.6上手动升级libvirt到5.8.0。

# 安装依赖包
yum -y install libxml2-devel readline-devel ncurses-devel libtasn1-devel gnutls-devel libattr-devel libblkid-devel augeas systemd-devel libpciaccess-devel yajl-devel sanlock-devel libpcap-devel libnl3-devel libselinux-devel dnsmasq radvd cyrus-sasl-devel libacl-devel parted-devel device-mapper-devel xfsprogs-devel librados2-devel librbd1-devel glusterfs-api-devel glusterfs-devel numactl-devel libcap-ng-devel fuse-devel netcf-devel libcurl-devel audit-libs-devel systemtap-sdt-devel nfs-utils dbus-devel scrub numad rpm-build git

# 下载源码并安装
wget https://ftp.icm.edu.pl/packages/libvirt/libvirt-5.8.0-1.fc30.src.rpm
rpm -i libvirt-5.8.0-1.fc30.src.rpm

# 生成rpm包
cd /root/rpmbuild/SPECS/
rpmbuild -ba libvirt.spec

# 安装rpm包
cd /root/rpmbuild/RPMS/{arch}/
yum -y install *.rpm

# 重启libvirtd服务
service libvirtd restart

# 关闭SELinux
setenforce 0

# 验证环境
virsh version

三、常用操作

# 创建虚机
virsh define test.xml
virsh start test

# 查看虚机列表
virsh list

# 进入虚机控制台
virsh console test

# 退出console
ctrl + ]

# qcow2扩容
qemu-img create -f qcow2 vm_add_200G.qcow2 200G
virt-resize --expand /dev/sda2 test.qcow2 vm_add_200G.qcow2
mv vm_add_200G.qcow2 test.qcow2

测试用虚机配置供参考:

<domain type = 'kvm'
	xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
	<name>test</name>
	<memory unit='GiB'>16</memory>
	<vcpu>8</vcpu>
	<os>
		<type arch = 'x86_64' machine = 'pc'>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>destroy</on_crash>
	<devices>
		<emulator>/usr/local/bin/qemu-system-x86_64</emulator>
		<disk type = 'file' device = 'disk'>
			<driver name = 'qemu' type = 'qcow2' cache = 'none'/>
			<source file = '/home/data/kvm/test.qcow2'/>
			<target dev = 'hda' bus = 'ide'/>
		</disk>
		<input type ='tablet' bus='usb'/>
		<input type = 'mouse' bus = 'ps2'/>
		<input type = 'keyboard' bus = 'ps2'/>
		<graphics type = 'vnc' port = '-1' listen = '0.0.0.0' autoport = 'yes' keymap = 'en-us'/>
		<serial type='pty'>
			<target port='0'/>
		</serial>
		<console type='pty'>
			<target type='serial' port='0'/>
		</console>
		<interface type='network'>
			<source network='default'/>
			<mac address='00:16:3e:1a:b3:4a'/>
		</interface>
	</devices>
</domain>

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐