Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2016, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. 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 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software 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 : 0 : 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, allocated, resolve_parent;
50 : :
51 : 0 : name = NULL;
52 : 0 : size = 0;
53 : 0 : physize = 0;
54 : 0 : parent = 0;
55 : 0 : fields = 0;
56 : 0 : depth = 0;
57 : 0 : fastresize = 0;
58 : 0 : marker = 0;
59 : 0 : allocated = 0;
60 : 0 : resolve_parent = 1;
61 : :
62 [ # # ]: 0 : if (!argc || !argv) {
63 : : err = -EINVAL;
64 : : goto usage;
65 : : }
66 : :
67 : 0 : optind = 0;
68 [ # # ]: 0 : while ((c = getopt(argc, argv, "n:vspfdSmauh")) != -1) {
69 [ # # # # : 0 : switch (c) {
# # # # #
# # # ]
70 : : case 'n':
71 : 0 : name = optarg;
72 : 0 : break;
73 : : case 'v':
74 : : size = 1;
75 : : break;
76 : : case 's':
77 : 0 : physize = 1;
78 : 0 : break;
79 : : case 'p':
80 : 0 : parent = 1;
81 : 0 : break;
82 : : case 'f':
83 : 0 : fields = 1;
84 : 0 : break;
85 : : case 'd':
86 : 0 : depth = 1;
87 : 0 : break;
88 : : case 'S':
89 : 0 : fastresize = 1;
90 : 0 : break;
91 : : case 'm':
92 : 0 : marker = 1;
93 : 0 : break;
94 : : case 'a':
95 : 0 : allocated = 1;
96 : 0 : break;
97 : : case 'u':
98 : 0 : resolve_parent = 0;
99 : 0 : break;
100 : : case 'h':
101 : : err = 0;
102 : : goto usage;
103 : : default:
104 : 0 : err = -EINVAL;
105 : 0 : goto usage;
106 : : }
107 : : }
108 : :
109 [ # # ][ # # ]: 0 : if (!name || optind != argc) {
110 : : err = -EINVAL;
111 : : goto usage;
112 : : }
113 : :
114 : 0 : err = vhd_open(&vhd, name, VHD_OPEN_RDONLY | VHD_OPEN_IGNORE_DISABLED);
115 [ # # ]: 0 : if (err) {
116 : : printf("error opening %s: %d\n", name, err);
117 : 0 : return err;
118 : : }
119 : :
120 [ # # ]: 0 : if (size)
121 : 0 : printf("%"PRIu64"\n", vhd.footer.curr_size >> 20);
122 : :
123 [ # # ]: 0 : if (physize) {
124 : 0 : err = vhd_get_phys_size(&vhd, &currsize);
125 [ # # ]: 0 : if (err)
126 : : printf("failed to get physical size: %d\n", err);
127 : : else
128 : 0 : printf("%"PRIu64"\n", currsize);
129 : : }
130 : :
131 [ # # ]: 0 : if (parent) {
132 : 0 : ret = 0;
133 : :
134 [ # # ]: 0 : if (vhd.footer.type != HD_TYPE_DIFF)
135 : : printf("%s has no parent\n", name);
136 : : else {
137 : : char *pname;
138 : :
139 [ # # ]: 0 : ret = resolve_parent ? vhd_parent_locator_get(&vhd, &pname) : vhd_parent_locator_unresolved_get(&vhd, &pname);
140 [ # # ]: 0 : if (ret)
141 : : printf("query failed\n");
142 : : else {
143 : 0 : printf("%s\n", pname);
144 : 0 : free(pname);
145 : : }
146 : : }
147 : :
148 [ # # ]: 0 : err = (err ? : ret);
149 : : }
150 : :
151 [ # # ]: 0 : if (fields) {
152 : : int hidden;
153 : :
154 : 0 : ret = vhd_hidden(&vhd, &hidden);
155 [ # # ]: 0 : if (ret)
156 : : printf("error checking 'hidden' field: %d\n", ret);
157 : : else
158 : 0 : printf("hidden: %d\n", hidden);
159 : :
160 [ # # ]: 0 : err = (err ? : ret);
161 : : }
162 : :
163 [ # # ]: 0 : if (marker) {
164 : : char marker;
165 : :
166 : 0 : ret = vhd_marker(&vhd, &marker);
167 [ # # ]: 0 : if (ret)
168 : : printf("error checking 'marker' field: %d\n", ret);
169 : : else
170 : 0 : printf("marker: %d\n", marker);
171 : :
172 [ # # ]: 0 : err = (err ? : ret);
173 : : }
174 : :
175 [ # # ]: 0 : if (depth) {
176 : : int length;
177 : :
178 : 0 : ret = vhd_chain_depth(&vhd, &length);
179 [ # # ]: 0 : if (ret)
180 : : printf("error checking chain depth: %d\n", ret);
181 : : else
182 : 0 : printf("chain depth: %d\n", length);
183 : :
184 [ # # ]: 0 : err = (err ? : ret);
185 : : }
186 : :
187 [ # # ]: 0 : if (allocated) {
188 : 0 : ret = vhd_get_bat(&vhd);
189 [ # # ]: 0 : if (ret)
190 : : printf("error reading bat: %d\n", ret);
191 : : else {
192 : : uint32_t i, used;
193 : :
194 [ # # ]: 0 : for (i = 0, used = 0; i < vhd.bat.entries; i++)
195 [ # # ]: 0 : if (vhd.bat.bat[i] != DD_BLK_UNUSED)
196 : 0 : used++;
197 : :
198 : : printf("%u\n", used);
199 : : }
200 : :
201 [ # # ]: 0 : err = (err ? : ret);
202 : : }
203 : :
204 [ # # ]: 0 : if (fastresize) {
205 : : uint64_t max_size;
206 : :
207 : 0 : max_size = vhd.header.max_bat_size << (VHD_BLOCK_SHIFT - 20);
208 : : printf("%"PRIu64"\n", max_size);
209 : : }
210 : :
211 : 0 : vhd_close(&vhd);
212 : : return err;
213 : :
214 : : usage:
215 : : printf("options: <-n name> [-v print virtual size (in MB)] "
216 : : "[-s print physical utilization (bytes)] [-p print parent] "
217 : : "[-f print fields] [-m print marker] [-d print chain depth] "
218 : : "[-S print max virtual size (MB) for fast resize] "
219 : : "[-a print allocated block count] "
220 : : "[-u don't resolve parent path] "
221 : : "[-h help]\n");
222 : : return err;
223 : : }
|