2 * Copyright (C) 2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include <libvirt/libvirt.h>
34 #include <libvirt/virterror.h>
43 /* These globals are shared with options.c. */
49 int keys_from_stdin = 0;
51 const char *libvirt_uri = NULL;
54 static int quiet = 0; /* --quiet */
56 static int scan (void);
59 bad_cast (char const *s)
64 static void __attribute__((noreturn))
67 if (status != EXIT_SUCCESS)
68 fprintf (stderr, _("Try `%s --help' for more information.\n"),
72 _("%s: check alignment of virtual machine partitions\n"
73 "Copyright (C) 2011 Red Hat Inc.\n"
75 " %s [--options] -d domname\n"
76 " %s [--options] -a disk.img [-a disk.img ...]\n"
78 " -a|--add image Add image\n"
79 " -c|--connect uri Specify libvirt URI for -d option\n"
80 " -d|--domain guest Add disks from libvirt guest\n"
81 " --format[=raw|..] Force disk format for -a option\n"
82 " --help Display brief help\n"
83 " -q|--quiet No output, just exit code\n"
84 " -v|--verbose Verbose messages\n"
85 " -V|--version Display version and exit\n"
86 " -x Trace libguestfs API calls\n"
87 "For more information, see the manpage %s(1).\n"),
88 program_name, program_name, program_name,
95 main (int argc, char *argv[])
97 /* Set global program name that is not polluted with libtool artifacts. */
98 set_program_name (argv[0]);
100 setlocale (LC_ALL, "");
101 bindtextdomain (PACKAGE, LOCALEBASEDIR);
102 textdomain (PACKAGE);
104 enum { HELP_OPTION = CHAR_MAX + 1 };
106 static const char *options = "a:c:d:qvVx";
107 static const struct option long_options[] = {
108 { "add", 1, 0, 'a' },
109 { "connect", 1, 0, 'c' },
110 { "domain", 1, 0, 'd' },
111 { "format", 2, 0, 0 },
112 { "help", 0, 0, HELP_OPTION },
113 { "quiet", 0, 0, 'q' },
114 { "verbose", 0, 0, 'v' },
115 { "version", 0, 0, 'V' },
118 struct drv *drvs = NULL;
120 const char *format = NULL;
125 g = guestfs_create ();
127 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
131 argv[0] = bad_cast (program_name);
134 c = getopt_long (argc, argv, options, long_options, &option_index);
138 case 0: /* options which are long only */
139 if (STREQ (long_options[option_index].name, "format")) {
140 if (!optarg || STREQ (optarg, ""))
145 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
146 program_name, long_options[option_index].name, option_index);
180 usage (EXIT_SUCCESS);
183 usage (EXIT_FAILURE);
187 /* These are really constants, but they have to be variables for the
188 * options parsing code. Assert here that they have known-good
191 assert (read_only == 1);
192 assert (inspector == 0);
195 /* Must be no extra arguments on the command line. */
197 usage (EXIT_FAILURE);
199 /* The user didn't specify any drives to scan. */
201 usage (EXIT_FAILURE);
203 /* Add domains/drives from the command line (for a single guest). */
204 add_drives (drvs, 'a');
206 if (guestfs_launch (g) == -1)
209 /* Free up data structures, no longer needed after this point. */
212 /* Perform the scan. */
228 struct guestfs_partition_list *parts;
230 devices = guestfs_list_devices (g);
232 for (i = 0; devices[i] != NULL; ++i) {
233 parts = guestfs_part_list (g, devices[i]);
235 /* Canonicalize the name of the device for printing. */
236 if (STRPREFIX (devices[i], "/dev/") &&
237 (devices[i][5] == 'h' || devices[i][5] == 'v') &&
238 devices[i][6] == 'd' &&
239 c_isalpha (devices[i][7]))
242 for (j = 0; j < parts->len; ++j) {
243 /* Start offset of the partition in bytes. */
244 start = parts->val[j].part_start;
247 printf ("%s%d %12" PRIu64 " ",
248 devices[i], (int) parts->val[j].part_num, start);
250 /* What's the alignment? */
251 if (start == 0) /* Probably not possible, but anyway. */
254 for (alignment = 0; (start & 1) == 0; alignment++, start /= 2)
259 printf ("%12" PRIu64 " ", UINT64_C(1) << alignment);
260 else if (alignment < 64)
261 printf ("%12" PRIu64 "K ", UINT64_C(1) << (alignment - 10));
266 if (alignment < 12) { /* Bad in general: < 4K alignment */
269 printf ("bad (%s)\n", _("alignment < 4K"));
270 } else if (alignment < 16) { /* Bad on NetApps: < 64K alignment */
274 printf ("bad (%s)\n", _("alignment < 64K"));
281 guestfs_free_partition_list (parts);