Files
cloudron-box/src/system.js
T

403 lines
16 KiB
JavaScript
Raw Normal View History

2019-08-19 13:50:44 -07:00
'use strict';
exports = module.exports = {
2023-08-04 13:41:13 +05:30
reboot,
2023-12-04 00:46:12 +01:00
getInfo,
2023-08-11 21:47:49 +05:30
getUbuntuVersion,
2025-07-09 18:28:55 +02:00
getKernelVersion,
2022-11-04 15:09:37 +01:00
getSwaps,
2021-01-04 11:05:42 -08:00
checkDiskSpace,
getMemory,
2023-08-04 13:41:13 +05:30
getLogs,
getBlockDevices,
runSystemChecks,
2023-12-04 00:23:25 +01:00
getProvider,
2023-12-04 00:31:18 +01:00
getCpus,
2025-07-16 23:09:06 +02:00
getFilesystems,
getFilesystemUsage
2019-08-19 13:50:44 -07:00
};
const apps = require('./apps.js'),
2025-08-14 11:17:38 +05:30
assert = require('node:assert'),
2025-07-16 23:09:06 +02:00
{ AsyncTask } = require('./asynctask.js'),
2025-09-12 09:48:37 +02:00
backupSites = require('./backupsites.js'),
2019-10-22 11:11:41 -07:00
BoxError = require('./boxerror.js'),
2024-01-30 11:52:59 +01:00
debug = require('debug')('box:system'),
2022-10-18 19:32:07 +02:00
df = require('./df.js'),
2019-08-19 13:50:44 -07:00
docker = require('./docker.js'),
eventlog = require('./eventlog.js'),
2025-08-14 11:17:38 +05:30
fs = require('node:fs'),
2023-08-04 13:41:13 +05:30
logs = require('./logs.js'),
2019-08-19 13:50:44 -07:00
notifications = require('./notifications.js'),
2025-08-14 11:17:38 +05:30
os = require('node:os'),
path = require('node:path'),
2019-11-21 12:55:17 -08:00
paths = require('./paths.js'),
2020-01-31 13:37:07 -08:00
safe = require('safetydance'),
2024-10-14 19:10:31 +02:00
shell = require('./shell.js')('system'),
2021-01-04 11:05:42 -08:00
volumes = require('./volumes.js');
2019-08-19 13:50:44 -07:00
2022-10-11 22:58:12 +02:00
const DU_CMD = path.join(__dirname, 'scripts/du.sh');
2023-01-27 21:05:25 +01:00
const HDPARM_CMD = path.join(__dirname, 'scripts/hdparm.sh');
2023-08-04 13:41:13 +05:30
const REBOOT_CMD = path.join(__dirname, 'scripts/reboot.sh');
2022-10-11 22:58:12 +02:00
2025-07-16 23:09:06 +02:00
async function du(file, options) {
2022-10-11 23:14:50 +02:00
assert.strictEqual(typeof file, 'string');
2025-07-16 23:09:06 +02:00
assert.strictEqual(typeof options, 'object');
2022-10-11 23:14:50 +02:00
2025-07-17 09:50:43 +02:00
const [error, stdoutResult] = await safe(shell.sudo([ DU_CMD, file ], { encoding: 'utf8', abortSignal: options.abortSignal }));
2022-10-11 22:58:12 +02:00
if (error) throw new BoxError(BoxError.FS_ERROR, error);
return parseInt(stdoutResult.trim(), 10);
}
2025-07-16 23:09:06 +02:00
async function hdparm(file, options) {
2023-01-27 21:05:25 +01:00
assert.strictEqual(typeof file, 'string');
2025-07-16 23:09:06 +02:00
assert.strictEqual(typeof options, 'object');
2023-01-27 21:05:25 +01:00
2025-07-17 09:50:43 +02:00
const [error, stdoutResult] = await safe(shell.sudo([ HDPARM_CMD, file ], { encoding: 'utf8', abortSignal: options.abortSignal }));
2023-01-27 21:05:25 +01:00
if (error) throw new BoxError(BoxError.FS_ERROR, error);
const lines = stdoutResult.split('\n');
if (lines.length != 4) return -1;
if (lines[2].split('=').length !== 2) return -1;
const speed = lines[2].split('=')[1].slice(0, 'MB/sec'.length).trim();
return Number(speed);
}
2022-11-04 15:09:37 +01:00
async function getSwaps() {
2024-10-15 10:10:15 +02:00
const [error, stdout] = await safe(shell.spawn('swapon', ['--noheadings', '--raw', '--bytes', '--show=type,size,used,name'], { encoding: 'utf8' }));
2024-02-20 22:57:36 +01:00
if (error) return {};
2024-03-22 10:39:35 +01:00
const output = stdout.trim();
if (!output) return {}; // no swaps
2022-11-04 15:09:37 +01:00
const swaps = {};
2024-03-22 10:39:35 +01:00
for (const line of output.split('\n')) {
2022-11-04 15:09:37 +01:00
const parts = line.split(' ', 4);
const name = parts[3];
swaps[name] = {
name: parts[3],
type: parts[0], // partition or file
size: parseInt(parts[1]),
used: parseInt(parts[2]),
};
}
return swaps;
}
2025-07-16 17:20:15 +02:00
// this gets information based on mounted filesystems, swaps and the filesystem contents
2024-11-30 11:46:28 +01:00
async function getFilesystems() {
2024-11-30 12:01:25 +01:00
const FS_TYPES = [ 'ext4', 'xfs', 'cifs', 'nfs', 'fuse.sshfs' ]; // we don't show size of contents in untracked disk types
2024-11-30 11:46:28 +01:00
const [dfError, dfEntries] = await safe(df.filesystems());
if (dfError) throw new BoxError(BoxError.FS_ERROR, `Error running df: ${dfError.message}`);
2025-07-16 17:20:15 +02:00
const filesystems = {}; // by file system (device path)
let rootDisk;
2024-11-30 12:00:36 +01:00
for (const dfEntry of dfEntries) {
if (!FS_TYPES.includes(dfEntry.type)) continue;
if (dfEntry.mountpoint === '/') rootDisk = dfEntry;
filesystems[dfEntry.filesystem] = {
filesystem: dfEntry.filesystem,
type: dfEntry.type,
size: dfEntry.size,
used: dfEntry.used,
available: dfEntry.available,
capacity: dfEntry.capacity,
mountpoint: dfEntry.mountpoint,
2025-07-18 13:02:53 +02:00
contents: [] // filled below . { type, id, path }
};
2021-05-11 17:50:48 -07:00
}
2021-01-04 11:05:42 -08:00
const standardPaths = [
2025-10-22 14:20:56 +02:00
{ type: 'standard', id: 'platformdata', name: 'Platform data', path: paths.PLATFORM_DATA_DIR },
{ type: 'standard', id: 'boxdata', name: 'Box data', path: paths.BOX_DATA_DIR },
{ type: 'standard', id: 'maildata', name: 'Mail data', path: paths.MAIL_DATA_DIR },
];
2021-01-04 11:05:42 -08:00
for (const stdPath of standardPaths) {
const [dfError, diskInfo] = await safe(df.file(stdPath.path));
if (dfError) throw new BoxError(BoxError.FS_ERROR, `Error getting std path: ${dfError.message}`);
2024-11-30 11:46:28 +01:00
filesystems[diskInfo.filesystem].contents.push(stdPath);
2021-08-20 09:19:44 -07:00
}
2021-01-04 11:05:42 -08:00
const sites = await backupSites.list();
for (const backupSite of sites) {
2025-09-12 09:48:37 +02:00
if (backupSite.provider === 'filesystem') {
const [, dfResult] = await safe(df.file(backupSite.config.backupDir));
const filesystem = dfResult?.filesystem || rootDisk.filesystem;
if (filesystems[filesystem]) filesystems[filesystem].contents.push({ type: 'cloudron-backup', id: backupSite.id, name: backupSite.name, path: backupSite.config.backupDir });
}
2021-01-04 11:05:42 -08:00
}
// often the default backup dir is not cleaned up
const siteForDefault = sites.find(s => s.provider === 'filesystem' && s.config.backupDir === paths.DEFAULT_BACKUP_DIR);
if (!siteForDefault) {
const [, dfResult] = await safe(df.file(paths.DEFAULT_BACKUP_DIR));
const filesystem = dfResult?.filesystem || rootDisk.filesystem;
2025-10-22 14:20:56 +02:00
if (filesystems[filesystem]) filesystems[filesystem].contents.push({ type: 'cloudron-backup-default', id: 'cloudron-backup-default', name: 'Default backup', path: paths.DEFAULT_BACKUP_DIR });
2023-08-01 17:38:48 +05:30
}
const [dockerError, dockerInfo] = await safe(docker.info());
if (!dockerError) {
const [, dfResult] = await safe(df.file(dockerInfo.DockerRootDir));
const filesystem = dfResult?.filesystem || rootDisk.filesystem;
2024-11-30 11:46:28 +01:00
if (filesystems[filesystem]) {
filesystems[filesystem].contents.push({ type: 'standard', id: 'docker', name: 'Docker', path: dockerInfo.DockerRootDir });
2025-10-22 14:20:56 +02:00
filesystems[filesystem].contents.push({ type: 'standard', id: 'docker-volumes', name: 'Docker volumes', path: dockerInfo.DockerRootDir });
2023-10-17 16:34:00 +02:00
}
2021-08-25 19:41:46 -07:00
}
for (const volume of await volumes.list()) {
2022-10-18 19:32:07 +02:00
const [, dfResult] = await safe(df.file(volume.hostPath));
const filesystem = dfResult?.filesystem || rootDisk.filesystem;
if (filesystems[filesystem]) filesystems[filesystem].contents.push({ type: 'volume', id: volume.id, name: volume.name, path: volume.hostPath });
}
2021-08-25 19:41:46 -07:00
for (const app of await apps.list()) {
2022-10-13 23:34:47 +02:00
if (!app.manifest.addons?.localstorage) continue;
2019-08-19 13:50:44 -07:00
const dataDir = await apps.getStorageDir(app);
if (dataDir === null) continue;
const [, dfResult] = await safe(df.file(dataDir));
const filesystem = dfResult?.filesystem || rootDisk.filesystem;
if (filesystems[filesystem]) filesystems[filesystem].contents.push({ type: 'app', id: app.id, name: app.label || app.fqdn, path: dataDir });
}
const swaps = await getSwaps();
for (const k in swaps) {
const swap = swaps[k];
if (swap.type !== 'file') continue;
const [, dfResult] = await safe(df.file(swap.name));
filesystems[dfResult?.filesystem || rootDisk.filesystem].contents.push({ type: 'swap', id: swap.name, name: swap.name, path: swap.name });
}
return Object.values(filesystems);
2021-08-25 19:41:46 -07:00
}
2019-08-19 13:50:44 -07:00
2021-08-25 19:41:46 -07:00
async function checkDiskSpace() {
debug('checkDiskSpace: checking disk space');
2019-08-19 13:50:44 -07:00
2024-11-30 11:46:28 +01:00
const filesystems = await getFilesystems();
2021-08-25 19:41:46 -07:00
let markdownMessage = '';
2019-08-19 13:50:44 -07:00
for (const filesystem of filesystems) {
2024-11-30 11:46:28 +01:00
if (filesystem.contents.length === 0) continue; // ignore if nothing interesting here
2021-06-03 12:20:44 -07:00
if (filesystem.capacity >= 0.90) { // > 90%
const prettyUsed = df.prettyBytes(filesystem.used),
prettyAvailable = df.prettyBytes(filesystem.available),
2025-05-07 13:32:45 +02:00
prettySize = df.prettyBytes(filesystem.size);
markdownMessage += `* ${filesystem.filesystem} (${filesystem.type}) mounted at ${filesystem.mountpoint} is at ${filesystem.capacity*100}% capacity. Used: ${prettyUsed} Available: ${prettyAvailable} Size: ${prettySize}\n`;
2021-08-25 19:41:46 -07:00
}
}
2021-08-25 19:41:46 -07:00
debug(`checkDiskSpace: disk space checked. low disk space: ${markdownMessage || 'no'}`);
2021-08-25 19:41:46 -07:00
if (markdownMessage) {
const finalMessage = `One or more file systems are running low on space. Please increase the disk size at the earliest.\n\n${markdownMessage}`;
await notifications.pin(notifications.TYPE_DISK_SPACE, 'Server is running out of disk space', finalMessage, {});
} else {
2024-12-11 19:17:44 +01:00
await notifications.unpin(notifications.TYPE_DISK_SPACE, {});
}
2019-08-19 13:50:44 -07:00
}
2019-11-21 12:55:17 -08:00
2022-11-04 15:09:37 +01:00
async function getSwapSize() {
const swaps = await getSwaps();
return Object.keys(swaps).map(n => swaps[n].size).reduce((acc, cur) => acc + cur, 0);
}
2021-08-25 19:41:46 -07:00
async function getMemory() {
return {
2019-11-21 12:55:17 -08:00
memory: os.totalmem(),
2022-11-04 15:09:37 +01:00
swap: await getSwapSize()
2021-08-25 19:41:46 -07:00
};
2019-11-21 12:55:17 -08:00
}
2025-07-16 23:09:06 +02:00
class FilesystemUsageTask extends AsyncTask {
#filesystem;
2022-10-12 10:26:21 +02:00
2025-07-16 23:09:06 +02:00
constructor(filesystem) {
super(`FileSystemUsageTask(${filesystem.filesystem})`);
this.#filesystem = filesystem;
}
2022-10-12 10:26:21 +02:00
2025-07-17 09:50:43 +02:00
async _run(abortSignal) {
2025-07-18 13:02:53 +02:00
const { filesystem, type, contents, mountpoint, used } = this.#filesystem;
2024-11-30 12:01:25 +01:00
2025-07-16 23:09:06 +02:00
let percent = 5;
2022-10-12 10:26:21 +02:00
2025-07-16 23:09:06 +02:00
if (type === 'ext4' || type === 'xfs') { // hdparm only works with block devices
this.emitProgress(percent, 'Calculating Disk Speed');
2025-07-17 09:50:43 +02:00
const [speedError, speed] = await safe(hdparm(filesystem, { abortSignal }));
2025-07-16 23:09:06 +02:00
if (speedError) debug(`hdparm error ${filesystem}: ${speedError.message}`);
this.emitData({ speed: speedError ? -1 : speed });
2023-08-01 16:31:01 +05:30
} else {
2025-07-16 23:09:06 +02:00
this.emitData({ speed: -1 });
2023-08-01 16:31:01 +05:30
}
2023-01-27 21:05:25 +01:00
2025-07-18 13:05:33 +02:00
let usage = 0, dockerDf;
2023-10-17 16:34:00 +02:00
2025-07-16 23:09:06 +02:00
for (const content of contents) {
2025-07-17 01:16:24 +02:00
percent += (90/contents.length+1);
2025-07-17 09:50:43 +02:00
if (abortSignal.aborted) return;
2025-07-16 23:09:06 +02:00
this.emitProgress(percent,`Checking du of ${content.id} ${content.path}`);
2025-07-18 13:05:33 +02:00
if (content.id === 'docker' || content.id === 'docker-volumes') {
if (!dockerDf) dockerDf = await docker.df({ abortSignal });
content.usage = content.id === 'docker' ? dockerDf.LayersSize : dockerDf.Volumes.map((v) => v.UsageData.Size).reduce((a,b) => a + b, 0);
2022-10-12 10:26:21 +02:00
} else {
2025-07-17 09:50:43 +02:00
const [error, usage] = await safe(du(content.path, { abortSignal }));
2025-07-16 23:09:06 +02:00
if (error) debug(`du error ${content.path}: ${error.message}`); // can happen if app is installing etc
2022-10-13 23:34:47 +02:00
content.usage = usage || 0;
2022-10-12 10:26:21 +02:00
}
2025-07-18 13:02:53 +02:00
usage += content.usage;
2025-07-16 23:09:06 +02:00
this.emitData({ content });
2022-10-12 10:26:21 +02:00
}
2025-07-18 13:02:53 +02:00
if (mountpoint === '/') this.emitData({ content: { type: 'standard', id: 'os', name: 'Ubuntu', usage: used - usage }});
2022-10-12 10:26:21 +02:00
}
2025-07-16 23:09:06 +02:00
}
2022-10-12 10:26:21 +02:00
2025-07-16 23:09:06 +02:00
async function getFilesystemUsage(fsPath) {
assert.strictEqual(typeof fsPath, 'string');
2022-10-12 10:26:21 +02:00
2025-07-16 23:09:06 +02:00
const filesystems = await getFilesystems();
const filesystem = filesystems.find(f => f.filesystem === fsPath);
if (!filesystem) throw new BoxError(BoxError.BAD_FIELD, 'No such filesystem');
2025-07-16 23:09:06 +02:00
return new FilesystemUsageTask(filesystem);
2022-10-12 10:26:21 +02:00
}
2023-08-04 13:41:13 +05:30
async function reboot() {
await notifications.unpin(notifications.TYPE_REBOOT, {});
2023-08-04 13:41:13 +05:30
const [error] = await safe(shell.sudo([ REBOOT_CMD ], {}));
2023-08-04 13:41:13 +05:30
if (error) debug('reboot: could not reboot. %o', error);
}
2023-12-04 00:46:12 +01:00
async function getInfo() {
2023-08-04 13:41:13 +05:30
// https://serverfault.com/questions/92932/how-does-ubuntu-keep-track-of-the-system-restart-required-flag-in-motd
2023-12-04 00:46:12 +01:00
const rebootRequired = fs.existsSync('/var/run/reboot-required');
2023-12-04 01:09:42 +01:00
const uptime = safe.fs.readFileSync('/proc/uptime', 'utf8');
const uptimeSecs = parseInt(uptime.split(' ')[0], 10);
// these files may not exist if kernel does not have access to dmi data
const sysVendor = safe.fs.readFileSync('/sys/devices/virtual/dmi/id/sys_vendor', 'utf8') || '';
const productName = safe.fs.readFileSync('/sys/devices/virtual/dmi/id/product_name', 'utf8') || '';
const productFamily = safe.fs.readFileSync('/sys/devices/virtual/dmi/id/product_family', 'utf8') || '';
2023-12-04 00:46:12 +01:00
const activationTime = (await eventlog.getActivationEvent())?.creationTime || null;
2023-12-04 00:46:12 +01:00
return {
sysVendor: sysVendor.trim(),
productName: productName.trim() || productFamily.trim(),
2023-12-04 01:09:42 +01:00
uptimeSecs,
rebootRequired,
activationTime
2023-12-04 00:46:12 +01:00
};
2023-08-04 13:41:13 +05:30
}
async function getLogs(unit, options) {
assert.strictEqual(typeof unit, 'string');
assert(options && typeof options === 'object');
2024-07-25 17:50:41 +02:00
if (unit !== 'box') throw new BoxError(BoxError.BAD_FIELD, `No such unit '${unit}'`);
2023-08-04 13:41:13 +05:30
2024-07-25 17:50:41 +02:00
const logFile = path.join(paths.LOG_DIR, 'box.log');
2023-08-04 13:41:13 +05:30
const cp = logs.tail([logFile], { lines: options.lines, follow: options.follow });
const logStream = new logs.LogStream({ format: options.format || 'json', source: unit });
logStream.on('close', () => cp.terminate()); // the caller has to call destroy() on logStream. destroy() of Transform emits 'close'
2023-08-04 13:41:13 +05:30
cp.stdout.pipe(logStream);
return logStream;
}
2024-11-30 10:41:01 +01:00
// this gets block devices as opposed to mounted filesystems. this is used for configuring backups and volumes in the frontend
2023-08-04 13:41:13 +05:30
async function getBlockDevices() {
2025-11-10 10:50:23 +01:00
const output = await shell.spawn('lsblk', ['--paths', '--bytes', '--json', '--list', '--fs', '--output', '+rota,fsused,fsavail'], { encoding: 'utf8' });
2025-09-10 11:40:13 +02:00
const info = safe.JSON.parse(output);
2024-02-20 22:57:36 +01:00
if (!info) throw new BoxError(BoxError.INTERNAL_ERROR, `failed to parse lsblk: ${safe.error.message}`);
2023-08-04 13:41:13 +05:30
2025-10-15 11:59:35 +02:00
// despite the function and variable names, this is the partitions and not the block devices!
2023-08-04 13:41:13 +05:30
const devices = info.blockdevices.filter(d => d.fstype === 'ext4' || d.fstype === 'xfs');
2025-09-10 11:40:13 +02:00
const result = [];
for (const device of devices) {
const mountpoints = Array.isArray(device.mountpoints)
? (device.mountpoints[0] === null ? [] : device.mountpoints) // convert [ null ] to []
: (device.mountpoint ? [ device.mountpoint ] : []); // old lsblk only exposed one .mountpoint
2025-09-10 11:40:13 +02:00
result.push({
path: device.name,
size: device.fsavail || 0,
2025-11-10 10:50:23 +01:00
used: device.fsused || 0,
type: device.fstype, // when null, it is not formatted
2025-09-10 11:40:13 +02:00
uuid: device.uuid,
rota: device.rota, // false (ssd) true (hdd) . unforuntately, this is not set correctly when virtualized (like in DO)
mountpoints
});
}
return result;
2023-08-04 13:41:13 +05:30
}
async function checkRebootRequired() {
2023-12-04 00:46:12 +01:00
const { rebootRequired } = await getInfo();
2023-08-04 13:41:13 +05:30
if (rebootRequired) {
await notifications.pin(notifications.TYPE_REBOOT, 'Reboot Required', 'To finish ubuntu security updates, a reboot is necessary.', {});
2023-08-04 13:41:13 +05:30
} else {
await notifications.unpin(notifications.TYPE_REBOOT, {});
2023-08-04 13:41:13 +05:30
}
}
2025-07-09 18:28:55 +02:00
async function getKernelVersion() {
const data = safe.fs.readFileSync('/proc/version', 'utf-8');
if (data === null) throw new BoxError(BoxError.FS_ERROR, safe.error.message);
const version = data.match(/^Linux version (\S+)/)[1];
return `Linux ${version}`;
}
2023-08-11 21:47:49 +05:30
async function getUbuntuVersion() {
const release = safe.fs.readFileSync('/etc/lsb-release', 'utf-8');
if (release === null) throw new BoxError(BoxError.FS_ERROR, safe.error.message);
return release.match(/DISTRIB_DESCRIPTION="(.*)"/)[1];
}
2025-02-26 16:37:00 +01:00
// https://wiki.ubuntu.com/Releases
2023-08-04 13:41:13 +05:30
async function checkUbuntuVersion() {
2025-02-26 16:37:00 +01:00
const isFocal = fs.readFileSync('/etc/lsb-release', 'utf-8').includes('20.04');
if (!isFocal) return;
2023-08-04 13:41:13 +05:30
2025-02-26 16:37:00 +01:00
await notifications.pin(notifications.TYPE_UPDATE_UBUNTU, 'Ubuntu upgrade required', 'Ubuntu 20.04 is reaching end of life and will not receive security and maintenance updates. Please follow https://docs.cloudron.io/guides/upgrade-ubuntu-22/ to upgrade to Ubuntu 22.04 at the earliest.', {});
2023-08-04 13:41:13 +05:30
}
async function runSystemChecks() {
debug('runSystemChecks: checking status');
const checks = [
checkRebootRequired(),
checkUbuntuVersion()
];
await Promise.allSettled(checks);
}
function getProvider() {
const provider = safe.fs.readFileSync(paths.PROVIDER_FILE, 'utf8');
return provider ? provider.trim() : 'generic';
}
2023-12-04 00:23:25 +01:00
async function getCpus() {
return os.cpus();
}