1 /* libguestfs - guestfish and guestmount shared option parsing
2 * Copyright (C) 2010 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., 675 Mass Ave, Cambridge, MA 02139, USA.
29 /* Global that saves the root device between inspect_mount and
30 * print_inspect_prompt.
32 static char *root = NULL;
35 free_strings (char **argv)
39 for (argc = 0; argv[argc] != NULL; ++argc)
45 count_strings (char *const *argv)
49 for (c = 0; argv[c]; ++c)
55 compare_keys_len (const void *p1, const void *p2)
57 const char *key1 = * (char * const *) p1;
58 const char *key2 = * (char * const *) p2;
59 return strlen (key1) - strlen (key2);
63 compare_keys (const void *p1, const void *p2)
65 const char *key1 = * (char * const *) p1;
66 const char *key2 = * (char * const *) p2;
67 return strcasecmp (key1, key2);
70 /* This function implements the -i option. */
74 char **roots = guestfs_inspect_os (g);
78 if (roots[0] == NULL) {
79 fprintf (stderr, _("%s: no operating system was found on this disk\n"),
84 if (roots[1] != NULL) {
85 fprintf (stderr, _("%s: multi-boot operating systems are not supported by the -i option\n"),
93 char **mountpoints = guestfs_inspect_get_mountpoints (g, root);
94 if (mountpoints == NULL)
97 /* Sort by key length, shortest key first, so that we end up
98 * mounting the filesystems in the correct order.
100 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
104 for (i = 0; mountpoints[i] != NULL; i += 2) {
107 r = guestfs_mount_options (g, "", mountpoints[i+1], mountpoints[i]);
109 r = guestfs_mount_ro (g, mountpoints[i+1], mountpoints[i]);
114 free_strings (mountpoints);
117 /* This function is called only if the above function was called,
118 * and only after we've printed the prompt in interactive mode.
121 print_inspect_prompt (void)
123 char *name = guestfs_inspect_get_product_name (g, root);
124 if (STRNEQ (name, "unknown"))
125 printf (_("Operating system: %s\n"), name);
128 char **mountpoints = guestfs_inspect_get_mountpoints (g, root);
129 if (mountpoints == NULL)
133 qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
137 for (i = 0; mountpoints[i] != NULL; i += 2)
138 printf (_("%s mounted on %s\n"), mountpoints[i+1], mountpoints[i]);
140 free_strings (mountpoints);