[PATCH v2 00/11] Another round of qemu:///embed fixes

v2 of: https://www.redhat.com/archives/libvir-list/2020-March/msg00992.html Basically, a lot of patches from v1 was ACKed, but there were some suggestions which I put into separate commits and now am resending as v2. diff to v1: - Renamed virDomainDriverHashRoot() to virDomainDriverGenerateRootHash() - Generate hashed path only if the base (prefix) would point outside of the root dir - Use "qemu" instead of "QEMU" when generating hashed path - New patches 2/11, 7/11 Michal Prívozník (11): tests: Fix virQEMUDriverConfigNew() calling with respect to @root qemu: Drop two layers of nesting of memoryBackingDir conf: Move virDomainGenerateMachineName to hypervisor/ virDomainDriverGenerateMachineName: Factor out embed path hashing qemu: Introduce virQEMUDriverGetEmbedRoot qemuDomainGetMachineName: Access embeddedRoot from driver rather than cfg qemu: use virQEMUDriverGetEmbedRoot() to access embeddedRoot Revert "qemu_conf: Track embed root dir" qemu: Make hugepages path generation embed driver aware qemu: Make memory path generation embed driver aware qemu: Make auto dump path generation embed driver aware src/conf/domain_conf.c | 72 --------------- src/conf/domain_conf.h | 7 -- src/hypervisor/domain_driver.c | 88 +++++++++++++++++++ src/hypervisor/domain_driver.h | 11 +++ src/libvirt_private.syms | 3 +- src/lxc/lxc_domain.c | 3 +- src/qemu/qemu_command.c | 19 ++-- src/qemu/qemu_conf.c | 69 +++++++++------ src/qemu/qemu_conf.h | 23 +++-- src/qemu/qemu_domain.c | 9 +- src/qemu/qemu_driver.c | 26 +++--- src/qemu/qemu_process.c | 9 +- tests/domaincapstest.c | 2 +- .../qemuxml2argvdata/cpu-numa-memshared.args | 8 +- .../fd-memory-no-numa-topology.args | 2 +- .../fd-memory-numa-topology.args | 4 +- .../fd-memory-numa-topology2.args | 8 +- .../fd-memory-numa-topology3.args | 12 +-- .../hugepages-memaccess2.args | 12 +-- .../qemuxml2argvdata/pages-dimm-discard.args | 4 +- ...vhost-user-fs-fd-memory.x86_64-latest.args | 2 +- tests/testutilsqemu.c | 2 +- tests/virsystemdtest.c | 5 +- 23 files changed, 217 insertions(+), 183 deletions(-) -- 2.24.1

The virQEMUDriverConfigNew() accepts path to root directory for embed mode as an argument. If the argument is not NULL it uses the passed value as prefix for some internal paths (e.g. cfg->libDir). If it is NULL though, it looks if the other argument, @privileged is true or false and generates internal paths accordingly. But when calling the function from the test suite, instead of passing NULL for @root, an empty string is passed. Fortunately, this doesn't create a problem because in both problematic cases the generated paths are "fixed" to point somewhere into build dir or the code which is tested doesn't access them. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- tests/domaincapstest.c | 2 +- tests/testutilsqemu.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c index fb803eaa47..c3a9f4ef91 100644 --- a/tests/domaincapstest.c +++ b/tests/domaincapstest.c @@ -369,7 +369,7 @@ mymain(void) #endif #if WITH_QEMU - virQEMUDriverConfigPtr cfg = virQEMUDriverConfigNew(false, ""); + virQEMUDriverConfigPtr cfg = virQEMUDriverConfigNew(false, NULL); if (!cfg) return EXIT_FAILURE; diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index f3b4e2b3b2..cb68ac0488 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -390,7 +390,7 @@ int qemuTestDriverInit(virQEMUDriver *driver) return -1; driver->hostarch = virArchFromHost(); - driver->config = virQEMUDriverConfigNew(false, ""); + driver->config = virQEMUDriverConfigNew(false, NULL); if (!driver->config) goto error; -- 2.24.1

