|
|
8042be |
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
8042be |
/*
|
|
|
8042be |
* An implementation of the host initiated guest snapshot for Hyper-V.
|
|
|
8042be |
*
|
|
|
8042be |
* Copyright (C) 2013, Microsoft, Inc.
|
|
|
8042be |
* Author : K. Y. Srinivasan <kys@microsoft.com>
|
|
|
8042be |
*/
|
|
|
8042be |
|
|
|
8042be |
|
|
|
8042be |
#include <sys/types.h>
|
|
|
8042be |
#include <sys/poll.h>
|
|
|
8042be |
#include <sys/ioctl.h>
|
|
|
8042be |
#include <sys/stat.h>
|
|
|
8042be |
#include <sys/sysmacros.h>
|
|
|
8042be |
#include <fcntl.h>
|
|
|
8042be |
#include <stdio.h>
|
|
|
8042be |
#include <mntent.h>
|
|
|
8042be |
#include <stdlib.h>
|
|
|
8042be |
#include <unistd.h>
|
|
|
8042be |
#include <string.h>
|
|
|
8042be |
#include <ctype.h>
|
|
|
8042be |
#include <errno.h>
|
|
|
8042be |
#include <linux/fs.h>
|
|
|
8042be |
#include <linux/major.h>
|
|
|
8042be |
#include <linux/hyperv.h>
|
|
|
8042be |
#include <syslog.h>
|
|
|
8042be |
#include <getopt.h>
|
|
|
8042be |
#include <stdbool.h>
|
|
|
8042be |
#include <dirent.h>
|
|
|
8042be |
|
|
|
8042be |
/* Don't use syslog() in the function since that can cause write to disk */
|
|
|
8042be |
static int vss_do_freeze(char *dir, unsigned int cmd)
|
|
|
8042be |
{
|
|
|
8042be |
int ret, fd = open(dir, O_RDONLY);
|
|
|
8042be |
|
|
|
8042be |
if (fd < 0)
|
|
|
8042be |
return 1;
|
|
|
8042be |
|
|
|
8042be |
ret = ioctl(fd, cmd, 0);
|
|
|
8042be |
|
|
|
8042be |
/*
|
|
|
8042be |
* If a partition is mounted more than once, only the first
|
|
|
8042be |
* FREEZE/THAW can succeed and the later ones will get
|
|
|
8042be |
* EBUSY/EINVAL respectively: there could be 2 cases:
|
|
|
8042be |
* 1) a user may mount the same partition to different directories
|
|
|
8042be |
* by mistake or on purpose;
|
|
|
8042be |
* 2) The subvolume of btrfs appears to have the same partition
|
|
|
8042be |
* mounted more than once.
|
|
|
8042be |
*/
|
|
|
8042be |
if (ret) {
|
|
|
8042be |
if ((cmd == FIFREEZE && errno == EBUSY) ||
|
|
|
8042be |
(cmd == FITHAW && errno == EINVAL)) {
|
|
|
8042be |
close(fd);
|
|
|
8042be |
return 0;
|
|
|
8042be |
}
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
close(fd);
|
|
|
8042be |
return !!ret;
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
static bool is_dev_loop(const char *blkname)
|
|
|
8042be |
{
|
|
|
8042be |
char *buffer;
|
|
|
8042be |
DIR *dir;
|
|
|
8042be |
struct dirent *entry;
|
|
|
8042be |
bool ret = false;
|
|
|
8042be |
|
|
|
8042be |
buffer = malloc(PATH_MAX);
|
|
|
8042be |
if (!buffer) {
|
|
|
8042be |
syslog(LOG_ERR, "Can't allocate memory!");
|
|
|
8042be |
exit(1);
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
snprintf(buffer, PATH_MAX, "%s/loop", blkname);
|
|
|
8042be |
if (!access(buffer, R_OK | X_OK)) {
|
|
|
8042be |
ret = true;
|
|
|
8042be |
goto free_buffer;
|
|
|
8042be |
} else if (errno != ENOENT) {
|
|
|
8042be |
syslog(LOG_ERR, "Can't access: %s; error:%d %s!",
|
|
|
8042be |
buffer, errno, strerror(errno));
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
snprintf(buffer, PATH_MAX, "%s/slaves", blkname);
|
|
|
8042be |
dir = opendir(buffer);
|
|
|
8042be |
if (!dir) {
|
|
|
8042be |
if (errno != ENOENT)
|
|
|
8042be |
syslog(LOG_ERR, "Can't opendir: %s; error:%d %s!",
|
|
|
8042be |
buffer, errno, strerror(errno));
|
|
|
8042be |
goto free_buffer;
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
while ((entry = readdir(dir)) != NULL) {
|
|
|
8042be |
if (strcmp(entry->d_name, ".") == 0 ||
|
|
|
8042be |
strcmp(entry->d_name, "..") == 0)
|
|
|
8042be |
continue;
|
|
|
8042be |
|
|
|
8042be |
snprintf(buffer, PATH_MAX, "%s/slaves/%s", blkname,
|
|
|
8042be |
entry->d_name);
|
|
|
8042be |
if (is_dev_loop(buffer)) {
|
|
|
8042be |
ret = true;
|
|
|
8042be |
break;
|
|
|
8042be |
}
|
|
|
8042be |
}
|
|
|
8042be |
closedir(dir);
|
|
|
8042be |
free_buffer:
|
|
|
8042be |
free(buffer);
|
|
|
8042be |
return ret;
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
static int vss_operate(int operation)
|
|
|
8042be |
{
|
|
|
8042be |
char match[] = "/dev/";
|
|
|
8042be |
FILE *mounts;
|
|
|
8042be |
struct mntent *ent;
|
|
|
8042be |
struct stat sb;
|
|
|
8042be |
char errdir[1024] = {0};
|
|
|
8042be |
char blkdir[23]; /* /sys/dev/block/XXX:XXX */
|
|
|
8042be |
unsigned int cmd;
|
|
|
8042be |
int error = 0, root_seen = 0, save_errno = 0;
|
|
|
8042be |
|
|
|
8042be |
switch (operation) {
|
|
|
8042be |
case VSS_OP_FREEZE:
|
|
|
8042be |
cmd = FIFREEZE;
|
|
|
8042be |
break;
|
|
|
8042be |
case VSS_OP_THAW:
|
|
|
8042be |
cmd = FITHAW;
|
|
|
8042be |
break;
|
|
|
8042be |
default:
|
|
|
8042be |
return -1;
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
mounts = setmntent("/proc/mounts", "r");
|
|
|
8042be |
if (mounts == NULL)
|
|
|
8042be |
return -1;
|
|
|
8042be |
|
|
|
8042be |
while ((ent = getmntent(mounts))) {
|
|
|
8042be |
if (strncmp(ent->mnt_fsname, match, strlen(match)))
|
|
|
8042be |
continue;
|
|
|
8042be |
if (stat(ent->mnt_fsname, &sb)) {
|
|
|
8042be |
syslog(LOG_ERR, "Can't stat: %s; error:%d %s!",
|
|
|
8042be |
ent->mnt_fsname, errno, strerror(errno));
|
|
|
8042be |
} else {
|
|
|
8042be |
sprintf(blkdir, "/sys/dev/block/%d:%d",
|
|
|
8042be |
major(sb.st_rdev), minor(sb.st_rdev));
|
|
|
8042be |
if (is_dev_loop(blkdir))
|
|
|
8042be |
continue;
|
|
|
8042be |
}
|
|
|
8042be |
if (hasmntopt(ent, MNTOPT_RO) != NULL)
|
|
|
8042be |
continue;
|
|
|
8042be |
if (strcmp(ent->mnt_type, "vfat") == 0)
|
|
|
8042be |
continue;
|
|
|
8042be |
if (strcmp(ent->mnt_dir, "/") == 0) {
|
|
|
8042be |
root_seen = 1;
|
|
|
8042be |
continue;
|
|
|
8042be |
}
|
|
|
8042be |
error |= vss_do_freeze(ent->mnt_dir, cmd);
|
|
|
8042be |
if (error && operation == VSS_OP_FREEZE)
|
|
|
8042be |
goto err;
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
endmntent(mounts);
|
|
|
8042be |
|
|
|
8042be |
if (root_seen) {
|
|
|
8042be |
error |= vss_do_freeze("/", cmd);
|
|
|
8042be |
if (error && operation == VSS_OP_FREEZE)
|
|
|
8042be |
goto err;
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
goto out;
|
|
|
8042be |
err:
|
|
|
8042be |
save_errno = errno;
|
|
|
8042be |
if (ent) {
|
|
|
8042be |
strncpy(errdir, ent->mnt_dir, sizeof(errdir)-1);
|
|
|
8042be |
endmntent(mounts);
|
|
|
8042be |
}
|
|
|
8042be |
vss_operate(VSS_OP_THAW);
|
|
|
8042be |
/* Call syslog after we thaw all filesystems */
|
|
|
8042be |
if (ent)
|
|
|
8042be |
syslog(LOG_ERR, "FREEZE of %s failed; error:%d %s",
|
|
|
8042be |
errdir, save_errno, strerror(save_errno));
|
|
|
8042be |
else
|
|
|
8042be |
syslog(LOG_ERR, "FREEZE of / failed; error:%d %s", save_errno,
|
|
|
8042be |
strerror(save_errno));
|
|
|
8042be |
out:
|
|
|
8042be |
return error;
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
void print_usage(char *argv[])
|
|
|
8042be |
{
|
|
|
8042be |
fprintf(stderr, "Usage: %s [options]\n"
|
|
|
8042be |
"Options are:\n"
|
|
|
8042be |
" -n, --no-daemon stay in foreground, don't daemonize\n"
|
|
|
8042be |
" -h, --help print this help\n", argv[0]);
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
int main(int argc, char *argv[])
|
|
|
8042be |
{
|
|
|
8042be |
int vss_fd, len;
|
|
|
8042be |
int error;
|
|
|
8042be |
struct pollfd pfd;
|
|
|
8042be |
int op;
|
|
|
8042be |
struct hv_vss_msg vss_msg[1];
|
|
|
8042be |
int daemonize = 1, long_index = 0, opt;
|
|
|
8042be |
int in_handshake = 1;
|
|
|
8042be |
__u32 kernel_modver;
|
|
|
8042be |
|
|
|
8042be |
static struct option long_options[] = {
|
|
|
8042be |
{"help", no_argument, 0, 'h' },
|
|
|
8042be |
{"no-daemon", no_argument, 0, 'n' },
|
|
|
8042be |
{0, 0, 0, 0 }
|
|
|
8042be |
};
|
|
|
8042be |
|
|
|
8042be |
while ((opt = getopt_long(argc, argv, "hn", long_options,
|
|
|
8042be |
&long_index)) != -1) {
|
|
|
8042be |
switch (opt) {
|
|
|
8042be |
case 'n':
|
|
|
8042be |
daemonize = 0;
|
|
|
8042be |
break;
|
|
|
8042be |
case 'h':
|
|
|
8042be |
print_usage(argv);
|
|
|
8042be |
exit(0);
|
|
|
8042be |
default:
|
|
|
8042be |
print_usage(argv);
|
|
|
8042be |
exit(EXIT_FAILURE);
|
|
|
8042be |
}
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
if (daemonize && daemon(1, 0))
|
|
|
8042be |
return 1;
|
|
|
8042be |
|
|
|
8042be |
openlog("Hyper-V VSS", 0, LOG_USER);
|
|
|
8042be |
syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
|
|
|
8042be |
|
|
|
8042be |
vss_fd = open("/dev/vmbus/hv_vss", O_RDWR);
|
|
|
8042be |
if (vss_fd < 0) {
|
|
|
8042be |
syslog(LOG_ERR, "open /dev/vmbus/hv_vss failed; error: %d %s",
|
|
|
8042be |
errno, strerror(errno));
|
|
|
8042be |
exit(EXIT_FAILURE);
|
|
|
8042be |
}
|
|
|
8042be |
/*
|
|
|
8042be |
* Register ourselves with the kernel.
|
|
|
8042be |
*/
|
|
|
8042be |
vss_msg->vss_hdr.operation = VSS_OP_REGISTER1;
|
|
|
8042be |
|
|
|
8042be |
len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
|
|
|
8042be |
if (len < 0) {
|
|
|
8042be |
syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
|
|
|
8042be |
errno, strerror(errno));
|
|
|
8042be |
close(vss_fd);
|
|
|
8042be |
exit(EXIT_FAILURE);
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
pfd.fd = vss_fd;
|
|
|
8042be |
|
|
|
8042be |
while (1) {
|
|
|
8042be |
pfd.events = POLLIN;
|
|
|
8042be |
pfd.revents = 0;
|
|
|
8042be |
|
|
|
8042be |
if (poll(&pfd, 1, -1) < 0) {
|
|
|
8042be |
syslog(LOG_ERR, "poll failed; error:%d %s", errno, strerror(errno));
|
|
|
8042be |
if (errno == EINVAL) {
|
|
|
8042be |
close(vss_fd);
|
|
|
8042be |
exit(EXIT_FAILURE);
|
|
|
8042be |
}
|
|
|
8042be |
else
|
|
|
8042be |
continue;
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
len = read(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
|
|
|
8042be |
|
|
|
8042be |
if (in_handshake) {
|
|
|
8042be |
if (len != sizeof(kernel_modver)) {
|
|
|
8042be |
syslog(LOG_ERR, "invalid version negotiation");
|
|
|
8042be |
exit(EXIT_FAILURE);
|
|
|
8042be |
}
|
|
|
8042be |
kernel_modver = *(__u32 *)vss_msg;
|
|
|
8042be |
in_handshake = 0;
|
|
|
8042be |
syslog(LOG_INFO, "VSS: kernel module version: %d",
|
|
|
8042be |
kernel_modver);
|
|
|
8042be |
continue;
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
if (len != sizeof(struct hv_vss_msg)) {
|
|
|
8042be |
syslog(LOG_ERR, "read failed; error:%d %s",
|
|
|
8042be |
errno, strerror(errno));
|
|
|
8042be |
close(vss_fd);
|
|
|
8042be |
return EXIT_FAILURE;
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
op = vss_msg->vss_hdr.operation;
|
|
|
8042be |
error = HV_S_OK;
|
|
|
8042be |
|
|
|
8042be |
switch (op) {
|
|
|
8042be |
case VSS_OP_FREEZE:
|
|
|
8042be |
case VSS_OP_THAW:
|
|
|
8042be |
error = vss_operate(op);
|
|
|
8042be |
syslog(LOG_INFO, "VSS: op=%s: %s\n",
|
|
|
8042be |
op == VSS_OP_FREEZE ? "FREEZE" : "THAW",
|
|
|
8042be |
error ? "failed" : "succeeded");
|
|
|
8042be |
|
|
|
8042be |
if (error) {
|
|
|
8042be |
error = HV_E_FAIL;
|
|
|
8042be |
syslog(LOG_ERR, "op=%d failed!", op);
|
|
|
8042be |
syslog(LOG_ERR, "report it with these files:");
|
|
|
8042be |
syslog(LOG_ERR, "/etc/fstab and /proc/mounts");
|
|
|
8042be |
}
|
|
|
8042be |
break;
|
|
|
8042be |
case VSS_OP_HOT_BACKUP:
|
|
|
8042be |
syslog(LOG_INFO, "VSS: op=CHECK HOT BACKUP\n");
|
|
|
8042be |
break;
|
|
|
8042be |
default:
|
|
|
8042be |
syslog(LOG_ERR, "Illegal op:%d\n", op);
|
|
|
8042be |
}
|
|
|
8042be |
vss_msg->error = error;
|
|
|
8042be |
len = write(vss_fd, vss_msg, sizeof(struct hv_vss_msg));
|
|
|
8042be |
if (len != sizeof(struct hv_vss_msg)) {
|
|
|
8042be |
syslog(LOG_ERR, "write failed; error: %d %s", errno,
|
|
|
8042be |
strerror(errno));
|
|
|
8042be |
|
|
|
8042be |
if (op == VSS_OP_FREEZE)
|
|
|
8042be |
vss_operate(VSS_OP_THAW);
|
|
|
8042be |
}
|
|
|
8042be |
}
|
|
|
8042be |
|
|
|
8042be |
close(vss_fd);
|
|
|
8042be |
exit(0);
|
|
|
8042be |
}
|