libvirt-qemu-用cgroup对虚拟机进行资源分割

(文章来自作者维护的社区微信公众号【虚拟化云计算】)
(目前有两个微信群《kvm虚拟化》和《openstack》,扫描二维码点击“云-交流”,进群交流提问)
    使用libvirt可以把创建的虚拟机放到cgroup划分的资源区中,从而实现所有虚拟机、部分虚拟机、单个虚拟机的资源控制,例如控制虚拟机的cpu使用情况、控制虚拟机在host上内存的使用大小等。
 
libvirt默认资源划分:
默认的libvirt使用cgroup创建一个名为machine的组:/sys/fs/cgroup/cpuset/machine/。
然后使用libvrit创建的虚拟机都在这个machine组中。
控制或改变machine组中的各个资源就可以控制虚拟机。
例如:
改变/sys/fs/cgroup/memory/machine/memory.limit_in_bytes的大小,就可以改变使用libvirt创建的所有虚拟机的实际占用host的总内存大小。
 
libvirt指定资源划分:
如果需要把部分虚拟机划分的一个资源组中,单独对这个资源组进行资源的控制,则需要手动创建资源组。
 
例如建立一个新的资源组名字为openstack.partition,步骤
1.在RESOURCE中建立目录:/sys/fs/cgroup/$RESOURCE/machine/openstack.partition/
(这里RESOURCE的取值为blkio cpu,cpuacct cpuset devices freezer memory net_cls perf_event)
 
2.根据需求修改/sys/fs/cgroup/$RESOURCE/machine/openstack.partition/中的资源,例如修改/sys/fs/cgroup/memory/machine/openstack.partition/memory.limit_in_bytes的大小。
 
3.在openstack.partition资源组中创建虚拟机。
libvirt的domain的xml:
 
<domain type='kvm' id='6'>
  ....
  <resource>
    <partition>/machine/openstack.partition</partition>
  </resource>
  ....
</domain>
4.启动虚拟机后,虚拟机的资源就收到了openstack.partition资源组的控制。
可以看到在/sys/fs/cgroup/memory/machine/openstack.partition/目录下多了此虚拟机的文件夹:instance-00000049.libvirt-qemu
 
注:
1./sys/fs/cgroup/中资源的控制是可以嵌套的,子目录的资源定制可以覆盖掉父目录的资源定制。
2.在上面例子是在ubuntu这种非systemd启动的系统中实践。在centos等systemd启动的系统中配置方式有所区别。
具体可参考libvirt文档: https://libvirt.org/cgroups.html
 
============================================================
关注微信公众号【虚拟化云计算】,阅读更多虚拟化云计算知识,纯技术干货更新不停。
 
 
 