On Thu, Mar 26, 2020 at 04:15:05PM +0100, Michal Privoznik wrote:
The virQEMUDriverConfigNew() accepts path to root directory for embed mode as an argument. If the argument is not NULL it uses the passed value as prefix for some internal paths (e.g. cfg->libDir). If it is NULL though, it looks if the other argument, @privileged is true or false and generates internal paths accordingly. But when calling the function from the test suite, instead of passing NULL for @root, an empty string is passed. Fortunately, this doesn't create a problem because in both problematic cases the generated paths are "fixed" to point somewhere into build dir or the code which is tested doesn't access them.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- tests/domaincapstest.c | 2 +- tests/testutilsqemu.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Initially introduced in v3.10.0-rc1~172. When generating a path for memory-backend-file or -mem-path, qemu driver will use the following pattern: $memoryBackingDir/libvirt/qemu/$id-$shortName where $memoryBackingDir defaults to /var/lib/libvirt/qemu/ram but can be overridden in qemu.conf. Anyway, the "/libvirt/qemu/" part looks redundant, because it's already contained in the default, or creates unnecessary nesting if overridden in qemu.conf. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/qemu/qemu_conf.c | 12 +----------- src/qemu/qemu_conf.h | 2 -- src/qemu/qemu_driver.c | 12 +----------- tests/qemuxml2argvdata/cpu-numa-memshared.args | 8 ++++---- .../qemuxml2argvdata/fd-memory-no-numa-topology.args | 2 +- tests/qemuxml2argvdata/fd-memory-numa-topology.args | 4 ++-- tests/qemuxml2argvdata/fd-memory-numa-topology2.args | 8 ++++---- tests/qemuxml2argvdata/fd-memory-numa-topology3.args | 12 ++++++------ tests/qemuxml2argvdata/hugepages-memaccess2.args | 12 ++++++------ tests/qemuxml2argvdata/pages-dimm-discard.args | 4 ++-- .../vhost-user-fs-fd-memory.x86_64-latest.args | 2 +- 11 files changed, 28 insertions(+), 50 deletions(-) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 15837cece4..9786e19f8f 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -1951,27 +1951,17 @@ qemuGetDomainHupageMemPath(const virDomainDef *def, } -void -qemuGetMemoryBackingBasePath(virQEMUDriverConfigPtr cfg, - char **path) -{ - *path = g_strdup_printf("%s/libvirt/qemu", cfg->memoryBackingDir); -} - - int qemuGetMemoryBackingDomainPath(const virDomainDef *def, virQEMUDriverConfigPtr cfg, char **path) { g_autofree char *shortName = NULL; - g_autofree char *base = NULL; if (!(shortName = virDomainDefGetShortName(def))) return -1; - qemuGetMemoryBackingBasePath(cfg, &base); - *path = g_strdup_printf("%s/%s", base, shortName); + *path = g_strdup_printf("%s/%s", cfg->memoryBackingDir, shortName); return 0; } diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index 14f9b9e81e..c8d3e439e0 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -398,8 +398,6 @@ int qemuGetDomainHupageMemPath(const virDomainDef *def, unsigned long long pagesize, char **memPath); -void qemuGetMemoryBackingBasePath(virQEMUDriverConfigPtr cfg, - char **path); int qemuGetMemoryBackingDomainPath(const virDomainDef *def, virQEMUDriverConfigPtr cfg, char **path); diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 570dc059e9..5cbf2f2478 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -640,7 +640,6 @@ qemuStateInitialize(bool privileged, virQEMUDriverConfigPtr cfg; uid_t run_uid = -1; gid_t run_gid = -1; - g_autofree char *memoryBackingPath = NULL; bool autostart = true; size_t i; const char *defsecmodel = NULL; @@ -935,17 +934,8 @@ qemuStateInitialize(bool privileged, goto error; } - qemuGetMemoryBackingBasePath(cfg, &memoryBackingPath); - - if (virFileMakePath(memoryBackingPath) < 0) { - virReportSystemError(errno, - _("unable to create memory backing path %s"), - memoryBackingPath); - goto error; - } - if (privileged && - virFileUpdatePerm(memoryBackingPath, + virFileUpdatePerm(cfg->memoryBackingDir, 0, S_IXGRP | S_IXOTH) < 0) goto error; diff --git a/tests/qemuxml2argvdata/cpu-numa-memshared.args b/tests/qemuxml2argvdata/cpu-numa-memshared.args index 752eed8d13..8e214189db 100644 --- a/tests/qemuxml2argvdata/cpu-numa-memshared.args +++ b/tests/qemuxml2argvdata/cpu-numa-memshared.args @@ -15,12 +15,12 @@ QEMU_AUDIO_DRV=none \ -realtime mlock=off \ -smp 16,sockets=2,cores=4,threads=2 \ -object memory-backend-file,id=ram-node0,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-QEMUGuest1/ram-node0,\ -share=yes,size=112197632 \ +mem-path=/var/lib/libvirt/qemu/ram/-1-QEMUGuest1/ram-node0,share=yes,\ +size=112197632 \ -numa node,nodeid=0,cpus=0-7,memdev=ram-node0 \ -object memory-backend-file,id=ram-node1,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-QEMUGuest1/ram-node1,\ -share=no,size=112197632 \ +mem-path=/var/lib/libvirt/qemu/ram/-1-QEMUGuest1/ram-node1,share=no,\ +size=112197632 \ -numa node,nodeid=1,cpus=8-15,memdev=ram-node1 \ -uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \ -display none \ diff --git a/tests/qemuxml2argvdata/fd-memory-no-numa-topology.args b/tests/qemuxml2argvdata/fd-memory-no-numa-topology.args index d23c575553..dec35cc10a 100644 --- a/tests/qemuxml2argvdata/fd-memory-no-numa-topology.args +++ b/tests/qemuxml2argvdata/fd-memory-no-numa-topology.args @@ -13,7 +13,7 @@ QEMU_AUDIO_DRV=none \ -machine pc-i440fx-2.3,accel=kvm,usb=off,dump-guest-core=off \ -m 14336 \ -mem-prealloc \ --mem-path /var/lib/libvirt/qemu/ram/libvirt/qemu/-1-instance-00000092/ram \ +-mem-path /var/lib/libvirt/qemu/ram/-1-instance-00000092/ram \ -realtime mlock=off \ -smp 8,sockets=8,cores=1,threads=1 \ -uuid 126f2720-6f8e-45ab-a886-ec9277079a67 \ diff --git a/tests/qemuxml2argvdata/fd-memory-numa-topology.args b/tests/qemuxml2argvdata/fd-memory-numa-topology.args index 4fbbc8185b..2d3e90ff7a 100644 --- a/tests/qemuxml2argvdata/fd-memory-numa-topology.args +++ b/tests/qemuxml2argvdata/fd-memory-numa-topology.args @@ -16,8 +16,8 @@ QEMU_AUDIO_DRV=none \ -realtime mlock=off \ -smp 8,sockets=1,cores=8,threads=1 \ -object memory-backend-file,id=ram-node0,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-instance-00000092/ram-node0,\ -share=yes,size=15032385536 \ +mem-path=/var/lib/libvirt/qemu/ram/-1-instance-00000092/ram-node0,share=yes,\ +size=15032385536 \ -numa node,nodeid=0,cpus=0-7,memdev=ram-node0 \ -uuid 126f2720-6f8e-45ab-a886-ec9277079a67 \ -display none \ diff --git a/tests/qemuxml2argvdata/fd-memory-numa-topology2.args b/tests/qemuxml2argvdata/fd-memory-numa-topology2.args index 1eeeaec0ce..6b1695feb1 100644 --- a/tests/qemuxml2argvdata/fd-memory-numa-topology2.args +++ b/tests/qemuxml2argvdata/fd-memory-numa-topology2.args @@ -16,12 +16,12 @@ QEMU_AUDIO_DRV=none \ -realtime mlock=off \ -smp 20,sockets=1,cores=8,threads=1 \ -object memory-backend-file,id=ram-node0,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-instance-00000092/ram-node0,\ -share=no,size=15032385536 \ +mem-path=/var/lib/libvirt/qemu/ram/-1-instance-00000092/ram-node0,share=no,\ +size=15032385536 \ -numa node,nodeid=0,cpus=0-7,memdev=ram-node0 \ -object memory-backend-file,id=ram-node1,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-instance-00000092/ram-node1,\ -share=yes,size=15032385536 \ +mem-path=/var/lib/libvirt/qemu/ram/-1-instance-00000092/ram-node1,share=yes,\ +size=15032385536 \ -numa node,nodeid=1,cpus=8-15,memdev=ram-node1 \ -uuid 126f2720-6f8e-45ab-a886-ec9277079a67 \ -display none \ diff --git a/tests/qemuxml2argvdata/fd-memory-numa-topology3.args b/tests/qemuxml2argvdata/fd-memory-numa-topology3.args index d75b67916c..205d14a7db 100644 --- a/tests/qemuxml2argvdata/fd-memory-numa-topology3.args +++ b/tests/qemuxml2argvdata/fd-memory-numa-topology3.args @@ -16,16 +16,16 @@ QEMU_AUDIO_DRV=none \ -realtime mlock=off \ -smp 32,sockets=1,cores=24,threads=1 \ -object memory-backend-file,id=ram-node0,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-instance-00000092/ram-node0,\ -share=yes,size=15032385536 \ +mem-path=/var/lib/libvirt/qemu/ram/-1-instance-00000092/ram-node0,share=yes,\ +size=15032385536 \ -numa node,nodeid=0,cpus=0-1,memdev=ram-node0 \ -object memory-backend-file,id=ram-node1,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-instance-00000092/ram-node1,\ -share=yes,size=15032385536 \ +mem-path=/var/lib/libvirt/qemu/ram/-1-instance-00000092/ram-node1,share=yes,\ +size=15032385536 \ -numa node,nodeid=1,cpus=2-3,memdev=ram-node1 \ -object memory-backend-file,id=ram-node2,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-instance-00000092/ram-node2,\ -share=no,size=15032385536 \ +mem-path=/var/lib/libvirt/qemu/ram/-1-instance-00000092/ram-node2,share=no,\ +size=15032385536 \ -numa node,nodeid=2,cpus=4-5,memdev=ram-node2 \ -uuid 126f2720-6f8e-45ab-a886-ec9277079a67 \ -display none \ diff --git a/tests/qemuxml2argvdata/hugepages-memaccess2.args b/tests/qemuxml2argvdata/hugepages-memaccess2.args index 654baebf9f..c1560e63c3 100644 --- a/tests/qemuxml2argvdata/hugepages-memaccess2.args +++ b/tests/qemuxml2argvdata/hugepages-memaccess2.args @@ -15,20 +15,20 @@ QEMU_AUDIO_DRV=none \ -realtime mlock=off \ -smp 4,sockets=4,cores=1,threads=1 \ -object memory-backend-file,id=ram-node0,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-QEMUGuest1/ram-node0,\ -share=no,size=1073741824,host-nodes=0-3,policy=bind \ +mem-path=/var/lib/libvirt/qemu/ram/-1-QEMUGuest1/ram-node0,share=no,\ +size=1073741824,host-nodes=0-3,policy=bind \ -numa node,nodeid=0,cpus=0,memdev=ram-node0 \ -object memory-backend-file,id=ram-node1,prealloc=yes,\ mem-path=/dev/hugepages2M/libvirt/qemu/-1-QEMUGuest1,share=yes,size=1073741824,\ host-nodes=0-3,policy=bind \ -numa node,nodeid=1,cpus=1,memdev=ram-node1 \ -object memory-backend-file,id=ram-node2,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-QEMUGuest1/ram-node2,\ -share=no,size=1073741824,host-nodes=0-3,policy=bind \ +mem-path=/var/lib/libvirt/qemu/ram/-1-QEMUGuest1/ram-node2,share=no,\ +size=1073741824,host-nodes=0-3,policy=bind \ -numa node,nodeid=2,cpus=2,memdev=ram-node2 \ -object memory-backend-file,id=ram-node3,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-QEMUGuest1/ram-node3,\ -share=no,size=1073741824,host-nodes=3,policy=bind \ +mem-path=/var/lib/libvirt/qemu/ram/-1-QEMUGuest1/ram-node3,share=no,\ +size=1073741824,host-nodes=3,policy=bind \ -numa node,nodeid=3,cpus=3,memdev=ram-node3 \ -object memory-backend-file,id=memdimm0,prealloc=yes,\ mem-path=/dev/hugepages2M/libvirt/qemu/-1-QEMUGuest1,share=yes,size=536870912,\ diff --git a/tests/qemuxml2argvdata/pages-dimm-discard.args b/tests/qemuxml2argvdata/pages-dimm-discard.args index fefa205597..96e9ffdec3 100644 --- a/tests/qemuxml2argvdata/pages-dimm-discard.args +++ b/tests/qemuxml2argvdata/pages-dimm-discard.args @@ -20,8 +20,8 @@ mem-path=/dev/hugepages1G/libvirt/qemu/-1-fedora,size=1073741824,\ host-nodes=1-3,policy=bind \ -device pc-dimm,node=0,memdev=memdimm0,id=dimm0,slot=0 \ -object memory-backend-file,id=memdimm1,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-fedora/dimm1,\ -discard-data=yes,share=no,size=536870912 \ +mem-path=/var/lib/libvirt/qemu/ram/-1-fedora/dimm1,discard-data=yes,share=no,\ +size=536870912 \ -device pc-dimm,node=0,memdev=memdimm1,id=dimm1,slot=1 \ -uuid 63840878-0deb-4095-97e6-fc444d9bc9fa \ -display none \ diff --git a/tests/qemuxml2argvdata/vhost-user-fs-fd-memory.x86_64-latest.args b/tests/qemuxml2argvdata/vhost-user-fs-fd-memory.x86_64-latest.args index a7df45a7f0..dd5f68abc5 100644 --- a/tests/qemuxml2argvdata/vhost-user-fs-fd-memory.x86_64-latest.args +++ b/tests/qemuxml2argvdata/vhost-user-fs-fd-memory.x86_64-latest.args @@ -18,7 +18,7 @@ file=/tmp/lib/domain--1-guest/master-key.aes \ -overcommit mem-lock=off \ -smp 2,sockets=2,cores=1,threads=1 \ -object memory-backend-file,id=ram-node0,\ -mem-path=/var/lib/libvirt/qemu/ram/libvirt/qemu/-1-guest/ram-node0,share=yes,\ +mem-path=/var/lib/libvirt/qemu/ram/-1-guest/ram-node0,share=yes,\ size=15032385536 \ -numa node,nodeid=0,cpus=0-1,memdev=ram-node0 \ -uuid 126f2720-6f8e-45ab-a886-ec9277079a67 \ -- 2.24.1

