From 735850a2ff2a7030c61775e716e57d2cc9c0268a Mon Sep 17 00:00:00 2001 From: David Teigland Date: Mon, 1 Mar 2021 18:32:50 -0600 Subject: lvmlockctl: use lvm.conf lvmlockctl_kill_command which specifies a command to run by lvmlockctl --kill. --- daemons/lvmlockd/lvmlockctl.c | 175 ++++++++++++++++++++++++++++++++---------- man/lvmlockctl.8_main | 14 ++-- 2 files changed, 145 insertions(+), 44 deletions(-) diff --git a/daemons/lvmlockd/lvmlockctl.c b/daemons/lvmlockd/lvmlockctl.c index c2a998c8c..ce8328e5d 100644 --- a/daemons/lvmlockd/lvmlockctl.c +++ b/daemons/lvmlockd/lvmlockctl.c @@ -30,6 +30,7 @@ static int kill_vg = 0; static int drop_vg = 0; static int gl_enable = 0; static int gl_disable = 0; +static int use_stderr = 0; static int stop_lockspaces = 0; static char *arg_vg_name = NULL; @@ -47,6 +48,22 @@ do { \ printf(fmt "\n", ##args); \ } while (0) +#define log_sys_emerg(fmt, args...) \ +do { \ + if (use_stderr) \ + fprintf(stderr, fmt "\n", ##args); \ + else \ + syslog(LOG_EMERG, fmt, ##args); \ +} while (0) + +#define log_sys_warn(fmt, args...) \ +do { \ + if (use_stderr) \ + fprintf(stderr, fmt "\n", ##args); \ + else \ + syslog(LOG_WARNING, fmt, ##args); \ +} while (0) + #define MAX_LINE 512 /* copied from lvmlockd-internal.h */ @@ -502,51 +519,61 @@ static int do_stop_lockspaces(void) return rv; } -static int do_kill(void) +/* FIXME: from configure */ +#define LVM_DIR "/sbin" + +static int _get_kill_command(char *kill_cmd) { - daemon_reply reply; - int result; - int rv; + char config_cmd[PATH_MAX] = { 0 }; + char config_val[1024] = { 0 }; + char line[PATH_MAX] = { 0 }; + char type[4] = { 0 }; + FILE *fp; - syslog(LOG_EMERG, "Lost access to sanlock lease storage in VG %s.", arg_vg_name); - /* These two lines explain the manual alternative to the FIXME below. */ - syslog(LOG_EMERG, "Immediately deactivate LVs in VG %s.", arg_vg_name); - syslog(LOG_EMERG, "Once VG is unused, run lvmlockctl --drop %s.", arg_vg_name); + snprintf(config_cmd, PATH_MAX, "%s/lvmconfig --typeconfig full global/lvmlockctl_kill_command", LVM_DIR); + type[0] = 'r'; - /* - * It may not be strictly necessary to notify lvmlockd of the kill, but - * lvmlockd can use this information to avoid attempting any new lock - * requests in the VG (which would fail anyway), and can return an - * error indicating that the VG has been killed. - */ + if (!(fp = popen(config_cmd, type))) { + log_error("failed to run %s", config_cmd); + return -1; + } - reply = _lvmlockd_send("kill_vg", - "cmd = %s", "lvmlockctl", - "pid = " FMTd64, (int64_t) getpid(), - "vg_name = %s", arg_vg_name, - NULL); + if (!fgets(line, sizeof(line), fp)) { + log_error("no output from %s", config_cmd); + goto bad; + } - if (!_lvmlockd_result(reply, &result)) { - log_error("lvmlockd result %d", result); - rv = result; - } else { - rv = 0; + if (sscanf(line, "lvmlockctl_kill_command=\"%256[^\n\"]\"", config_val) != 1) { + log_error("unrecognized config value from %s", config_cmd); + goto bad; } - daemon_reply_destroy(reply); + if (!config_val[0] || (config_val[0] == ' ')) { + log_error("invalid config value from %s", config_cmd); + goto bad; + } - /* - * FIXME: here is where we should implement a strong form of - * blkdeactivate, and if it completes successfully, automatically call - * do_drop() afterward. (The drop step may not always be necessary - * if the lvm commands run while shutting things down release all the - * leases.) - * - * run_strong_blkdeactivate(); - * do_drop(); - */ + printf("Found lvmlockctl_kill_command: %s\n", config_val); - return rv; + snprintf(kill_cmd, PATH_MAX, "%s %s", config_val, arg_vg_name); + + pclose(fp); + return 0; +bad: + pclose(fp); + return -1; +} + +static int _run_kill_command(char *kill_cmd) +{ + int status; + + status = system(kill_cmd); + + if (!WEXITSTATUS(status)) + return 0; + + return -1; } static int do_drop(void) @@ -555,7 +582,7 @@ static int do_drop(void) int result; int rv; - syslog(LOG_WARNING, "Dropping locks for VG %s.", arg_vg_name); + log_sys_warn("Dropping locks for VG %s.", arg_vg_name); /* * Check for misuse by looking for any active LVs in the VG @@ -583,6 +610,70 @@ static int do_drop(void) return rv; } +static int do_kill(void) +{ + char kill_cmd[PATH_MAX] = { 0 }; + daemon_reply reply; + int manual = 0; + int result; + int rv; + + log_sys_emerg("lvmlockd lost access to locks in VG %s.", arg_vg_name); + + rv = _get_kill_command(kill_cmd); + if (rv < 0) { + log_sys_emerg("Immediately deactivate LVs in VG %s.", arg_vg_name); + log_sys_emerg("Once VG is unused, run lvmlockctl --drop %s.", arg_vg_name); + manual = 1; + } + + /* + * It may not be strictly necessary to notify lvmlockd of the kill, but + * lvmlockd can use this information to avoid attempting any new lock + * requests in the VG (which would fail anyway), and can return an + * error indicating that the VG has been killed. + */ + + reply = _lvmlockd_send("kill_vg", + "cmd = %s", "lvmlockctl", + "pid = " FMTd64, (int64_t) getpid(), + "vg_name = %s", arg_vg_name, + NULL); + + if (!_lvmlockd_result(reply, &result)) + log_error("lvmlockd result %d", result); + + daemon_reply_destroy(reply); + + if (manual) + return 0; + + /* + * If kill command completes successfully, call do_drop() afterward. + * (The drop step may not always be necessary if the lvm commands run + * while shutting things down release all the leases.) + */ + + rv = _run_kill_command(kill_cmd); + if (rv < 0) { + log_sys_emerg("Failed to run VG %s kill command %s", arg_vg_name, kill_cmd); + log_sys_emerg("Immediately deactivate LVs in VG %s.", arg_vg_name); + log_sys_emerg("Once VG is unused, run lvmlockctl --drop %s.", arg_vg_name); + return -1; + } + + log_sys_warn("Successful VG %s kill command %s", arg_vg_name, kill_cmd); + + rv = do_drop(); + if (rv < 0) { + log_sys_emerg("Failed to drop locks in VG %s", arg_vg_name); + return -1; + } + + return 0; +} + + static void print_usage(void) { printf("lvmlockctl options\n"); @@ -600,7 +691,7 @@ static void print_usage(void) printf("--force | -f 0|1>\n"); printf(" Force option for other commands.\n"); printf("--kill | -k \n"); - printf(" Kill access to the VG when sanlock cannot renew lease.\n"); + printf(" Kill access to the VG locks are lost (see lvmlockctl_kill_command).\n"); printf("--drop | -r \n"); printf(" Clear locks for the VG when it is unused after kill (-k).\n"); printf("--gl-enable | -E \n"); @@ -609,6 +700,8 @@ static void print_usage(void) printf(" Tell lvmlockd to disable the global lock in a sanlock VG.\n"); printf("--stop-lockspaces | -S\n"); printf(" Stop all lockspaces.\n"); + printf("--stderr | -e\n"); + printf(" Send kill and drop messages to stderr instead of syslog\n"); } static int read_options(int argc, char *argv[]) @@ -628,6 +721,7 @@ static int read_options(int argc, char *argv[]) {"gl-enable", required_argument, 0, 'E' }, {"gl-disable", required_argument, 0, 'D' }, {"stop-lockspaces", no_argument, 0, 'S' }, + {"stderr", no_argument, 0, 'e' }, {0, 0, 0, 0 } }; @@ -637,7 +731,7 @@ static int read_options(int argc, char *argv[]) } while (1) { - c = getopt_long(argc, argv, "hqidE:D:w:k:r:S", long_options, &option_index); + c = getopt_long(argc, argv, "hqidE:D:w:k:r:Se", long_options, &option_index); if (c == -1) break; @@ -680,6 +774,9 @@ static int read_options(int argc, char *argv[]) case 'S': stop_lockspaces = 1; break; + case 'e': + use_stderr = 1; + break; default: print_usage(); exit(1); diff --git a/man/lvmlockctl.8_main b/man/lvmlockctl.8_main index b7ac0ecd6..14ce926b5 100644 --- a/man/lvmlockctl.8_main +++ b/man/lvmlockctl.8_main @@ -65,17 +65,21 @@ and prints it. .SS kill This is run by sanlock when it loses access to the storage holding leases -for a VG. It currently emits a syslog message stating that the VG must -be immediately deactivated. In the future it may automatically attempt to -forcibly deactivate the VG. For more, see +for a VG. It runs the command specified in lvm.conf +lvmlockctl_kill_command to deactivate LVs in the VG. If the specified +command is successful, locks will be dropped for the VG in lvmlockd +(the equivalent of lvmlockctl --drop will be run.) If no command +is specified, or the command fails, then the user must intervene +to forcefully deactivate LVs in the VG, and if successful, run +lvmlockctl --drop. For more, see .BR lvmlockd (8). .SS drop This should only be run after a VG has been successfully deactivated following an lvmlockctl --kill command. It clears the stale lockspace -from lvmlockd. In the future, this may become automatic along with an -automatic handling of --kill. For more, see +from lvmlockd. When lvmlockctl_kill_command is used, the --kill +command may run drop automatically. For more, see .BR lvmlockd (8). .SS gl-enable -- cgit v1.2.1