Update FSF address.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 size_t
47 count_strings (char *const *argv)
48 {
49   size_t i;
50
51   for (i = 0; argv[i]; ++i)
52     ;
53   return i;
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   if (live) {
77     fprintf (stderr, _("%s: don't use --live and -i options together\n"),
78              program_name);
79     exit (EXIT_FAILURE);
80   }
81
82   inspect_do_decrypt ();
83
84   char **roots = guestfs_inspect_os (g);
85   if (roots == NULL)
86     exit (EXIT_FAILURE);
87
88   if (roots[0] == NULL) {
89     fprintf (stderr, _("%s: no operating system was found on this disk\n"),
90              program_name);
91     free_strings (roots);
92     exit (EXIT_FAILURE);
93   }
94
95   if (roots[1] != NULL) {
96     fprintf (stderr, _("%s: multi-boot operating systems are not supported by the -i option\n"),
97              program_name);
98     free_strings (roots);
99     exit (EXIT_FAILURE);
100   }
101
102   root = roots[0];
103   free (roots);
104
105   inspect_mount_root (root);
106 }
107
108 void
109 inspect_mount_root (const char *root)
110 {
111   char **mountpoints = guestfs_inspect_get_mountpoints (g, root);
112   if (mountpoints == NULL)
113     exit (EXIT_FAILURE);
114
115   /* Sort by key length, shortest key first, so that we end up
116    * mounting the filesystems in the correct order.
117    */
118   qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
119          compare_keys_len);
120
121   size_t i;
122   size_t mount_errors = 0;
123   for (i = 0; mountpoints[i] != NULL; i += 2) {
124     int r;
125     if (!read_only)
126       r = guestfs_mount_options (g, "", mountpoints[i+1], mountpoints[i]);
127     else
128       r = guestfs_mount_ro (g, mountpoints[i+1], mountpoints[i]);
129     if (r == -1)
130       mount_errors++;
131   }
132
133   free_strings (mountpoints);
134
135   if (mount_errors)
136     fprintf (stderr, _("%s: some filesystems could not be mounted (ignored)\n"),
137              program_name);
138 }
139
140 /* This function is called only if the above function was called,
141  * and only after we've printed the prompt in interactive mode.
142  */
143 void
144 print_inspect_prompt (void)
145 {
146   char *name = guestfs_inspect_get_product_name (g, root);
147   if (name && STRNEQ (name, "unknown"))
148     printf (_("Operating system: %s\n"), name);
149   free (name);
150
151   char **mountpoints = guestfs_inspect_get_mountpoints (g, root);
152   if (mountpoints == NULL)
153     return;
154
155   /* Sort by key. */
156   qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
157          compare_keys);
158
159   size_t i;
160   for (i = 0; mountpoints[i] != NULL; i += 2)
161     printf (_("%s mounted on %s\n"), mountpoints[i+1], mountpoints[i]);
162
163   free_strings (mountpoints);
164 }
165
166 /* Make a LUKS map name from the partition name,
167  * eg "/dev/vda2" => "luksvda2"
168  */
169 static void
170 make_mapname (const char *device, char *mapname, size_t len)
171 {
172   size_t i = 0;
173
174   if (len < 5)
175     abort ();
176   strcpy (mapname, "luks");
177   mapname += 4;
178   len -= 4;
179
180   if (STRPREFIX (device, "/dev/"))
181     i = 5;
182
183   for (; device[i] != '\0' && len >= 1; ++i) {
184     if (c_isalnum (device[i])) {
185       *mapname++ = device[i];
186       len--;
187     }
188   }
189
190   *mapname = '\0';
191 }
192
193 /* Simple implementation of decryption: look for any crypto_LUKS
194  * partitions and decrypt them, then rescan for VGs.  This only works
195  * for Fedora whole-disk encryption.  WIP to make this work for other
196  * encryption schemes.
197  */
198 void
199 inspect_do_decrypt (void)
200 {
201   char **partitions = guestfs_list_partitions (g);
202   if (partitions == NULL)
203     exit (EXIT_FAILURE);
204
205   int need_rescan = 0;
206   size_t i;
207   for (i = 0; partitions[i] != NULL; ++i) {
208     char *type = guestfs_vfs_type (g, partitions[i]);
209     if (type && STREQ (type, "crypto_LUKS")) {
210       char mapname[32];
211       make_mapname (partitions[i], mapname, sizeof mapname);
212
213       char *key = read_key (partitions[i]);
214       /* XXX Should we call guestfs_luks_open_ro if readonly flag
215        * is set?  This might break 'mount_ro'.
216        */
217       if (guestfs_luks_open (g, partitions[i], key, mapname) == -1)
218         exit (EXIT_FAILURE);
219
220       free (key);
221
222       need_rescan = 1;
223     }
224     free (type);
225   }
226
227   free_strings (partitions);
228
229   if (need_rescan) {
230     if (guestfs_vgscan (g) == -1)
231       exit (EXIT_FAILURE);
232     if (guestfs_vg_activate_all (g, 1) == -1)
233       exit (EXIT_FAILURE);
234   }
235 }