diff --git a/debian/build/tools/hv/.gitignore b/debian/build/tools/hv/.gitignore index b6961eb1b..568499933 100644 --- a/debian/build/tools/hv/.gitignore +++ b/debian/build/tools/hv/.gitignore @@ -1 +1,2 @@ /hv_*_daemon +/check-hyperv diff --git a/debian/build/tools/hv/Makefile b/debian/build/tools/hv/Makefile index a1fddeb49..fb3ff37a2 100644 --- a/debian/build/tools/hv/Makefile +++ b/debian/build/tools/hv/Makefile @@ -1,7 +1,8 @@ PROGS = \ hv_fcopy_daemon \ hv_kvp_daemon \ - hv_vss_daemon + hv_vss_daemon \ + check-hyperv OUTDIR = tools/hv prefix = /usr/sbin @@ -9,7 +10,8 @@ prefix = /usr/sbin include ../../Makefile.inc install-local-progs: $(PROGS) - @for p in $^; do \ + @for p in $(filter-out check-hyperv,$^); do \ echo " install -m755 '$$p' '$(DESTDIR)/$(prefix)'"; \ install -D -m755 "$$p" "$(DESTDIR)/$(prefix)/$$(basename $$p)"; \ done + install -D -m755 check-hyperv '$(DESTDIR)/lib/hyperv-daemons/check-hyperv' diff --git a/debian/build/tools/hv/check-hyperv.c b/debian/build/tools/hv/check-hyperv.c new file mode 100644 index 000000000..4d2b6f515 --- /dev/null +++ b/debian/build/tools/hv/check-hyperv.c @@ -0,0 +1,103 @@ +/* + * This program is derived from systemd. + * + * Copyright 2011 Lennart Poettering + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; If not, see . + */ + +#include +#include +#include + +#define streq(a, b) (!strcmp(a, b)) +#define ELEMENTSOF(a) (sizeof(a) / sizeof((a)[0])) + +enum { + VIRTUALIZATION_NONE, + VIRTUALIZATION_VM_OTHER, + VIRTUALIZATION_MICROSOFT, +}; + +static int detect_vm_cpuid(void) { + + static const struct { + const char *cpuid; + int id; + } cpuid_vendor_table[] = { + /* http://msdn.microsoft.com/en-us/library/ff542428.aspx */ + { "Microsoft Hv", VIRTUALIZATION_MICROSOFT }, + }; + + uint32_t eax, ecx; + bool hypervisor; + + /* http://lwn.net/Articles/301888/ */ + +#if defined (__i386__) +#define REG_a "eax" +#define REG_b "ebx" +#elif defined (__amd64__) +#define REG_a "rax" +#define REG_b "rbx" +#endif + + /* First detect whether there is a hypervisor */ + eax = 1; + __asm__ __volatile__ ( + /* ebx/rbx is being used for PIC! */ + " push %%"REG_b" \n\t" + " cpuid \n\t" + " pop %%"REG_b" \n\t" + + : "=a" (eax), "=c" (ecx) + : "0" (eax) + ); + + hypervisor = !!(ecx & 0x80000000U); + + if (hypervisor) { + union { + uint32_t sig32[3]; + char text[13]; + } sig = {}; + unsigned j; + + /* There is a hypervisor, see what it is */ + eax = 0x40000000U; + __asm__ __volatile__ ( + /* ebx/rbx is being used for PIC! */ + " push %%"REG_b" \n\t" + " cpuid \n\t" + " mov %%ebx, %1 \n\t" + " pop %%"REG_b" \n\t" + + : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2]) + : "0" (eax) + ); + + for (j = 0; j < ELEMENTSOF(cpuid_vendor_table); j ++) + if (streq(sig.text, cpuid_vendor_table[j].cpuid)) + return cpuid_vendor_table[j].id; + + return VIRTUALIZATION_VM_OTHER; + } + + return VIRTUALIZATION_NONE; +} + +int main(void) +{ + return detect_vm_cpuid() != VIRTUALIZATION_MICROSOFT; +} diff --git a/debian/changelog b/debian/changelog index 886434600..76e72f7a6 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +linux-tools (4.4-3) UNRELEASED; urgency=medium + + * hyperv-daemons: Add init scripts + + -- Ben Hutchings Sun, 21 Feb 2016 15:03:44 +0000 + linux-tools (4.4-2) unstable; urgency=medium * linux-perf: Include version number in strace groups installation directory diff --git a/debian/copyright b/debian/copyright index 1e7200f61..7ba027e84 100644 --- a/debian/copyright +++ b/debian/copyright @@ -30,3 +30,22 @@ License: GPL-2 Files: debian/* Copyright: 2006-2012 Debian kernel team License: GPL-2 + +Files: debian/build/tools/hv/check-hyperv.c +Copyright: 2011 Lennart Poettering +License: LGPL-2.1 + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 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 + Lesser General Public License for more details. + . + You should have received a copy of the GNU Lesser General Public License + along with this program; If not, see . + . + On Debian systems, the complete text of the GNU Lesser General Public + License version 2.1 can be found in `/usr/share/common-licenses/LGPL-2.1'. diff --git a/debian/hyperv-daemons.hv-fcopy-daemon.init b/debian/hyperv-daemons.hv-fcopy-daemon.init new file mode 100755 index 000000000..24d944d01 --- /dev/null +++ b/debian/hyperv-daemons.hv-fcopy-daemon.init @@ -0,0 +1,110 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: hyperv-daemons.hv-fcopy-daemon +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Hyper-V file copy service (FCOPY) daemon +### END INIT INFO + +PATH=/sbin:/usr/sbin:/bin:/usr/bin +DESC="Hyper-V file copy service (FCOPY) daemon" +NAME=hv_fcopy_daemon +DAEMON=/usr/sbin/$NAME +PIDFILE=/var/run/$NAME.pid +SCRIPTNAME=/etc/init.d/hyperv-daemons.hv-fcopy-daemon + +# Exit if the package is not installed +[ -x "$DAEMON" ] || exit 0 + +# Exit if we are not running under Hyper-V or the kernel device does not exist +/lib/hyperv-daemons/check-hyperv || exit 0 +[ -e "/dev/vmbus/hv_fcopy" ] || exit 0 + +# Load the VERBOSE setting and other rcS variables +. /lib/init/vars.sh + +# Define LSB log_* functions. +. /lib/lsb/init-functions + +# +# Function that starts the daemon/service +# +do_start() +{ + # Return + # 0 if daemon has been started + # 1 if daemon was already running + # 2 if daemon could not be started + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \ + || return 1 + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --background --make-pidfile -- -n \ + || return 2 +} + +# +# Function that stops the daemon/service +# +do_stop() +{ + # Return + # 0 if daemon has been stopped + # 1 if daemon was already stopped + # 2 if daemon could not be stopped + # other if a failure occurred + start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME + [ "$?" = 2 ] && return 2 + start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON + RETVAL=$? + [ "$RETVAL" = 2 ] && return 2 + # Many daemons don't delete their pidfiles when they exit. + rm -f $PIDFILE + return "$RETVAL" +} + +case "$1" in + start) + [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" + do_start + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + stop) + [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" + do_stop + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + status) + status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? + ;; + restart|force-reload) + log_daemon_msg "Restarting $DESC" "$NAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac + ;; + *) + echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 + exit 3 + ;; +esac + +: diff --git a/debian/hyperv-daemons.hv-kvp-daemon.init b/debian/hyperv-daemons.hv-kvp-daemon.init new file mode 100755 index 000000000..e5908ec28 --- /dev/null +++ b/debian/hyperv-daemons.hv-kvp-daemon.init @@ -0,0 +1,109 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: hyperv-daemons.hv-kvp-daemon +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Hyper-V key-value pair (KVP) daemon +### END INIT INFO + +PATH=/sbin:/usr/sbin:/bin:/usr/bin +DESC="Hyper-V key-value pair (KVP) daemon" +NAME=hv_kvp_daemon +DAEMON=/usr/sbin/$NAME +PIDFILE=/var/run/$NAME.pid +SCRIPTNAME=/etc/init.d/hyperv-daemons.hv-kvp-daemon + +# Exit if the package is not installed +[ -x "$DAEMON" ] || exit 0 + +# Exit if we are not running under Hyper-V +/lib/hyperv-daemons/check-hyperv || exit 0 + +# Load the VERBOSE setting and other rcS variables +. /lib/init/vars.sh + +# Define LSB log_* functions. +. /lib/lsb/init-functions + +# +# Function that starts the daemon/service +# +do_start() +{ + # Return + # 0 if daemon has been started + # 1 if daemon was already running + # 2 if daemon could not be started + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \ + || return 1 + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --background --make-pidfile -- -n \ + || return 2 +} + +# +# Function that stops the daemon/service +# +do_stop() +{ + # Return + # 0 if daemon has been stopped + # 1 if daemon was already stopped + # 2 if daemon could not be stopped + # other if a failure occurred + start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME + [ "$?" = 2 ] && return 2 + start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON + RETVAL=$? + [ "$RETVAL" = 2 ] && return 2 + # Many daemons don't delete their pidfiles when they exit. + rm -f $PIDFILE + return "$RETVAL" +} + +case "$1" in + start) + [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" + do_start + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + stop) + [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" + do_stop + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + status) + status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? + ;; + restart|force-reload) + log_daemon_msg "Restarting $DESC" "$NAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac + ;; + *) + echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 + exit 3 + ;; +esac + +: diff --git a/debian/hyperv-daemons.hv-vss-daemon.init b/debian/hyperv-daemons.hv-vss-daemon.init new file mode 100755 index 000000000..aff28fcbe --- /dev/null +++ b/debian/hyperv-daemons.hv-vss-daemon.init @@ -0,0 +1,109 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: hyperv-daemons.hv-vss-daemon +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Hyper-V volume shadow copy service (VSS) daemon +### END INIT INFO + +PATH=/sbin:/usr/sbin:/bin:/usr/bin +DESC="Hyper-V volume shadow copy service (VSS) daemon" +NAME=hv_vss_daemon +DAEMON=/usr/sbin/$NAME +PIDFILE=/var/run/$NAME.pid +SCRIPTNAME=/etc/init.d/hyperv-daemons.hv-vss-daemon + +# Exit if the package is not installed +[ -x "$DAEMON" ] || exit 0 + +# Exit if we are not running under Hyper-V +/lib/hyperv-daemons/check-hyperv || exit 0 + +# Load the VERBOSE setting and other rcS variables +. /lib/init/vars.sh + +# Define LSB log_* functions. +. /lib/lsb/init-functions + +# +# Function that starts the daemon/service +# +do_start() +{ + # Return + # 0 if daemon has been started + # 1 if daemon was already running + # 2 if daemon could not be started + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \ + || return 1 + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --background --make-pidfile -- -n \ + || return 2 +} + +# +# Function that stops the daemon/service +# +do_stop() +{ + # Return + # 0 if daemon has been stopped + # 1 if daemon was already stopped + # 2 if daemon could not be stopped + # other if a failure occurred + start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME + [ "$?" = 2 ] && return 2 + start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON + RETVAL=$? + [ "$RETVAL" = 2 ] && return 2 + # Many daemons don't delete their pidfiles when they exit. + rm -f $PIDFILE + return "$RETVAL" +} + +case "$1" in + start) + [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" + do_start + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + stop) + [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" + do_stop + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + status) + status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? + ;; + restart|force-reload) + log_daemon_msg "Restarting $DESC" "$NAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac + ;; + *) + echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 + exit 3 + ;; +esac + +: diff --git a/debian/rules.real b/debian/rules.real index 1eb2cf8e4..ca34334c8 100644 --- a/debian/rules.real +++ b/debian/rules.real @@ -95,9 +95,19 @@ install-hyperv-daemons: $(STAMPS_DIR)/build dh_prep $(MAKE) -C $(BUILD_DIR)/tools/hv install top_srcdir=$(CURDIR) DESTDIR=$(DIR) dh_install + for service in fcopy kvp vss; do \ + install -D -m755 debian/hyperv-daemons.hv-$$service-daemon.init \ + $(DIR)/etc/init.d/hyperv-daemons.hv-$$service-daemon \ + || break; \ + done dh_installchangelogs dh_installdocs dh_systemd_enable + for service in fcopy kvp vss; do \ + dh_installinit --name hyperv-daemons.hv-$$service-daemon \ + --onlyscripts \ + || break; \ + done dh_systemd_start dh_lintian dh_strip diff --git a/debian/templates/control.main.in b/debian/templates/control.main.in index 6106752f6..9da6d26ef 100644 --- a/debian/templates/control.main.in +++ b/debian/templates/control.main.in @@ -55,7 +55,7 @@ Description: USB device sharing system over IP network Package: hyperv-daemons Architecture: i386 amd64 x32 -Depends: ${shlibs:Depends}, ${misc:Depends} +Depends: lsb-base (>= 3.2-14), ${shlibs:Depends}, ${misc:Depends} Section: admin Description: Support daemons for Linux running on Hyper-V Suite of daemons for Linux guests running on Hyper-V, consisting of