Libvirt-Qemu-Kvm 操作手记

(持续更新~)

本文主要用于记录在操作libvirt + qemu + kvm过程中遇到的问题及原因分析。

Hugepage

让qemu使用大页可以减少tdp的size,一定程度上可以提高性能;使用大页可以用memfd或者file backend。

memfd

操作步骤如下:

  1. 在系统中reserve大页;命令参考http://t.csdnimg.cn/PPetb,例如:

    cpp 复制代码
    echo 16 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
  2. 重新挂载/dev/hugepages,使其pagesize为1g,

    cpp 复制代码
    mount -o remount,pagesize=1g /dev/hugepages
  3. 重启libvirtd,

    cpp 复制代码
    systemctl restart libvirtd
  4. 修改虚拟机xml文件如下:

    cpp 复制代码
     <memory unit='KiB'>16777216</memory>
      <currentMemory unit='KiB'>16777216</currentMemory>
      <memoryBacking>
        <hugepages/>
        <source type='memfd'/>
        <access mode='shared'/>
      </memoryBacking

启动虚拟机之后,我们会看到memfd文件;

之所以会显示deleted,是memfd创建文件的方式导致的,参考内核代码:

cpp 复制代码
proc_pid_readlink()
  -> do_proc_readlink()
	-> d_path()
	   ---
		if (unlikely(d_unlinked(path->dentry)))
			prepend(&b, " (deleted)", 11);
		else
			prepend(&b, "", 1);
	   ---

static inline int d_unlinked(const struct dentry *dentry)
{
	return d_unhashed(dentry) && !IS_ROOT(dentry);
}

SYSCALL_DEFINE2(memfd_create)
  -> hugetlb_file_setup()
	-> alloc_file_pseudo()
	   ---
		path.dentry = d_alloc_pseudo(mnt->mnt_sb, &this);
		...
		path.mnt = mntget(mnt);
		d_instantiate(path.dentry, inode);
	   ---
其并没有调用d_splice_alias()、d_add()接口,所以是unhashed的

另外,初次测试时,并没有step 2,导致内存分配失败;原因是:libvirt传给qemu的hugetlbszie是2M,而我预留的是1G;追查libvirt代码,原因在于:

cpp 复制代码
virQEMUDriverConfigNew()
---
    /* For privileged driver, try and find hugetlbfs mounts automatically.
     * Non-privileged driver requires admin to create a dir for the
     * user, chown it, and then let user configure it manually. */
    if (privileged &&
        virFileFindHugeTLBFS(&cfg->hugetlbfs, &cfg->nhugetlbfs) < 0) {
		...
    }
---

libvirt会参考系统中挂载的hugetlbfs的pagesize,以此作为参考。

相关推荐
ZLRRLZ21 小时前
【ProtoBuffer】protobuffer的安装与使用
服务器·网络
这周也會开心21 小时前
云服务器安装JDK、Tomcat、MySQL
java·服务器·tomcat
hrrrrb1 天前
【Spring Security】Spring Security 概念
java·数据库·spring
小信丶1 天前
Spring 中解决 “Could not autowire. There is more than one bean of type“ 错误
java·spring
sdgsdgdsgc1 天前
Next.js企业级应用开发:SSR、ISR与性能监控方案
开发语言·前端·javascript
哲此一生9841 天前
搭建Vue3工程(去除不必要的文件)
前端·javascript·vue.js
周杰伦_Jay1 天前
【Java虚拟机(JVM)全面解析】从原理到面试实战、JVM故障处理、类加载、内存区域、垃圾回收
java·jvm
黑云压城After1 天前
H5使用环信实现视频或语音通话
前端·javascript·vue.js
未来之窗软件服务1 天前
自己写算法(九)网页数字动画函数——东方仙盟化神期
前端·javascript·算法·仙盟创梦ide·东方仙盟·东方仙盟算法
程序员小凯1 天前
Spring Boot测试框架详解
java·spring boot·后端