On Thu, Mar 26, 2020 at 04:15:06PM +0100, Michal Privoznik wrote:
Initially introduced in v3.10.0-rc1~172.
When generating a path for memory-backend-file or -mem-path, qemu driver will use the following pattern:
$memoryBackingDir/libvirt/qemu/$id-$shortName
where $memoryBackingDir defaults to /var/lib/libvirt/qemu/ram but can be overridden in qemu.conf. Anyway, the "/libvirt/qemu/" part looks redundant, because it's already contained in the default, or creates unnecessary nesting if overridden in qemu.conf.
I think this was copied from our earlier huge pages code which used /dev/hugepages, and then added "/libvirt/qemu" to it. Now we're stripping off "/libvirt/qemu" though, we're liable to a filename clashes if someone edits qemu.conf to point to /dev/shm. IOW, even though "/libvirt/qemu" is redundant when using our default path, I think we need to keep it to avoid clashing with custom paths. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On 30. 3. 2020 13:09, Daniel P. Berrangé wrote:
On Thu, Mar 26, 2020 at 04:15:06PM +0100, Michal Privoznik wrote:
Initially introduced in v3.10.0-rc1~172.
When generating a path for memory-backend-file or -mem-path, qemu driver will use the following pattern:
$memoryBackingDir/libvirt/qemu/$id-$shortName
where $memoryBackingDir defaults to /var/lib/libvirt/qemu/ram but can be overridden in qemu.conf. Anyway, the "/libvirt/qemu/" part looks redundant, because it's already contained in the default, or creates unnecessary nesting if overridden in qemu.conf.
I think this was copied from our earlier huge pages code which used /dev/hugepages, and then added "/libvirt/qemu" to it.
Now we're stripping off "/libvirt/qemu" though, we're liable to a filename clashes if someone edits qemu.conf to point to /dev/shm.
IOW, even though "/libvirt/qemu" is redundant when using our default path, I think we need to keep it to avoid clashing with custom paths.
Alright. So what if I'd squash this in? diff --git i/src/qemu/qemu_conf.c w/src/qemu/qemu_conf.c index 9786e19f8f..2ead5d5920 100644 --- i/src/qemu/qemu_conf.c +++ w/src/qemu/qemu_conf.c @@ -970,7 +970,18 @@ static int virQEMUDriverConfigLoadMemoryEntry(virQEMUDriverConfigPtr cfg, virConfPtr conf) { - return virConfGetValueString(conf, "memory_backing_dir", &cfg->memoryBackingDir); + char *dir = NULL; + int rc; + + if ((rc = virConfGetValueString(conf, "memory_backing_dir", &dir)) < 0) { + return -1; + } else if (rc > 0) { + VIR_FREE(cfg->memoryBackingDir); + cfg->memoryBackingDir = g_strdup_printf("%s/libvirt/qemu", dir); + return 1; + } + + return 0; } This way we would drop "/libvirt/qemu" in the default configuration and keep it if user overrides it in qemu.conf. Michal

On Mon, Mar 30, 2020 at 06:16:29PM +0200, Michal Prívozník wrote:
On 30. 3. 2020 13:09, Daniel P. Berrangé wrote:
On Thu, Mar 26, 2020 at 04:15:06PM +0100, Michal Privoznik wrote:
Initially introduced in v3.10.0-rc1~172.
When generating a path for memory-backend-file or -mem-path, qemu driver will use the following pattern:
$memoryBackingDir/libvirt/qemu/$id-$shortName
where $memoryBackingDir defaults to /var/lib/libvirt/qemu/ram but can be overridden in qemu.conf. Anyway, the "/libvirt/qemu/" part looks redundant, because it's already contained in the default, or creates unnecessary nesting if overridden in qemu.conf.
I think this was copied from our earlier huge pages code which used /dev/hugepages, and then added "/libvirt/qemu" to it.
Now we're stripping off "/libvirt/qemu" though, we're liable to a filename clashes if someone edits qemu.conf to point to /dev/shm.
IOW, even though "/libvirt/qemu" is redundant when using our default path, I think we need to keep it to avoid clashing with custom paths.
Alright. So what if I'd squash this in?
diff --git i/src/qemu/qemu_conf.c w/src/qemu/qemu_conf.c index 9786e19f8f..2ead5d5920 100644 --- i/src/qemu/qemu_conf.c +++ w/src/qemu/qemu_conf.c @@ -970,7 +970,18 @@ static int virQEMUDriverConfigLoadMemoryEntry(virQEMUDriverConfigPtr cfg, virConfPtr conf) { - return virConfGetValueString(conf, "memory_backing_dir", &cfg->memoryBackingDir); + char *dir = NULL; + int rc; + + if ((rc = virConfGetValueString(conf, "memory_backing_dir", &dir)) < 0) { + return -1; + } else if (rc > 0) { + VIR_FREE(cfg->memoryBackingDir); + cfg->memoryBackingDir = g_strdup_printf("%s/libvirt/qemu", dir); + return 1; + } + + return 0; }
This way we would drop "/libvirt/qemu" in the default configuration and keep it if user overrides it in qemu.conf.
Yeah, I think that works. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

