summaryrefslogtreecommitdiff
path: root/contrib/hyperv
diff options
context:
space:
mode:
authorSepherosa Ziehau <sephe@FreeBSD.org>2016-11-15 02:36:12 +0000
committerSepherosa Ziehau <sephe@FreeBSD.org>2016-11-15 02:36:12 +0000
commit168fce73b59d6023cab45d063a452551a1f2103e (patch)
tree91c618511d1add8ec8d6330790266410dba10b04 /contrib/hyperv
parentf8a67728f3b657f7f45a402e6841bee1736862eb (diff)
downloadsrc-test-168fce73b59d6023cab45d063a452551a1f2103e.tar.gz
src-test-168fce73b59d6023cab45d063a452551a1f2103e.zip
hyperv/vss: Add driver and tools for VSS
VSS stands for "Volume Shadow Copy Service". Unlike virtual machine snapshot, it only takes snapshot for the virtual disks, so both filesystem and applications have to aware of it, and cooperate the whole VSS process. This driver exposes two device files to the userland: /dev/hv_fsvss_dev Normally userland programs should _not_ mess with this device file. It is currently used by the hv_vss_daemon(8), which freezes and thaws the filesystem. NOTE: currently only UFS is supported, if the system mounts _any_ other filesystems, the hv_vss_daemon(8) will veto the VSS process. If hv_vss_daemon(8) was disabled, then this device file must be opened, and proper ioctls must be issued to keep the VSS working. /dev/hv_appvss_dev Userland application can opened this device file to receive the VSS freeze notification, hold the VSS for a while (mainly to flush application data to filesystem), release the VSS process, and receive the VSS thaw notification i.e. applications can run again. The VSS will still work, even if this device file is not opened. However, only filesystem consistency is promised, if this device file is not opened or is not operated properly. hv_vss_daemon(8) is started by devd(8) by default. It can be disabled by editting /etc/devd/hyperv.conf. Submitted by: Hongjiang Zhang <honzhan microsoft com> Reviewed by: kib, mckusick MFC after: 3 weeks Sponsored by: Microsoft Differential Revision: https://reviews.freebsd.org/D8224
Notes
Notes: svn path=/head/; revision=308664
Diffstat (limited to 'contrib/hyperv')
-rw-r--r--contrib/hyperv/tools/hv_vss_daemon.888
-rw-r--r--contrib/hyperv/tools/hv_vss_daemon.c275
2 files changed, 363 insertions, 0 deletions
diff --git a/contrib/hyperv/tools/hv_vss_daemon.8 b/contrib/hyperv/tools/hv_vss_daemon.8
new file mode 100644
index 0000000000000..090690c368bf6
--- /dev/null
+++ b/contrib/hyperv/tools/hv_vss_daemon.8
@@ -0,0 +1,88 @@
+.\" Copyright (c) 2016 Microsoft Corp.
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.Dd October 12, 2016
+.Dt HV_VSS_DAEMON 8
+.Os
+.Sh NAME
+.Nm hv_vss_daemon
+.Nd Hyper-V Volume Shadow Copy Service Daemon
+.Sh SYNOPSIS
+.Nm
+.Op Fl dn
+.Sh DESCRIPTION
+The
+.Nm
+daemon provides the ability to freeze and thaw the file system for
+.Fx
+guest partitions running on Hyper-V.
+.Pp
+Hyper-V allows administrators to backup or restore the
+.Fx
+guest partition.
+Administrators can
+use Windows Powershell scripts to backup or restore the
+.Fx
+VM.
+.Pp
+The
+.Nm
+accepts file system freeze and thaw requests from the
+.Xr hv_utils 4
+driver and performs the actual file-system operation.
+.Pp
+The file system freeze and thaw functionality is
+useful when the Hyper-V host wants to do live backup of
+.Fx
+guest. Hyper-V host sends file system freezing request to
+.Nm
+which conducts the real operation. After successfully freezing file
+system, Hyper-V host takes a snapshot of the VM. In the future,
+Hyper-V host can restore the
+.Fx
+VM through that snapshot.
+.Pp
+The options are as follows:
+.Bl -tag -width indent
+.It Fl d
+Run as regular process instead of a daemon for debugging purpose.
+.It Fl n
+Generate debugging output.
+.El
+.Sh SEE ALSO
+.Xr hv_vmbus 4 ,
+.Xr hv_utils 4 ,
+.Xr hv_netvsc 4 ,
+.Xr hv_storvsc 4 ,
+.Xr hv_kvp 4
+.Sh HISTORY
+The daemon was introduced in October 2016 and developed by Microsoft Corp.
+.Sh AUTHORS
+.An -nosplit
+.Fx
+support for
+.Nm
+was first added by
+.An Microsoft BSD Integration Services Team Aq Mt bsdic@microsoft.com .
diff --git a/contrib/hyperv/tools/hv_vss_daemon.c b/contrib/hyperv/tools/hv_vss_daemon.c
new file mode 100644
index 0000000000000..8b58bc93a259c
--- /dev/null
+++ b/contrib/hyperv/tools/hv_vss_daemon.c
@@ -0,0 +1,275 @@
+#include <string.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <sys/ucred.h>
+#include <sys/mount.h>
+#include <sys/types.h>
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <poll.h>
+#include <stdint.h>
+#include <syslog.h>
+#include <errno.h>
+#include <err.h>
+#include <fcntl.h>
+#include <ufs/ffs/fs.h>
+#include <paths.h>
+#include <sysexits.h>
+
+#include "hv_snapshot.h"
+
+#define UNDEF_FREEZE_THAW (0)
+#define FREEZE (1)
+#define THAW (2)
+
+#define VSS_LOG(priority, format, args...) do { \
+ if (is_debugging == 1) { \
+ if (is_daemon == 1) \
+ syslog(priority, format, ## args); \
+ else \
+ printf(format, ## args); \
+ } else { \
+ if (priority < LOG_DEBUG) { \
+ if (is_daemon == 1) \
+ syslog(priority, format, ## args); \
+ else \
+ printf(format, ## args); \
+ } \
+ } \
+ } while(0)
+
+static int is_daemon = 1;
+static int is_debugging = 0;
+static int g_ufs_suspend_handle = -1;
+
+static const char *dev = "/dev";
+
+static int
+check(void)
+{
+ struct statfs *mntbuf, *statfsp;
+ int mntsize;
+ int i;
+
+ mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
+ if (mntsize == 0) {
+ VSS_LOG(LOG_ERR, "There is no mount information\n");
+ return (EINVAL);
+ }
+ for (i = mntsize - 1; i >= 0; --i)
+ {
+ statfsp = &mntbuf[i];
+
+ if (strncmp(statfsp->f_mntonname, dev, strlen(dev)) == 0) {
+ continue; /* skip to freeze '/dev' */
+ } else if (statfsp->f_flags & MNT_RDONLY) {
+ continue; /* skip to freeze RDONLY partition */
+ } else if (strncmp(statfsp->f_fstypename, "ufs", 3) != 0) {
+ return (EPERM); /* only UFS can be freezed */
+ }
+ }
+
+ return (0);
+}
+
+static int
+freeze(void)
+{
+ struct statfs *mntbuf, *statfsp;
+ int mntsize;
+ int error = 0;
+ int i;
+
+ g_ufs_suspend_handle = open(_PATH_UFSSUSPEND, O_RDWR);
+ if (g_ufs_suspend_handle == -1) {
+ VSS_LOG(LOG_ERR, "unable to open %s", _PATH_UFSSUSPEND);
+ return (errno);
+ }
+
+ mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
+ if (mntsize == 0) {
+ VSS_LOG(LOG_ERR, "There is no mount information\n");
+ return (EINVAL);
+ }
+ for (i = mntsize - 1; i >= 0; --i)
+ {
+ statfsp = &mntbuf[i];
+
+ if (strncmp(statfsp->f_mntonname, dev, strlen(dev)) == 0) {
+ continue; /* skip to freeze '/dev' */
+ } else if (statfsp->f_flags & MNT_RDONLY) {
+ continue; /* skip to freeze RDONLY partition */
+ } else if (strncmp(statfsp->f_fstypename, "ufs", 3) != 0) {
+ continue; /* only UFS can be freezed */
+ }
+ error = ioctl(g_ufs_suspend_handle, UFSSUSPEND, &statfsp->f_fsid);
+ if (error != 0) {
+ VSS_LOG(LOG_ERR, "error: %d\n", errno);
+ error = errno;
+ } else {
+ VSS_LOG(LOG_INFO, "Successfully suspend fs: %s\n",
+ statfsp->f_mntonname);
+ }
+ }
+
+ return (error);
+}
+
+/**
+ * close the opened handle will thaw the FS.
+ */
+static int
+thaw(void)
+{
+ int error = 0;
+ if (g_ufs_suspend_handle != -1) {
+ error = close(g_ufs_suspend_handle);
+ if (!error) {
+ g_ufs_suspend_handle = -1;
+ VSS_LOG(LOG_INFO, "Successfully thaw the fs\n");
+ } else {
+ error = errno;
+ VSS_LOG(LOG_ERR, "Fail to thaw the fs: "
+ "%d %s\n", errno, strerror(errno));
+ }
+ } else {
+ VSS_LOG(LOG_INFO, "The fs has already been thawed\n");
+ }
+
+ return (error);
+}
+
+static void
+usage(const char* cmd)
+{
+ fprintf(stderr, "%s: daemon for UFS file system freeze/thaw\n"
+ " -d : enable debug log printing. Default is disabled.\n"
+ " -n : run as a regular process instead of a daemon. Default is a daemon.\n"
+ " -h : print usage.\n", cmd);
+ exit(1);
+}
+
+int
+main(int argc, char* argv[])
+{
+ struct hv_vss_opt_msg userdata;
+
+ struct pollfd hv_vss_poll_fd[1];
+ uint32_t op;
+ int ch, r, len, error;
+ int hv_vss_dev_fd;
+
+ int freeze_thaw = UNDEF_FREEZE_THAW;
+ while ((ch = getopt(argc, argv, "dnh")) != -1) {
+ switch (ch) {
+ case 'n':
+ /* Run as regular process for debugging purpose. */
+ is_daemon = 0;
+ break;
+ case 'd':
+ /* Generate debugging output */
+ is_debugging = 1;
+ break;
+ case 'h':
+ default:
+ usage(argv[0]);
+ break;
+ }
+ }
+
+ openlog("HV_VSS", 0, LOG_USER);
+
+ /* Become daemon first. */
+ if (is_daemon == 1)
+ daemon(1, 0);
+ else
+ VSS_LOG(LOG_DEBUG, "Run as regular process.\n");
+
+ VSS_LOG(LOG_INFO, "HV_VSS starting; pid is: %d\n", getpid());
+
+ memset(&userdata, 0, sizeof(struct hv_vss_opt_msg));
+ /* register the daemon */
+ hv_vss_dev_fd = open(VSS_DEV(FS_VSS_DEV_NAME), O_RDWR);
+
+ if (hv_vss_dev_fd < 0) {
+ VSS_LOG(LOG_ERR, "Fail to open %s, error: %d %s\n",
+ VSS_DEV(FS_VSS_DEV_NAME), errno, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ hv_vss_poll_fd[0].fd = hv_vss_dev_fd;
+ hv_vss_poll_fd[0].events = POLLIN | POLLRDNORM;
+
+ while (1) {
+ r = poll(hv_vss_poll_fd, 1, INFTIM);
+
+ VSS_LOG(LOG_DEBUG, "poll returned r = %d, revent = 0x%x\n",
+ r, hv_vss_poll_fd[0].revents);
+
+ if (r == 0 || (r < 0 && errno == EAGAIN) ||
+ (r < 0 && errno == EINTR)) {
+ /* Nothing to read */
+ continue;
+ }
+
+ if (r < 0) {
+ /*
+ * For poll return failure other than EAGAIN,
+ * we want to exit.
+ */
+ VSS_LOG(LOG_ERR, "Poll failed.\n");
+ perror("poll");
+ exit(EIO);
+ }
+
+ /* Read from character device */
+ error = ioctl(hv_vss_dev_fd, IOCHVVSSREAD, &userdata);
+ if (error < 0) {
+ VSS_LOG(LOG_ERR, "Read failed.\n");
+ perror("pread");
+ exit(EIO);
+ }
+
+ if (userdata.status != 0) {
+ VSS_LOG(LOG_ERR, "data read error\n");
+ continue;
+ }
+
+ /*
+ * We will use the KVP header information to pass back
+ * the error from this daemon. So, first save the op
+ * and pool info to local variables.
+ */
+
+ op = userdata.opt;
+
+ switch (op) {
+ case HV_VSS_CHECK:
+ error = check();
+ break;
+ case HV_VSS_FREEZE:
+ error = freeze();
+ break;
+ case HV_VSS_THAW:
+ error = thaw();
+ break;
+ default:
+ VSS_LOG(LOG_ERR, "Illegal operation: %d\n", op);
+ error = VSS_FAIL;
+ }
+ if (error)
+ userdata.status = VSS_FAIL;
+ else
+ userdata.status = VSS_SUCCESS;
+ error = ioctl(hv_vss_dev_fd, IOCHVVSSWRITE, &userdata);
+ if (error != 0) {
+ VSS_LOG(LOG_ERR, "Fail to write to device\n");
+ exit(EXIT_FAILURE);
+ } else {
+ VSS_LOG(LOG_INFO, "Send response %d for %s to kernel\n",
+ userdata.status, op == HV_VSS_FREEZE ? "Freeze" :
+ (op == HV_VSS_THAW ? "Thaw" : "Check"));
+ }
+ }
+}