713501e90c19669c672308d44ab66157d7e5219d
[libguestfs.git] / fish / inspect.c
1 /* libguestfs - guestfish and guestmount shared option parsing
2  * Copyright (C) 2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "c-ctype.h"
26
27 #include "guestfs.h"
28
29 #include "options.h"
30
31 /* Global that saves the root device between inspect_mount and
32  * print_inspect_prompt.
33  */
34 static char *root = NULL;
35
36 static void
37 free_strings (char **argv)
38 {
39   int argc;
40
41   for (argc = 0; argv[argc] != NULL; ++argc)
42     free (argv[argc]);
43   free (argv);
44 }
45
46 static int
47 count_strings (char *const *argv)
48 {
49   int c;
50
51   for (c = 0; argv[c]; ++c)
52     ;
53   return c;
54 }
55
56 static int
57 compare_keys_len (const void *p1, const void *p2)
58 {
59   const char *key1 = * (char * const *) p1;
60   const char *key2 = * (char * const *) p2;
61   return strlen (key1) - strlen (key2);
62 }
63
64 static int
65 compare_keys (const void *p1, const void *p2)
66 {
67   const char *key1 = * (char * const *) p1;
68   const char *key2 = * (char * const *) p2;
69   return strcasecmp (key1, key2);
70 }
71
72 /* This function implements the -i option. */
73 void
74 inspect_mount (void)
75 {
76   inspect_do_decrypt ();
77
78   char **roots = guestfs_inspect_os (g);
79   if (roots == NULL)
80     exit (EXIT_FAILURE);
81
82   if (roots[0] == NULL) {
83     fprintf (stderr, _("%s: no operating system was found on this disk\n"),
84              program_name);
85     exit (EXIT_FAILURE);
86   }
87
88   if (roots[1] != NULL) {
89     fprintf (stderr, _("%s: multi-boot operating systems are not supported by the -i option\n"),
90              program_name);
91     exit (EXIT_FAILURE);
92   }
93
94   root = roots[0];
95   free (roots);
96
97   inspect_mount_root (root);
98 }
99
100 void
101 inspect_mount_root (const char *root)
102 {
103   char **mountpoints = guestfs_inspect_get_mountpoints (g, root);
104   if (mountpoints == NULL)
105     exit (EXIT_FAILURE);
106
107   /* Sort by key length, shortest key first, so that we end up
108    * mounting the filesystems in the correct order.
109    */
110   qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
111          compare_keys_len);
112
113   size_t i;
114   size_t mount_errors = 0;
115   for (i = 0; mountpoints[i] != NULL; i += 2) {
116     int r;
117     if (!read_only)
118       r = guestfs_mount_options (g, "", mountpoints[i+1], mountpoints[i]);
119     else
120       r = guestfs_mount_ro (g, mountpoints[i+1], mountpoints[i]);
121     if (r == -1)
122       mount_errors++;
123   }
124
125   free_strings (mountpoints);
126
127   if (mount_errors)
128     fprintf (stderr, _("%s: some filesystems could not be mounted (ignored)\n"),
129              program_name);
130 }
131
132 /* This function is called only if the above function was called,
133  * and only after we've printed the prompt in interactive mode.
134  */
135 void
136 print_inspect_prompt (void)
137 {
138   char *name = guestfs_inspect_get_product_name (g, root);
139   if (STRNEQ (name, "unknown"))
140     printf (_("Operating system: %s\n"), name);
141   free (name);
142
143   char **mountpoints = guestfs_inspect_get_mountpoints (g, root);
144   if (mountpoints == NULL)
145     return;
146
147   /* Sort by key. */
148   qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
149          compare_keys);
150
151   size_t i;
152   for (i = 0; mountpoints[i] != NULL; i += 2)
153     printf (_("%s mounted on %s\n"), mountpoints[i+1], mountpoints[i]);
154
155   free_strings (mountpoints);
156 }
157
158 /* Make a LUKS map name from the partition name,
159  * eg "/dev/vda2" => "luksvda2"
160  */
161 static void
162 make_mapname (const char *device, char *mapname, size_t len)
163 {
164   size_t i = 0;
165
166   if (len < 5)
167     abort ();
168   strcpy (mapname, "luks");
169   mapname += 4;
170   len -= 4;
171
172   if (STRPREFIX (device, "/dev/"))
173     i = 5;
174
175   for (; device[i] != '\0' && len >= 1; ++i) {
176     if (c_isalnum (device[i])) {
177       *mapname++ = device[i];
178       len--;
179     }
180   }
181
182   *mapname = '\0';
183 }
184
185 /* Simple implementation of decryption: look for any crypto_LUKS
186  * partitions and decrypt them, then rescan for VGs.  This only works
187  * for Fedora whole-disk encryption.  WIP to make this work for other
188  * encryption schemes.
189  */
190 void
191 inspect_do_decrypt (void)
192 {
193   char **partitions = guestfs_list_partitions (g);
194   if (partitions == NULL)
195     exit (EXIT_FAILURE);
196
197   int need_rescan = 0;
198   size_t i;
199   for (i = 0; partitions[i] != NULL; ++i) {
200     char *type = guestfs_vfs_type (g, partitions[i]);
201     if (type && STREQ (type, "crypto_LUKS")) {
202       char mapname[32];
203       make_mapname (partitions[i], mapname, sizeof mapname);
204
205       char *key = read_key (partitions[i]);
206       /* XXX Should we call guestfs_luks_open_ro if readonly flag
207        * is set?  This might break 'mount_ro'.
208        */
209       if (guestfs_luks_open (g, partitions[i], key, mapname) == -1)
210         exit (EXIT_FAILURE);
211
212       free (key);
213
214       need_rescan = 1;
215     }
216     free (type);
217   }
218
219   free_strings (partitions);
220
221   if (need_rescan) {
222     if (guestfs_vgscan (g) == -1)
223       exit (EXIT_FAILURE);
224     if (guestfs_vg_activate_all (g, 1) == -1)
225       exit (EXIT_FAILURE);
226   }
227 }