The virDomainGenerateMachineName() function doesn't belong in src/conf/ really, because it has nothing to do with domain XML parsing. It landed there because of lack of better place in the past. But now that we have src/hypervisor/ the function should live there. At the same time, the function name is changed to match new location. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/conf/domain_conf.c | 72 --------------------------------- src/conf/domain_conf.h | 7 ---- src/hypervisor/domain_driver.c | 74 ++++++++++++++++++++++++++++++++++ src/hypervisor/domain_driver.h | 7 ++++ src/libvirt_private.syms | 2 +- src/lxc/lxc_domain.c | 3 +- src/qemu/qemu_domain.c | 7 ++-- tests/virsystemdtest.c | 5 ++- 8 files changed, 91 insertions(+), 86 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 27bc5a797b..239455ef58 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -62,7 +62,6 @@ #include "virdomainsnapshotobjlist.h" #include "virdomaincheckpointobjlist.h" #include "virutil.h" -#include "vircrypto.h" #define VIR_FROM_THIS VIR_FROM_DOMAIN @@ -31032,77 +31031,6 @@ virDomainDiskSetBlockIOTune(virDomainDiskDefPtr disk, return 0; } -#define HOSTNAME_CHARS \ - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-" - -static void -virDomainMachineNameAppendValid(virBufferPtr buf, - const char *name) -{ - bool skip = true; - - for (; *name; name++) { - if (strlen(virBufferCurrentContent(buf)) >= 64) - break; - - if (*name == '.' || *name == '-') { - if (!skip) - virBufferAddChar(buf, *name); - skip = true; - continue; - } - - skip = false; - - if (!strchr(HOSTNAME_CHARS, *name)) - continue; - - virBufferAddChar(buf, *name); - } - - /* trailing dashes or dots are not allowed */ - virBufferTrimChars(buf, "-."); -} - -#undef HOSTNAME_CHARS - -char * -virDomainGenerateMachineName(const char *drivername, - const char *root, - int id, - const char *name, - bool privileged) -{ - virBuffer buf = VIR_BUFFER_INITIALIZER; - - virBufferAsprintf(&buf, "%s-", drivername); - - if (root) { - g_autofree char *hash = NULL; - - /* When two embed drivers start two domains with the same @name and @id - * we would generate a non-unique name. Include parts of hashed @root - * which guarantees uniqueness. The first 8 characters of SHA256 ought - * to be enough for anybody. */ - if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, root, &hash) < 0) - return NULL; - - virBufferAsprintf(&buf, "embed-%.8s-", hash); - } else if (!privileged) { - g_autofree char *username = NULL; - if (!(username = virGetUserName(geteuid()))) { - virBufferFreeAndReset(&buf); - return NULL; - } - virBufferAsprintf(&buf, "%s-", username); - } - - virBufferAsprintf(&buf, "%d-", id); - virDomainMachineNameAppendValid(&buf, name); - - return virBufferContentAndReset(&buf); -} - /** * virDomainNetTypeSharesHostView: diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 33875d942f..575290a6ac 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -3649,13 +3649,6 @@ virDomainGetBlkioParametersAssignFromDef(virDomainDefPtr def, int virDomainDiskSetBlockIOTune(virDomainDiskDefPtr disk, virDomainBlockIoTuneInfo *info); -char * -virDomainGenerateMachineName(const char *drivername, - const char *root, - int id, - const char *name, - bool privileged); - bool virDomainNetTypeSharesHostView(const virDomainNetDef *net); diff --git a/src/hypervisor/domain_driver.c b/src/hypervisor/domain_driver.c index fc5b6eeefe..7bf0fb3f98 100644 --- a/src/hypervisor/domain_driver.c +++ b/src/hypervisor/domain_driver.c @@ -23,10 +23,84 @@ #include "domain_driver.h" #include "viralloc.h" #include "virstring.h" +#include "vircrypto.h" +#include "virutil.h" #define VIR_FROM_THIS VIR_FROM_DOMAIN +#define HOSTNAME_CHARS \ + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-" + +static void +virDomainMachineNameAppendValid(virBufferPtr buf, + const char *name) +{ + bool skip = true; + + for (; *name; name++) { + if (strlen(virBufferCurrentContent(buf)) >= 64) + break; + + if (*name == '.' || *name == '-') { + if (!skip) + virBufferAddChar(buf, *name); + skip = true; + continue; + } + + skip = false; + + if (!strchr(HOSTNAME_CHARS, *name)) + continue; + + virBufferAddChar(buf, *name); + } + + /* trailing dashes or dots are not allowed */ + virBufferTrimChars(buf, "-."); +} + +#undef HOSTNAME_CHARS + +char * +virDomainDriverGenerateMachineName(const char *drivername, + const char *root, + int id, + const char *name, + bool privileged) +{ + virBuffer buf = VIR_BUFFER_INITIALIZER; + + virBufferAsprintf(&buf, "%s-", drivername); + + if (root) { + g_autofree char *hash = NULL; + + /* When two embed drivers start two domains with the same @name and @id + * we would generate a non-unique name. Include parts of hashed @root + * which guarantees uniqueness. The first 8 characters of SHA256 ought + * to be enough for anybody. */ + if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, root, &hash) < 0) + return NULL; + + virBufferAsprintf(&buf, "embed-%.8s-", hash); + } else if (!privileged) { + g_autofree char *username = NULL; + if (!(username = virGetUserName(geteuid()))) { + virBufferFreeAndReset(&buf); + return NULL; + } + virBufferAsprintf(&buf, "%s-", username); + } + + virBufferAsprintf(&buf, "%d-", id); + virDomainMachineNameAppendValid(&buf, name); + + return virBufferContentAndReset(&buf); +} + + /* Modify dest_array to reflect all blkio device changes described in * src_array. */ int diff --git a/src/hypervisor/domain_driver.h b/src/hypervisor/domain_driver.h index b6d5e66bba..c52e37f038 100644 --- a/src/hypervisor/domain_driver.h +++ b/src/hypervisor/domain_driver.h @@ -22,6 +22,13 @@ #include "domain_conf.h" +char * +virDomainDriverGenerateMachineName(const char *drivername, + const char *root, + int id, + const char *name, + bool privileged); + int virDomainDriverMergeBlkioDevice(virBlkioDevicePtr *dest_array, size_t *dest_size, virBlkioDevicePtr src_array, diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3f032c7963..69f278f6fb 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -406,7 +406,6 @@ virDomainFSTypeFromString; virDomainFSTypeToString; virDomainFSWrpolicyTypeFromString; virDomainFSWrpolicyTypeToString; -virDomainGenerateMachineName; virDomainGetBlkioParametersAssignFromDef; virDomainGetFilesystemForTarget; virDomainGraphicsAuthConnectedTypeFromString; @@ -1403,6 +1402,7 @@ virDomainCgroupSetupMemtune; # hypervisor/domain_driver.h +virDomainDriverGenerateMachineName; virDomainDriverMergeBlkioDevice; virDomainDriverParseBlkioDeviceStr; virDomainDriverSetupPersistentDefBlkioParams; diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c index ebd2c2b56e..59f803837a 100644 --- a/src/lxc/lxc_domain.c +++ b/src/lxc/lxc_domain.c @@ -31,6 +31,7 @@ #include "virtime.h" #include "virsystemd.h" #include "virinitctl.h" +#include "domain_driver.h" #define VIR_FROM_THIS VIR_FROM_LXC @@ -406,7 +407,7 @@ virLXCDomainGetMachineName(virDomainDefPtr def, pid_t pid) } if (!ret) - ret = virDomainGenerateMachineName("lxc", NULL, def->id, def->name, true); + ret = virDomainDriverGenerateMachineName("lxc", NULL, def->id, def->name, true); return ret; } diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 7d29f3f114..a58d658e0b 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -44,6 +44,7 @@ #include "virfile.h" #include "domain_addr.h" #include "domain_capabilities.h" +#include "domain_driver.h" #include "domain_event.h" #include "virtime.h" #include "virnetdevopenvswitch.h" @@ -16537,9 +16538,9 @@ qemuDomainGetMachineName(virDomainObjPtr vm) } if (!ret) - ret = virDomainGenerateMachineName("qemu", cfg->root, - vm->def->id, vm->def->name, - virQEMUDriverIsPrivileged(driver)); + ret = virDomainDriverGenerateMachineName("qemu", cfg->root, + vm->def->id, vm->def->name, + virQEMUDriverIsPrivileged(driver)); return ret; } diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c index 050941dce8..e7dcdea8e9 100644 --- a/tests/virsystemdtest.c +++ b/tests/virsystemdtest.c @@ -34,6 +34,7 @@ # include "virlog.h" # include "virmock.h" # include "rpc/virnetsocket.h" +# include "domain_driver.h" # define VIR_FROM_THIS VIR_FROM_NONE VIR_LOG_INIT("tests.systemdtest"); @@ -414,8 +415,8 @@ testMachineName(const void *opaque) int ret = -1; char *actual = NULL; - if (!(actual = virDomainGenerateMachineName("qemu", data->root, - data->id, data->name, true))) + if (!(actual = virDomainDriverGenerateMachineName("qemu", data->root, + data->id, data->name, true))) goto cleanup; if (STRNEQ(actual, data->expected)) { -- 2.24.1

On Thu, Mar 26, 2020 at 04:15:07PM +0100, Michal Privoznik wrote:
The virDomainGenerateMachineName() function doesn't belong in src/conf/ really, because it has nothing to do with domain XML parsing. It landed there because of lack of better place in the past. But now that we have src/hypervisor/ the function should live there. At the same time, the function name is changed to match new location.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/conf/domain_conf.c | 72 --------------------------------- src/conf/domain_conf.h | 7 ---- src/hypervisor/domain_driver.c | 74 ++++++++++++++++++++++++++++++++++ src/hypervisor/domain_driver.h | 7 ++++ src/libvirt_private.syms | 2 +- src/lxc/lxc_domain.c | 3 +- src/qemu/qemu_domain.c | 7 ++-- tests/virsystemdtest.c | 5 ++- 8 files changed, 91 insertions(+), 86 deletions(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

