root / vhd / lib / vhd-util-query.c @ abdb293f
History | View | Annotate | Download (4.4 kB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2007, XenSource Inc.
|
3 |
* Copyright (c) 2010, Citrix Systems, Inc.
|
4 |
*
|
5 |
* All rights reserved.
|
6 |
*
|
7 |
* Redistribution and use in source and binary forms, with or without
|
8 |
* modification, are permitted provided that the following conditions are met:
|
9 |
* * Redistributions of source code must retain the above copyright
|
10 |
* notice, this list of conditions and the following disclaimer.
|
11 |
* * Redistributions in binary form must reproduce the above copyright
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
13 |
* documentation and/or other materials provided with the distribution.
|
14 |
* * Neither the name of XenSource Inc. nor the names of its contributors
|
15 |
* may be used to endorse or promote products derived from this software
|
16 |
* without specific prior written permission.
|
17 |
*
|
18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
22 |
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
23 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
24 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
25 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
26 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
27 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
28 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29 |
*/
|
30 |
|
31 |
#ifdef HAVE_CONFIG_H
|
32 |
#include "config.h" |
33 |
#endif
|
34 |
|
35 |
#include <errno.h> |
36 |
#include <fcntl.h> |
37 |
#include <stdio.h> |
38 |
#include <stdlib.h> |
39 |
#include <unistd.h> |
40 |
|
41 |
#include "libvhd.h" |
42 |
|
43 |
int
|
44 |
vhd_util_query(int argc, char **argv) |
45 |
{ |
46 |
char *name;
|
47 |
vhd_context_t vhd; |
48 |
off64_t currsize; |
49 |
int ret, err, c, size, physize, parent, fields, depth, fastresize, marker;
|
50 |
|
51 |
name = NULL;
|
52 |
size = 0;
|
53 |
physize = 0;
|
54 |
parent = 0;
|
55 |
fields = 0;
|
56 |
depth = 0;
|
57 |
fastresize = 0;
|
58 |
marker = 0;
|
59 |
|
60 |
if (!argc || !argv) {
|
61 |
err = -EINVAL; |
62 |
goto usage;
|
63 |
} |
64 |
|
65 |
optind = 0;
|
66 |
while ((c = getopt(argc, argv, "n:vspfdSmh")) != -1) { |
67 |
switch (c) {
|
68 |
case 'n': |
69 |
name = optarg; |
70 |
break;
|
71 |
case 'v': |
72 |
size = 1;
|
73 |
break;
|
74 |
case 's': |
75 |
physize = 1;
|
76 |
break;
|
77 |
case 'p': |
78 |
parent = 1;
|
79 |
break;
|
80 |
case 'f': |
81 |
fields = 1;
|
82 |
break;
|
83 |
case 'd': |
84 |
depth = 1;
|
85 |
break;
|
86 |
case 'S': |
87 |
fastresize = 1;
|
88 |
break;
|
89 |
case 'm': |
90 |
marker = 1;
|
91 |
break;
|
92 |
case 'h': |
93 |
err = 0;
|
94 |
goto usage;
|
95 |
default:
|
96 |
err = -EINVAL; |
97 |
goto usage;
|
98 |
} |
99 |
} |
100 |
|
101 |
if (!name || optind != argc) {
|
102 |
err = -EINVAL; |
103 |
goto usage;
|
104 |
} |
105 |
|
106 |
err = vhd_open(&vhd, name, VHD_OPEN_RDONLY | VHD_OPEN_IGNORE_DISABLED); |
107 |
if (err) {
|
108 |
printf("error opening %s: %d\n", name, err);
|
109 |
return err;
|
110 |
} |
111 |
|
112 |
if (size)
|
113 |
printf("%"PRIu64"\n", vhd.footer.curr_size >> 20); |
114 |
|
115 |
if (physize) {
|
116 |
err = vhd_get_phys_size(&vhd, &currsize); |
117 |
if (err)
|
118 |
printf("failed to get physical size: %d\n", err);
|
119 |
else
|
120 |
printf("%"PRIu64"\n", currsize); |
121 |
} |
122 |
|
123 |
if (parent) {
|
124 |
ret = 0;
|
125 |
|
126 |
if (vhd.footer.type != HD_TYPE_DIFF)
|
127 |
printf("%s has no parent\n", name);
|
128 |
else {
|
129 |
char *pname;
|
130 |
|
131 |
ret = vhd_parent_locator_get(&vhd, &pname); |
132 |
if (ret)
|
133 |
printf("query failed\n");
|
134 |
else {
|
135 |
printf("%s\n", pname);
|
136 |
free(pname); |
137 |
} |
138 |
} |
139 |
|
140 |
err = (err ? : ret); |
141 |
} |
142 |
|
143 |
if (fields) {
|
144 |
int hidden;
|
145 |
|
146 |
ret = vhd_hidden(&vhd, &hidden); |
147 |
if (ret)
|
148 |
printf("error checking 'hidden' field: %d\n", ret);
|
149 |
else
|
150 |
printf("hidden: %d\n", hidden);
|
151 |
|
152 |
err = (err ? : ret); |
153 |
} |
154 |
|
155 |
if (marker) {
|
156 |
char marker;
|
157 |
|
158 |
ret = vhd_marker(&vhd, &marker); |
159 |
if (ret)
|
160 |
printf("error checking 'marker' field: %d\n", ret);
|
161 |
else
|
162 |
printf("marker: %d\n", marker);
|
163 |
|
164 |
err = (err ? : ret); |
165 |
} |
166 |
|
167 |
if (depth) {
|
168 |
int length;
|
169 |
|
170 |
ret = vhd_chain_depth(&vhd, &length); |
171 |
if (ret)
|
172 |
printf("error checking chain depth: %d\n", ret);
|
173 |
else
|
174 |
printf("chain depth: %d\n", length);
|
175 |
|
176 |
err = (err ? : ret); |
177 |
} |
178 |
|
179 |
if (fastresize) {
|
180 |
uint64_t max_size; |
181 |
|
182 |
max_size = vhd.header.max_bat_size << (VHD_BLOCK_SHIFT - 20);
|
183 |
printf("%"PRIu64"\n", max_size); |
184 |
} |
185 |
|
186 |
vhd_close(&vhd); |
187 |
return err;
|
188 |
|
189 |
usage:
|
190 |
printf("options: <-n name> [-v print virtual size (in MB)] "
|
191 |
"[-s print physical utilization (bytes)] [-p print parent] "
|
192 |
"[-f print fields] [-m print marker] [-d print chain depth] "
|
193 |
"[-S print max virtual size (MB) for fast resize] [-h help]\n");
|
194 |
return err;
|
195 |
} |