]>
sourceware.org Git - lvm2.git/blob - scripts/lvm2_activation_generator_systemd_red_hat.c
2 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
4 * This file is part of the device-mapper userspace tools.
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU General Public License v.2.
10 * You should have received a copy of the GNU General Public License
11 * along with this program; if not, write to the Free Software Foundation,
12 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <sys/types.h>
24 #define KMSG_DEV_PATH "/dev/kmsg"
25 #define LVM_CONF_USE_LVMETAD "global/use_lvmetad"
27 #define DEFAULT_UNIT_DIR "/tmp"
28 #define UNIT_NAME_EARLY "lvm2-activation-early.service"
29 #define UNIT_NAME "lvm2-activation.service"
30 #define UNIT_TARGET "local-fs.target"
32 static char unit_path
[PATH_MAX
];
33 static char target_path
[PATH_MAX
];
34 static char message
[PATH_MAX
];
35 static int kmsg_fd
= -1;
37 static void kmsg(const char *format
, ...)
43 n
= vsnprintf(message
, sizeof(message
), format
, ap
);
46 if (kmsg_fd
< 0 || (n
< 0 || ((unsigned) n
+ 1 > sizeof(message
))))
49 write(kmsg_fd
, message
, n
+ 1);
52 static int lvm_uses_lvmetad(void)
57 if (!(lvm
= lvm_init(NULL
))) {
58 kmsg("LVM: Failed to initialize library context for activation generator.\n");
61 r
= lvm_config_find_bool(lvm
, LVM_CONF_USE_LVMETAD
, 0);
67 static int register_unit_with_target(const char *dir
, const char *unit
, const char *target
)
71 if (dm_snprintf(target_path
, PATH_MAX
, "%s/%s.wants", dir
, target
) < 0) {
74 (void) dm_prepare_selinux_context(target_path
, S_IFDIR
);
75 if (mkdir(target_path
, 0755) < 0 && errno
!= EEXIST
) {
76 kmsg("LVM: Failed to create target directory %s: %m.\n", target_path
);
80 if (dm_snprintf(target_path
, PATH_MAX
, "%s/%s.wants/%s", dir
, target
, unit
) < 0) {
83 (void) dm_prepare_selinux_context(target_path
, S_IFLNK
);
84 if (symlink(unit_path
, target_path
) < 0) {
85 kmsg("LVM: Failed to create symlink for unit %s: %m.\n", unit
);
89 dm_prepare_selinux_context(NULL
, 0);
93 static int generate_unit(const char *dir
, int early
)
96 const char *unit
= early
? UNIT_NAME_EARLY
: UNIT_NAME
;
98 if (dm_snprintf(unit_path
, PATH_MAX
, "%s/%s", dir
, unit
) < 0)
101 if (!(f
= fopen(unit_path
, "wxe"))) {
102 kmsg("LVM: Failed to create unit file %s: %m.\n", unit
);
106 fputs("# Automatically generated by lvm2-activation-generator.\n"
108 "# This unit is responsible for direct activation of LVM2 logical volumes\n"
109 "# if lvmetad daemon is not used (global/use_lvmetad=0 lvm.conf setting),\n"
110 "# hence volume autoactivation is not applicable.\n"
111 "# Direct LVM2 activation requires udev to be settled!\n\n"
113 "Description=Activation of LVM2 logical volumes\n"
114 "Documentation=man:lvm(8) man:vgchange(8)\n"
115 "SourcePath=/etc/lvm/lvm.conf\n"
116 "DefaultDependencies=no\n", f
);
119 fputs("After=fedora-wait-storage.service\n", f
);
121 fputs("After=lvm2-activation-early.service cryptsetup.target\n", f
);
123 fputs("Before=local-fs.target shutdown.target\n"
124 "Wants=fedora-wait-storage.service\n\n"
126 "ExecStart=/usr/sbin/lvm vgchange -aay --sysinit\n"
127 "Type=oneshot\n", f
);
130 kmsg("LVM: Failed to write unit file %s: %m.\n", unit
);
134 if (!register_unit_with_target(dir
, unit
, UNIT_TARGET
)) {
135 kmsg("LVM: Failed to register unit %s with target %s.\n", unit
, UNIT_TARGET
);
142 int main(int argc
, char *argv
[])
145 int r
= EXIT_SUCCESS
;
147 kmsg_fd
= open(KMSG_DEV_PATH
, O_WRONLY
|O_NOCTTY
);
149 if (argc
> 1 && argc
!= 4) {
150 kmsg("LVM: Activation generator takes three or no arguments.\n");
151 r
= EXIT_FAILURE
; goto out
;
154 /* If lvmetad used, rely on autoactivation instead of direct activation. */
155 if (lvm_uses_lvmetad()) {
156 kmsg("LVM: Logical Volume autoactivation enabled.\n");
160 dir
= argc
> 1 ? argv
[1] : DEFAULT_UNIT_DIR
;
162 if (!generate_unit(dir
, 1) || !generate_unit(dir
, 0))
165 kmsg("LVM: Activation generator %s.\n", r
? "failed" : "successfully completed");
This page took 1.789633 seconds and 5 git commands to generate.