| 1 |
/* |
| 2 |
* Copyright (C) 2016 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from 'cockpit'; |
| 7 |
import { proxy as serviceProxy } from 'service'; |
| 8 |
import { ConfigFile } from './config-client.js'; |
| 9 |
import { ConfigFileSUSE } from './config-client-suse.js'; |
| 10 |
|
| 11 |
import crashKernelScript from './crashkernel.sh'; |
| 12 |
import testWritableScript from './testwritable.sh'; |
| 13 |
const _ = cockpit.gettext; |
| 14 |
|
| 15 |
/* initializes the kdump status |
| 16 |
* emits "kdumpStatusChanged" when the status changes, along with a status object: |
| 17 |
* { |
| 18 |
* installed: true/false, |
| 19 |
* enabled: true/false, |
| 20 |
* state: current state, |
| 21 |
* config: settings from kdump.conf |
| 22 |
* target: dump target info, content depends on dump type |
| 23 |
* always contains the keys: |
| 24 |
* type value in ["local", "nfs", "ssh", "raw", "mount", "unknown"] |
| 25 |
* multipleTargets true if the config file has more than one target defined, false otherwise |
| 26 |
* } |
| 27 |
* |
| 28 |
*/ |
| 29 |
export class KdumpClient { |
| 30 |
constructor() { |
| 31 |
this.state = { |
| 32 |
installed: undefined, |
| 33 |
enabled: undefined, |
| 34 |
state: undefined, |
| 35 |
config: undefined, |
| 36 |
target: undefined, |
| 37 |
}; |
| 38 |
cockpit.event_target(this); |
| 39 |
|
| 40 |
// listen to service status changes |
| 41 |
this.kdumpService = serviceProxy("kdump"); |
| 42 |
this.kdumpService.addEventListener('changed', () => { |
| 43 |
this.state.installed = this.kdumpService.exists; |
| 44 |
this.state.enabled = this.kdumpService.enabled; |
| 45 |
this.state.state = this.kdumpService.state; |
| 46 |
this.dispatchEvent("kdumpStatusChanged", this.state); |
| 47 |
}); |
| 48 |
|
| 49 |
// watch the config file |
| 50 |
this.configClient = new ConfigFile("/etc/kdump.conf", true); |
| 51 |
this._watchConfigChanges(); |
| 52 |
|
| 53 |
this.configClient.wait().then(() => { |
| 54 |
// if no configuration found, try SUSE version |
| 55 |
if (this.configClient.settings === null) { |
| 56 |
this.configClient.close(); |
| 57 |
this.configClient = new ConfigFileSUSE("/etc/sysconfig/kdump", true); |
| 58 |
this._watchConfigChanges(); |
| 59 |
} |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
_watchConfigChanges() { |
| 64 |
// catch config changes |
| 65 |
this.configClient.addEventListener('kdumpConfigChanged', () => { |
| 66 |
this.state.config = this.configClient.settings; |
| 67 |
this.state.target = this.targetFromSettings(this.configClient.settings); |
| 68 |
this.dispatchEvent("kdumpStatusChanged", this.state); |
| 69 |
}); |
| 70 |
} |
| 71 |
|
| 72 |
ensureOn() { |
| 73 |
// we consider the state to be "on" when it's enabled and running |
| 74 |
return Promise.all([ |
| 75 |
this.kdumpService.enable(), |
| 76 |
this.kdumpService.start() |
| 77 |
]); |
| 78 |
} |
| 79 |
|
| 80 |
ensureOff() { |
| 81 |
// we consider the state to be "off" when it's disabled and stopped |
| 82 |
return Promise.all([ |
| 83 |
this.kdumpService.stop(), |
| 84 |
this.kdumpService.disable() |
| 85 |
]); |
| 86 |
} |
| 87 |
|
| 88 |
crashKernel() { |
| 89 |
// crash the system kernel |
| 90 |
return cockpit.script(crashKernelScript, [], { superuser: "require" }); |
| 91 |
} |
| 92 |
|
| 93 |
validateSettings(settings) { |
| 94 |
const target = this.targetFromSettings(settings); |
| 95 |
let path; |
| 96 |
if (target && target.path) |
| 97 |
path = target.path; |
| 98 |
// if path is invalid or we haven't set one, use default |
| 99 |
if (!path) |
| 100 |
path = "/var/crash"; |
| 101 |
|
| 102 |
return new Promise((resolve, reject) => { |
| 103 |
if (target.type === "local") { |
| 104 |
// local path, try to see if we can write |
| 105 |
cockpit.script(testWritableScript, [path], { superuser: "try" }) |
| 106 |
.then(resolve) |
| 107 |
.catch(() => reject(cockpit.format(_("Directory $0 isn't writable or doesn't exist."), path))); |
| 108 |
return; |
| 109 |
} else if (target.type === "nfs") { |
| 110 |
if (!target.server || !target.server.trim()) |
| 111 |
reject(_("nfs server is empty")); |
| 112 |
// IPv6 must be enclosed in square brackets |
| 113 |
if (target.server.trim().match(/^\[.*[^\]]$/)) |
| 114 |
reject(_("nfs server is not valid IPv6")); |
| 115 |
if (!target.export || !target.export.trim()) |
| 116 |
reject(_("nfs export is empty")); |
| 117 |
} else if (target.type === "ssh") { |
| 118 |
if (!target.server || !target.server.trim()) |
| 119 |
reject(_("ssh server is empty")); |
| 120 |
if (target.sshkey && !target.sshkey.match("/.+")) |
| 121 |
reject(_("ssh key isn't a path")); |
| 122 |
} |
| 123 |
|
| 124 |
/* no-op if already rejected */ |
| 125 |
resolve(); |
| 126 |
}); |
| 127 |
} |
| 128 |
|
| 129 |
writeSettings(settings) { |
| 130 |
return this.configClient.write(settings) |
| 131 |
.then(() => { |
| 132 |
// after we've written the new config, we have to restart the service to pick up changes or clean up after errors |
| 133 |
if (this.kdumpService.enabled) { |
| 134 |
return this.kdumpService.restart() |
| 135 |
.catch(error => this.kdumpService.getRunJournal(["--output=cat", "--identifier=kdumpctl"]) |
| 136 |
.then(journal => { |
| 137 |
error.details = journal; |
| 138 |
return Promise.reject(error); |
| 139 |
}, ex => { |
| 140 |
console.warn("Failed to get journal of kdump.service:", ex.toString()); |
| 141 |
return Promise.reject(error); |
| 142 |
}) |
| 143 |
); |
| 144 |
} else { |
| 145 |
return true; |
| 146 |
} |
| 147 |
}); |
| 148 |
} |
| 149 |
|
| 150 |
exportConfig(settings) { |
| 151 |
return this.configClient.generateConfig(settings).trim(); |
| 152 |
} |
| 153 |
|
| 154 |
targetFromSettings(settings) { |
| 155 |
const target = { |
| 156 |
type: "unknown", |
| 157 |
multipleTargets: false, |
| 158 |
}; |
| 159 |
|
| 160 |
if (!settings || Object.keys(settings.targets).length === 0) |
| 161 |
return target; |
| 162 |
|
| 163 |
// copy first target |
| 164 |
Object.assign(target, Object.values(settings.targets)[0]); |
| 165 |
target.multipleTargets = Object.keys(settings.targets).length > 1; |
| 166 |
return target; |
| 167 |
} |
| 168 |
} |