daemon: Don't use ../src path to include generator_protocol.h
[libguestfs.git] / daemon / lvm-filter.c
1 /* libguestfs - the guestfsd daemon
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 <inttypes.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "daemon.h"
28 #include "c-ctype.h"
29 #include "actions.h"
30
31 /* Does the current line match the regexp /^\s*filter\s*=/ */
32 static int
33 is_filter_line (const char *line)
34 {
35   while (*line && c_isspace (*line))
36     line++;
37   if (!*line)
38     return 0;
39
40   if (! STRPREFIX (line, "filter"))
41     return 0;
42   line += 6;
43
44   while (*line && c_isspace (*line))
45     line++;
46   if (!*line)
47     return 0;
48
49   if (*line != '=')
50     return 0;
51
52   return 1;
53 }
54
55 /* Rewrite the 'filter = [ ... ]' line in /etc/lvm/lvm.conf. */
56 static int
57 set_filter (const char *filter)
58 {
59   if (verbose)
60     fprintf (stderr, "LVM: setting device filter to %s\n", filter);
61
62   FILE *ifp = fopen ("/etc/lvm/lvm.conf", "r");
63   if (ifp == NULL) {
64     reply_with_perror ("open: /etc/lvm/lvm.conf");
65     return -1;
66   }
67   FILE *ofp = fopen ("/etc/lvm/lvm.conf.new", "w");
68   if (ofp == NULL) {
69     reply_with_perror ("open: /etc/lvm/lvm.conf.new");
70     fclose (ifp);
71     return -1;
72   }
73
74   char *line = NULL;
75   size_t len = 0;
76   while (getline (&line, &len, ifp) != -1) {
77     int r;
78     if (is_filter_line (line)) {
79       if (verbose)
80         fprintf (stderr, "LVM: replacing config line:\n%s", line);
81       r = fprintf (ofp, "    filter = [ %s ]\n", filter);
82     } else {
83       r = fprintf (ofp, "%s", line);
84     }
85     if (r < 0) {
86       /* NB. fprintf doesn't set errno on error. */
87       reply_with_error ("/etc/lvm/lvm.conf.new: write failed");
88       fclose (ifp);
89       fclose (ofp);
90       free (line);
91       unlink ("/etc/lvm/lvm.conf.new");
92       return -1;
93     }
94   }
95
96   free (line);
97
98   if (fclose (ifp) == EOF) {
99     reply_with_perror ("/etc/lvm/lvm.conf.new");
100     unlink ("/etc/lvm/lvm.conf.new");
101     fclose (ofp);
102     return -1;
103   }
104   if (fclose (ofp) == EOF) {
105     reply_with_perror ("/etc/lvm/lvm.conf.new");
106     unlink ("/etc/lvm/lvm.conf.new");
107     return -1;
108   }
109
110   if (rename ("/etc/lvm/lvm.conf.new", "/etc/lvm/lvm.conf") == -1) {
111     reply_with_perror ("rename: /etc/lvm/lvm.conf");
112     unlink ("/etc/lvm/lvm.conf.new");
113     return -1;
114   }
115
116   return 0;
117 }
118
119 static int
120 vgchange (const char *vgchange_flag)
121 {
122   char *err;
123   int r = command (NULL, &err, "lvm", "vgchange", vgchange_flag, NULL);
124   if (r == -1) {
125     reply_with_error ("vgscan: %s", err);
126     free (err);
127     return -1;
128   }
129
130   free (err);
131   return 0;
132 }
133
134 /* Deactivate all VGs. */
135 static int
136 deactivate (void)
137 {
138   return vgchange ("-an");
139 }
140
141 /* Reactivate all VGs. */
142 static int
143 reactivate (void)
144 {
145   return vgchange ("-ay");
146 }
147
148 /* Clear the cache and rescan. */
149 static int
150 rescan (void)
151 {
152   unlink ("/etc/lvm/cache/.cache");
153
154   char *err;
155   int r = command (NULL, &err, "lvm", "vgscan", NULL);
156   if (r == -1) {
157     reply_with_error ("vgscan: %s", err);
158     free (err);
159     return -1;
160   }
161
162   free (err);
163   return 0;
164 }
165
166 /* Construct the new, specific filter string.  We can assume that
167  * the 'devices' array does not contain any regexp metachars,
168  * because it's already been checked by the stub code.
169  */
170 static char *
171 make_filter_string (char *const *devices)
172 {
173   size_t i;
174   size_t len = 64;
175   for (i = 0; devices[i] != NULL; ++i)
176     len += strlen (devices[i]) + 16;
177
178   char *filter = malloc (len);
179   if (filter == NULL) {
180     reply_with_perror ("malloc");
181     return NULL;
182   }
183
184   char *p = filter;
185   for (i = 0; devices[i] != NULL; ++i) {
186     /* Because of the way matching works in LVM, each match clause
187      * should be either:
188      *   "a|^/dev/sda|",      for whole block devices, or
189      *   "a|^/dev/sda1$|",    for single partitions
190      * (the assumption being we have <= 26 block devices XXX).
191      */
192     size_t slen = strlen (devices[i]);
193     char str[slen+16];
194
195     if (c_isdigit (devices[i][slen-1]))
196       snprintf (str, slen+16, "\"a|^%s$|\", ", devices[i]);
197     else
198       snprintf (str, slen+16, "\"a|^%s|\", ", devices[i]);
199
200     strcpy (p, str);
201     p += strlen (str);
202   }
203   strcpy (p, "\"r|.*|\"");
204
205   return filter;                /* Caller must free. */
206 }
207
208 int
209 do_lvm_set_filter (char *const *devices)
210 {
211   char *filter = make_filter_string (devices);
212   if (filter == NULL)
213     return -1;
214
215   if (deactivate () == -1) {
216     free (filter);
217     return -1;
218   }
219
220   int r = set_filter (filter);
221   free (filter);
222   if (r == -1)
223     return -1;
224
225   if (rescan () == -1)
226     return -1;
227
228   return reactivate ();
229 }
230
231 int
232 do_lvm_clear_filter (void)
233 {
234   if (deactivate () == -1)
235     return -1;
236
237   if (set_filter ("\"a/.*/\"") == -1)
238     return -1;
239
240   if (rescan () == -1)
241     return -1;
242
243   return reactivate ();
244 }