1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2010-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.
29 #include "ignore-value.h"
34 /* This runs during daemon start up and creates a complete copy of
35 * /etc/lvm so that we can modify it as we desire. We set
36 * LVM_SYSTEM_DIR to point to the copy.
38 static char lvm_system_dir[] = "/tmp/lvmXXXXXX";
40 static void rm_lvm_system_dir (void);
49 /* If /etc/lvm directory doesn't exist (or isn't a directory) assume
50 * that this system doesn't support LVM and do nothing.
52 r = stat ("/etc/lvm", &statbuf);
54 perror ("copy_lvm: stat: /etc/lvm");
57 if (! S_ISDIR (statbuf.st_mode)) {
58 fprintf (stderr, "copy_lvm: warning: /etc/lvm is not a directory\n");
62 if (mkdtemp (lvm_system_dir) == NULL) {
63 perror (lvm_system_dir);
67 /* Hopefully no dotfiles in there ... XXX */
68 snprintf (cmd, sizeof cmd, "cp -a /etc/lvm/* %s", lvm_system_dir);
72 rmdir (lvm_system_dir);
76 if (WEXITSTATUS (r) != 0) {
77 fprintf (stderr, "cp command failed with return code %d\n",
79 rmdir (lvm_system_dir);
83 /* Set environment variable so we use the copy. */
84 setenv ("LVM_SYSTEM_DIR", lvm_system_dir, 1);
86 /* Set a handler to remove the temporary directory at exit. */
87 atexit (rm_lvm_system_dir);
91 rm_lvm_system_dir (void)
95 snprintf (cmd, sizeof cmd, "rm -rf %s", lvm_system_dir);
96 ignore_value (system (cmd));
99 /* Does the current line match the regexp /^\s*filter\s*=/ */
101 is_filter_line (const char *line)
103 while (*line && c_isspace (*line))
108 if (! STRPREFIX (line, "filter"))
112 while (*line && c_isspace (*line))
123 /* Rewrite the 'filter = [ ... ]' line in lvm.conf. */
125 set_filter (const char *filter)
128 snprintf (lvm_conf, sizeof lvm_conf, "%s/lvm.conf", lvm_system_dir);
130 char lvm_conf_new[64];
131 snprintf (lvm_conf_new, sizeof lvm_conf, "%s/lvm.conf.new", lvm_system_dir);
133 FILE *ifp = fopen (lvm_conf, "r");
135 reply_with_perror ("open: %s", lvm_conf);
138 FILE *ofp = fopen (lvm_conf_new, "w");
140 reply_with_perror ("open: %s", lvm_conf_new);
147 while (getline (&line, &len, ifp) != -1) {
149 if (is_filter_line (line)) {
150 r = fprintf (ofp, " filter = [ %s ]\n", filter);
152 r = fprintf (ofp, "%s", line);
155 /* NB. fprintf doesn't set errno on error. */
156 reply_with_error ("%s: write failed", lvm_conf_new);
160 unlink (lvm_conf_new);
167 if (fclose (ifp) == EOF) {
168 reply_with_perror ("close: %s", lvm_conf);
169 unlink (lvm_conf_new);
173 if (fclose (ofp) == EOF) {
174 reply_with_perror ("close: %s", lvm_conf_new);
175 unlink (lvm_conf_new);
179 if (rename (lvm_conf_new, lvm_conf) == -1) {
180 reply_with_perror ("rename: %s", lvm_conf);
181 unlink (lvm_conf_new);
189 vgchange (const char *vgchange_flag)
192 int r = command (NULL, &err, "lvm", "vgchange", vgchange_flag, NULL);
194 reply_with_error ("vgchange: %s", err);
203 /* Deactivate all VGs. */
207 return vgchange ("-an");
210 /* Reactivate all VGs. */
214 return vgchange ("-ay");
217 /* Clear the cache and rescan. */
222 snprintf (lvm_cache, sizeof lvm_cache, "%s/cache/.cache", lvm_system_dir);
227 int r = command (NULL, &err, "lvm", "vgscan", NULL);
229 reply_with_error ("vgscan: %s", err);
238 /* Construct the new, specific filter string. We can assume that
239 * the 'devices' array does not contain any regexp metachars,
240 * because it's already been checked by the stub code.
243 make_filter_string (char *const *devices)
247 for (i = 0; devices[i] != NULL; ++i)
248 len += strlen (devices[i]) + 16;
250 char *filter = malloc (len);
251 if (filter == NULL) {
252 reply_with_perror ("malloc");
257 for (i = 0; devices[i] != NULL; ++i) {
258 /* Because of the way matching works in LVM, each match clause
260 * "a|^/dev/sda|", for whole block devices, or
261 * "a|^/dev/sda1$|", for single partitions
262 * (the assumption being we have <= 26 block devices XXX).
264 size_t slen = strlen (devices[i]);
267 if (c_isdigit (devices[i][slen-1]))
268 snprintf (str, slen+16, "\"a|^%s$|\", ", devices[i]);
270 snprintf (str, slen+16, "\"a|^%s|\", ", devices[i]);
275 strcpy (p, "\"r|.*|\"");
277 return filter; /* Caller must free. */
281 do_lvm_set_filter (char *const *devices)
283 char *filter = make_filter_string (devices);
287 if (deactivate () == -1) {
292 int r = set_filter (filter);
300 return reactivate ();
304 do_lvm_clear_filter (void)
306 if (deactivate () == -1)
309 if (set_filter ("\"a/.*/\"") == -1)
315 return reactivate ();