* [LTP] [PATCH] tst_virt: Make use of systemd-detect-virt if available
@ 2016-08-18 12:38 Cyril Hrubis
2016-08-18 13:15 ` Jan Stancek
0 siblings, 1 reply; 4+ messages in thread
From: Cyril Hrubis @ 2016-08-18 12:38 UTC (permalink / raw)
To: ltp
The problem is that there is no defined way to detect if we are inside
of a virtual machine, only bunch of heuristics. If you look at the
src/basic/virt.c in systemd source code it's ~500 lines of code that
tries many different things to try to guess if we are running
virtualized and under what hypervisor.
Currenlty LTP fails to detect KVM in OpenStack Cloud (and possibly many
more) since the /proc/cpuinfo does not contain the QEMU string there.
You can also boot QEMU with non-default -cpu option and you will get the
same result.
The easiest solution is to try the systemd-detect-virt first if it
exists, then we fall back to the previously implemented detections for
older distributions. This is not complete solution though, as the
detection still fails with older and non-systemd distributions.
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
lib/tst_virt.c | 43 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/lib/tst_virt.c b/lib/tst_virt.c
index 87f73dc..d40ba39 100644
--- a/lib/tst_virt.c
+++ b/lib/tst_virt.c
@@ -64,8 +64,51 @@ static int is_xen(void)
return 0;
}
+static int try_systemd_detect_virt(void)
+{
+ FILE *f;
+ char virt_type[64];
+ int ret;
+
+ /* See tst_run_cmd.c */
+ void *old_handler = signal(SIGCHLD, SIG_DFL);
+
+ f = popen("systemd-detect-virt", "r");
+ if (!f) {
+ signal(SIGCHLD, old_handler);
+ return 0;
+ }
+
+ if (!fgets(virt_type, sizeof(virt_type), f))
+ virt_type[0] = '\0';
+
+ ret = pclose(f);
+
+ signal(SIGCHLD, old_handler);
+
+ /*
+ * systemd-detect-virt not found by shell or no virtualization detected
+ * (systemd-detect-virt returns non-zero)
+ */
+ if (ret)
+ return 0;
+
+ if (strncmp("kvm", virt_type, 3))
+ return VIRT_KVM;
+
+ if (strncmp("xen", virt_type, 3))
+ return VIRT_XEN;
+
+ return 0;
+}
+
int tst_is_virt(int virt_type)
{
+ int ret = try_systemd_detect_virt();
+
+ if (ret)
+ return ret == virt_type;
+
switch (virt_type) {
case VIRT_XEN:
return is_xen();
--
2.7.3
--
Cyril Hrubis
chrubis@suse.cz
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [LTP] [PATCH] tst_virt: Make use of systemd-detect-virt if available
2016-08-18 12:38 [LTP] [PATCH] tst_virt: Make use of systemd-detect-virt if available Cyril Hrubis
@ 2016-08-18 13:15 ` Jan Stancek
2016-08-25 14:09 ` Cyril Hrubis
2016-08-31 13:51 ` Cyril Hrubis
0 siblings, 2 replies; 4+ messages in thread
From: Jan Stancek @ 2016-08-18 13:15 UTC (permalink / raw)
To: ltp
----- Original Message -----
> From: "Cyril Hrubis" <chrubis@suse.cz>
> To: ltp@lists.linux.it
> Cc: "Jan Stancek" <jstancek@redhat.com>
> Sent: Thursday, 18 August, 2016 2:38:09 PM
> Subject: [PATCH] tst_virt: Make use of systemd-detect-virt if available
>
> The problem is that there is no defined way to detect if we are inside
> of a virtual machine, only bunch of heuristics. If you look at the
> src/basic/virt.c in systemd source code it's ~500 lines of code that
> tries many different things to try to guess if we are running
> virtualized and under what hypervisor.
>
> Currenlty LTP fails to detect KVM in OpenStack Cloud (and possibly many
> more) since the /proc/cpuinfo does not contain the QEMU string there.
I noticed the same in some RHEL KVM guests.
> You can also boot QEMU with non-default -cpu option and you will get the
> same result.
>
> The easiest solution is to try the systemd-detect-virt first if it
> exists, then we fall back to the previously implemented detections for
> older distributions. This is not complete solution though, as the
> detection still fails with older and non-systemd distributions.
>
> Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
> ---
We could also add a function that tries "virt-what", to increase our chances.
Anyway, I think it's good idea to not rely just on /proc/cpuinfo.
Regards,
Jan
^ permalink raw reply [flat|nested] 4+ messages in thread
* [LTP] [PATCH] tst_virt: Make use of systemd-detect-virt if available
2016-08-18 13:15 ` Jan Stancek
@ 2016-08-25 14:09 ` Cyril Hrubis
2016-08-31 13:51 ` Cyril Hrubis
1 sibling, 0 replies; 4+ messages in thread
From: Cyril Hrubis @ 2016-08-25 14:09 UTC (permalink / raw)
To: ltp
Hi!
> > You can also boot QEMU with non-default -cpu option and you will get the
> > same result.
> >
> > The easiest solution is to try the systemd-detect-virt first if it
> > exists, then we fall back to the previously implemented detections for
> > older distributions. This is not complete solution though, as the
> > detection still fails with older and non-systemd distributions.
> >
> > Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
> > ---
>
> We could also add a function that tries "virt-what", to increase our chances.
> Anyway, I think it's good idea to not rely just on /proc/cpuinfo.
Looking around virt-what does not seem to be installed by default on
anything I have here so it does not seem to add too much value. Feel
free to add it if you have a usecase.
Another option would be more or less copying what systemd-detect-virt
does, checking dmi, etc. But that is 500 lines of heuristics and
maintaining that would take take non-trivial amount of effort I would
rather like to spend on something more productive...
--
Cyril Hrubis
chrubis@suse.cz
^ permalink raw reply [flat|nested] 4+ messages in thread
* [LTP] [PATCH] tst_virt: Make use of systemd-detect-virt if available
2016-08-18 13:15 ` Jan Stancek
2016-08-25 14:09 ` Cyril Hrubis
@ 2016-08-31 13:51 ` Cyril Hrubis
1 sibling, 0 replies; 4+ messages in thread
From: Cyril Hrubis @ 2016-08-31 13:51 UTC (permalink / raw)
To: ltp
Hi!
> We could also add a function that tries "virt-what", to increase our chances.
> Anyway, I think it's good idea to not rely just on /proc/cpuinfo.
Pushed with your Acked-by.
--
Cyril Hrubis
chrubis@suse.cz
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-08-31 13:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-18 12:38 [LTP] [PATCH] tst_virt: Make use of systemd-detect-virt if available Cyril Hrubis
2016-08-18 13:15 ` Jan Stancek
2016-08-25 14:09 ` Cyril Hrubis
2016-08-31 13:51 ` Cyril Hrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox