1
0
forked from pool/util-linux
OBS User unknown 2008-09-24 15:39:02 +00:00 committed by Git OBS Bridge
parent 5f36eb3e3c
commit bb14144eb8
3 changed files with 267 additions and 2 deletions

View File

@ -0,0 +1,250 @@
Index: util-linux-ng-2.14.1/sys-utils/hypervisor.c
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ util-linux-ng-2.14.1/sys-utils/hypervisor.c 2008-09-24 12:21:05.000000000 +0200
@@ -0,0 +1,169 @@
+/* Detect the presence of a hypervisor
+ * Copyright (C) 2008 Novell, Inc.
+ * Author: Ky Srinivasan <ksrinivasan@novell.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <getopt.h>
+
+static int opt_quiet = 0;
+
+struct option options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "quiet", no_argument, NULL, 'q' },
+ { NULL, 9, NULL, 0 }
+};
+
+enum {
+ HYPER_NONE = 0,
+ HYPER_XEN,
+ HYPER_KVM,
+ HYPER_MSHV,
+ HYPER_MSHV_XENPV
+};
+
+static inline void cpuid(unsigned int op,
+ unsigned int *eax, unsigned int *ebx,
+ unsigned int *ecx, unsigned int *edx)
+{
+ __asm__("cpuid"
+ : "=a" (*eax),
+ "=b" (*ebx),
+ "=c" (*ecx),
+ "=d" (*edx)
+ : "0" (op), "c"(0));
+}
+
+static int hypervisor(void)
+{
+ unsigned int eax, ebx, ecx, edx;
+ char signature[13];
+ int xen_pv =0;
+
+ /*
+ * First check if we are running in a para-virtualized guest.
+ */
+ if ((!access("/sys/hypervisor", F_OK) ||
+ (!access("/proc/xen", F_OK)))) {
+ /*
+ * For now we are only dealing with para-virtualized
+ * Linux guests (para-virtualized on Xen). So, this must be
+ * Xen based.
+ */
+ xen_pv = 1;
+ }
+ cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
+ *(unsigned int*)(signature + 0) = ebx;
+ *(unsigned int*)(signature + 4) = ecx;
+ *(unsigned int*)(signature + 8) = edx;
+ signature[12] = 0;
+
+ if ((!strncmp("XenVMMXenVMM", signature, 12) || (xen_pv)))
+ return HYPER_XEN;
+ if (!strncmp("KVMKVMKVM", signature, 9))
+ return HYPER_KVM;
+ if (!strncmp("Microsoft Hv", signature, 12)) {
+ if (xen_pv)
+ return HYPER_MSHV_XENPV;
+ else
+ return HYPER_MSHV;
+ }
+
+ return 0;
+}
+
+static void help(void)
+{
+ printf("Usage: hypervisor [OPTION]\n");
+ printf("Detects the presence/absence of a hypervisor.\n\n");
+ printf("Options:\n");
+ printf(" -h, --help\tShow this message and exit.\n");
+ printf(" -q, --quiet\tDo not show output message.\n\n");
+ printf("Exit status:\n");
+ printf(" 0\tNo hypervisor detected.\n");
+ printf(" 1\tXen detected.\n");
+ printf(" 2\tKVM detected.\n");
+ printf(" 3\tHyperV detected.\n");
+ printf(" 4\tHyperV emulating Xen detected.\n");
+}
+
+
+/*
+ * Program to determine if we are being hosted on the hypervisor.
+ * If the exit status is 0; we are running on bare hardware. The details of
+ * non-zero return values are as follows:
+ *
+ * 0: No hypervisor (running on bare hardware)
+ * 1: Xen is the hypervisor
+ * 2: KVM is the hypervisor
+ * 3: Veridian
+ * 4: Veridian hypervisor emulating Xen.
+ * TODO: VmWare Detection.
+ */
+int main(int argc, char **argv)
+{
+ int option_index, c;
+ int ret;
+
+ while (1) {
+ c = getopt_long(argc, argv, "hq", options, &option_index);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'h':
+ help();
+ exit(EXIT_SUCCESS);
+ case 'q':
+ opt_quiet = 1;
+ break;
+ default:
+ break;
+ }
+ }
+
+ ret = hypervisor();
+
+ if (!opt_quiet) {
+ switch (ret) {
+ case HYPER_NONE:
+ printf("No");
+ break;
+ case HYPER_XEN:
+ printf("Xen");
+ break;
+ case HYPER_KVM:
+ printf("KVM");
+ break;
+ case HYPER_MSHV:
+ case HYPER_MSHV_XENPV:
+ printf("Microsoft");
+ break;
+ default:
+ break;
+ }
+ printf(" Hypervisor found\n");
+ }
+
+ return ret;
+}
+
Index: util-linux-ng-2.14.1/sys-utils/Makefile.am
===================================================================
--- util-linux-ng-2.14.1.orig/sys-utils/Makefile.am 2008-09-10 11:02:43.000000000 +0200
+++ util-linux-ng-2.14.1/sys-utils/Makefile.am 2008-09-24 13:03:21.000000000 +0200
@@ -35,6 +35,11 @@ RDEV_LINKS += ramsize vidmode rootflags
dist_man_MANS += rdev.8
endif
+if BUILD_HYPERVISOR
+bin_PROGRAMS += hypervisor
+dist_man_MANS += hypervisor.1
+endif
+
SETARCH_LINKS = linux32 linux64
if ARCH_S390
Index: util-linux-ng-2.14.1/configure.ac
===================================================================
--- util-linux-ng-2.14.1.orig/configure.ac 2008-09-10 12:13:27.000000000 +0200
+++ util-linux-ng-2.14.1/configure.ac 2008-09-24 11:43:15.000000000 +0200
@@ -471,6 +471,12 @@ AC_ARG_ENABLE([elvtune],
)
AM_CONDITIONAL(BUILD_ELVTUNE, test "x$enable_elvtune" = xyes)
+AC_ARG_ENABLE([hypervisor],
+ AS_HELP_STRING([--enable-hypervisor], [build hypervisor]),
+ [], enable_hypervisor=no
+)
+AM_CONDITIONAL(BUILD_HYPERVISOR, test "x$enable_hypervisor" = xyes)
+
AC_ARG_ENABLE([init],
AS_HELP_STRING([--enable-init], [build simpleinit, shutdown, initctl]),
Index: util-linux-ng-2.14.1/sys-utils/hypervisor.1
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ util-linux-ng-2.14.1/sys-utils/hypervisor.1 2008-09-24 13:06:24.000000000 +0200
@@ -0,0 +1,38 @@
+.TH hypervisor 1 "September 2008" "" "User Commands"
+.SH NAME
+hypervisor \- detect presence of a hypervisor
+.SH SYNOPSIS
+.B hypervisor
+[\fIOPTION\fR]
+.SH DESCRIPTION
+Detects the presence/absence of a hypervisor.
+.SH OPTIONS
+.TP
+\fB\-h\fR, \fB\-\-help\fR
+Show this message and exit.
+.TP
+\fB\-q\fR, \fB\-\-quiet\fR
+Do not show output message.
+.SH EXIT CODES
+.TP
+0
+No hypervisor detected.
+.TP
+1
+Xen detected.
+.TP
+2
+KVM detected.
+.TP
+3
+HyperV detected.
+.TP
+4
+HyperV emulating Xen detected.
+.SH AUTHOR
+Ky Srinivasan <ksrinivasan@novell.com>
+.SH COPYRIGHT
+This is free software. You may redistribute copies of it under the terms
+of the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
+There is NO WARRANTY, to the extent permitted by law.
+

View File

@ -1,3 +1,10 @@
-------------------------------------------------------------------
Wed Sep 24 11:28:07 CEST 2008 - mkoenig@suse.de
- add new tool /bin/hypervisor for x86, x86_64
from Ky Srinivasan <ksrinivasan@novell.com>
to detect the presence of a hypervisor [fate#303051]
-------------------------------------------------------------------
Wed Sep 10 15:58:52 CEST 2008 - mkoenig@suse.de

View File

@ -30,7 +30,7 @@ License: BSD 3-Clause; GPL v2 or later
Group: System/Base
AutoReqProv: on
Version: 2.14.1
Release: 1
Release: 2
Requires: %name-lang = %{version}
Summary: A collection of basic system utilities
Source: ftp://ftp.kernel.org/pub/linux/utils/util-linux/%name-ng-%version.tar.bz2
@ -71,6 +71,7 @@ Patch4: util-linux-2.13-hwclock_rtc_wait_busy_tempfix.patch
#
Patch5: util-linux-2.13.1-fdisk_cfdisk_yesno.patch
Patch7: util-linux-2.14-mount_retry_on_nomedium.patch
Patch8: util-linux-2.14.1-hypervisor_detection.patch
# crypto patch
Patch20: util-linux-mount_losetup_crypto.patch
##
@ -114,6 +115,7 @@ Authors:
%patch4 -p1
%patch5 -p1
%patch7 -p1
%patch8 -p1
%patch20 -p1
cp %{SOURCE7} %{SOURCE8} .
#
@ -183,7 +185,7 @@ CFLAGS=-DCONFIG_SMP
# architecture dependent builds
BUILD_ENABLE=
%ifarch %ix86 x86_64
BUILD_ENABLE="--enable-rdev"
BUILD_ENABLE="--enable-rdev --enable-hypervisor"
%endif
# Use autogen, when building from git tree
autoreconf -fi
@ -534,10 +536,12 @@ fi
/sbin/sfdisk
%endif
%ifarch %ix86 x86_64
/bin/hypervisor
/usr/sbin/ramsize
/usr/sbin/rdev
/usr/sbin/rootflags
/usr/sbin/vidmode
%{_mandir}/man1/hypervisor.1.gz
%{_mandir}/man8/ramsize.8.gz
%{_mandir}/man8/rdev.8.gz
%{_mandir}/man8/rootflags.8.gz
@ -579,6 +583,10 @@ fi
#%endif
%changelog
* Wed Sep 24 2008 mkoenig@suse.de
- add new tool /bin/hypervisor for x86, x86_64
from Ky Srinivasan <ksrinivasan@novell.com>
to detect the presence of a hypervisor [fate#303051]
* Wed Sep 10 2008 mkoenig@suse.de
- update to version 2.14.1
* fdisk: don't check for GPT when asked for disk size only