1 /* $NetBSD: toolcontext.c,v 1.6 2009/12/02 01:53:25 haad Exp $ */
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "toolcontext.h"
22 #include "lvm-string.h"
25 #include "filter-composite.h"
26 #include "filter-md.h"
27 #include "filter-persistent.h"
28 #include "filter-regex.h"
29 #include "filter-sysfs.h"
32 #include "format-text.h"
38 #include "dev-cache.h"
42 #include "sharedlib.h"
50 #include "format_pool.h"
55 #include <sys/utsname.h>
63 static int _get_env_vars(struct cmd_context
*cmd
)
67 /* Set to "" to avoid using any system directory */
68 if ((e
= getenv("LVM_SYSTEM_DIR"))) {
69 if (dm_snprintf(cmd
->system_dir
, sizeof(cmd
->system_dir
),
71 log_error("LVM_SYSTEM_DIR environment variable "
80 static void _get_sysfs_dir(struct cmd_context
*cmd
)
82 static char proc_mounts
[PATH_MAX
];
83 static char *split
[4], buffer
[PATH_MAX
+ 16];
87 cmd
->sysfs_dir
[0] = '\0';
88 if (!*cmd
->proc_dir
) {
89 log_debug("No proc filesystem found: skipping sysfs detection");
93 if (dm_snprintf(proc_mounts
, sizeof(proc_mounts
),
94 "%s/mounts", cmd
->proc_dir
) < 0) {
95 log_error("Failed to create /proc/mounts string for sysfs detection");
99 if (!(fp
= fopen(proc_mounts
, "r"))) {
100 log_sys_error("_get_sysfs_dir: fopen %s", proc_mounts
);
104 while (fgets(buffer
, sizeof(buffer
), fp
)) {
105 if (dm_split_words(buffer
, 4, 0, split
) == 4 &&
106 !strcmp(split
[2], "sysfs")) {
113 log_sys_error("fclose", proc_mounts
);
116 log_error("Failed to find sysfs mount point");
120 strncpy(cmd
->sysfs_dir
, sys_mnt
, sizeof(cmd
->sysfs_dir
));
123 static void _init_logging(struct cmd_context
*cmd
)
128 const char *log_file
;
132 cmd
->default_settings
.syslog
=
133 find_config_tree_int(cmd
, "log/syslog", DEFAULT_SYSLOG
);
134 if (cmd
->default_settings
.syslog
!= 1)
137 if (cmd
->default_settings
.syslog
> 1)
138 init_syslog(cmd
->default_settings
.syslog
);
140 /* Debug level for log file output */
141 cmd
->default_settings
.debug
=
142 find_config_tree_int(cmd
, "log/level", DEFAULT_LOGLEVEL
);
143 init_debug(cmd
->default_settings
.debug
);
145 /* Verbose level for tty output */
146 cmd
->default_settings
.verbose
=
147 find_config_tree_int(cmd
, "log/verbose", DEFAULT_VERBOSE
);
148 init_verbose(cmd
->default_settings
.verbose
+ VERBOSE_BASE_LEVEL
);
150 /* Log message formatting */
151 init_indent(find_config_tree_int(cmd
, "log/indent",
154 cmd
->default_settings
.msg_prefix
= find_config_tree_str(cmd
,
157 init_msg_prefix(cmd
->default_settings
.msg_prefix
);
159 cmd
->default_settings
.cmd_name
= find_config_tree_int(cmd
,
162 init_cmd_name(cmd
->default_settings
.cmd_name
);
165 cmd
->default_settings
.test
=
166 find_config_tree_int(cmd
, "global/test", 0);
167 init_test(cmd
->default_settings
.test
);
169 /* Settings for logging to file */
170 if (find_config_tree_int(cmd
, "log/overwrite", DEFAULT_OVERWRITE
))
173 log_file
= find_config_tree_str(cmd
, "log/file", 0);
176 release_log_memory();
178 init_log_file(log_file
, append
);
181 log_file
= find_config_tree_str(cmd
, "log/activate_file", 0);
183 init_log_direct(log_file
, append
);
185 init_log_while_suspended(find_config_tree_int(cmd
,
186 "log/activation", 0));
189 ctime_r(&t
, &timebuf
[0]);
191 log_verbose("Logging initialised at %s", timebuf
);
193 /* Tell device-mapper about our logging */
194 #ifdef DEVMAPPER_SUPPORT
195 dm_log_with_errno_init(print_log
);
199 static int _process_config(struct cmd_context
*cmd
)
202 const char *read_ahead
;
206 cmd
->default_settings
.umask
= find_config_tree_int(cmd
,
210 if ((old_umask
= umask((mode_t
) cmd
->default_settings
.umask
)) !=
211 (mode_t
) cmd
->default_settings
.umask
)
212 log_verbose("Set umask to %04o", cmd
->default_settings
.umask
);
215 if (dm_snprintf(cmd
->dev_dir
, sizeof(cmd
->dev_dir
), "%s/",
216 find_config_tree_str(cmd
, "devices/dir",
217 DEFAULT_DEV_DIR
)) < 0) {
218 log_error("Device directory given in config file too long");
221 #ifdef DEVMAPPER_SUPPORT
222 dm_set_dev_dir(cmd
->dev_dir
);
226 if (dm_snprintf(cmd
->proc_dir
, sizeof(cmd
->proc_dir
), "%s",
227 find_config_tree_str(cmd
, "global/proc",
228 DEFAULT_PROC_DIR
)) < 0) {
229 log_error("Device directory given in config file too long");
233 if (*cmd
->proc_dir
&& !dir_exists(cmd
->proc_dir
)) {
234 log_error("WARNING: proc dir %s not found - some checks will be bypassed",
236 *cmd
->proc_dir
= '\0';
242 cmd
->default_settings
.activation
= find_config_tree_int(cmd
,
245 set_activation(cmd
->default_settings
.activation
);
247 cmd
->default_settings
.suffix
= find_config_tree_int(cmd
,
251 if (!(cmd
->default_settings
.unit_factor
=
252 units_to_bytes(find_config_tree_str(cmd
,
255 &cmd
->default_settings
.unit_type
))) {
256 log_error("Invalid units specification");
260 read_ahead
= find_config_tree_str(cmd
, "activation/readahead", DEFAULT_READ_AHEAD
);
261 if (!strcasecmp(read_ahead
, "auto"))
262 cmd
->default_settings
.read_ahead
= DM_READ_AHEAD_AUTO
;
263 else if (!strcasecmp(read_ahead
, "none"))
264 cmd
->default_settings
.read_ahead
= DM_READ_AHEAD_NONE
;
266 log_error("Invalid readahead specification");
270 cmd
->default_settings
.udev_sync
= find_config_tree_int(cmd
,
271 "activation/udev_sync",
274 cmd
->stripe_filler
= find_config_tree_str(cmd
,
275 "activation/missing_stripe_filler",
276 DEFAULT_STRIPE_FILLER
);
278 /* FIXME Missing error code checks from the stats, not log_warn?, notify if setting overridden, delay message/check till it is actually used (eg consider if lvm shell - file could appear later after this check)? */
279 if (!strcmp(cmd
->stripe_filler
, "/dev/ioerror") &&
280 stat(cmd
->stripe_filler
, &st
))
281 cmd
->stripe_filler
= "error";
283 if (strcmp(cmd
->stripe_filler
, "error")) {
284 if (stat(cmd
->stripe_filler
, &st
)) {
285 log_warn("WARNING: activation/missing_stripe_filler = \"%s\" "
286 "is invalid,", cmd
->stripe_filler
);
287 log_warn(" stat failed: %s", strerror(errno
));
288 log_warn("Falling back to \"error\" missing_stripe_filler.");
289 cmd
->stripe_filler
= "error";
290 } else if (!S_ISBLK(st
.st_mode
)) {
291 log_warn("WARNING: activation/missing_stripe_filler = \"%s\" "
292 "is not a block device.", cmd
->stripe_filler
);
293 log_warn("Falling back to \"error\" missing_stripe_filler.");
294 cmd
->stripe_filler
= "error";
298 cmd
->si_unit_consistency
= find_config_tree_int(cmd
,
299 "global/si_unit_consistency",
300 DEFAULT_SI_UNIT_CONSISTENCY
);
305 static int _set_tag(struct cmd_context
*cmd
, const char *tag
)
307 log_very_verbose("Setting host tag: %s", dm_pool_strdup(cmd
->libmem
, tag
));
309 if (!str_list_add(cmd
->libmem
, &cmd
->tags
, tag
)) {
310 log_error("_set_tag: str_list_add %s failed", tag
);
317 static int _check_host_filters(struct cmd_context
*cmd
, struct config_node
*hn
,
320 struct config_node
*cn
;
321 struct config_value
*cv
;
325 for (cn
= hn
; cn
; cn
= cn
->sib
) {
328 if (!strcmp(cn
->key
, "host_list")) {
330 if (cn
->v
->type
== CFG_EMPTY_ARRAY
)
332 for (cv
= cn
->v
; cv
; cv
= cv
->next
) {
333 if (cv
->type
!= CFG_STRING
) {
334 log_error("Invalid hostname string "
335 "for tag %s", cn
->key
);
338 if (!strcmp(cv
->v
.str
, cmd
->hostname
)) {
344 if (!strcmp(cn
->key
, "host_filter")) {
345 log_error("host_filter not supported yet");
353 static int _init_tags(struct cmd_context
*cmd
, struct config_tree
*cft
)
355 const struct config_node
*tn
, *cn
;
359 if (!(tn
= find_config_node(cft
->root
, "tags")) || !tn
->child
)
362 /* NB hosttags 0 when already 1 intentionally does not delete the tag */
363 if (!cmd
->hosttags
&& find_config_int(cft
->root
, "tags/hosttags",
365 /* FIXME Strip out invalid chars: only A-Za-z0-9_+.- */
366 if (!_set_tag(cmd
, cmd
->hostname
))
371 for (cn
= tn
->child
; cn
; cn
= cn
->sib
) {
377 if (!validate_name(tag
)) {
378 log_error("Invalid tag in config file: %s", cn
->key
);
383 if (!_check_host_filters(cmd
, cn
->child
, &passes
))
388 if (!_set_tag(cmd
, tag
))
395 static int _load_config_file(struct cmd_context
*cmd
, const char *tag
)
397 char config_file
[PATH_MAX
] = "";
398 const char *filler
= "";
400 struct config_tree_list
*cfl
;
405 if (dm_snprintf(config_file
, sizeof(config_file
), "%s/lvm%s%s.conf",
406 cmd
->system_dir
, filler
, tag
) < 0) {
407 log_error("LVM_SYSTEM_DIR or tag was too long");
411 if (!(cfl
= dm_pool_alloc(cmd
->libmem
, sizeof(*cfl
)))) {
412 log_error("config_tree_list allocation failed");
416 if (!(cfl
->cft
= create_config_tree(config_file
, 0))) {
417 log_error("config_tree allocation failed");
421 /* Is there a config file? */
422 if (stat(config_file
, &info
) == -1) {
423 if (errno
== ENOENT
) {
424 dm_list_add(&cmd
->config_files
, &cfl
->list
);
427 log_sys_error("stat", config_file
);
428 destroy_config_tree(cfl
->cft
);
432 log_very_verbose("Loading config file: %s", config_file
);
433 if (!read_config_file(cfl
->cft
)) {
434 log_error("Failed to load config file %s", config_file
);
435 destroy_config_tree(cfl
->cft
);
439 dm_list_add(&cmd
->config_files
, &cfl
->list
);
443 _init_tags(cmd
, cfl
->cft
);
445 /* Use temporary copy of lvm.conf while loading other files */
451 /* Find and read first config file */
452 static int _init_lvm_conf(struct cmd_context
*cmd
)
454 /* No config file if LVM_SYSTEM_DIR is empty */
455 if (!*cmd
->system_dir
) {
456 if (!(cmd
->cft
= create_config_tree(NULL
, 0))) {
457 log_error("Failed to create config tree");
463 if (!_load_config_file(cmd
, ""))
469 /* Read any additional config files */
470 static int _init_tag_configs(struct cmd_context
*cmd
)
474 /* Tag list may grow while inside this loop */
475 dm_list_iterate_items(sl
, &cmd
->tags
) {
476 if (!_load_config_file(cmd
, sl
->str
))
483 static int _merge_config_files(struct cmd_context
*cmd
)
485 struct config_tree_list
*cfl
;
487 /* Replace temporary duplicate copy of lvm.conf */
488 if (cmd
->cft
->root
) {
489 if (!(cmd
->cft
= create_config_tree(NULL
, 0))) {
490 log_error("Failed to create config tree");
495 dm_list_iterate_items(cfl
, &cmd
->config_files
) {
496 /* Merge all config trees into cmd->cft using merge/tag rules */
497 if (!merge_config_tree(cmd
, cmd
->cft
, cfl
->cft
))
504 static void _destroy_tags(struct cmd_context
*cmd
)
506 struct dm_list
*slh
, *slht
;
508 dm_list_iterate_safe(slh
, slht
, &cmd
->tags
) {
513 int config_files_changed(struct cmd_context
*cmd
)
515 struct config_tree_list
*cfl
;
517 dm_list_iterate_items(cfl
, &cmd
->config_files
) {
518 if (config_file_changed(cfl
->cft
))
525 static void _destroy_tag_configs(struct cmd_context
*cmd
)
527 struct config_tree_list
*cfl
;
529 dm_list_iterate_items(cfl
, &cmd
->config_files
) {
530 if (cfl
->cft
== cmd
->cft
)
532 destroy_config_tree(cfl
->cft
);
536 destroy_config_tree(cmd
->cft
);
540 dm_list_init(&cmd
->config_files
);
543 static int _init_dev_cache(struct cmd_context
*cmd
)
545 const struct config_node
*cn
;
546 struct config_value
*cv
;
548 if (!dev_cache_init(cmd
))
551 if (!(cn
= find_config_tree_node(cmd
, "devices/scan"))) {
552 if (!dev_cache_add_dir("/dev")) {
553 log_error("Failed to add /dev to internal "
557 log_verbose("device/scan not in config file: "
558 "Defaulting to /dev");
562 for (cv
= cn
->v
; cv
; cv
= cv
->next
) {
563 if (cv
->type
!= CFG_STRING
) {
564 log_error("Invalid string in config file: "
569 if (!dev_cache_add_dir(cv
->v
.str
)) {
570 log_error("Failed to add %s to internal device cache",
576 if (!(cn
= find_config_tree_node(cmd
, "devices/loopfiles")))
579 for (cv
= cn
->v
; cv
; cv
= cv
->next
) {
580 if (cv
->type
!= CFG_STRING
) {
581 log_error("Invalid string in config file: "
582 "devices/loopfiles");
586 if (!dev_cache_add_loopfile(cv
->v
.str
)) {
587 log_error("Failed to add loopfile %s to internal "
588 "device cache", cv
->v
.str
);
597 #define MAX_FILTERS 4
599 static struct dev_filter
*_init_filter_components(struct cmd_context
*cmd
)
601 unsigned nr_filt
= 0;
602 const struct config_node
*cn
;
603 struct dev_filter
*filters
[MAX_FILTERS
];
605 memset(filters
, 0, sizeof(filters
));
608 * Filters listed in order: top one gets applied first.
609 * Failure to initialise some filters is not fatal.
610 * Update MAX_FILTERS definition above when adding new filters.
614 * sysfs filter. Only available on 2.6 kernels. Non-critical.
615 * Listed first because it's very efficient at eliminating
616 * unavailable devices.
618 if (find_config_tree_bool(cmd
, "devices/sysfs_scan",
619 DEFAULT_SYSFS_SCAN
)) {
620 if ((filters
[nr_filt
] = sysfs_filter_create(cmd
->sysfs_dir
)))
624 /* regex filter. Optional. */
625 if (!(cn
= find_config_tree_node(cmd
, "devices/filter")))
626 log_very_verbose("devices/filter not found in config file: "
627 "no regex filter installed");
629 else if (!(filters
[nr_filt
++] = regex_filter_create(cn
->v
))) {
630 log_error("Failed to create regex device filter");
634 /* device type filter. Required. */
635 cn
= find_config_tree_node(cmd
, "devices/types");
636 if (!(filters
[nr_filt
++] = lvm_type_filter_create(cmd
->proc_dir
, cn
))) {
637 log_error("Failed to create lvm type filter");
641 /* md component filter. Optional, non-critical. */
642 if (find_config_tree_bool(cmd
, "devices/md_component_detection",
643 DEFAULT_MD_COMPONENT_DETECTION
)) {
644 init_md_filtering(1);
645 if ((filters
[nr_filt
] = md_filter_create()))
649 /* Only build a composite filter if we really need it. */
650 return (nr_filt
== 1) ?
651 filters
[0] : composite_filter_create(nr_filt
, filters
);
654 static int _init_filters(struct cmd_context
*cmd
, unsigned load_persistent_cache
)
656 const char *dev_cache
= NULL
, *cache_dir
, *cache_file_prefix
;
657 struct dev_filter
*f3
, *f4
;
659 char cache_file
[PATH_MAX
];
661 cmd
->dump_filter
= 0;
663 if (!(f3
= _init_filter_components(cmd
)))
666 init_ignore_suspended_devices(find_config_tree_int(cmd
,
667 "devices/ignore_suspended_devices", DEFAULT_IGNORE_SUSPENDED_DEVICES
));
670 * If 'cache_dir' or 'cache_file_prefix' is set, ignore 'cache'.
672 cache_dir
= find_config_tree_str(cmd
, "devices/cache_dir", NULL
);
673 cache_file_prefix
= find_config_tree_str(cmd
, "devices/cache_file_prefix", NULL
);
675 if (cache_dir
|| cache_file_prefix
) {
676 if (dm_snprintf(cache_file
, sizeof(cache_file
),
678 cache_dir
? "" : cmd
->system_dir
,
679 cache_dir
? "" : "/",
680 cache_dir
? : DEFAULT_CACHE_SUBDIR
,
681 cache_file_prefix
? : DEFAULT_CACHE_FILE_PREFIX
) < 0) {
682 log_error("Persistent cache filename too long.");
685 } else if (!(dev_cache
= find_config_tree_str(cmd
, "devices/cache", NULL
)) &&
686 (dm_snprintf(cache_file
, sizeof(cache_file
),
688 cmd
->system_dir
, DEFAULT_CACHE_SUBDIR
,
689 DEFAULT_CACHE_FILE_PREFIX
) < 0)) {
690 log_error("Persistent cache filename too long.");
695 dev_cache
= cache_file
;
697 if (!(f4
= persistent_filter_create(f3
, dev_cache
))) {
698 log_error("Failed to create persistent device filter");
702 /* Should we ever dump persistent filter state? */
703 if (find_config_tree_int(cmd
, "devices/write_cache_state", 1))
704 cmd
->dump_filter
= 1;
706 if (!*cmd
->system_dir
)
707 cmd
->dump_filter
= 0;
710 * Only load persistent filter device cache on startup if it is newer
711 * than the config file and this is not a long-lived process.
713 if (load_persistent_cache
&& !cmd
->is_long_lived
&&
714 !stat(dev_cache
, &st
) &&
715 (st
.st_ctime
> config_file_timestamp(cmd
->cft
)) &&
716 !persistent_filter_load(f4
, NULL
))
717 log_verbose("Failed to load existing device cache from %s",
725 static int _init_formats(struct cmd_context
*cmd
)
729 struct format_type
*fmt
;
732 const struct config_node
*cn
;
738 if (!(fmt
= init_lvm1_format(cmd
)))
741 dm_list_add(&cmd
->formats
, &fmt
->list
);
745 if (!(fmt
= init_pool_format(cmd
)))
748 dm_list_add(&cmd
->formats
, &fmt
->list
);
752 /* Load any formats in shared libs if not static */
754 (cn
= find_config_tree_node(cmd
, "global/format_libraries"))) {
756 struct config_value
*cv
;
757 struct format_type
*(*init_format_fn
) (struct cmd_context
*);
760 for (cv
= cn
->v
; cv
; cv
= cv
->next
) {
761 if (cv
->type
!= CFG_STRING
) {
762 log_error("Invalid string in config file: "
763 "global/format_libraries");
766 if (!(lib
= load_shared_library(cmd
, cv
->v
.str
,
770 if (!(init_format_fn
= dlsym(lib
, "init_format"))) {
771 log_error("Shared library %s does not contain "
772 "format functions", cv
->v
.str
);
777 if (!(fmt
= init_format_fn(cmd
)))
780 dm_list_add(&cmd
->formats
, &fmt
->list
);
785 if (!(fmt
= create_text_format(cmd
)))
788 dm_list_add(&cmd
->formats
, &fmt
->list
);
790 cmd
->fmt_backup
= fmt
;
792 format
= find_config_tree_str(cmd
, "global/format",
795 dm_list_iterate_items(fmt
, &cmd
->formats
) {
796 if (!strcasecmp(fmt
->name
, format
) ||
797 (fmt
->alias
&& !strcasecmp(fmt
->alias
, format
))) {
798 cmd
->default_settings
.fmt
= fmt
;
799 cmd
->fmt
= cmd
->default_settings
.fmt
;
804 log_error("_init_formats: Default format (%s) not found", format
);
808 int init_lvmcache_orphans(struct cmd_context
*cmd
)
810 struct format_type
*fmt
;
812 dm_list_iterate_items(fmt
, &cmd
->formats
)
813 if (!lvmcache_add_orphan_vginfo(fmt
->orphan_vg_name
, fmt
))
819 struct segtype_library
{
820 struct cmd_context
*cmd
;
825 int lvm_register_segtype(struct segtype_library
*seglib
,
826 struct segment_type
*segtype
)
828 struct segment_type
*segtype2
;
830 segtype
->library
= seglib
->lib
;
831 segtype
->cmd
= seglib
->cmd
;
833 dm_list_iterate_items(segtype2
, &seglib
->cmd
->segtypes
) {
834 if (strcmp(segtype2
->name
, segtype
->name
))
836 log_error("Duplicate segment type %s: "
837 "unloading shared library %s",
838 segtype
->name
, seglib
->libname
);
839 segtype
->ops
->destroy(segtype
);
843 dm_list_add(&seglib
->cmd
->segtypes
, &segtype
->list
);
852 static int _init_single_segtype(struct cmd_context
*cmd
,
853 struct segtype_library
*seglib
)
855 struct segment_type
*(*init_segtype_fn
) (struct cmd_context
*);
856 struct segment_type
*segtype
;
858 if (!(init_segtype_fn
= dlsym(seglib
->lib
, "init_segtype"))) {
859 log_error("Shared library %s does not contain segment type "
860 "functions", seglib
->libname
);
864 if (!(segtype
= init_segtype_fn(seglib
->cmd
)))
867 return lvm_register_segtype(seglib
, segtype
);
870 static int _init_segtypes(struct cmd_context
*cmd
)
872 struct segment_type
*segtype
;
873 struct segtype_library seglib
= { .cmd
= cmd
};
876 const struct config_node
*cn
;
879 if (!(segtype
= init_striped_segtype(cmd
)))
881 segtype
->library
= NULL
;
882 dm_list_add(&cmd
->segtypes
, &segtype
->list
);
884 if (!(segtype
= init_zero_segtype(cmd
)))
886 segtype
->library
= NULL
;
887 dm_list_add(&cmd
->segtypes
, &segtype
->list
);
889 if (!(segtype
= init_error_segtype(cmd
)))
891 segtype
->library
= NULL
;
892 dm_list_add(&cmd
->segtypes
, &segtype
->list
);
894 if (!(segtype
= init_free_segtype(cmd
)))
896 segtype
->library
= NULL
;
897 dm_list_add(&cmd
->segtypes
, &segtype
->list
);
899 #ifdef SNAPSHOT_INTERNAL
900 if (!(segtype
= init_snapshot_segtype(cmd
)))
902 segtype
->library
= NULL
;
903 dm_list_add(&cmd
->segtypes
, &segtype
->list
);
906 #ifdef MIRRORED_INTERNAL
907 if (!(segtype
= init_mirrored_segtype(cmd
)))
909 segtype
->library
= NULL
;
910 dm_list_add(&cmd
->segtypes
, &segtype
->list
);
914 /* Load any formats in shared libs unless static */
916 (cn
= find_config_tree_node(cmd
, "global/segment_libraries"))) {
918 struct config_value
*cv
;
919 int (*init_multiple_segtypes_fn
) (struct cmd_context
*,
920 struct segtype_library
*);
922 for (cv
= cn
->v
; cv
; cv
= cv
->next
) {
923 if (cv
->type
!= CFG_STRING
) {
924 log_error("Invalid string in config file: "
925 "global/segment_libraries");
928 seglib
.libname
= cv
->v
.str
;
929 if (!(seglib
.lib
= load_shared_library(cmd
,
934 if ((init_multiple_segtypes_fn
=
935 dlsym(seglib
.lib
, "init_multiple_segtypes"))) {
936 if (dlsym(seglib
.lib
, "init_segtype"))
937 log_warn("WARNING: Shared lib %s has "
938 "conflicting init fns. Using"
939 " init_multiple_segtypes().",
942 init_multiple_segtypes_fn
=
943 _init_single_segtype
;
945 if (!init_multiple_segtypes_fn(cmd
, &seglib
)) {
946 struct dm_list
*sgtl
, *tmp
;
947 log_error("init_multiple_segtypes() failed: "
948 "Unloading shared library %s",
950 dm_list_iterate_safe(sgtl
, tmp
, &cmd
->segtypes
) {
951 segtype
= dm_list_item(sgtl
, struct segment_type
);
952 if (segtype
->library
== seglib
.lib
) {
953 dm_list_del(&segtype
->list
);
954 segtype
->ops
->destroy(segtype
);
967 static int _init_hostname(struct cmd_context
*cmd
)
972 log_sys_error("uname", "_init_hostname");
976 if (!(cmd
->hostname
= dm_pool_strdup(cmd
->libmem
, uts
.nodename
))) {
977 log_error("_init_hostname: dm_pool_strdup failed");
981 if (!(cmd
->kernel_vsn
= dm_pool_strdup(cmd
->libmem
, uts
.release
))) {
982 log_error("_init_hostname: dm_pool_strdup kernel_vsn failed");
989 static int _init_backup(struct cmd_context
*cmd
)
992 char default_dir
[PATH_MAX
];
995 if (!cmd
->system_dir
) {
996 log_warn("WARNING: Metadata changes will NOT be backed up");
997 backup_init(cmd
, "", 0);
998 archive_init(cmd
, "", 0, 0, 0);
1002 /* set up archiving */
1003 cmd
->default_settings
.archive
=
1004 find_config_tree_bool(cmd
, "backup/archive",
1005 DEFAULT_ARCHIVE_ENABLED
);
1007 days
= (uint32_t) find_config_tree_int(cmd
, "backup/retain_days",
1008 DEFAULT_ARCHIVE_DAYS
);
1010 min
= (uint32_t) find_config_tree_int(cmd
, "backup/retain_min",
1011 DEFAULT_ARCHIVE_NUMBER
);
1014 (default_dir
, sizeof(default_dir
), "%s/%s", cmd
->system_dir
,
1015 DEFAULT_ARCHIVE_SUBDIR
) == -1) {
1016 log_error("Couldn't create default archive path '%s/%s'.",
1017 cmd
->system_dir
, DEFAULT_ARCHIVE_SUBDIR
);
1021 dir
= find_config_tree_str(cmd
, "backup/archive_dir",
1024 if (!archive_init(cmd
, dir
, days
, min
,
1025 cmd
->default_settings
.archive
)) {
1026 log_debug("archive_init failed.");
1030 /* set up the backup */
1031 cmd
->default_settings
.backup
=
1032 find_config_tree_bool(cmd
, "backup/backup",
1033 DEFAULT_BACKUP_ENABLED
);
1036 (default_dir
, sizeof(default_dir
), "%s/%s", cmd
->system_dir
,
1037 DEFAULT_BACKUP_SUBDIR
) == -1) {
1038 log_error("Couldn't create default backup path '%s/%s'.",
1039 cmd
->system_dir
, DEFAULT_BACKUP_SUBDIR
);
1043 dir
= find_config_tree_str(cmd
, "backup/backup_dir", default_dir
);
1045 if (!backup_init(cmd
, dir
, cmd
->default_settings
.backup
)) {
1046 log_debug("backup_init failed.");
1053 static void _init_rand(struct cmd_context
*cmd
)
1055 if (read_urandom(&cmd
->rand_seed
, sizeof(cmd
->rand_seed
)))
1058 cmd
->rand_seed
= (unsigned) time(NULL
) + (unsigned) getpid();
1061 static void _init_globals(struct cmd_context
*cmd
)
1063 init_full_scan_done(0);
1064 init_mirror_in_sync(0);
1069 struct cmd_context
*create_toolcontext(unsigned is_long_lived
,
1070 const char *system_dir
)
1072 struct cmd_context
*cmd
;
1075 mallopt(M_MMAP_MAX
, 0);
1078 if (!setlocale(LC_ALL
, ""))
1079 log_very_verbose("setlocale failed");
1082 bindtextdomain(INTL_PACKAGE
, LOCALEDIR
);
1085 init_syslog(DEFAULT_LOG_FACILITY
);
1087 if (!(cmd
= dm_malloc(sizeof(*cmd
)))) {
1088 log_error("Failed to allocate command context");
1091 memset(cmd
, 0, sizeof(*cmd
));
1092 cmd
->is_long_lived
= is_long_lived
;
1093 cmd
->handles_missing_pvs
= 0;
1094 cmd
->handles_unknown_segments
= 0;
1096 dm_list_init(&cmd
->formats
);
1097 dm_list_init(&cmd
->segtypes
);
1098 dm_list_init(&cmd
->tags
);
1099 dm_list_init(&cmd
->config_files
);
1101 /* FIXME Make this configurable? */
1105 * Environment variable LVM_SYSTEM_DIR overrides this below.
1108 strncpy(cmd
->system_dir
, system_dir
, sizeof(cmd
->system_dir
) - 1);
1110 strcpy(cmd
->system_dir
, DEFAULT_SYS_DIR
);
1112 if (!_get_env_vars(cmd
))
1115 /* Create system directory if it doesn't already exist */
1116 if (*cmd
->system_dir
&& !dm_create_dir(cmd
->system_dir
)) {
1117 log_error("Failed to create LVM2 system dir for metadata backups, config "
1118 "files and internal cache.");
1119 log_error("Set environment variable LVM_SYSTEM_DIR to alternative location "
1120 "or empty string.");
1124 if (!(cmd
->libmem
= dm_pool_create("library", 4 * 1024))) {
1125 log_error("Library memory pool creation failed");
1129 if (!_init_lvm_conf(cmd
))
1134 if (!_init_hostname(cmd
))
1137 if (!_init_tags(cmd
, cmd
->cft
))
1140 if (!_init_tag_configs(cmd
))
1143 if (!_merge_config_files(cmd
))
1146 if (!_process_config(cmd
))
1149 if (!_init_dev_cache(cmd
))
1152 if (!_init_filters(cmd
, 1))
1155 if (!(cmd
->mem
= dm_pool_create("command", 4 * 1024))) {
1156 log_error("Command memory pool creation failed");
1162 if (!_init_formats(cmd
))
1165 if (!init_lvmcache_orphans(cmd
))
1168 if (!_init_segtypes(cmd
))
1171 if (!_init_backup(cmd
))
1178 cmd
->default_settings
.cache_vgmetadata
= 1;
1179 cmd
->current_settings
= cmd
->default_settings
;
1181 cmd
->config_valid
= 1;
1186 static void _destroy_formats(struct dm_list
*formats
)
1188 struct dm_list
*fmtl
, *tmp
;
1189 struct format_type
*fmt
;
1192 dm_list_iterate_safe(fmtl
, tmp
, formats
) {
1193 fmt
= dm_list_item(fmtl
, struct format_type
);
1194 dm_list_del(&fmt
->list
);
1196 fmt
->ops
->destroy(fmt
);
1204 static void _destroy_segtypes(struct dm_list
*segtypes
)
1206 struct dm_list
*sgtl
, *tmp
;
1207 struct segment_type
*segtype
;
1210 dm_list_iterate_safe(sgtl
, tmp
, segtypes
) {
1211 segtype
= dm_list_item(sgtl
, struct segment_type
);
1212 dm_list_del(&segtype
->list
);
1213 lib
= segtype
->library
;
1214 segtype
->ops
->destroy(segtype
);
1217 * If no segtypes remain from this library, close it.
1220 struct segment_type
*segtype2
;
1221 dm_list_iterate_items(segtype2
, segtypes
)
1222 if (segtype2
->library
== lib
)
1232 int refresh_filters(struct cmd_context
*cmd
)
1235 cmd
->filter
->destroy(cmd
->filter
);
1239 return _init_filters(cmd
, 0);
1242 int refresh_toolcontext(struct cmd_context
*cmd
)
1244 log_verbose("Reloading config files");
1247 * Don't update the persistent filter cache as we will
1248 * perform a full rescan.
1251 activation_release();
1252 lvmcache_destroy(cmd
, 0);
1254 _destroy_segtypes(&cmd
->segtypes
);
1255 _destroy_formats(&cmd
->formats
);
1257 cmd
->filter
->destroy(cmd
->filter
);
1262 _destroy_tag_configs(cmd
);
1264 cmd
->config_valid
= 0;
1268 if (!_init_lvm_conf(cmd
))
1273 if (!_init_tags(cmd
, cmd
->cft
))
1276 if (!_init_tag_configs(cmd
))
1279 if (!_merge_config_files(cmd
))
1282 if (!_process_config(cmd
))
1285 if (!_init_dev_cache(cmd
))
1288 if (!_init_filters(cmd
, 0))
1291 if (!_init_formats(cmd
))
1294 if (!init_lvmcache_orphans(cmd
))
1297 if (!_init_segtypes(cmd
))
1300 cmd
->config_valid
= 1;
1306 void destroy_toolcontext(struct cmd_context
*cmd
)
1308 if (cmd
->dump_filter
)
1309 persistent_filter_dump(cmd
->filter
);
1313 lvmcache_destroy(cmd
, 0);
1315 _destroy_segtypes(&cmd
->segtypes
);
1316 _destroy_formats(&cmd
->formats
);
1318 cmd
->filter
->destroy(cmd
->filter
);
1320 dm_pool_destroy(cmd
->mem
);
1323 _destroy_tag_configs(cmd
);
1325 dm_pool_destroy(cmd
->libmem
);
1328 release_log_memory();