The code that generates "qemu-embed-$hash" is going to be useful in more places. Separate it out into a function. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/hypervisor/domain_driver.c | 42 ++++++++++++++++++++++------------ src/hypervisor/domain_driver.h | 4 ++++ src/libvirt_private.syms | 1 + 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/hypervisor/domain_driver.c b/src/hypervisor/domain_driver.c index 7bf0fb3f98..31821fc712 100644 --- a/src/hypervisor/domain_driver.c +++ b/src/hypervisor/domain_driver.c @@ -28,6 +28,22 @@ #define VIR_FROM_THIS VIR_FROM_DOMAIN +char * +virDomainDriverGenerateRootHash(const char *drivername, + const char *root) +{ + g_autofree char *hash = NULL; + + if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, root, &hash) < 0) + return NULL; + + /* When two embed drivers start two domains with the same @name and @id + * we would generate a non-unique name. Include parts of hashed @root + * which guarantees uniqueness. The first 8 characters of SHA256 ought + * to be enough for anybody. */ + return g_strdup_printf("%s-embed-%.8s", drivername, hash); +} + #define HOSTNAME_CHARS \ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-" @@ -72,26 +88,24 @@ virDomainDriverGenerateMachineName(const char *drivername, { virBuffer buf = VIR_BUFFER_INITIALIZER; - virBufferAsprintf(&buf, "%s-", drivername); - if (root) { g_autofree char *hash = NULL; - /* When two embed drivers start two domains with the same @name and @id - * we would generate a non-unique name. Include parts of hashed @root - * which guarantees uniqueness. The first 8 characters of SHA256 ought - * to be enough for anybody. */ - if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, root, &hash) < 0) + if (!(hash = virDomainDriverGenerateRootHash(drivername, root))) return NULL; - virBufferAsprintf(&buf, "embed-%.8s-", hash); - } else if (!privileged) { - g_autofree char *username = NULL; - if (!(username = virGetUserName(geteuid()))) { - virBufferFreeAndReset(&buf); - return NULL; + virBufferAsprintf(&buf, "%s-", hash); + } else { + virBufferAsprintf(&buf, "%s-", drivername); + if (!privileged) { + + g_autofree char *username = NULL; + if (!(username = virGetUserName(geteuid()))) { + virBufferFreeAndReset(&buf); + return NULL; + } + virBufferAsprintf(&buf, "%s-", username); } - virBufferAsprintf(&buf, "%s-", username); } virBufferAsprintf(&buf, "%d-", id); diff --git a/src/hypervisor/domain_driver.h b/src/hypervisor/domain_driver.h index c52e37f038..b66ae2d421 100644 --- a/src/hypervisor/domain_driver.h +++ b/src/hypervisor/domain_driver.h @@ -22,6 +22,10 @@ #include "domain_conf.h" +char * +virDomainDriverGenerateRootHash(const char *drivername, + const char *root); + char * virDomainDriverGenerateMachineName(const char *drivername, const char *root, diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 69f278f6fb..58a895a593 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1403,6 +1403,7 @@ virDomainCgroupSetupMemtune; # hypervisor/domain_driver.h virDomainDriverGenerateMachineName; +virDomainDriverGenerateRootHash; virDomainDriverMergeBlkioDevice; virDomainDriverParseBlkioDeviceStr; virDomainDriverSetupPersistentDefBlkioParams; -- 2.24.1

On Thu, Mar 26, 2020 at 04:15:08PM +0100, Michal Privoznik wrote:
The code that generates "qemu-embed-$hash" is going to be useful in more places. Separate it out into a function.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/hypervisor/domain_driver.c | 42 ++++++++++++++++++++++------------ src/hypervisor/domain_driver.h | 4 ++++ src/libvirt_private.syms | 1 + 3 files changed, 33 insertions(+), 14 deletions(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

This function returns embeddedRoot member of the driver structure. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_conf.c | 12 ++++++++++++ src/qemu/qemu_conf.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 9786e19f8f..fdc6f53ad3 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -1230,6 +1230,18 @@ virQEMUDriverIsPrivileged(virQEMUDriverPtr driver) return driver->privileged; } +/* virQEMUDriverGetEmbedRoot: + * @driver: the QEMU driver + * + * Returns root directory specified in connection URI for embed + * mode, NULL otherwise. + */ +const char * +virQEMUDriverGetEmbedRoot(virQEMUDriverPtr driver) +{ + return driver->embeddedRoot; +} + virDomainXMLOptionPtr virQEMUDriverCreateXMLConf(virQEMUDriverPtr driver, const char *defsecmodel) diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index c8d3e439e0..e27ce0b92a 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -334,6 +334,7 @@ virQEMUDriverConfigSetDefaults(virQEMUDriverConfigPtr cfg); virQEMUDriverConfigPtr virQEMUDriverGetConfig(virQEMUDriverPtr driver); bool virQEMUDriverIsPrivileged(virQEMUDriverPtr driver); +const char *virQEMUDriverGetEmbedRoot(virQEMUDriverPtr driver); virCapsHostNUMAPtr virQEMUDriverGetHostNUMACaps(virQEMUDriverPtr driver); virCPUDefPtr virQEMUDriverGetHostCPU(virQEMUDriverPtr driver); -- 2.24.1

On Thu, Mar 26, 2020 at 04:15:09PM +0100, Michal Privoznik wrote:
This function returns embeddedRoot member of the driver structure.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_conf.c | 12 ++++++++++++ src/qemu/qemu_conf.h | 1 + 2 files changed, 13 insertions(+)
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 9786e19f8f..fdc6f53ad3 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -1230,6 +1230,18 @@ virQEMUDriverIsPrivileged(virQEMUDriverPtr driver) return driver->privileged; }
+/* virQEMUDriverGetEmbedRoot: + * @driver: the QEMU driver + * + * Returns root directory specified in connection URI for embed + * mode, NULL otherwise. + */ +const char * +virQEMUDriverGetEmbedRoot(virQEMUDriverPtr driver) +{ + return driver->embeddedRoot; +}
I don't really see the benefit in this method. The embeddedRoot field is immutable so we can just be accessing it directly with no need for APIs wrappers, as we often do when accesing the privileged field. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On 30. 3. 2020 13:16, Daniel P. Berrangé wrote:
On Thu, Mar 26, 2020 at 04:15:09PM +0100, Michal Privoznik wrote:
This function returns embeddedRoot member of the driver structure.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_conf.c | 12 ++++++++++++ src/qemu/qemu_conf.h | 1 + 2 files changed, 13 insertions(+)
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 9786e19f8f..fdc6f53ad3 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -1230,6 +1230,18 @@ virQEMUDriverIsPrivileged(virQEMUDriverPtr driver) return driver->privileged; }
+/* virQEMUDriverGetEmbedRoot: + * @driver: the QEMU driver + * + * Returns root directory specified in connection URI for embed + * mode, NULL otherwise. + */ +const char * +virQEMUDriverGetEmbedRoot(virQEMUDriverPtr driver) +{ + return driver->embeddedRoot; +}
I don't really see the benefit in this method. The embeddedRoot field is immutable so we can just be accessing it directly with no need for APIs wrappers, as we often do when accesing the privileged field.
I just wanted to follow what virQEMUDriverIsPrivileged() is doing. I don't care honestly. Michal

On Mon, Mar 30, 2020 at 06:20:28PM +0200, Michal Prívozník wrote:
On 30. 3. 2020 13:16, Daniel P. Berrangé wrote:
On Thu, Mar 26, 2020 at 04:15:09PM +0100, Michal Privoznik wrote:
This function returns embeddedRoot member of the driver structure.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_conf.c | 12 ++++++++++++ src/qemu/qemu_conf.h | 1 + 2 files changed, 13 insertions(+)
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 9786e19f8f..fdc6f53ad3 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -1230,6 +1230,18 @@ virQEMUDriverIsPrivileged(virQEMUDriverPtr driver) return driver->privileged; }
+/* virQEMUDriverGetEmbedRoot: + * @driver: the QEMU driver + * + * Returns root directory specified in connection URI for embed + * mode, NULL otherwise. + */ +const char * +virQEMUDriverGetEmbedRoot(virQEMUDriverPtr driver) +{ + return driver->embeddedRoot; +}
I don't really see the benefit in this method. The embeddedRoot field is immutable so we can just be accessing it directly with no need for APIs wrappers, as we often do when accesing the privileged field.
I just wanted to follow what virQEMUDriverIsPrivileged() is doing. I don't care honestly.
Hmm, I had forgotten virQEMUDriverIsPrivileged() exists - we don't seem to be using that consistently either. My preference is to have neither of those methods. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

