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 : : vhd_context_t vhd;
47 : : off64_t currsize;
48 : : int ret, err, c;
49 : :
50 : 0 : char *name = NULL;
51 : 0 : int size = 0;
52 : 0 : int physize = 0;
53 : 0 : int parent = 0;
54 : 0 : int fields = 0;
55 : 0 : int depth = 0;
56 : 0 : int fastresize = 0;
57 : 0 : int marker = 0;
58 : 0 : int allocated = 0;
59 : 0 : int resolve_parent = 1;
60 : 0 : int flags = VHD_OPEN_RDONLY | VHD_OPEN_IGNORE_DISABLED;
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:vspfdSmaubh")) != -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 'b':
101 : 0 : flags |= VHD_OPEN_USE_BKP_FOOTER;
102 : 0 : break;
103 : : case 'h':
104 : : err = 0;
105 : : goto usage;
106 : : default:
107 : 0 : err = -EINVAL;
108 : 0 : goto usage;
109 : : }
110 : : }
111 : :
112 [ # # ][ # # ]: 0 : if (!name || optind != argc) {
113 : : err = -EINVAL;
114 : : goto usage;
115 : : }
116 : :
117 : 0 : err = vhd_open(&vhd, name, flags);
118 [ # # ]: 0 : if (err) {
119 : : printf("error opening %s: %d\n", name, err);
120 : 0 : return err;
121 : : }
122 : :
123 [ # # ]: 0 : if (size)
124 : 0 : printf("%"PRIu64"\n", vhd.footer.curr_size >> 20);
125 : :
126 [ # # ]: 0 : if (physize) {
127 : 0 : err = vhd_get_phys_size(&vhd, &currsize);
128 [ # # ]: 0 : if (err)
129 : : printf("failed to get physical size: %d\n", err);
130 : : else
131 : 0 : printf("%"PRIu64"\n", currsize);
132 : : }
133 : :
134 [ # # ]: 0 : if (parent) {
135 : 0 : ret = 0;
136 : :
137 [ # # ]: 0 : if (vhd.footer.type != HD_TYPE_DIFF)
138 : : printf("%s has no parent\n", name);
139 : : else {
140 : : char *pname;
141 : :
142 [ # # ]: 0 : ret = resolve_parent ? vhd_parent_locator_get(&vhd, &pname) : vhd_parent_locator_unresolved_get(&vhd, &pname);
143 [ # # ]: 0 : if (ret)
144 : : printf("query failed\n");
145 : : else {
146 : 0 : printf("%s\n", pname);
147 : 0 : free(pname);
148 : : }
149 : : }
150 : :
151 [ # # ]: 0 : err = (err ? : ret);
152 : : }
153 : :
154 [ # # ]: 0 : if (fields) {
155 : : int hidden;
156 : :
157 : 0 : ret = vhd_hidden(&vhd, &hidden);
158 [ # # ]: 0 : if (ret)
159 : : printf("error checking 'hidden' field: %d\n", ret);
160 : : else
161 : 0 : printf("hidden: %d\n", hidden);
162 : :
163 [ # # ]: 0 : err = (err ? : ret);
164 : : }
165 : :
166 [ # # ]: 0 : if (marker) {
167 : : char marker;
168 : :
169 : 0 : ret = vhd_marker(&vhd, &marker);
170 [ # # ]: 0 : if (ret)
171 : : printf("error checking 'marker' field: %d\n", ret);
172 : : else
173 : 0 : printf("marker: %d\n", marker);
174 : :
175 [ # # ]: 0 : err = (err ? : ret);
176 : : }
177 : :
178 [ # # ]: 0 : if (depth) {
179 : : int length;
180 : :
181 : 0 : ret = vhd_chain_depth(&vhd, &length);
182 [ # # ]: 0 : if (ret)
183 : : printf("error checking chain depth: %d\n", ret);
184 : : else
185 : 0 : printf("chain depth: %d\n", length);
186 : :
187 [ # # ]: 0 : err = (err ? : ret);
188 : : }
189 : :
190 [ # # ]: 0 : if (allocated) {
191 : 0 : ret = vhd_get_bat(&vhd);
192 [ # # ]: 0 : if (ret)
193 : : printf("error reading bat: %d\n", ret);
194 : : else {
195 : : uint32_t i, used;
196 : :
197 [ # # ]: 0 : for (i = 0, used = 0; i < vhd.bat.entries; i++)
198 [ # # ]: 0 : if (vhd.bat.bat[i] != DD_BLK_UNUSED)
199 : 0 : used++;
200 : :
201 : : printf("%u\n", used);
202 : : }
203 : :
204 [ # # ]: 0 : err = (err ? : ret);
205 : : }
206 : :
207 [ # # ]: 0 : if (fastresize) {
208 : : uint64_t max_size;
209 : :
210 : 0 : max_size = vhd.header.max_bat_size << (VHD_BLOCK_SHIFT - 20);
211 : : printf("%"PRIu64"\n", max_size);
212 : : }
213 : :
214 : 0 : vhd_close(&vhd);
215 : : return err;
216 : :
217 : : usage:
218 : : printf("options: <-n name> [-v print virtual size (in MB)] "
219 : : "[-s print physical utilization (bytes)] [-p print parent] "
220 : : "[-f print fields] [-m print marker] [-d print chain depth] "
221 : : "[-S print max virtual size (MB) for fast resize] "
222 : : "[-a print allocated block count] "
223 : : "[-u don't resolve parent path] "
224 : : "[-b don't trust the footer, use the back-up one instead] "
225 : : "[-h help]\n");
226 : : return err;
227 : : }
|