1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/* libguestfs - the guestfsd daemon
* Copyright (C) 2010-2012 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <augeas.h>
#include "c-ctype.h"
#include "ignore-value.h"
#include "daemon.h"
#include "actions.h"
GUESTFSD_EXT_CMD(str_lvm, lvm);
GUESTFSD_EXT_CMD(str_cp, cp);
GUESTFSD_EXT_CMD(str_rm, rm);
GUESTFSD_EXT_CMD(str_lvmetad, lvmetad);
/* This runs during daemon start up and creates a complete copy of
* /etc/lvm so that we can modify it as we desire. We set
* LVM_SYSTEM_DIR to point to the copy. Note that the final directory
* layout is:
* /tmp/lvmXXXXXX (lvm_system_dir set to this)
* /tmp/lvmXXXXXX/lvm ($LVM_SYSTEM_DIR set to this)
* /tmp/lvmXXXXXX/lvm/lvm.conf (configuration file)
* /tmp/lvmXXXXXX/lvm/cache
* etc.
*/
static char lvm_system_dir[] = "/tmp/lvmXXXXXX";
static void rm_lvm_system_dir (void);
void
copy_lvm (void)
{
struct stat statbuf;
char cmd[64], env[64];
int r;
/* If /etc/lvm directory doesn't exist (or isn't a directory) assume
* that this system doesn't support LVM and do nothing.
*/
r = stat ("/etc/lvm", &statbuf);
if (r == -1) {
perror ("copy_lvm: stat: /etc/lvm");
return;
}
if (! S_ISDIR (statbuf.st_mode)) {
fprintf (stderr, "copy_lvm: warning: /etc/lvm is not a directory\n");
return;
}
if (mkdtemp (lvm_system_dir) == NULL)
error (EXIT_FAILURE, errno, "mkdtemp: %s", lvm_system_dir);
/* Copy the entire directory */
snprintf (cmd, sizeof cmd, "%s -a /etc/lvm/ %s", str_cp, lvm_system_dir);
r = system (cmd);
if (r == -1) {
perror (cmd);
rmdir (lvm_system_dir);
exit (EXIT_FAILURE);
}
if (WEXITSTATUS (r) != 0) {
fprintf (stderr, "cp command failed with return code %d\n",
WEXITSTATUS (r));
rmdir (lvm_system_dir);
exit (EXIT_FAILURE);
}
/* Set environment variable so we use the copy. */
snprintf (env, sizeof env, "%s/lvm", lvm_system_dir);
setenv ("LVM_SYSTEM_DIR", env, 1);
/* Set a handler to remove the temporary directory at exit. */
atexit (rm_lvm_system_dir);
}
/* Try to run lvmetad, without failing if it couldn't. */
void
start_lvmetad (void)
{
char cmd[64];
int r;
snprintf (cmd, sizeof cmd, "%s", str_lvmetad);
if (verbose)
printf ("%s\n", cmd);
r = system (cmd);
if (r == -1)
perror ("system/lvmetad");
else if (!WIFEXITED (r) || WEXITSTATUS (r) != 0)
fprintf (stderr, "warning: lvmetad command failed\n");
}
static void
rm_lvm_system_dir (void)
{
char cmd[64];
snprintf (cmd, sizeof cmd, "%s -rf %s", str_rm, lvm_system_dir);
ignore_value (system (cmd));
}
/* Rewrite the 'filter = [ ... ]' line in lvm.conf. */
static int
set_filter (char *const *filters)
{
CLEANUP_AUG_CLOSE augeas *aug = NULL;
int r;
int count;
/* Small optimization: do not load the files at init time,
* but do that only after having applied the transformation.
*/
const int flags = AUG_NO_ERR_CLOSE | AUG_NO_LOAD;
aug = aug_init (lvm_system_dir, "/usr/share/guestfs/", flags);
if (!aug) {
reply_with_error ("augeas initialization failed");
return -1;
}
if (aug_error (aug) != AUG_NOERROR) {
AUGEAS_ERROR ("aug_init");
return -1;
}
r = aug_transform (aug, "guestfs_lvm_conf", "/lvm/lvm.conf",
0 /* = included */);
if (r == -1) {
AUGEAS_ERROR ("aug_transform");
return -1;
}
if (aug_load (aug) == -1) {
AUGEAS_ERROR ("aug_load");
return -1;
}
/* Remove all the old filters ... */
r = aug_rm (aug, "/files/lvm/lvm.conf/devices/dict/filter/list/*");
if (r == -1) {
AUGEAS_ERROR ("aug_rm/filter");
return -1;
}
r = aug_rm (aug, "/files/lvm/lvm.conf/devices/dict/global_filter/list/*");
if (r == -1) {
AUGEAS_ERROR ("aug_rm/global_filter");
return -1;
}
/* ... and add the new ones. */
for (count = 0; filters[count] != NULL; ++count) {
char buf[128];
snprintf (buf, sizeof buf,
"/files/lvm/lvm.conf/devices/dict/filter/list/%d/str",
count + 1);
if (aug_set (aug, buf, filters[count]) == -1) {
AUGEAS_ERROR ("aug_set/filter: %d: %s", count, filters[count]);
return -1;
}
snprintf (buf, sizeof buf,
"/files/lvm/lvm.conf/devices/dict/global_filter/list/%d/str",
count + 1);
if (aug_set (aug, buf, filters[count]) == -1) {
AUGEAS_ERROR ("aug_set/global_filter: %d: %s", count, filters[count]);
return -1;
}
}
/* Safety check for the written filter nodes. */
r = aug_match (aug, "/files/lvm/lvm.conf/devices/dict/filter/list/*/str",
NULL);
if (r == -1) {
AUGEAS_ERROR ("aug_match/filter");
return -1;
}
if (r != count) {
reply_with_error ("filters# vs matches mismatch: %d vs %d", count, r);
return -1;
}
r = aug_match (aug, "/files/lvm/lvm.conf/devices/dict/global_filter/list/*/str",
NULL);
if (r == -1) {
AUGEAS_ERROR ("aug_match/global_filter");
return -1;
}
if (r != count) {
reply_with_error ("global_filter# vs matches mismatch: %d vs %d", count, r);
return -1;
}
if (aug_save (aug) == -1) {
AUGEAS_ERROR ("aug_save");
return -1;
}
return 0;
}
static int
vgchange (const char *vgchange_flag)
{
CLEANUP_FREE char *err = NULL;
int r = command (NULL, &err, str_lvm, "vgchange", vgchange_flag, NULL);
if (r == -1) {
reply_with_error ("vgchange %s: %s", vgchange_flag, err);
return -1;
}
return 0;
}
/* Deactivate all VGs. */
static int
deactivate (void)
{
return vgchange ("-an");
}
/* Reactivate all VGs. */
static int
reactivate (void)
{
return vgchange ("-ay");
}
/* Clear the cache and rescan. */
static int
rescan (void)
{
char lvm_cache[64];
snprintf (lvm_cache, sizeof lvm_cache, "%s/lvm/cache/.cache", lvm_system_dir);
unlink (lvm_cache);
CLEANUP_FREE char *err = NULL;
int r = command (NULL, &err, str_lvm, "vgscan", NULL);
if (r == -1) {
reply_with_error ("vgscan: %s", err);
return -1;
}
return 0;
}
/* Construct the new, specific filter strings. We can assume that
* the 'devices' array does not contain any regexp metachars,
* because it's already been checked by the stub code.
*/
static char **
make_filter_strings (char *const *devices)
{
size_t i;
CLEANUP_FREE_STRINGSBUF DECLARE_STRINGSBUF (ret);
for (i = 0; devices[i] != NULL; ++i) {
/* Because of the way matching works in LVM (yes, they wrote their
* own regular expression engine!), each match clause should be either:
*
* for single partitions:
* "a|^/dev/sda1$|",
* for whole block devices:
* "a|^/dev/sda$|", "a|^/dev/sda[0-9]|",
*/
const size_t slen = strlen (devices[i]);
if (add_sprintf (&ret, "a|^%s$|", devices[i]) == -1)
return NULL;
if (!c_isdigit (devices[i][slen-1])) {
/* whole block device */
if (add_sprintf (&ret, "a|^%s[0-9]|", devices[i]) == -1)
return NULL;
}
}
if (add_string (&ret, "r|.*|") == -1)
return NULL;
if (end_stringsbuf (&ret) == -1)
return NULL;
return take_stringsbuf (&ret);
}
int
do_lvm_set_filter (char *const *devices)
{
CLEANUP_FREE_STRING_LIST char **filters = make_filter_strings (devices);
if (filters == NULL)
return -1;
if (deactivate () == -1)
return -1;
int r = set_filter (filters);
if (r == -1)
return -1;
if (rescan () == -1)
return -1;
return reactivate ();
}
int
do_lvm_clear_filter (void)
{
const char *const filters[2] = { "a/.*/", NULL };
if (deactivate () == -1)
return -1;
if (set_filter ((char *const *) filters) == -1)
return -1;
if (rescan () == -1)
return -1;
return reactivate ();
}
|