The cfg->root is going away, therefore get the info right from the driver structure. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_domain.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index a58d658e0b..e71010b7e5 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -16528,7 +16528,6 @@ qemuDomainGetMachineName(virDomainObjPtr vm) { qemuDomainObjPrivatePtr priv = vm->privateData; virQEMUDriverPtr driver = priv->driver; - g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver); char *ret = NULL; if (vm->pid > 0) { @@ -16538,7 +16537,8 @@ qemuDomainGetMachineName(virDomainObjPtr vm) } if (!ret) - ret = virDomainDriverGenerateMachineName("qemu", cfg->root, + ret = virDomainDriverGenerateMachineName("qemu", + virQEMUDriverGetEmbedRoot(driver), vm->def->id, vm->def->name, virQEMUDriverIsPrivileged(driver)); -- 2.24.1

Now that we have shiny accessor for driver->embeddedRoot use it instead of accessing the struct member directly. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/qemu/qemu_driver.c | 6 +++--- src/qemu/qemu_process.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 5cbf2f2478..664ea4eeda 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1173,7 +1173,7 @@ static virDrvOpenStatus qemuConnectOpen(virConnectPtr conn, return VIR_DRV_OPEN_ERROR; } - if (qemu_driver->embeddedRoot) { + if (virQEMUDriverGetEmbedRoot(qemu_driver)) { const char *root = virURIGetParam(conn->uri, "root"); if (!root) return VIR_DRV_OPEN_ERROR; @@ -1184,11 +1184,11 @@ static virDrvOpenStatus qemuConnectOpen(virConnectPtr conn, return VIR_DRV_OPEN_ERROR; } - if (STRNEQ(root, qemu_driver->embeddedRoot)) { + if (STRNEQ(root, virQEMUDriverGetEmbedRoot(qemu_driver))) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Cannot open embedded driver at path '%s', " "already open with path '%s'"), - root, qemu_driver->embeddedRoot); + root, virQEMUDriverGetEmbedRoot(qemu_driver)); return VIR_DRV_OPEN_ERROR; } } else { diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index fcb47e8596..b0b2258850 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -6715,7 +6715,7 @@ qemuProcessLaunch(virConnectPtr conn, _("Domain autodestroy requires a connection handle")); return -1; } - if (driver->embeddedRoot) { + if (virQEMUDriverGetEmbedRoot(driver)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Domain autodestroy not supported for embedded drivers yet")); return -1; -- 2.24.1

On Thu, Mar 26, 2020 at 04:15:11PM +0100, Michal Privoznik wrote:
Now that we have shiny accessor for driver->embeddedRoot use it instead of accessing the struct member directly.
Is this really better ?
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/qemu/qemu_driver.c | 6 +++--- src/qemu/qemu_process.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 5cbf2f2478..664ea4eeda 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1173,7 +1173,7 @@ static virDrvOpenStatus qemuConnectOpen(virConnectPtr conn, return VIR_DRV_OPEN_ERROR; }
- if (qemu_driver->embeddedRoot) { + if (virQEMUDriverGetEmbedRoot(qemu_driver)) { const char *root = virURIGetParam(conn->uri, "root"); if (!root) return VIR_DRV_OPEN_ERROR; @@ -1184,11 +1184,11 @@ static virDrvOpenStatus qemuConnectOpen(virConnectPtr conn, return VIR_DRV_OPEN_ERROR; }
- if (STRNEQ(root, qemu_driver->embeddedRoot)) { + if (STRNEQ(root, virQEMUDriverGetEmbedRoot(qemu_driver))) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Cannot open embedded driver at path '%s', " "already open with path '%s'"), - root, qemu_driver->embeddedRoot); + root, virQEMUDriverGetEmbedRoot(qemu_driver)); return VIR_DRV_OPEN_ERROR; } } else { diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index fcb47e8596..b0b2258850 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -6715,7 +6715,7 @@ qemuProcessLaunch(virConnectPtr conn, _("Domain autodestroy requires a connection handle")); return -1; } - if (driver->embeddedRoot) { + if (virQEMUDriverGetEmbedRoot(driver)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Domain autodestroy not supported for embedded drivers yet")); return -1; -- 2.24.1
Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

This reverts commit 06a19921b6a522cd7b4d352c9320909752947de3. What I haven't realized when writing this ^^ commit is that the virQEMUDriver structure already stores the root directory path. And since the pointer is immutable it can be accessed right from the structure and thus there is no need to duplicate it in the driver config. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_conf.c | 2 -- src/qemu/qemu_conf.h | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index fdc6f53ad3..3782c2b43b 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -115,7 +115,6 @@ virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged, if (root) { cfg->uri = g_strdup_printf("qemu:///embed?root=%s", root); - cfg->root = g_strdup(root); } else { cfg->uri = g_strdup(privileged ? "qemu:///system" : "qemu:///session"); } @@ -302,7 +301,6 @@ static void virQEMUDriverConfigDispose(void *obj) virStringListFree(cfg->cgroupDeviceACL); VIR_FREE(cfg->uri); - VIR_FREE(cfg->root); VIR_FREE(cfg->configBaseDir); VIR_FREE(cfg->configDir); diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index e27ce0b92a..76669e2139 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -76,8 +76,6 @@ struct _virQEMUDriverConfig { virObject parent; char *uri; - char *root; /* The root directory for embed driver, - NULL for system/session connections */ uid_t user; gid_t group; -- 2.24.1

On Thu, Mar 26, 2020 at 04:15:12PM +0100, Michal Privoznik wrote:
This reverts commit 06a19921b6a522cd7b4d352c9320909752947de3.
What I haven't realized when writing this ^^ commit is that the virQEMUDriver structure already stores the root directory path. And since the pointer is immutable it can be accessed right from the structure and thus there is no need to duplicate it in the driver config.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_conf.c | 2 -- src/qemu/qemu_conf.h | 2 -- 2 files changed, 4 deletions(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

So far, libvirt generates the following path for hugepages: $mnt/libvirt/qemu/$id-$shortName where $mnt is the mount point of hugetlbfs corresponding to hugepages of desired size (e.g. /dev/hugepages), $id is domain ID and $shortName is shortened version of domain name. So for instance, the generated path may look something like this: /dev/hugepages/libvirt/qemu/1-QEMUGuest But this won't work with embed driver really, because if there are two instances of embed driver, and they both want to start a domain with the same name and with hugepages, both drivers will generate the same path which is not desired. Fortunately, we can reuse the approach for machined name generation (v6.1.0-178-gc9bd08ee35) and include part of hash of the root in the generated path. Note, the important change is in qemuGetBaseHugepagePath(). The rest is needed to pass driver around. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_command.c | 4 ++-- src/qemu/qemu_conf.c | 24 +++++++++++++++++------- src/qemu/qemu_conf.h | 10 ++++++---- src/qemu/qemu_driver.c | 2 +- src/qemu/qemu_process.c | 2 +- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index d1b689dfd3..9afb59828c 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -3465,7 +3465,7 @@ qemuBuildMemoryBackendProps(virJSONValuePtr *backendProps, if (!priv->memPrealloc) prealloc = true; } else if (useHugepage) { - if (qemuGetDomainHupageMemPath(def, cfg, pagesize, &memPath) < 0) + if (qemuGetDomainHupageMemPath(priv->driver, def, pagesize, &memPath) < 0) return -1; if (!priv->memPrealloc) prealloc = true; @@ -7251,7 +7251,7 @@ qemuBuildMemPathStr(virQEMUDriverConfigPtr cfg, if (!pagesize && qemuBuildMemoryGetDefaultPagesize(cfg, &pagesize) < 0) return -1; - if (qemuGetDomainHupageMemPath(def, cfg, pagesize, &mem_path) < 0) + if (qemuGetDomainHupageMemPath(priv->driver, def, pagesize, &mem_path) < 0) return -1; } else if (def->mem.source == VIR_DOMAIN_MEMORY_SOURCE_FILE) { if (qemuGetMemoryBackingPath(def, cfg, "ram", &mem_path) < 0) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 3782c2b43b..de2a52adb3 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -40,6 +40,7 @@ #include "virxml.h" #include "virlog.h" #include "cpu/cpu.h" +#include "domain_driver.h" #include "domain_nwfilter.h" #include "virfile.h" #include "virsocket.h" @@ -1894,21 +1895,29 @@ qemuTranslateSnapshotDiskSourcePool(virDomainSnapshotDiskDefPtr def) } char * -qemuGetBaseHugepagePath(virHugeTLBFSPtr hugepage) +qemuGetBaseHugepagePath(virQEMUDriverPtr driver, + virHugeTLBFSPtr hugepage) { + const char *root = virQEMUDriverGetEmbedRoot(driver); char *ret; - ret = g_strdup_printf("%s/libvirt/qemu", hugepage->mnt_dir); + if (root && !STRPREFIX(hugepage->mnt_dir, root)) { + g_autofree char * hash = virDomainDriverGenerateRootHash("qemu", root); + ret = g_strdup_printf("%s/libvirt/%s", hugepage->mnt_dir, hash); + } else { + ret = g_strdup_printf("%s/libvirt/qemu", hugepage->mnt_dir); + } return ret; } char * -qemuGetDomainHugepagePath(const virDomainDef *def, +qemuGetDomainHugepagePath(virQEMUDriverPtr driver, + const virDomainDef *def, virHugeTLBFSPtr hugepage) { - g_autofree char *base = qemuGetBaseHugepagePath(hugepage); + g_autofree char *base = qemuGetBaseHugepagePath(driver, hugepage); g_autofree char *domPath = virDomainDefGetShortName(def); char *ret = NULL; @@ -1927,11 +1936,12 @@ qemuGetDomainHugepagePath(const virDomainDef *def, * -1 otherwise. */ int -qemuGetDomainHupageMemPath(const virDomainDef *def, - virQEMUDriverConfigPtr cfg, +qemuGetDomainHupageMemPath(virQEMUDriverPtr driver, + const virDomainDef *def, unsigned long long pagesize, char **memPath) { + g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver); size_t i = 0; if (!cfg->nhugetlbfs) { @@ -1954,7 +1964,7 @@ qemuGetDomainHupageMemPath(const virDomainDef *def, return -1; } - if (!(*memPath = qemuGetDomainHugepagePath(def, &cfg->hugetlbfs[i]))) + if (!(*memPath = qemuGetDomainHugepagePath(driver, def, &cfg->hugetlbfs[i]))) return -1; return 0; diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index 76669e2139..0099ac5be4 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -388,12 +388,14 @@ virDomainXMLOptionPtr virQEMUDriverCreateXMLConf(virQEMUDriverPtr driver, int qemuTranslateSnapshotDiskSourcePool(virDomainSnapshotDiskDefPtr def); -char * qemuGetBaseHugepagePath(virHugeTLBFSPtr hugepage); -char * qemuGetDomainHugepagePath(const virDomainDef *def, +char * qemuGetBaseHugepagePath(virQEMUDriverPtr driver, + virHugeTLBFSPtr hugepage); +char * qemuGetDomainHugepagePath(virQEMUDriverPtr driver, + const virDomainDef *def, virHugeTLBFSPtr hugepage); -int qemuGetDomainHupageMemPath(const virDomainDef *def, - virQEMUDriverConfigPtr cfg, +int qemuGetDomainHupageMemPath(virQEMUDriverPtr driver, + const virDomainDef *def, unsigned long long pagesize, char **memPath); diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 664ea4eeda..0033a72d0b 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -917,7 +917,7 @@ qemuStateInitialize(bool privileged, for (i = 0; i < cfg->nhugetlbfs; i++) { g_autofree char *hugepagePath = NULL; - hugepagePath = qemuGetBaseHugepagePath(&cfg->hugetlbfs[i]); + hugepagePath = qemuGetBaseHugepagePath(qemu_driver, &cfg->hugetlbfs[i]); if (!hugepagePath) goto error; diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index b0b2258850..aa6741e1f4 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -3881,7 +3881,7 @@ qemuProcessBuildDestroyMemoryPaths(virQEMUDriverPtr driver, if (!build || shouldBuildHP) { for (i = 0; i < cfg->nhugetlbfs; i++) { g_autofree char *path = NULL; - path = qemuGetDomainHugepagePath(vm->def, &cfg->hugetlbfs[i]); + path = qemuGetDomainHugepagePath(driver, vm->def, &cfg->hugetlbfs[i]); if (!path) return -1; -- 2.24.1

On Thu, Mar 26, 2020 at 04:15:13PM +0100, Michal Privoznik wrote:
So far, libvirt generates the following path for hugepages:
$mnt/libvirt/qemu/$id-$shortName
where $mnt is the mount point of hugetlbfs corresponding to hugepages of desired size (e.g. /dev/hugepages), $id is domain ID and $shortName is shortened version of domain name. So for instance, the generated path may look something like this:
/dev/hugepages/libvirt/qemu/1-QEMUGuest
But this won't work with embed driver really, because if there are two instances of embed driver, and they both want to start a domain with the same name and with hugepages, both drivers will generate the same path which is not desired. Fortunately, we can reuse the approach for machined name generation (v6.1.0-178-gc9bd08ee35) and include part of hash of the root in the generated path.
Note, the important change is in qemuGetBaseHugepagePath(). The rest is needed to pass driver around.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_command.c | 4 ++-- src/qemu/qemu_conf.c | 24 +++++++++++++++++------- src/qemu/qemu_conf.h | 10 ++++++---- src/qemu/qemu_driver.c | 2 +- src/qemu/qemu_process.c | 2 +- 5 files changed, 27 insertions(+), 15 deletions(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

So far, libvirt generates the following path for memory: $memoryBackingDir/$id-$shortName/ram-nodeN where $memoryBackingDir is the path where QEMU mmaps() memory for the guest (e.g. /var/lib/libvirt/qemu/ram), $id is domain ID and $shortName is shortened version of domain name. So for instance, the generated path may look something like this: /var/lib/libvirt/qemu/ram/1-QEMUGuest/ram-node0 While in case of embed driver the following path would be generated by default: $root/lib/qemu/ram/1-QEMUGuest/ram-node0 which is not clashing with other embed drivers, we allow users to override the default and have all embed drivers use the same prefix. This can create clashing paths. Fortunately, we can reuse the approach for machined name generation (v6.1.0-178-gc9bd08ee35) and include part of hash of the root in the generated path. Note, the important change is in qemuGetMemoryBackingBasePath(). The rest is needed to pass driver around. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_command.c | 15 +++++++-------- src/qemu/qemu_conf.c | 21 ++++++++++++++------- src/qemu/qemu_conf.h | 8 ++++---- src/qemu/qemu_process.c | 5 ++--- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 9afb59828c..308c32f4ee 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -3472,7 +3472,7 @@ qemuBuildMemoryBackendProps(virJSONValuePtr *backendProps, } else { /* We can have both pagesize and mem source. If that's the case, * prefer hugepages as those are more specific. */ - if (qemuGetMemoryBackingPath(def, cfg, mem->info.alias, &memPath) < 0) + if (qemuGetMemoryBackingPath(priv->driver, def, mem->info.alias, &memPath) < 0) return -1; } @@ -7233,11 +7233,11 @@ qemuBuildSmpCommandLine(virCommandPtr cmd, static int -qemuBuildMemPathStr(virQEMUDriverConfigPtr cfg, - const virDomainDef *def, +qemuBuildMemPathStr(const virDomainDef *def, virCommandPtr cmd, qemuDomainObjPrivatePtr priv) { + g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(priv->driver); const long system_page_size = virGetSystemPageSizeKB(); g_autofree char *mem_path = NULL; @@ -7254,7 +7254,7 @@ qemuBuildMemPathStr(virQEMUDriverConfigPtr cfg, if (qemuGetDomainHupageMemPath(priv->driver, def, pagesize, &mem_path) < 0) return -1; } else if (def->mem.source == VIR_DOMAIN_MEMORY_SOURCE_FILE) { - if (qemuGetMemoryBackingPath(def, cfg, "ram", &mem_path) < 0) + if (qemuGetMemoryBackingPath(priv->driver, def, "ram", &mem_path) < 0) return -1; } else { return 0; @@ -7273,7 +7273,6 @@ qemuBuildMemPathStr(virQEMUDriverConfigPtr cfg, static int qemuBuildMemCommandLine(virCommandPtr cmd, - virQEMUDriverConfigPtr cfg, const virDomainDef *def, virQEMUCapsPtr qemuCaps, qemuDomainObjPrivatePtr priv) @@ -7305,7 +7304,7 @@ qemuBuildMemCommandLine(virCommandPtr cmd, * the hugepages and no numa node is specified. */ if (!virDomainNumaGetNodeCount(def->numa) && - qemuBuildMemPathStr(cfg, def, cmd, priv) < 0) + qemuBuildMemPathStr(def, cmd, priv) < 0) return -1; if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_OVERCOMMIT)) { @@ -7386,7 +7385,7 @@ qemuBuildNumaArgStr(virQEMUDriverConfigPtr cfg, } if (!needBackend && - qemuBuildMemPathStr(cfg, def, cmd, priv) < 0) + qemuBuildMemPathStr(def, cmd, priv) < 0) goto cleanup; for (i = 0; i < ncells; i++) { @@ -9879,7 +9878,7 @@ qemuBuildCommandLine(virQEMUDriverPtr driver, if (!migrateURI && !snapshot && qemuDomainAlignMemorySizes(def) < 0) return NULL; - if (qemuBuildMemCommandLine(cmd, cfg, def, qemuCaps, priv) < 0) + if (qemuBuildMemCommandLine(cmd, def, qemuCaps, priv) < 0) return NULL; if (qemuBuildSmpCommandLine(cmd, def, qemuCaps) < 0) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index de2a52adb3..7f0c915bd1 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -1972,16 +1972,23 @@ qemuGetDomainHupageMemPath(virQEMUDriverPtr driver, int -qemuGetMemoryBackingDomainPath(const virDomainDef *def, - virQEMUDriverConfigPtr cfg, +qemuGetMemoryBackingDomainPath(virQEMUDriverPtr driver, + const virDomainDef *def, char **path) { + g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver); + const char *root = virQEMUDriverGetEmbedRoot(driver); g_autofree char *shortName = NULL; if (!(shortName = virDomainDefGetShortName(def))) return -1; - *path = g_strdup_printf("%s/%s", cfg->memoryBackingDir, shortName); + if (root && !STRPREFIX(cfg->memoryBackingDir, root)) { + g_autofree char * hash = virDomainDriverGenerateRootHash("qemu", root); + *path = g_strdup_printf("%s/%s-%s", cfg->memoryBackingDir, hash, shortName); + } else { + *path = g_strdup_printf("%s/%s", cfg->memoryBackingDir, shortName); + } return 0; } @@ -1989,8 +1996,8 @@ qemuGetMemoryBackingDomainPath(const virDomainDef *def, /** * qemuGetMemoryBackingPath: + * @driver: the qemu driver * @def: domain definition - * @cfg: the driver config * @alias: memory object alias * @memPath: constructed path * @@ -2000,8 +2007,8 @@ qemuGetMemoryBackingDomainPath(const virDomainDef *def, * -1 otherwise (with error reported). */ int -qemuGetMemoryBackingPath(const virDomainDef *def, - virQEMUDriverConfigPtr cfg, +qemuGetMemoryBackingPath(virQEMUDriverPtr driver, + const virDomainDef *def, const char *alias, char **memPath) { @@ -2014,7 +2021,7 @@ qemuGetMemoryBackingPath(const virDomainDef *def, return -1; } - if (qemuGetMemoryBackingDomainPath(def, cfg, &domainPath) < 0) + if (qemuGetMemoryBackingDomainPath(driver, def, &domainPath) < 0) return -1; *memPath = g_strdup_printf("%s/%s", domainPath, alias); diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index 0099ac5be4..31c23f7638 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -399,10 +399,10 @@ int qemuGetDomainHupageMemPath(virQEMUDriverPtr driver, unsigned long long pagesize, char **memPath); -int qemuGetMemoryBackingDomainPath(const virDomainDef *def, - virQEMUDriverConfigPtr cfg, +int qemuGetMemoryBackingDomainPath(virQEMUDriverPtr driver, + const virDomainDef *def, char **path); -int qemuGetMemoryBackingPath(const virDomainDef *def, - virQEMUDriverConfigPtr cfg, +int qemuGetMemoryBackingPath(virQEMUDriverPtr driver, + const virDomainDef *def, const char *alias, char **memPath); diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index aa6741e1f4..c1250ef66c 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -3894,7 +3894,7 @@ qemuProcessBuildDestroyMemoryPaths(virQEMUDriverPtr driver, if (!build || shouldBuildMB) { g_autofree char *path = NULL; - if (qemuGetMemoryBackingDomainPath(vm->def, cfg, &path) < 0) + if (qemuGetMemoryBackingDomainPath(driver, vm->def, &path) < 0) return -1; if (qemuProcessBuildDestroyMemoryPathsImpl(driver, vm, @@ -3911,10 +3911,9 @@ qemuProcessDestroyMemoryBackingPath(virQEMUDriverPtr driver, virDomainObjPtr vm, virDomainMemoryDefPtr mem) { - g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver); g_autofree char *path = NULL; - if (qemuGetMemoryBackingPath(vm->def, cfg, mem->info.alias, &path) < 0) + if (qemuGetMemoryBackingPath(driver, vm->def, mem->info.alias, &path) < 0) return -1; if (unlink(path) < 0 && -- 2.24.1

On Thu, Mar 26, 2020 at 04:15:14PM +0100, Michal Privoznik wrote:
So far, libvirt generates the following path for memory:
$memoryBackingDir/$id-$shortName/ram-nodeN
where $memoryBackingDir is the path where QEMU mmaps() memory for the guest (e.g. /var/lib/libvirt/qemu/ram), $id is domain ID and $shortName is shortened version of domain name. So for instance, the generated path may look something like this:
/var/lib/libvirt/qemu/ram/1-QEMUGuest/ram-node0
While in case of embed driver the following path would be generated by default:
$root/lib/qemu/ram/1-QEMUGuest/ram-node0
which is not clashing with other embed drivers, we allow users to override the default and have all embed drivers use the same prefix. This can create clashing paths. Fortunately, we can reuse the approach for machined name generation (v6.1.0-178-gc9bd08ee35) and include part of hash of the root in the generated path.
Note, the important change is in qemuGetMemoryBackingBasePath(). The rest is needed to pass driver around.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_command.c | 15 +++++++-------- src/qemu/qemu_conf.c | 21 ++++++++++++++------- src/qemu/qemu_conf.h | 8 ++++---- src/qemu/qemu_process.c | 5 ++--- 4 files changed, 27 insertions(+), 22 deletions(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

So far, libvirt generates the following path for automatic dumps: $autoDumpPath/$id-$shortName-$timestamp where $autoDumpPath is where libvirt stores dumps of guests (e.g. /var/lib/libvirt/qemu/dump), $id is domain ID and $shortName is shortened version of domain name. So for instance, the generated path may look something like this: /var/lib/libvirt/qemu/dump/1-QEMUGuest-2020-03-25-10:40:50 While in case of embed driver the following path would be generated by default: $root/lib/libvirt/qemu/dump/1-QEMUGuest-2020-03-25-10:40:50 which is not clashing with other embed drivers, we allow users to override the default and have all embed drivers use the same prefix. This can create clashing paths. Fortunately, we can reuse the approach for machined name generation (v6.1.0-178-gc9bd08ee35) and include part of hash of the root in the generated path. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_driver.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 0033a72d0b..36b94f3f1b 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -4104,6 +4104,7 @@ static char * getAutoDumpPath(virQEMUDriverPtr driver, virDomainObjPtr vm) { + const char *root = virQEMUDriverGetEmbedRoot(driver); g_autofree char *domname = virDomainDefGetShortName(vm->def); g_autoptr(GDateTime) now = g_date_time_new_now_local(); g_autofree char *nowstr = NULL; @@ -4116,6 +4117,11 @@ getAutoDumpPath(virQEMUDriverPtr driver, nowstr = g_date_time_format(now, "%Y-%m-%d-%H:%M:%S"); + if (root && !STRPREFIX(cfg->autoDumpPath, root)) { + g_autofree char * hash = virDomainDriverGenerateRootHash(QEMU_DRIVER_NAME, root); + return g_strdup_printf("%s/%s-%s-%s", cfg->autoDumpPath, hash, domname, nowstr); + } + return g_strdup_printf("%s/%s-%s", cfg->autoDumpPath, domname, nowstr); } -- 2.24.1

On Thu, Mar 26, 2020 at 04:15:15PM +0100, Michal Privoznik wrote:
So far, libvirt generates the following path for automatic dumps:
$autoDumpPath/$id-$shortName-$timestamp
where $autoDumpPath is where libvirt stores dumps of guests (e.g. /var/lib/libvirt/qemu/dump), $id is domain ID and $shortName is shortened version of domain name. So for instance, the generated path may look something like this:
/var/lib/libvirt/qemu/dump/1-QEMUGuest-2020-03-25-10:40:50
While in case of embed driver the following path would be generated by default:
$root/lib/libvirt/qemu/dump/1-QEMUGuest-2020-03-25-10:40:50
which is not clashing with other embed drivers, we allow users to override the default and have all embed drivers use the same prefix. This can create clashing paths. Fortunately, we can reuse the approach for machined name generation (v6.1.0-178-gc9bd08ee35) and include part of hash of the root in the generated path.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_driver.c | 6 ++++++ 1 file changed, 6 insertions(+)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
participants (3)
-
Daniel P. Berrangé
-
Michal Privoznik
-
Michal Prívozník