1
/* Copyright (c) 2008, XenSource Inc.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
6
* * Redistributions of source code must retain the above copyright
7
* notice, this list of conditions and the following disclaimer.
8
* * Redistributions in binary form must reproduce the above copyright
9
* notice, this list of conditions and the following disclaimer in the
10
* documentation and/or other materials provided with the distribution.
11
* * Neither the name of XenSource Inc. nor the names of its contributors
12
* may be used to endorse or promote products derived from this software
13
* without specific prior written permission.
15
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
vhd_util_find_snapshot_target(const char *name, char **result, int *parent_raw)
45
target = strdup(name);
50
err = vhd_open(&vhd, target, VHD_OPEN_RDONLY);
54
if (vhd.footer.type != HD_TYPE_DIFF)
57
err = vhd_get_bat(&vhd);
61
for (i = 0; i < vhd.bat.entries; i++)
62
if (vhd.bat.bat[i] != DD_BLK_UNUSED)
66
err = vhd_parent_locator_get(&vhd, &target);
70
if (vhd_parent_raw(&vhd)) {
89
vhd_util_check_depth(const char *name, int *depth)
94
err = vhd_open(&vhd, name, VHD_OPEN_RDONLY);
98
err = vhd_chain_depth(&vhd, depth);
105
vhd_util_snapshot(int argc, char **argv)
107
vhd_flag_creat_t flags;
108
int c, err, prt_raw, limit;
109
char *name, *pname, *ppath, *backing;
121
if (!argc || !argv) {
127
while ((c = getopt(argc, argv, "n:p:l:mh")) != -1) {
136
limit = strtol(optarg, NULL, 10);
139
vhd_flag_set(flags, VHD_FLAG_CREAT_PARENT_RAW);
150
if (!name || !pname || optind != argc) {
155
ppath = realpath(pname, NULL);
159
if (vhd_flag_test(flags, VHD_FLAG_CREAT_PARENT_RAW)) {
160
backing = strdup(ppath);
166
err = vhd_util_find_snapshot_target(ppath, &backing, &prt_raw);
173
* if the sizes of the parent chain are non-uniform, we need to
174
* pick the right size: that of the supplied parent
176
if (strcmp(ppath, backing)) {
177
err = vhd_open(&vhd, ppath, VHD_OPEN_RDONLY);
180
size = vhd.footer.curr_size;
185
vhd_flag_set(flags, VHD_FLAG_CREAT_PARENT_RAW);
188
if (limit && !vhd_flag_test(flags, VHD_FLAG_CREAT_PARENT_RAW)) {
191
err = vhd_util_check_depth(backing, &depth);
193
printf("error checking snapshot depth: %d\n", err);
194
else if (depth + 1 > limit) {
196
printf("snapshot depth exceeded: "
197
"current depth: %d, limit: %d\n", depth, limit);
204
err = vhd_snapshot(name, size, backing, flags);
213
printf("options: <-n name> <-p parent name> [-l snapshot depth limit]"
214
" [-m parent_is_raw] [-h help]\n");