root@ubuntu:/opt# virt-install --name kylin10 --ram 4096 --disk kylin-image.qcow2,format=raw --network network=default --graphics vnc,port=5909,listen=0.0.0.0 --noautoconsole --os-variant centos8 --virt-type qemu --cdrom=Kylin-Server-V10-SP3-2403-Release-20240426-X86_64.iso ERROR internal error: client socket is closed root@ubuntu:/opt# systemctl status libvirtd ● libvirtd.service - libvirt legacy monolithic daemon Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; preset: enabled) Active: active (running) since Sun 2025-09-14 15:29:43 CST; 5s ago TriggeredBy: ● libvirtd.socket ● libvirtd-ro.socket ● libvirtd-admin.socket Docs: man:libvirtd(8) https://libvirt.org/ Main PID: 28876 (libvirtd) Tasks: 52 (limit: 32768) Memory: 364.0M (peak: 367.5M) CPU: 684ms CGroup: /system.slice/libvirtd.service ├─ 2397 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper ├─ 2398 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper ├─ 6570 /usr/local/bin/qemu-system-xtensa -S -no-user-config -nodefaults -nographic -machine none,accel=kvm:tcg -qmp unix:/var/lib/libvirt/qemu/qmp-5XLSC3/qmp.monitor,server> ├─ 7752 /usr/local/bin/qemu-system-xtensa -S -no-user-config -nodefaults -nographic -machine none,accel=kvm:tcg -qmp unix:/var/lib/libvirt/qemu/qmp-2SRUC3/qmp.monitor,server> ├─14039 /usr/local/bin/qemu-system-xtensa -S -no-user-config -nodefaults -nographic -machine none,accel=kvm:tcg -qmp unix:/var/lib/libvirt/qemu/qmp-VZMNC3/qmp.monitor,server> ├─14174 /usr/local/bin/qemu-system-xtensa -S -no-user-config -nodefaults -nographic -machine none,accel=kvm:tcg -qmp unix:/var/lib/libvirt/qemu/qmp-2WZXC3/qmp.monitor,server> ├─14720 /usr/local/bin/qemu-system-xtensa -S -no-user-config -nodefaults -nographic -machine none,accel=kvm:tcg -qmp unix:/var/lib/libvirt/qemu/qmp-F6U6C3/qmp.monitor,server> ├─14942 /usr/local/bin/qemu-system-xtensa -S -no-user-config -nodefaults -nographic -machine none,accel=kvm:tcg -qmp unix:/var/lib/libvirt/qemu/qmp-MYCQC3/qmp.monitor,server> ├─17218 /usr/local/bin/qemu-system-xtensa -S -no-user-config -nodefaults -nographic -machine none,accel=kvm:tcg -qmp unix:/var/lib/libvirt/qemu/qmp-VBRLC3/qmp.monitor,server> ├─28493 /usr/local/bin/qemu-system-xtensa -S -no-user-config -nodefaults -nographic -machine none,accel=kvm:tcg -qmp unix:/var/lib/libvirt/qemu/qmp-05ZQC3/qmp.monitor,server> ├─28633 /usr/local/bin/qemu-system-xtensa -S -no-user-config -nodefaults -nographic -machine none,accel=kvm:tcg -qmp unix:/var/lib/libvirt/qemu/qmp-DSHSC3/qmp.monitor,server> ├─28871 /usr/local/bin/qemu-system-xtensa -S -no-user-config -nodefaults -nographic -machine none,accel=kvm:tcg -qmp unix:/var/lib/libvirt/qemu/qmp-N1AWC3/qmp.monitor,server> └─28876 /usr/sbin/libvirtd --timeout 120 Sep 14 15:29:43 ubuntu systemd[1]: libvirtd.service: This usually indicates unclean termination of a previous run, or service implementation deficiencies. Sep 14 15:29:43 ubuntu systemd[1]: libvirtd.service: Found left-over process 28633 (qemu-system-xte) in control group while starting unit. Ignoring. Sep 14 15:29:43 ubuntu systemd[1]: libvirtd.service: This usually indicates unclean termination of a previous run, or service implementation deficiencies. Sep 14 15:29:43 ubuntu systemd[1]: libvirtd.service: Found left-over process 28871 (qemu-system-xte) in control group while starting unit. Ignoring. Sep 14 15:29:43 ubuntu systemd[1]: libvirtd.service: This usually indicates unclean termination of a previous run, or service implementation deficiencies. Sep 14 15:29:43 ubuntu systemd[1]: Starting libvirtd.service - libvirt legacy monolithic daemon... Sep 14 15:29:43 ubuntu systemd[1]: Started libvirtd.service - libvirt legacy monolithic daemon. Sep 14 15:29:44 ubuntu dnsmasq[2397]: read /etc/hosts - 8 names Sep 14 15:29:44 ubuntu dnsmasq[2397]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 names Sep 14 15:29:44 ubuntu dnsmasq-dhcp[2397]: read /var/lib/libvirt/dnsmasq/default.hostsfile
最新发布
09-15
### 解决 'ERROR internal error: client socket is closed' 问题 - **检查 libvirtd 服务状态**:此错误可能是由于 libvirtd 服务未正常运行导致的。需确保 libvirtd 服务已启动且没有报错。可使用以下命令启动和检查服务状态: ```bash systemctl start libvirtd.service systemctl status libvirtd.service ``` - **检查网络连接**:该错误也可能是网络连接问题造成的。要确保主机网络正常,虚拟机相关的网络配置无误。可以使用 `ping` 命令测试网络连通性: ```bash ping 8.8.8.8 ``` - **检查权限**:确保当前用户有足够的权限运行 `virt-install` 命令。通常需要使用 `root` 用户或者具有相关权限的用户来执行该命令。 - **检查资源限制**:系统资源不足(如内存、磁盘空间等)可能会导致该错误。可使用以下命令检查系统资源使用情况: ```bash free -h # 检查内存使用情况 df -h # 检查磁盘空间使用情况 ``` ### 解决 'usually indicates unclean termination of a previous run, or service implementation deficiencies' 问题 - **清理残留文件**:该提示通常意味着上一次服务异常终止,残留了一些文件。可以尝试清理 `/var/run/libvirt` 目录下的残留文件: ```bash rm -rf /var/run/libvirt/* ``` 然后重新启动 `libvirtd` 服务: ```bash systemctl start libvirtd.service ``` - **检查日志文件**:查看 `/var/log/libvirt/libvirtd.log` 文件,从中获取更多详细的错误信息,根据日志中的提示进行相应的处理。 ```bash cat /var/log/libvirt/libvirtd.log ``` - **更新相关软件包**:确保 `libvirt`、`qemu-kvm` 等相关软件包是最新版本。可以使用以下命令进行更新: ```bash yum update libvirt qemu-kvm virt-install ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值