2 * An implementation of the host initiated guest snapshot for Hyper-V.
5 * Copyright (C) 2013, Microsoft, Inc.
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15 * NON INFRINGEMENT. See the GNU General Public License for more
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <linux/types.h>
31 #include <arpa/inet.h>
32 #include <linux/connector.h>
33 #include <linux/hyperv.h>
34 #include <linux/netlink.h>
37 static char vss_recv_buffer[4096];
38 static char vss_send_buffer[4096];
39 static struct sockaddr_nl addr;
42 #define SOL_NETLINK 270
46 static int vss_operate(int operation)
65 file = popen("mount | awk '/^\/dev\// { print $3}'", "r");
69 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
72 if (!strncmp(p, "/", sizeof("/")))
75 sprintf(cmd, "%s %s %s", "fsfreeze ", fs_op, p);
76 syslog(LOG_INFO, "VSS cmd is %s\n", cmd);
81 sprintf(cmd, "%s %s %s", "fsfreeze ", fs_op, "/");
82 syslog(LOG_INFO, "VSS cmd is %s\n", cmd);
88 static int netlink_send(int fd, struct cn_msg *msg)
92 struct msghdr message;
96 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
98 nlh = (struct nlmsghdr *)buffer;
100 nlh->nlmsg_pid = getpid();
101 nlh->nlmsg_type = NLMSG_DONE;
102 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
103 nlh->nlmsg_flags = 0;
105 iov[0].iov_base = nlh;
106 iov[0].iov_len = sizeof(*nlh);
108 iov[1].iov_base = msg;
109 iov[1].iov_len = size;
111 memset(&message, 0, sizeof(message));
112 message.msg_name = &addr;
113 message.msg_namelen = sizeof(addr);
114 message.msg_iov = iov;
115 message.msg_iovlen = 2;
117 return sendmsg(fd, &message, 0);
122 int fd, len, nl_group;
124 struct cn_msg *message;
126 struct nlmsghdr *incoming_msg;
127 struct cn_msg *incoming_cn_msg;
129 struct hv_vss_msg *vss_msg;
132 openlog("Hyper-V VSS", 0, LOG_USER);
133 syslog(LOG_INFO, "VSS starting; pid is:%d", getpid());
135 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
137 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
140 addr.nl_family = AF_NETLINK;
146 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
148 syslog(LOG_ERR, "bind failed; error:%d", error);
152 nl_group = CN_VSS_IDX;
153 setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group));
155 * Register ourselves with the kernel.
157 message = (struct cn_msg *)vss_send_buffer;
158 message->id.idx = CN_VSS_IDX;
159 message->id.val = CN_VSS_VAL;
161 vss_msg = (struct hv_vss_msg *)message->data;
162 vss_msg->vss_hdr.operation = VSS_OP_REGISTER;
164 message->len = sizeof(struct hv_vss_msg);
166 len = netlink_send(fd, message);
168 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
176 struct sockaddr *addr_p = (struct sockaddr *) &addr;
177 socklen_t addr_l = sizeof(addr);
182 len = recvfrom(fd, vss_recv_buffer, sizeof(vss_recv_buffer), 0,
185 if (len < 0 || addr.nl_pid) {
186 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
187 addr.nl_pid, errno, strerror(errno));
192 incoming_msg = (struct nlmsghdr *)vss_recv_buffer;
194 if (incoming_msg->nlmsg_type != NLMSG_DONE)
197 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
198 vss_msg = (struct hv_vss_msg *)incoming_cn_msg->data;
199 op = vss_msg->vss_hdr.operation;
205 error = vss_operate(op);
210 syslog(LOG_ERR, "Illegal op:%d\n", op);
212 vss_msg->error = error;
213 len = netlink_send(fd, incoming_cn_msg);
215 syslog(LOG_ERR, "net_link send failed; error:%d", len);