/
vmunbundle.c
387 lines (340 loc) · 10.3 KB
/
vmunbundle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define VMBUNDLE_MAGIC "MAGIC-VMBUNDLE"
#define VMBUNDLE_VERSION "1"
#define VMBUNDLE_NUM_SIZE 32
#define VMBUNDLE_HEADER_SIZE 512
typedef struct {
unsigned int version;
const char *checksum;
const char *name;
size_t size;
size_t padded_size;
} header_t;
char *progname;
int get_header(int fd, header_t *header, int fallback_to_raw);
ssize_t read_bytes(int fd, char *data, size_t bytes);
ssize_t write_bytes(int fd, const void *buf, size_t bytes);
size_t zfs_receive(int fd, const char * snapshot);
/*
* RETURNS
*
* -1 * remaining (unwritten bytes) on failure
* # bytes written on succcess
*
*/
ssize_t
write_bytes(int fd, const void *buf, size_t bytes)
{
ssize_t numwritten;
void * ptr;
size_t remain;
ptr = (void *) buf;
remain = bytes;
while ((remain > 0) && (numwritten = write(fd, ptr, remain))) {
remain -= numwritten;
ptr += numwritten;
}
if (numwritten < 0) {
perror("write");
return ((ssize_t) (remain * -1));
} else if (remain > 0) {
return ((ssize_t) (remain * -1));
}
return ((ssize_t) bytes);
}
ssize_t
read_bytes(int fd, char *data, size_t bytes)
{
size_t remain;
ssize_t numread;
ssize_t total_read = 0;
char *ptr;
remain = bytes;
ptr = data;
while (remain > 0 && (numread = read(fd, ptr, remain)) > 0) {
remain -= numread;
ptr += numread;
total_read += numread;
}
if (numread < 0) {
perror("read");
return ((ssize_t) (remain * -1));
}
return (total_read);
}
size_t
zfs_receive(int fd, const char * snapshot)
{
char *argv[4] = {"/usr/sbin/zfs", "receive", 0, 0};
char *evp[1] = {0};
pid_t pid;
int stat;
pid_t waitee;
pid = fork();
if (pid == 0) {
argv[2] = (char *)snapshot;
if (dup2(fd, 0) < 0) {
perror("dup2()");
return (1);
}
execve("/usr/sbin/zfs", argv, evp);
perror("execve");
return (2);
} else if (pid > 0) {
while ((waitee = waitpid(pid, &stat, 0)) != pid) {
if (waitee == -1 && errno != EINTR) {
perror("waitpid");
return (3);
}
}
if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0) {
fprintf(stderr, "zfs receive barfed on %s\n", snapshot);
return (4);
}
} else {
perror("fork");
return (5);
}
return (0);
}
int
get_header(int fd, header_t *header, int fallback_to_raw)
{
char data[VMBUNDLE_HEADER_SIZE] = {0};
ssize_t data_size = 0;
int found_magic = 0;
ssize_t nread;
size_t offset = 0;
size_t str_pos = 0;
char tmp[VMBUNDLE_HEADER_SIZE] = {0};
fprintf(stderr, "looking for magic...\n");
if ((nread = read_bytes(fd, (char *)data, VMBUNDLE_HEADER_SIZE)) == 0) {
fprintf(stderr, "EOF looking for magic (no data)\n");
return (-3);
} else if (nread < 0) {
fprintf(stderr, "Error reading magic\n");
return (-1);
} else {
data_size = nread;
}
errno = 0;
while (found_magic == 0 && data_size == nread &&
offset < (strlen(VMBUNDLE_MAGIC) + 1)) {
if (strncmp(data + offset, VMBUNDLE_MAGIC,
strlen(VMBUNDLE_MAGIC) + 1) == 0) {
fprintf(stderr, "found magic at offset %zd\n", offset);
if (offset > 0) {
(void) memmove(data, data + offset,
VMBUNDLE_HEADER_SIZE - offset);
nread = read_bytes(fd, data + (VMBUNDLE_HEADER_SIZE - offset),
offset);
if (nread == 0) {
fprintf(stderr, "EOF while reading header.\n");
return (-1);
} else if (nread < 1) {
fprintf(stderr, "Error %d while reading header.\n", nread);
return (-1);
} else if ((size_t) nread != offset) {
fprintf(
stderr,
"ERROR short read reading second header chunk.\n");
return (-1);
}
}
str_pos = strlen(VMBUNDLE_MAGIC) + 1;
/* .version */
if (strlcpy(tmp, data + str_pos, VMBUNDLE_HEADER_SIZE - str_pos)
>= (VMBUNDLE_HEADER_SIZE - str_pos)) {
perror("strlcpy");
return (-1);
}
header->version =
(unsigned int) strtoul((const char *)tmp, NULL, 10);
if (errno) {
perror("strtoul");
return (-1);
}
str_pos += strlen(tmp) + 1;
/* .checksum */
if (strlcpy(tmp, data + str_pos, VMBUNDLE_HEADER_SIZE - str_pos)
>= (VMBUNDLE_HEADER_SIZE - str_pos)) {
perror("strlcpy");
return (-1);
}
if (strncmp(tmp, "0", strlen(tmp) == 0)) {
header->checksum = "0";
}
str_pos += strlen(tmp) + 1;
/* .name */
if (strlcpy(tmp, data + str_pos, VMBUNDLE_HEADER_SIZE - str_pos)
>= (VMBUNDLE_HEADER_SIZE - str_pos)) {
perror("strlcpy");
return (-1);
}
/*
* Note: up to the caller to free this memory, this tool uses name
* until about the end though so it is not freed.
*/
header->name = strdup(tmp);
str_pos += strlen(tmp) + 1;
/* .size */
if (strlcpy(tmp, data + str_pos, VMBUNDLE_HEADER_SIZE - str_pos)
>= (VMBUNDLE_HEADER_SIZE - str_pos)) {
perror("strlcpy");
return (-1);
}
header->size = (size_t) strtoull((const char *)tmp, NULL, 10);
if (errno) {
perror("strtoull");
return (-1);
}
str_pos += strlen(tmp) + 1;
/* .padded_size */
if (strlcpy(tmp, data + str_pos, VMBUNDLE_HEADER_SIZE - str_pos)
>= (VMBUNDLE_HEADER_SIZE - str_pos)) {
perror("strlcpy");
return (-1);
}
header->padded_size =
(size_t) strtoull((const char *)tmp, NULL, 10);
if (errno) {
perror("strtoull");
return (-1);
}
str_pos += strlen(tmp) + 1;
found_magic = 1;
}
offset++;
}
if (!found_magic) {
if (fallback_to_raw) {
/*
* EOF without magic, just dump what we got to stdout. This
* handles the case where you just pipe raw JSON to vmadm receive.
*
*/
fprintf(stderr, "No magic! Dumping raw JSON.\n");
(void) write_bytes(1, data, data_size);
while ((nread = read_bytes(fd, (char *)data,
VMBUNDLE_HEADER_SIZE)) > 0) {
fprintf(stderr, "got %d bytes\n", nread);
(void) write_bytes(1, data, nread);
}
if (nread < 0) {
fprintf(stderr, "Error %d reading raw data\n", nread);
return (-1);
}
return (-2);
} else {
fprintf(stderr, "No magic!\n");
}
return (1);
}
return (0);
}
char *
read_json(header_t header)
{
char * json = NULL;
ssize_t nread;
json = (char *)malloc(header.padded_size);
if (json == NULL) {
perror("malloc");
exit(1);
}
nread = read_bytes(0, (char *)json, header.padded_size);
if (nread == 0) {
fprintf(stderr, "EOF reading JSON\n");
exit(1);
} else if (nread < 0) {
fprintf(stderr, "Error %d reading JSON\n", nread);
exit(1);
} else if ((size_t) nread != header.padded_size) {
fprintf(stderr, "JSON truncated: %zu of %zu bytes\n", nread,
header.padded_size);
exit(1);
}
return (json);
}
void
usage(void)
{
fprintf(stderr, "Usage: %s [json|dataset]\n", progname);
exit(1);
}
int
main(int argc, char *argv[])
{
header_t header;
char * json = NULL;
enum {JSON = 0, DATASET} mode;
int res;
progname = argv[0];
// Ensure correct usage
if (argc != 2) {
usage();
/* NOTREACHED */
}
if (strcmp(argv[1], "json") == 0) {
mode = JSON;
} else if (strcmp(argv[1], "dataset") == 0) {
mode = DATASET;
} else {
usage();
/* NOTREACHED */
}
res = get_header(0, &header, (mode == JSON) ? 1 : 0);
if (res == -1 || res > 0) {
fprintf(stderr, "No header: this doesn't look like a vmbundle.\n");
exit(1);
} else if (res == -3) {
exit(3);
} else if ((mode == JSON) && (res == -2)) {
/*
* We dumped the raw data (passed input through unchanged)
* This option exists to support the manual receive of JSON then
* install.
*/
exit(0);
} else if ((mode == JSON) && (res != 0)) {
fprintf(stderr, "Error %d: reading vmbundle header.\n", res);
exit(1);
}
fprintf(stderr, "Version: %zu\n", header.version);
fprintf(stderr, "Name: [%s]\n", header.name);
fprintf(stderr, "Size: %zu\n", header.size);
fprintf(stderr, "Padded Size: %zu\n", header.padded_size);
if (mode == JSON) {
if (strcmp("JSON", header.name) == 0) {
json = read_json(header);
write_bytes(1, json, header.size);
if (json != NULL) {
free(json);
json = NULL;
}
fsync(1);
fprintf(stderr, "END JSON\n");
} else {
fprintf(stderr, "FATAL: expecting JSON, got '%s'\n", header.name);
exit(1);
}
} else if (mode == DATASET) {
fprintf(stderr, "Attempting zfs receive %s\n", header.name);
if ((res = zfs_receive(0, header.name)) != 0) {
fprintf(stderr, "Failed to receive dataset code: %d\n", res);
exit(1);
}
fprintf(stderr, "END DATASET\n");
} else {
fprintf(stderr, "Internal error!\n");
exit(1);
}
exit(0);
}